@nextgis/ngw-map 3.0.0-alpha.4 → 3.0.0-alpha.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- /** Bundle of @nextgis/ngw-map; version: 3.0.0-alpha.4; author: NextGIS */
1
+ /** Bundle of @nextgis/ngw-map; version: 3.0.0-alpha.6; author: NextGIS */
2
2
  var NgwMap = (function (exports) {
3
3
  'use strict';
4
4
 
@@ -32,7 +32,7 @@ var NgwMap = (function (exports) {
32
32
  this.domain = null;
33
33
  if (EventEmitter.usingDomains) {
34
34
  // if there is an active domain, then attach to it.
35
- if (domain.active ) ;
35
+ if (domain.active) ;
36
36
  }
37
37
 
38
38
  if (!this._events || this._events === Object.getPrototypeOf(this)._events) {
@@ -827,12 +827,12 @@ var NgwMap = (function (exports) {
827
827
  return url.replace(/([^:]\/)\/+/g, "$1");
828
828
  }
829
829
 
830
- var __defProp$L = Object.defineProperty;
831
- var __defNormalProp$L = (obj, key, value) => key in obj ? __defProp$L(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
832
- var __publicField$w = (obj, key, value) => __defNormalProp$L(obj, typeof key !== "symbol" ? key + "" : key, value);
830
+ var __defProp$P = Object.defineProperty;
831
+ var __defNormalProp$P = (obj, key, value) => key in obj ? __defProp$P(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
832
+ var __publicField$y = (obj, key, value) => __defNormalProp$P(obj, typeof key !== "symbol" ? key + "" : key, value);
833
833
  const _Cache = class _Cache {
834
834
  constructor(options = {}) {
835
- __publicField$w(this, "cache", []);
835
+ __publicField$y(this, "cache", []);
836
836
  var _a;
837
837
  const namespace = (_a = options.namespace) != null ? _a : "default";
838
838
  if (_Cache.instance[namespace]) {
@@ -938,12 +938,12 @@ var NgwMap = (function (exports) {
938
938
  return objectDeepEqual(itemProps || {}, objectRemoveEmpty(props));
939
939
  }
940
940
  };
941
- __publicField$w(_Cache, "instance", {});
941
+ __publicField$y(_Cache, "instance", {});
942
942
  let Cache = _Cache;
943
943
 
944
944
  var pkg = {
945
945
  name: "@nextgis/ngw-connector",
946
- version: "3.0.0-alpha.4",
946
+ version: "3.0.0-alpha.6",
947
947
  _priority: 12,
948
948
  description: "A lightweight HTTP client optimized for use with NextGIS Web API",
949
949
  main: "index.js",
@@ -952,15 +952,15 @@ var NgwMap = (function (exports) {
952
952
  jsdelivr: "lib/ngw-connector.global.prod.js",
953
953
  types: "lib/index.d.ts",
954
954
  dependencies: {
955
- "@nextgis/cache": "^3.0.0-alpha.4",
956
- "@nextgis/cancelable-promise": "^3.0.0-alpha.3",
957
- "@nextgis/utils": "^3.0.0-alpha.4",
955
+ "@nextgis/cache": "^3.0.0-alpha.5",
956
+ "@nextgis/cancelable-promise": "^3.0.0-alpha.5",
957
+ "@nextgis/utils": "^3.0.0-alpha.5",
958
958
  "@types/events": "^3.0.0",
959
959
  events: "*",
960
960
  "form-data": "^4.0.0"
961
961
  },
962
962
  devDependencies: {
963
- "@nextgis/build-tools": "^3.0.0-alpha.3"
963
+ "@nextgis/build-tools": "^3.0.0-alpha.5"
964
964
  },
965
965
  scripts: {
966
966
  clean: "rimraf ./lib",
@@ -994,87 +994,179 @@ var NgwMap = (function (exports) {
994
994
  type: "git",
995
995
  url: "git://github.com/nextgis/nextgis_frontend.git"
996
996
  },
997
+ engines: {
998
+ node: ">=18.20.4"
999
+ },
997
1000
  gitHead: "edc68033f914f6c95c4125b9738bba6d36e990b4"
998
1001
  };
999
1002
 
1000
- var __defProp$K = Object.defineProperty;
1001
- var __defNormalProp$K = (obj, key, value) => key in obj ? __defProp$K(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1002
- var __publicField$v = (obj, key, value) => __defNormalProp$K(obj, typeof key !== "symbol" ? key + "" : key, value);
1003
+ const CONNECTORS = [];
1004
+ function addConnector(connector) {
1005
+ CONNECTORS.push(connector);
1006
+ }
1007
+ function findConnector(options) {
1008
+ return CONNECTORS.find((x) => {
1009
+ if (x.options.baseUrl === options.baseUrl) {
1010
+ if (options.auth) {
1011
+ if (x.options.auth) {
1012
+ return objectDeepEqual(x.options.auth, options.auth);
1013
+ }
1014
+ } else {
1015
+ return true;
1016
+ }
1017
+ }
1018
+ });
1019
+ }
1020
+ function removeConnector(connector) {
1021
+ const index = CONNECTORS.indexOf(connector);
1022
+ if (index !== -1) {
1023
+ CONNECTORS.splice(index, 1);
1024
+ }
1025
+ }
1026
+
1027
+ class AbortError extends Error {
1028
+ constructor(message = "AbortError") {
1029
+ super(message);
1030
+ this.name = "AbortError";
1031
+ }
1032
+ }
1033
+
1034
+ var __defProp$O = Object.defineProperty;
1035
+ var __defNormalProp$O = (obj, key, value) => key in obj ? __defProp$O(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1036
+ var __publicField$x = (obj, key, value) => __defNormalProp$O(obj, typeof key !== "symbol" ? key + "" : key, value);
1003
1037
  class NgwError extends Error {
1004
1038
  constructor(er) {
1005
1039
  super();
1006
- __publicField$v(this, "name", "NgwError");
1007
- __publicField$v(this, "title");
1008
- __publicField$v(this, "message");
1009
- __publicField$v(this, "detail");
1010
- __publicField$v(this, "exception");
1011
- __publicField$v(this, "status_code");
1012
- __publicField$v(this, "data");
1013
- __publicField$v(this, "guru_meditation");
1040
+ __publicField$x(this, "name", "NgwError");
1041
+ __publicField$x(this, "title");
1042
+ __publicField$x(this, "message");
1043
+ __publicField$x(this, "detail");
1044
+ __publicField$x(this, "exception");
1045
+ __publicField$x(this, "status_code");
1046
+ __publicField$x(this, "data");
1047
+ __publicField$x(this, "guru_meditation");
1014
1048
  Object.assign(this, er);
1015
1049
  Object.setPrototypeOf(this, NgwError.prototype);
1016
1050
  }
1017
1051
  }
1018
1052
 
1019
- var __defProp$J = Object.defineProperty;
1020
- var __defNormalProp$J = (obj, key, value) => key in obj ? __defProp$J(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1021
- var __publicField$u = (obj, key, value) => __defNormalProp$J(obj, typeof key !== "symbol" ? key + "" : key, value);
1053
+ var __defProp$N = Object.defineProperty;
1054
+ var __defNormalProp$N = (obj, key, value) => key in obj ? __defProp$N(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1055
+ var __publicField$w = (obj, key, value) => __defNormalProp$N(obj, typeof key !== "symbol" ? key + "" : key, value);
1056
+ class InsufficientPermissionsError extends NgwError {
1057
+ constructor(obj) {
1058
+ super(obj);
1059
+ __publicField$w(this, "name", "InsufficientPermissionsError");
1060
+ __publicField$w(this, "exception", "nextgisweb.core.exception.InsufficientPermissions");
1061
+ Object.setPrototypeOf(this, InsufficientPermissionsError.prototype);
1062
+ }
1063
+ }
1064
+
1065
+ var __defProp$M = Object.defineProperty;
1066
+ var __defNormalProp$M = (obj, key, value) => key in obj ? __defProp$M(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1067
+ var __publicField$v = (obj, key, value) => __defNormalProp$M(obj, typeof key !== "symbol" ? key + "" : key, value);
1022
1068
  class ResourceNotFoundError extends NgwError {
1023
1069
  constructor(obj) {
1024
1070
  super(obj);
1025
- __publicField$u(this, "name", "ResourceNotFoundError");
1026
- __publicField$u(this, "exception", "nextgisweb.resource.exception.ResourceNotFound");
1071
+ __publicField$v(this, "name", "ResourceNotFoundError");
1072
+ __publicField$v(this, "exception", "nextgisweb.resource.exception.ResourceNotFound");
1027
1073
  Object.setPrototypeOf(this, ResourceNotFoundError.prototype);
1028
1074
  }
1029
1075
  }
1030
1076
 
1031
- function isObject(val) {
1032
- return Object.prototype.toString.call(val) === "[object Object]";
1077
+ var __defProp$L = Object.defineProperty;
1078
+ var __defNormalProp$L = (obj, key, value) => key in obj ? __defProp$L(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1079
+ var __publicField$u = (obj, key, value) => __defNormalProp$L(obj, typeof key !== "symbol" ? key + "" : key, value);
1080
+ class BaseAPIError extends Error {
1081
+ constructor(message) {
1082
+ super(message || "Something went wrong.");
1083
+ __publicField$u(this, "title");
1084
+ this.name = "BaseAPIError";
1085
+ this.title = "Unknown API error";
1086
+ if (Error.captureStackTrace) {
1087
+ Error.captureStackTrace(this, BaseAPIError);
1088
+ }
1089
+ }
1033
1090
  }
1034
-
1035
- function resourceCompare(res1, res2) {
1036
- return objectDeepEqual(res1, res2);
1091
+ class NetworksResponseError extends BaseAPIError {
1092
+ // prettier-ignore
1093
+ constructor(message) {
1094
+ super(message || "There is no response from the server or problem connecting to server.");
1095
+ __publicField$u(this, "detail");
1096
+ this.title = "Network error";
1097
+ this.detail = "Check network connectivity and try again later.";
1098
+ }
1037
1099
  }
1038
-
1039
- const exclude = ["description"];
1040
- function resourceToQuery(resource, prefix = "") {
1041
- prefix = prefix ? prefix + "__" : "";
1042
- const query = {};
1043
- for (const [key, value] of Object.entries(resource)) {
1044
- if (exclude.indexOf(key) === -1) {
1045
- if (isObject(value)) {
1046
- if (key === "owner_user") {
1047
- const children = resourceToQuery(value, key);
1048
- Object.assign(query, children);
1049
- } else if (key === "parent" && "id" in value) {
1050
- query.parent_id = value.id;
1051
- }
1052
- } else if (defined(value)) {
1053
- query[prefix + key] = value;
1054
- }
1100
+ class InvalidResponseError extends BaseAPIError {
1101
+ constructor(message) {
1102
+ super(message || "Something went wrong.");
1103
+ this.title = "Unexpected server response";
1104
+ }
1105
+ }
1106
+ class ServerResponseError extends BaseAPIError {
1107
+ constructor(data) {
1108
+ super(data.message);
1109
+ __publicField$u(this, "detail");
1110
+ __publicField$u(this, "data");
1111
+ this.title = data.title || this.title;
1112
+ this.detail = data.detail || null;
1113
+ this.data = data;
1114
+ }
1115
+ }
1116
+ class LunkwillError extends Error {
1117
+ // prettier-ignore
1118
+ constructor(message, data = {}) {
1119
+ super(message || "Unexpected error while processing long-running request.");
1120
+ __publicField$u(this, "title");
1121
+ __publicField$u(this, "data");
1122
+ this.name = "LunkwillError";
1123
+ this.title = "Long-running request error";
1124
+ this.data = data;
1125
+ if (Error.captureStackTrace) {
1126
+ Error.captureStackTrace(this, LunkwillError);
1055
1127
  }
1056
1128
  }
1057
- return query;
1129
+ }
1130
+ class LunkwillRequestCancelled extends LunkwillError {
1131
+ constructor(data) {
1132
+ super("Long-running request was cancelled.", data);
1133
+ }
1134
+ }
1135
+ class LunkwillRequestFailed extends LunkwillError {
1136
+ constructor(data) {
1137
+ super(void 0, data);
1138
+ }
1058
1139
  }
1059
1140
 
1060
- var __defProp$I = Object.defineProperty;
1061
- var __getOwnPropSymbols$w = Object.getOwnPropertySymbols;
1062
- var __hasOwnProp$w = Object.prototype.hasOwnProperty;
1063
- var __propIsEnum$w = Object.prototype.propertyIsEnumerable;
1064
- var __defNormalProp$I = (obj, key, value) => key in obj ? __defProp$I(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1065
- var __spreadValues$v = (a, b) => {
1141
+ var __defProp$K = Object.defineProperty;
1142
+ var __getOwnPropSymbols$y = Object.getOwnPropertySymbols;
1143
+ var __hasOwnProp$y = Object.prototype.hasOwnProperty;
1144
+ var __propIsEnum$y = Object.prototype.propertyIsEnumerable;
1145
+ var __defNormalProp$K = (obj, key, value) => key in obj ? __defProp$K(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1146
+ var __spreadValues$x = (a, b) => {
1066
1147
  for (var prop in b || (b = {}))
1067
- if (__hasOwnProp$w.call(b, prop))
1068
- __defNormalProp$I(a, prop, b[prop]);
1069
- if (__getOwnPropSymbols$w)
1070
- for (var prop of __getOwnPropSymbols$w(b)) {
1071
- if (__propIsEnum$w.call(b, prop))
1072
- __defNormalProp$I(a, prop, b[prop]);
1148
+ if (__hasOwnProp$y.call(b, prop))
1149
+ __defNormalProp$K(a, prop, b[prop]);
1150
+ if (__getOwnPropSymbols$y)
1151
+ for (var prop of __getOwnPropSymbols$y(b)) {
1152
+ if (__propIsEnum$y.call(b, prop))
1153
+ __defNormalProp$K(a, prop, b[prop]);
1073
1154
  }
1074
1155
  return a;
1075
1156
  };
1076
- var __publicField$t = (obj, key, value) => __defNormalProp$I(obj, typeof key !== "symbol" ? key + "" : key, value);
1077
- var __async$s = (__this, __arguments, generator) => {
1157
+ var __objRest$5 = (source, exclude) => {
1158
+ var target = {};
1159
+ for (var prop in source)
1160
+ if (__hasOwnProp$y.call(source, prop) && exclude.indexOf(prop) < 0)
1161
+ target[prop] = source[prop];
1162
+ if (source != null && __getOwnPropSymbols$y)
1163
+ for (var prop of __getOwnPropSymbols$y(source)) {
1164
+ if (exclude.indexOf(prop) < 0 && __propIsEnum$y.call(source, prop))
1165
+ target[prop] = source[prop];
1166
+ }
1167
+ return target;
1168
+ };
1169
+ var __async$u = (__this, __arguments, generator) => {
1078
1170
  return new Promise((resolve, reject) => {
1079
1171
  var fulfilled = (value) => {
1080
1172
  try {
@@ -1094,307 +1186,238 @@ var NgwMap = (function (exports) {
1094
1186
  step((generator = generator.apply(__this, __arguments)).next());
1095
1187
  });
1096
1188
  };
1097
- class ResourcesControl {
1098
- constructor({
1099
- connector,
1100
- cacheId
1101
- }) {
1102
- __publicField$t(this, "cache");
1103
- __publicField$t(this, "connector");
1104
- this.connector = connector;
1105
- this.cache = new Cache({ namespace: cacheId });
1106
- }
1107
- // -------------------------------------------------------------------------
1108
- // Resource Methods
1109
- // -------------------------------------------------------------------------
1110
- /**
1111
- * Receive resource from NGW by id, keyname or search-object parameter.
1112
- * @param resource - Resource id, keyname or search-object
1113
- *
1114
- * @remarks
1115
- * Fetching resource would be cached to speed up next call
1116
- */
1117
- getOne(resource, requestOptions) {
1118
- const opt = __spreadValues$v({}, requestOptions);
1119
- if (typeof resource === "string") ; else if (typeof resource === "number") ; else if (isObject(resource)) {
1120
- if (resource.id !== void 0) {
1121
- resource.id;
1122
- } else {
1123
- if (resource.keyname) {
1124
- resource.keyname;
1125
- }
1126
- if (resource.display_name) {
1127
- resource.display_name;
1128
- }
1189
+ function lunkwillCheckResponse(lwResp) {
1190
+ const ct = lwResp.headers.get("content-type");
1191
+ return ct !== void 0 && ct !== null && ct.includes("application/vnd.lunkwill.request-summary+json");
1192
+ }
1193
+ function responseJson(response) {
1194
+ return __async$u(this, null, function* () {
1195
+ try {
1196
+ return response.json();
1197
+ } catch (e) {
1198
+ throw new Error();
1199
+ }
1200
+ });
1201
+ }
1202
+ function lunkwillResponseUrl(lwResp) {
1203
+ return __async$u(this, null, function* () {
1204
+ const lwData = yield responseJson(lwResp);
1205
+ let delay = lwData.delay_ms;
1206
+ const retry = lwData.retry_ms !== void 0 ? lwData.retry_ms : 2e3;
1207
+ const sum = `/api/lunkwill/${lwData.id}/summary`;
1208
+ const res = `/api/lunkwill/${lwData.id}/response`;
1209
+ const sleep = (msec) => new Promise((resolve) => setTimeout(resolve, msec));
1210
+ let failed = false;
1211
+ let ready = false;
1212
+ while (!ready) {
1213
+ yield sleep(failed ? retry : delay);
1214
+ failed = false;
1215
+ let lwResp2;
1216
+ let lwData2;
1217
+ try {
1218
+ lwResp2 = yield fetch(sum, { credentials: "same-origin" });
1219
+ lwData2 = yield lwResp2.json();
1220
+ } catch (e) {
1221
+ failed = true;
1222
+ continue;
1223
+ }
1224
+ switch (lwData2.status) {
1225
+ case void 0:
1226
+ throw new LunkwillError(void 0, lwData2);
1227
+ case "ready":
1228
+ ready = true;
1229
+ break;
1230
+ case "cancelled":
1231
+ throw new LunkwillRequestCancelled(lwData2);
1232
+ case "failed":
1233
+ throw new LunkwillRequestFailed(lwData2);
1234
+ case "spooled":
1235
+ case "processing":
1236
+ case "buffering":
1237
+ delay = lwData2.delay_ms;
1238
+ break;
1239
+ default:
1240
+ throw new LunkwillError(void 0, lwData2);
1129
1241
  }
1130
1242
  }
1131
- if (typeof resource === "string") {
1132
- return this._fetchResourceBy({ keyname: resource }, opt);
1133
- } else if (typeof resource === "number") {
1134
- return this._fetchResourceById(resource, opt);
1135
- } else if (isObject(resource)) {
1136
- return this._fetchResourceBy(resource, opt);
1243
+ return res;
1244
+ });
1245
+ }
1246
+ function lunkwillFetch(lwRespUrl) {
1247
+ return __async$u(this, null, function* () {
1248
+ try {
1249
+ return yield window.fetch(lwRespUrl, { credentials: "same-origin" });
1250
+ } catch (e) {
1251
+ throw new NetworksResponseError();
1137
1252
  }
1138
- return Promise.resolve(void 0);
1139
- }
1140
- getOneOrFail(resource, requestOptions) {
1141
- return this.getOne(resource, requestOptions).then((res) => {
1142
- if (res) {
1143
- return res;
1144
- }
1145
- throw new ResourceNotFoundError();
1146
- });
1147
- }
1148
- /**
1149
- * A fast way to retrieve resource ID for any resource definition.
1150
- * @param resource - Any available resource definition
1151
- *
1152
- * @remarks
1153
- * There are situations when exactly the resource id is needed
1154
- * (for example, to compose the correct request to the api)
1155
- * then this method will come in handy to facilitate the extraction of the identifier
1156
- * if the resource is specified through a keyname or other parameters.
1157
- */
1158
- getId(resource, requestOptions) {
1159
- if (typeof resource === "number") {
1160
- return Promise.resolve(resource);
1161
- } else if (typeof resource === "string" || isObject(resource)) {
1162
- return this.getOne(resource, requestOptions).then((res) => {
1163
- if (res) {
1164
- return res.resource.id;
1253
+ });
1254
+ }
1255
+ const mediaContentTypes = [
1256
+ "application/pdf",
1257
+ "image/png",
1258
+ "image/jpeg",
1259
+ "image/tiff",
1260
+ "text/csv"
1261
+ ];
1262
+ const mediaContentTypesRegex = new RegExp(mediaContentTypes.join("|"));
1263
+ function isMediaContentType(contentType) {
1264
+ return mediaContentTypesRegex.test(contentType);
1265
+ }
1266
+ function encodeQueryParams(value) {
1267
+ const result = [];
1268
+ for (const [k, v] of Object.entries(value)) {
1269
+ if (typeof v === "string" || typeof v === "number" || typeof v === "boolean") {
1270
+ result.push(`${k}=${encodeURIComponent(v)}`);
1271
+ } else if (Array.isArray(v)) {
1272
+ result.push(`${k}=${v.map(encodeURIComponent).join(",")}`);
1273
+ } else {
1274
+ for (const [sk, sv] of Object.entries(v)) {
1275
+ const ske = `${k}[${encodeURIComponent(sk)}]`;
1276
+ if (typeof sv === "string" || typeof sv === "number" || typeof sv === "boolean") {
1277
+ result.push(`${ske}=${encodeURIComponent(sv)}`);
1278
+ } else if (Array.isArray(sv)) {
1279
+ const sve = sv.map(encodeURIComponent);
1280
+ result.push(`${ske}=${sve.join(",")}`);
1165
1281
  }
1166
- });
1282
+ }
1167
1283
  }
1168
- return Promise.resolve(void 0);
1169
1284
  }
1170
- /**
1171
- * A fast way to retrieve resource ID for any resource definition.
1172
- * @param resource - Any available resource definition
1173
- *
1174
- * @remarks
1175
- * Similar with {@link NgwConnector.getResourceId | getResourceId} but rise error if resource is not exist.
1176
- * To not make one more checks if the resource is definitely exists
1177
- */
1178
- getIdOrFail(resource, requestOptions) {
1179
- return this.getId(resource, requestOptions).then((resp) => {
1180
- if (resp === void 0) {
1181
- throw new Error();
1182
- }
1183
- return resp;
1184
- });
1285
+ return result.join("&");
1286
+ }
1287
+ function generateUrl(path, query) {
1288
+ let urlParams = "";
1289
+ if (query !== void 0) {
1290
+ urlParams = "?" + encodeQueryParams(query);
1185
1291
  }
1186
- getMany(resource, requestOptions) {
1187
- return this._resourceCacheFilter(resource).then((items) => {
1188
- if (!items.length) {
1189
- const query = {};
1190
- if (resource.keyname) {
1191
- query.keyname = resource.keyname;
1192
- } else {
1193
- Object.assign(query, resourceToQuery(resource));
1292
+ return path + urlParams;
1293
+ }
1294
+ function request(path, options, cacheEngine) {
1295
+ return __async$u(this, null, function* () {
1296
+ var _b;
1297
+ const defaults = {
1298
+ method: "GET",
1299
+ credentials: "same-origin",
1300
+ headers: {}
1301
+ };
1302
+ const _a = __spreadValues$x(__spreadValues$x({}, defaults), options), {
1303
+ withCredentials,
1304
+ responseType,
1305
+ cacheProps,
1306
+ cacheName,
1307
+ lunkwill,
1308
+ cache,
1309
+ query,
1310
+ json
1311
+ } = _a, opt = __objRest$5(_a, [
1312
+ "withCredentials",
1313
+ "responseType",
1314
+ "cacheProps",
1315
+ "cacheName",
1316
+ "lunkwill",
1317
+ "cache",
1318
+ "query",
1319
+ "json"
1320
+ ]);
1321
+ if (withCredentials) {
1322
+ opt.credentials = "include";
1323
+ }
1324
+ let useLunkwill = false;
1325
+ if (lunkwill !== void 0) {
1326
+ lunkwill.toHeaders(opt.headers || {});
1327
+ useLunkwill = true;
1328
+ }
1329
+ const lunkwillReturnUrl = !!opt.lunkwillReturnUrl;
1330
+ delete opt.lunkwillReturnUrl;
1331
+ if (json !== void 0) {
1332
+ opt.body = JSON.stringify(json);
1333
+ const headers = opt.headers || {};
1334
+ headers["Content-Type"] = "application/json";
1335
+ opt.headers = headers;
1336
+ }
1337
+ const url = generateUrl(path, query);
1338
+ const makeRequest = () => __async$u(this, null, function* () {
1339
+ let response;
1340
+ try {
1341
+ response = yield fetch(url, opt);
1342
+ } catch (e) {
1343
+ if (e.name === "AbortError") {
1344
+ throw e;
1194
1345
  }
1195
- return this.connector.get("resource.search", requestOptions, __spreadValues$v({
1196
- serialization: "full"
1197
- }, query)).then((resources) => {
1198
- if ((requestOptions == null ? void 0 : requestOptions.cache) && resources) {
1199
- for (const x of resources) {
1200
- this.cache.add("resource.item", Promise.resolve(x), {
1201
- id: x.resource.id
1202
- });
1203
- }
1204
- }
1205
- return resources;
1206
- });
1346
+ throw new NetworksResponseError();
1207
1347
  }
1208
- return items;
1209
- });
1210
- }
1211
- getParent(resource, requestOptions) {
1212
- return this.getOne(resource, requestOptions).then((child) => {
1213
- if (child) {
1214
- return this.getOne(child.resource.parent.id, requestOptions);
1348
+ if (useLunkwill && lunkwillCheckResponse(response)) {
1349
+ const lwRespUrl = yield lunkwillResponseUrl(response);
1350
+ if (lunkwillReturnUrl) {
1351
+ return lwRespUrl;
1352
+ }
1353
+ response = yield lunkwillFetch(lwRespUrl);
1215
1354
  }
1216
- return Promise.resolve(void 0);
1217
- });
1218
- }
1219
- getChildrenOf(resource, requestOptions) {
1220
- return this.getIdOrFail(resource).then(
1221
- (parent) => this._getChildrenOf(parent, requestOptions)
1222
- );
1223
- }
1224
- update(resource, data) {
1225
- return this.getId(resource).then((id) => {
1226
- if (id !== void 0) {
1227
- return this.connector.put("resource.item", { data }, { id });
1355
+ const respCType = response.headers.get("content-type");
1356
+ const respJSON = respCType && (respCType.includes("application/json") || respCType.includes("application/vnd.lunkwill.request-summary+json"));
1357
+ let body;
1358
+ try {
1359
+ const respMedia = respCType && isMediaContentType(respCType);
1360
+ if (responseType === "blob" || respMedia) {
1361
+ body = yield response.blob();
1362
+ } else if (respJSON) {
1363
+ body = yield response.json();
1364
+ } else {
1365
+ throw new InvalidResponseError();
1366
+ }
1367
+ } catch (e) {
1368
+ if (e.name === "AbortError" || e instanceof InvalidResponseError) {
1369
+ throw e;
1370
+ }
1371
+ throw new InvalidResponseError();
1228
1372
  }
1373
+ if (400 <= response.status && response.status <= 599) {
1374
+ throw new ServerResponseError(body);
1375
+ }
1376
+ return body;
1229
1377
  });
1230
- }
1231
- /**
1232
- * Fast way to delete resource from NGW and clean cache.
1233
- * @param resource - Resource definition
1234
- */
1235
- delete(resource) {
1236
- return this.getId(resource).then((id) => {
1237
- if (id !== void 0) {
1238
- return this.connector.delete("resource.item", null, { id }).then(() => {
1239
- this._cleanResourceItemCache(id);
1240
- return void 0;
1241
- });
1242
- }
1243
- });
1244
- }
1245
- _getChildrenOf(parent, requestOptions, _items = []) {
1246
- return this.connector.get("resource.collection", requestOptions, {
1247
- parent
1248
- }).then((items) => {
1249
- const recursivePromises = [];
1250
- for (const item of items) {
1251
- if (requestOptions == null ? void 0 : requestOptions.cache) {
1252
- this.cache.add("resource.item", Promise.resolve(item), {
1253
- id: item.resource.id
1254
- });
1255
- }
1256
- _items.push(item);
1257
- if ((requestOptions == null ? void 0 : requestOptions.recursive) && item.resource.children) {
1258
- recursivePromises.push(
1259
- this._getChildrenOf(item.resource.id, requestOptions, _items)
1260
- );
1261
- }
1262
- }
1263
- if (recursivePromises.length) {
1264
- return Promise.all(recursivePromises).then(() => {
1265
- return _items;
1266
- });
1267
- }
1268
- return _items;
1269
- });
1270
- }
1271
- _cleanResourceItemCache(id) {
1272
- return __async$s(this, null, function* () {
1273
- var _a;
1274
- const all = this.cache.all();
1275
- const toDelete = [];
1276
- for (const c of all) {
1277
- const cid = (_a = c.props) == null ? void 0 : _a.id;
1278
- if (["resource.item", "resource"].includes(c.key) && cid !== void 0) {
1279
- if (typeof cid === "number") {
1280
- if (cid === id) {
1281
- toDelete.push(c);
1282
- }
1283
- } else {
1284
- const rid = yield this.getId(cid);
1285
- if (rid === id) {
1286
- toDelete.push(c);
1287
- }
1288
- }
1289
- }
1290
- }
1291
- for (const d of toDelete) {
1292
- this.cache.delete(d);
1293
- }
1294
- });
1295
- }
1296
- _fetchResourceById(id, requestOptions) {
1297
- return this.connector.get("resource.item", requestOptions, { id });
1298
- }
1299
- _fetchResourceBy(resource, requestOptions) {
1300
- return this.getMany(resource, requestOptions).then((resources) => {
1301
- return resources[0];
1302
- });
1303
- }
1304
- _resourceCacheFilter(resource) {
1305
- return Promise.all(this.cache.matchAll("resource.item")).then(
1306
- (resources) => {
1307
- const items = [];
1308
- resources.filter((x) => {
1309
- if (x) {
1310
- if (resource.keyname && x.resource.keyname) {
1311
- return resource.keyname === x.resource.keyname;
1312
- }
1313
- if (defined(resource.id) && defined(x.resource.id)) {
1314
- return resource.id === x.resource.id;
1315
- }
1316
- return resourceCompare(resource, x.resource);
1317
- }
1318
- });
1319
- return items;
1320
- }
1321
- );
1322
- }
1323
- }
1324
-
1325
- const CONNECTORS = [];
1326
- function addConnector(connector) {
1327
- CONNECTORS.push(connector);
1328
- }
1329
- function findConnector(options) {
1330
- return CONNECTORS.find((x) => {
1331
- if (x.options.baseUrl === options.baseUrl) {
1332
- if (options.auth) {
1333
- if (x.options.auth) {
1334
- return objectDeepEqual(x.options.auth, options.auth);
1335
- }
1336
- } else {
1337
- return true;
1338
- }
1339
- }
1340
- });
1341
- }
1342
- function removeConnector(connector) {
1343
- const index = CONNECTORS.indexOf(connector);
1344
- if (index !== -1) {
1345
- CONNECTORS.splice(index, 1);
1346
- }
1347
- }
1348
-
1349
- class AbortError extends Error {
1350
- constructor(message = "AbortError") {
1351
- super(message);
1352
- this.name = "AbortError";
1353
- }
1354
- }
1355
-
1356
- var __defProp$H = Object.defineProperty;
1357
- var __defNormalProp$H = (obj, key, value) => key in obj ? __defProp$H(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1358
- var __publicField$s = (obj, key, value) => __defNormalProp$H(obj, typeof key !== "symbol" ? key + "" : key, value);
1359
- class InsufficientPermissionsError extends NgwError {
1360
- constructor(obj) {
1361
- super(obj);
1362
- __publicField$s(this, "name", "InsufficientPermissionsError");
1363
- __publicField$s(this, "exception", "nextgisweb.core.exception.InsufficientPermissions");
1364
- Object.setPrototypeOf(this, InsufficientPermissionsError.prototype);
1365
- }
1366
- }
1367
-
1368
- const templateRe = /\{ *([\w_-]+) *\}/g;
1369
- function template(str, data) {
1370
- return str.replace(templateRe, (s, key) => {
1371
- let value = data[key];
1372
- if (value === void 0) {
1373
- throw new Error("No value provided for letiable " + s);
1374
- } else if (typeof value === "function") {
1375
- value = value(data);
1378
+ if (cacheEngine && cache && ((_b = opt.method) == null ? void 0 : _b.toUpperCase()) === "GET") {
1379
+ const cacheOptions = cacheProps ? cacheProps : __spreadValues$x({}, objectRemoveEmpty({
1380
+ withCredentials,
1381
+ responseType
1382
+ }));
1383
+ return cacheEngine.add(cacheName || url, makeRequest, cacheOptions, false);
1376
1384
  }
1377
- return value;
1385
+ return makeRequest();
1378
1386
  });
1379
1387
  }
1380
1388
 
1381
- var __defProp$G = Object.defineProperty;
1382
- var __getOwnPropSymbols$v = Object.getOwnPropertySymbols;
1383
- var __hasOwnProp$v = Object.prototype.hasOwnProperty;
1384
- var __propIsEnum$v = Object.prototype.propertyIsEnumerable;
1385
- var __defNormalProp$G = (obj, key, value) => key in obj ? __defProp$G(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1386
- var __spreadValues$u = (a, b) => {
1389
+ var __defProp$J = Object.defineProperty;
1390
+ var __defProps$h = Object.defineProperties;
1391
+ var __getOwnPropDescs$h = Object.getOwnPropertyDescriptors;
1392
+ var __getOwnPropSymbols$x = Object.getOwnPropertySymbols;
1393
+ var __hasOwnProp$x = Object.prototype.hasOwnProperty;
1394
+ var __propIsEnum$x = Object.prototype.propertyIsEnumerable;
1395
+ var __defNormalProp$J = (obj, key, value) => key in obj ? __defProp$J(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1396
+ var __spreadValues$w = (a, b) => {
1387
1397
  for (var prop in b || (b = {}))
1388
- if (__hasOwnProp$v.call(b, prop))
1389
- __defNormalProp$G(a, prop, b[prop]);
1390
- if (__getOwnPropSymbols$v)
1391
- for (var prop of __getOwnPropSymbols$v(b)) {
1392
- if (__propIsEnum$v.call(b, prop))
1393
- __defNormalProp$G(a, prop, b[prop]);
1398
+ if (__hasOwnProp$x.call(b, prop))
1399
+ __defNormalProp$J(a, prop, b[prop]);
1400
+ if (__getOwnPropSymbols$x)
1401
+ for (var prop of __getOwnPropSymbols$x(b)) {
1402
+ if (__propIsEnum$x.call(b, prop))
1403
+ __defNormalProp$J(a, prop, b[prop]);
1394
1404
  }
1395
1405
  return a;
1396
1406
  };
1397
- var __async$r = (__this, __arguments, generator) => {
1407
+ var __spreadProps$h = (a, b) => __defProps$h(a, __getOwnPropDescs$h(b));
1408
+ var __objRest$4 = (source, exclude) => {
1409
+ var target = {};
1410
+ for (var prop in source)
1411
+ if (__hasOwnProp$x.call(source, prop) && exclude.indexOf(prop) < 0)
1412
+ target[prop] = source[prop];
1413
+ if (source != null && __getOwnPropSymbols$x)
1414
+ for (var prop of __getOwnPropSymbols$x(source)) {
1415
+ if (exclude.indexOf(prop) < 0 && __propIsEnum$x.call(source, prop))
1416
+ target[prop] = source[prop];
1417
+ }
1418
+ return target;
1419
+ };
1420
+ var __async$t = (__this, __arguments, generator) => {
1398
1421
  return new Promise((resolve, reject) => {
1399
1422
  var fulfilled = (value) => {
1400
1423
  try {
@@ -1414,70 +1437,94 @@ var NgwMap = (function (exports) {
1414
1437
  step((generator = generator.apply(__this, __arguments)).next());
1415
1438
  });
1416
1439
  };
1417
- function apiRequest(opt) {
1418
- return __async$r(this, null, function* () {
1419
- const { params, name, connector, requestOptions } = opt;
1420
- const apiItems = yield connector.connect();
1421
- let apiItem = apiItems && apiItems[name];
1422
- if (apiItem) {
1423
- apiItem = [...apiItem];
1424
- let url = apiItem.shift();
1425
- if (apiItem.length) {
1426
- const replaceParams = {};
1427
- for (let fry = 0; fry < apiItem.length; fry++) {
1428
- const arg = apiItem[fry];
1429
- replaceParams[fry] = `{${arg}}`;
1430
- if (params[arg] === void 0) {
1431
- throw new Error(`\`${arg}\` URL API argument is not specified`);
1432
- }
1433
- }
1434
- if (url) {
1435
- url = template(url, replaceParams);
1436
- }
1437
- }
1438
- if (params) {
1439
- const paramArray = [];
1440
- const paramList = params.paramList;
1441
- if (Array.isArray(paramList)) {
1442
- paramList.forEach(([key, value]) => {
1443
- paramArray.push(`${key}=${value}`);
1444
- });
1445
- }
1446
- for (const p in params) {
1447
- if (apiItem.indexOf(p) === -1) {
1448
- paramArray.push(`${p}=${params[p]}`);
1449
- }
1450
- }
1451
- if (paramArray.length) {
1452
- url = `${url}?${paramArray.join("&")}`;
1453
- }
1454
- }
1455
- if (url) {
1456
- return connector.makeQuery(url, params, __spreadValues$u({
1457
- cacheName: name
1458
- }, requestOptions));
1459
- } else {
1460
- throw new Error("Request URL is not set");
1461
- }
1462
- } else {
1463
- return void 0;
1440
+ function routeURL(name, baseUrl, routeData, ...rest) {
1441
+ const [template, ...params] = routeData[name];
1442
+ const first = rest[0];
1443
+ let sub;
1444
+ if (first === void 0) {
1445
+ sub = [];
1446
+ } else if (typeof first === "object" && first !== null) {
1447
+ if (rest.length > 1) {
1448
+ throw new Error("Too many arguments for route(name, object)!");
1449
+ }
1450
+ sub = [];
1451
+ for (const [p, v] of Object.entries(first)) {
1452
+ sub[params.indexOf(p)] = String(v);
1464
1453
  }
1465
- });
1454
+ } else {
1455
+ sub = rest.map((v) => String(v));
1456
+ }
1457
+ return fixUrlStr(
1458
+ baseUrl + template.replace(/\{(\w+)\}/g, function(m, a) {
1459
+ const idx = parseInt(a);
1460
+ const value = sub[idx];
1461
+ if (value === void 0) {
1462
+ const msg = `Undefined parameter ${idx} in "${template}".`;
1463
+ throw new Error(msg);
1464
+ }
1465
+ return String(value);
1466
+ })
1467
+ );
1468
+ }
1469
+ function route(name, connector, ...rest) {
1470
+ const result = {
1471
+ url: (opt) => __async$t(this, null, function* () {
1472
+ var _a;
1473
+ const routeData = yield connector.connect();
1474
+ const template = routeURL(
1475
+ name,
1476
+ (_a = connector.options.baseUrl) != null ? _a : "",
1477
+ routeData,
1478
+ ...rest
1479
+ );
1480
+ return generateUrl(template, opt == null ? void 0 : opt.query);
1481
+ })
1482
+ };
1483
+ const methods = ["get", "post", "put", "delete", "patch"];
1484
+ for (const method of methods) {
1485
+ const methodResp = (requestOptions) => __async$t(this, null, function* () {
1486
+ var _b;
1487
+ const _a = requestOptions || {}, { headers: optHeaders } = _a, restOpt = __objRest$4(_a, ["headers"]);
1488
+ const routeData = yield connector.connect();
1489
+ const template = routeURL(
1490
+ name,
1491
+ (_b = connector.options.baseUrl) != null ? _b : "",
1492
+ routeData,
1493
+ ...rest
1494
+ );
1495
+ const headers = objectRemoveEmpty(__spreadValues$w(__spreadValues$w({}, connector.getAuthorizationHeaders()), optHeaders != null ? optHeaders : {}));
1496
+ return request(
1497
+ template,
1498
+ __spreadProps$h(__spreadValues$w({
1499
+ headers
1500
+ }, restOpt), {
1501
+ method
1502
+ }),
1503
+ connector.cache
1504
+ );
1505
+ });
1506
+ result[method] = methodResp;
1507
+ }
1508
+ return result;
1466
1509
  }
1467
1510
 
1468
- var __defProp$F = Object.defineProperty;
1469
- var __defNormalProp$F = (obj, key, value) => key in obj ? __defProp$F(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1470
- var __publicField$r = (obj, key, value) => __defNormalProp$F(obj, key + "" , value);
1511
+ var __defProp$I = Object.defineProperty;
1512
+ var __defNormalProp$I = (obj, key, value) => key in obj ? __defProp$I(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1513
+ var __publicField$t = (obj, key, value) => __defNormalProp$I(obj, key + "" , value);
1471
1514
  class NetworkError extends Error {
1472
1515
  constructor(url) {
1473
1516
  super();
1474
- __publicField$r(this, "name", "NetworkError");
1517
+ __publicField$t(this, "name", "NetworkError");
1475
1518
  Object.setPrototypeOf(this, NetworkError.prototype);
1476
1519
  this.message = `Unable to request ${url}.
1477
1520
  Possibly invalid NGW URL entered or CORS not configured to get request from ${location.origin}`;
1478
1521
  }
1479
1522
  }
1480
1523
 
1524
+ function isObject(val) {
1525
+ return Object.prototype.toString.call(val) === "[object Object]";
1526
+ }
1527
+
1481
1528
  let loadData;
1482
1529
  {
1483
1530
  loadData = (url, callback, options = {}, error, onCancel) => {
@@ -1560,21 +1607,34 @@ var NgwMap = (function (exports) {
1560
1607
  };
1561
1608
  }
1562
1609
 
1563
- var __defProp$E = Object.defineProperty;
1610
+ const templateRe = /\{ *([\w_-]+) *\}/g;
1611
+ function template(str, data) {
1612
+ return str.replace(templateRe, (s, key) => {
1613
+ let value = data[key];
1614
+ if (value === void 0) {
1615
+ throw new Error("No value provided for letiable " + s);
1616
+ } else if (typeof value === "function") {
1617
+ value = value(data);
1618
+ }
1619
+ return value;
1620
+ });
1621
+ }
1622
+
1623
+ var __defProp$H = Object.defineProperty;
1564
1624
  var __defProps$g = Object.defineProperties;
1565
1625
  var __getOwnPropDescs$g = Object.getOwnPropertyDescriptors;
1566
- var __getOwnPropSymbols$u = Object.getOwnPropertySymbols;
1567
- var __hasOwnProp$u = Object.prototype.hasOwnProperty;
1568
- var __propIsEnum$u = Object.prototype.propertyIsEnumerable;
1569
- var __defNormalProp$E = (obj, key, value) => key in obj ? __defProp$E(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1570
- var __spreadValues$t = (a, b) => {
1626
+ var __getOwnPropSymbols$w = Object.getOwnPropertySymbols;
1627
+ var __hasOwnProp$w = Object.prototype.hasOwnProperty;
1628
+ var __propIsEnum$w = Object.prototype.propertyIsEnumerable;
1629
+ var __defNormalProp$H = (obj, key, value) => key in obj ? __defProp$H(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1630
+ var __spreadValues$v = (a, b) => {
1571
1631
  for (var prop in b || (b = {}))
1572
- if (__hasOwnProp$u.call(b, prop))
1573
- __defNormalProp$E(a, prop, b[prop]);
1574
- if (__getOwnPropSymbols$u)
1575
- for (var prop of __getOwnPropSymbols$u(b)) {
1576
- if (__propIsEnum$u.call(b, prop))
1577
- __defNormalProp$E(a, prop, b[prop]);
1632
+ if (__hasOwnProp$w.call(b, prop))
1633
+ __defNormalProp$H(a, prop, b[prop]);
1634
+ if (__getOwnPropSymbols$w)
1635
+ for (var prop of __getOwnPropSymbols$w(b)) {
1636
+ if (__propIsEnum$w.call(b, prop))
1637
+ __defNormalProp$H(a, prop, b[prop]);
1578
1638
  }
1579
1639
  return a;
1580
1640
  };
@@ -1582,17 +1642,17 @@ var NgwMap = (function (exports) {
1582
1642
  var __objRest$3 = (source, exclude) => {
1583
1643
  var target = {};
1584
1644
  for (var prop in source)
1585
- if (__hasOwnProp$u.call(source, prop) && exclude.indexOf(prop) < 0)
1645
+ if (__hasOwnProp$w.call(source, prop) && exclude.indexOf(prop) < 0)
1586
1646
  target[prop] = source[prop];
1587
- if (source != null && __getOwnPropSymbols$u)
1588
- for (var prop of __getOwnPropSymbols$u(source)) {
1589
- if (exclude.indexOf(prop) < 0 && __propIsEnum$u.call(source, prop))
1647
+ if (source != null && __getOwnPropSymbols$w)
1648
+ for (var prop of __getOwnPropSymbols$w(source)) {
1649
+ if (exclude.indexOf(prop) < 0 && __propIsEnum$w.call(source, prop))
1590
1650
  target[prop] = source[prop];
1591
1651
  }
1592
1652
  return target;
1593
1653
  };
1594
- var __publicField$q = (obj, key, value) => __defNormalProp$E(obj, typeof key !== "symbol" ? key + "" : key, value);
1595
- var __async$q = (__this, __arguments, generator) => {
1654
+ var __publicField$s = (obj, key, value) => __defNormalProp$H(obj, typeof key !== "symbol" ? key + "" : key, value);
1655
+ var __async$s = (__this, __arguments, generator) => {
1596
1656
  return new Promise((resolve, reject) => {
1597
1657
  var fulfilled = (value) => {
1598
1658
  try {
@@ -1617,24 +1677,23 @@ var NgwMap = (function (exports) {
1617
1677
  class NgwConnector {
1618
1678
  constructor(options) {
1619
1679
  this.options = options;
1620
- __publicField$q(this, "id", ID$2++);
1621
- __publicField$q(this, "emitter", new EventEmitter());
1622
- __publicField$q(this, "user");
1623
- __publicField$q(this, "resources");
1624
- __publicField$q(this, "cache");
1625
- __publicField$q(this, "withCredentials");
1626
- __publicField$q(this, "client", `NextGIS-NGW-Connector/${pkg.version}`);
1627
- __publicField$q(this, "routeStr", "/api/component/pyramid/route");
1628
- __publicField$q(this, "activeRequests", {});
1629
- __publicField$q(this, "requestTransform");
1680
+ __publicField$s(this, "id", ID$2++);
1681
+ __publicField$s(this, "emitter", new EventEmitter());
1682
+ __publicField$s(this, "user");
1683
+ __publicField$s(this, "cache");
1684
+ __publicField$s(this, "withCredentials");
1685
+ __publicField$s(this, "client", `NextGIS-NGW-Connector/${pkg.version}`);
1686
+ __publicField$s(this, "routeStr", "/api/component/pyramid/route");
1687
+ __publicField$s(this, "activeRequests", {});
1688
+ __publicField$s(this, "requestTransform");
1630
1689
  const exist = findConnector(options);
1631
1690
  this.cache = new Cache({ namespace: options.cacheId });
1632
1691
  if (exist) {
1633
1692
  return exist;
1634
1693
  } else {
1635
- const { route, requestTransform, withCredentials } = this.options;
1636
- if (route) {
1637
- this.routeStr = route;
1694
+ const { route: route2, requestTransform, withCredentials } = this.options;
1695
+ if (route2) {
1696
+ this.routeStr = route2;
1638
1697
  }
1639
1698
  if (requestTransform) {
1640
1699
  this.requestTransform = requestTransform;
@@ -1642,10 +1701,6 @@ var NgwMap = (function (exports) {
1642
1701
  if (withCredentials !== void 0) {
1643
1702
  this.withCredentials = withCredentials;
1644
1703
  }
1645
- this.resources = new ResourcesControl({
1646
- connector: this,
1647
- cacheId: options.cacheId
1648
- });
1649
1704
  addConnector(this);
1650
1705
  }
1651
1706
  }
@@ -1657,7 +1712,6 @@ var NgwMap = (function (exports) {
1657
1712
  */
1658
1713
  clearCache() {
1659
1714
  this.cache.clean();
1660
- this.resources.cache.clean();
1661
1715
  }
1662
1716
  setRequestTransform(requestTransform) {
1663
1717
  this.requestTransform = requestTransform;
@@ -1679,14 +1733,14 @@ var NgwMap = (function (exports) {
1679
1733
  * Can be used to check connection.
1680
1734
  * @example
1681
1735
  * ```javascript
1682
- * const connector = new NgwConnector({baseUrl: 'https://demo.nextgis.com'});
1736
+ * const connector = new NgwConnector({ baseUrl: 'https://demo.nextgis.com' });
1683
1737
  * connector.connect()
1684
1738
  * .then(() => console.log('Ok'))
1685
1739
  * .catch((er) => console.log('Connection problem', er));
1686
1740
  * ```
1687
1741
  */
1688
1742
  connect() {
1689
- return __async$q(this, null, function* () {
1743
+ return __async$s(this, null, function* () {
1690
1744
  const auth = this.options.auth;
1691
1745
  if (auth) {
1692
1746
  const { login, password } = auth;
@@ -1694,90 +1748,630 @@ var NgwMap = (function (exports) {
1694
1748
  yield this._login({ login, password });
1695
1749
  }
1696
1750
  }
1697
- const routeUrl = `${this.routeStr}?client=${this.client}`;
1698
- return yield this.makeQuery(
1699
- routeUrl,
1700
- {},
1701
- {
1702
- cacheName: "route",
1703
- cache: true,
1704
- cacheProps: {
1705
- id: this.id,
1706
- auth,
1707
- baseUrl: this.options.baseUrl
1751
+ const routeUrl = `${this.routeStr}?client=${this.client}`;
1752
+ return yield this.makeQuery(
1753
+ routeUrl,
1754
+ {},
1755
+ {
1756
+ cacheName: "route",
1757
+ cache: true,
1758
+ cacheProps: {
1759
+ id: this.id,
1760
+ auth,
1761
+ baseUrl: this.options.baseUrl
1762
+ }
1763
+ }
1764
+ );
1765
+ });
1766
+ }
1767
+ /**
1768
+ * Quick way to change NextGIS Web user.
1769
+ * @param credentials - New user credentials
1770
+ */
1771
+ login(credentials, options) {
1772
+ this.logout();
1773
+ addConnector(this);
1774
+ return this._login(credentials, options);
1775
+ }
1776
+ /**
1777
+ * Disconnecting a user. Aborting all current requests
1778
+ */
1779
+ logout() {
1780
+ this.abort();
1781
+ removeConnector(this);
1782
+ this.options.auth = void 0;
1783
+ this.user = void 0;
1784
+ this.clearCache();
1785
+ this.emitter.emit("logout");
1786
+ }
1787
+ getUserInfo(credentials, options) {
1788
+ return __async$s(this, null, function* () {
1789
+ if (this.user && this.user.id) {
1790
+ return this.user;
1791
+ }
1792
+ if (credentials) {
1793
+ this.options.auth = credentials;
1794
+ }
1795
+ const options_ = __spreadValues$v({
1796
+ headers: this.getAuthorizationHeaders(credentials),
1797
+ cache: true
1798
+ }, options);
1799
+ return this.makeQuery(
1800
+ "/api/component/auth/current_user",
1801
+ {},
1802
+ options_
1803
+ );
1804
+ });
1805
+ }
1806
+ /**
1807
+ * Obtaining the required Headers for authentication of requests in the NGW.
1808
+ */
1809
+ getAuthorizationHeaders(credentials) {
1810
+ const client = this.makeClientId(credentials);
1811
+ if (client) {
1812
+ return {
1813
+ Authorization: `Basic ${client}`
1814
+ };
1815
+ }
1816
+ return {};
1817
+ }
1818
+ makeClientId(credentials) {
1819
+ credentials = credentials || this.options.auth;
1820
+ if (credentials) {
1821
+ const { login, password } = credentials;
1822
+ const encodedStr = [login, password].map(encodeURIComponent).join(":");
1823
+ {
1824
+ return window.btoa(encodedStr);
1825
+ }
1826
+ }
1827
+ }
1828
+ /** Stop all api requests */
1829
+ abort() {
1830
+ for (const abortController of Object.values(this.activeRequests)) {
1831
+ abortController.abort();
1832
+ }
1833
+ this.activeRequests = {};
1834
+ }
1835
+ getActiveApiRequests() {
1836
+ return __spreadValues$v({}, this.activeRequests);
1837
+ }
1838
+ route(name, ...rest) {
1839
+ return route(name, this, ...rest);
1840
+ }
1841
+ /**
1842
+ * Send request to NGW.
1843
+ * @param url - URL address to NGW
1844
+ * @param params - Query params
1845
+ * @param options - Request options
1846
+ */
1847
+ makeQuery(_0, _1) {
1848
+ return __async$s(this, arguments, function* (url, params, options = {}) {
1849
+ var _b;
1850
+ url = (this.options.baseUrl ? this.options.baseUrl : "") + url;
1851
+ if (!url) {
1852
+ throw new Error("Empty `url` not allowed");
1853
+ }
1854
+ if (params) {
1855
+ const _a = params, restParams = __objRest$3(_a, ["paramList"]);
1856
+ url = template(url, restParams);
1857
+ }
1858
+ url = encodeURI(fixUrlStr(url));
1859
+ options = __spreadValues$v({ withCredentials: this.withCredentials }, options);
1860
+ const {
1861
+ cache,
1862
+ signal: externalSignal,
1863
+ method = "GET",
1864
+ headers,
1865
+ cacheName,
1866
+ cacheProps,
1867
+ responseType,
1868
+ withCredentials
1869
+ } = options;
1870
+ const internalAbortController = new AbortController();
1871
+ const internalSignal = internalAbortController.signal;
1872
+ if (externalSignal) {
1873
+ if (externalSignal.aborted) {
1874
+ throw new AbortError();
1875
+ }
1876
+ externalSignal.addEventListener("abort", () => {
1877
+ internalAbortController.abort();
1878
+ });
1879
+ }
1880
+ options.signal = internalSignal;
1881
+ const createPromise = () => __async$s(this, null, function* () {
1882
+ const id = REQUEST_ID++;
1883
+ this.activeRequests[id] = internalAbortController;
1884
+ try {
1885
+ return this._loadData(url, options);
1886
+ } finally {
1887
+ this._cleanActiveRequest(id);
1888
+ }
1889
+ });
1890
+ if (cache && method === "GET") {
1891
+ const cacheOptions = cacheProps ? cacheProps : __spreadProps$g(__spreadValues$v({}, objectRemoveEmpty({
1892
+ headers,
1893
+ withCredentials,
1894
+ responseType,
1895
+ baseUrl: this.options.baseUrl,
1896
+ userId: (_b = this.user) == null ? void 0 : _b.id
1897
+ })), {
1898
+ params
1899
+ });
1900
+ return this.cache.add(
1901
+ cacheName || url,
1902
+ createPromise,
1903
+ cacheOptions,
1904
+ false
1905
+ );
1906
+ }
1907
+ return createPromise();
1908
+ });
1909
+ }
1910
+ _loadData(url, options) {
1911
+ options.responseType = options.responseType || "json";
1912
+ return new Promise((resolve, reject) => {
1913
+ var _a;
1914
+ if (this.user) {
1915
+ options = options || {};
1916
+ options.headers = __spreadValues$v(__spreadValues$v({}, this.getAuthorizationHeaders()), options.headers);
1917
+ }
1918
+ if (this.requestTransform) {
1919
+ const [transUrl, transOptions] = this.requestTransform(url, options);
1920
+ url = transUrl;
1921
+ options = transOptions;
1922
+ }
1923
+ let runOnAbort = void 0;
1924
+ loadData(url, resolve, options, reject, (handler) => {
1925
+ runOnAbort = handler;
1926
+ });
1927
+ (_a = options.signal) == null ? void 0 : _a.addEventListener("abort", () => {
1928
+ if (runOnAbort !== void 0) {
1929
+ runOnAbort();
1930
+ }
1931
+ reject(new AbortError());
1932
+ });
1933
+ }).catch((httpError) => {
1934
+ if (httpError.name !== "AbortError") {
1935
+ {
1936
+ console.warn("DEV WARN", httpError);
1937
+ }
1938
+ const er = this._handleHttpError(httpError);
1939
+ if (er) {
1940
+ throw er;
1941
+ }
1942
+ }
1943
+ throw httpError;
1944
+ });
1945
+ }
1946
+ _login(credentials, options) {
1947
+ return __async$s(this, null, function* () {
1948
+ try {
1949
+ const data = yield this.getUserInfo(credentials, options);
1950
+ this.user = data;
1951
+ this.emitter.emit("login", data);
1952
+ return data;
1953
+ } catch (er) {
1954
+ this.emitter.emit("login:error", er);
1955
+ throw er;
1956
+ }
1957
+ });
1958
+ }
1959
+ _cleanActiveRequest(requestId) {
1960
+ delete this.activeRequests[requestId];
1961
+ }
1962
+ _handleHttpError(er) {
1963
+ if (er) {
1964
+ if (er instanceof NgwError) {
1965
+ if (er.exception === "nextgisweb.resource.exception.ResourceNotFound") {
1966
+ throw new ResourceNotFoundError(er);
1967
+ } else if (er.exception === "nextgisweb.core.exception.InsufficientPermissions") {
1968
+ throw new InsufficientPermissionsError(er);
1969
+ }
1970
+ }
1971
+ }
1972
+ return er;
1973
+ }
1974
+ }
1975
+ __publicField$s(NgwConnector, "errors", {
1976
+ NgwError,
1977
+ ResourceNotFoundError
1978
+ });
1979
+
1980
+ function resourceCompare(res1, res2) {
1981
+ return objectDeepEqual(res1, res2);
1982
+ }
1983
+
1984
+ const exclude = ["description"];
1985
+ function resourceToQuery(resource, prefix = "") {
1986
+ prefix = prefix ? prefix + "__" : "";
1987
+ const query = {};
1988
+ for (const [key, value] of Object.entries(resource)) {
1989
+ if (exclude.indexOf(key) === -1) {
1990
+ if (isObject(value)) {
1991
+ if (key === "owner_user") {
1992
+ const children = resourceToQuery(value, key);
1993
+ Object.assign(query, children);
1994
+ } else if (key === "parent" && "id" in value) {
1995
+ query.parent_id = value.id;
1996
+ }
1997
+ } else if (defined(value)) {
1998
+ query[prefix + key] = value;
1999
+ }
2000
+ }
2001
+ }
2002
+ return query;
2003
+ }
2004
+
2005
+ var __defProp$G = Object.defineProperty;
2006
+ var __getOwnPropSymbols$v = Object.getOwnPropertySymbols;
2007
+ var __hasOwnProp$v = Object.prototype.hasOwnProperty;
2008
+ var __propIsEnum$v = Object.prototype.propertyIsEnumerable;
2009
+ var __defNormalProp$G = (obj, key, value) => key in obj ? __defProp$G(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
2010
+ var __spreadValues$u = (a, b) => {
2011
+ for (var prop in b || (b = {}))
2012
+ if (__hasOwnProp$v.call(b, prop))
2013
+ __defNormalProp$G(a, prop, b[prop]);
2014
+ if (__getOwnPropSymbols$v)
2015
+ for (var prop of __getOwnPropSymbols$v(b)) {
2016
+ if (__propIsEnum$v.call(b, prop))
2017
+ __defNormalProp$G(a, prop, b[prop]);
2018
+ }
2019
+ return a;
2020
+ };
2021
+ var __publicField$r = (obj, key, value) => __defNormalProp$G(obj, typeof key !== "symbol" ? key + "" : key, value);
2022
+ var __async$r = (__this, __arguments, generator) => {
2023
+ return new Promise((resolve, reject) => {
2024
+ var fulfilled = (value) => {
2025
+ try {
2026
+ step(generator.next(value));
2027
+ } catch (e) {
2028
+ reject(e);
2029
+ }
2030
+ };
2031
+ var rejected = (value) => {
2032
+ try {
2033
+ step(generator.throw(value));
2034
+ } catch (e) {
2035
+ reject(e);
2036
+ }
2037
+ };
2038
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
2039
+ step((generator = generator.apply(__this, __arguments)).next());
2040
+ });
2041
+ };
2042
+ class ResourcesControl {
2043
+ constructor({
2044
+ connector,
2045
+ cacheId
2046
+ }) {
2047
+ __publicField$r(this, "cache");
2048
+ __publicField$r(this, "connector");
2049
+ this.connector = connector;
2050
+ this.cache = new Cache({ namespace: cacheId });
2051
+ }
2052
+ // -------------------------------------------------------------------------
2053
+ // Resource Methods
2054
+ // -------------------------------------------------------------------------
2055
+ /**
2056
+ * Receive resource from NGW by id, keyname or search-object parameter.
2057
+ * @param resource - Resource id, keyname or search-object
2058
+ *
2059
+ * @remarks
2060
+ * Fetching resource would be cached to speed up next call
2061
+ */
2062
+ getOne(resource, requestOptions) {
2063
+ const opt = __spreadValues$u({}, requestOptions);
2064
+ if (typeof resource === "string") ; else if (typeof resource === "number") ; else if (isObject(resource)) {
2065
+ if (resource.id !== void 0) {
2066
+ resource.id;
2067
+ } else {
2068
+ if (resource.keyname) {
2069
+ resource.keyname;
2070
+ }
2071
+ if (resource.display_name) {
2072
+ resource.display_name;
2073
+ }
2074
+ }
2075
+ }
2076
+ if (typeof resource === "string") {
2077
+ return this._fetchResourceBy({ keyname: resource }, opt);
2078
+ } else if (typeof resource === "number") {
2079
+ return this._fetchResourceById(resource, opt);
2080
+ } else if (isObject(resource)) {
2081
+ return this._fetchResourceBy(resource, opt);
2082
+ }
2083
+ return Promise.resolve(void 0);
2084
+ }
2085
+ getOneOrFail(resource, requestOptions) {
2086
+ return this.getOne(resource, requestOptions).then((res) => {
2087
+ if (res) {
2088
+ return res;
2089
+ }
2090
+ throw new ResourceNotFoundError();
2091
+ });
2092
+ }
2093
+ /**
2094
+ * A fast way to retrieve resource ID for any resource definition.
2095
+ * @param resource - Any available resource definition
2096
+ *
2097
+ * @remarks
2098
+ * There are situations when exactly the resource id is needed
2099
+ * (for example, to compose the correct request to the api)
2100
+ * then this method will come in handy to facilitate the extraction of the identifier
2101
+ * if the resource is specified through a keyname or other parameters.
2102
+ */
2103
+ getId(resource, requestOptions) {
2104
+ if (typeof resource === "number") {
2105
+ return Promise.resolve(resource);
2106
+ } else if (typeof resource === "string" || isObject(resource)) {
2107
+ return this.getOne(resource, requestOptions).then((res) => {
2108
+ if (res) {
2109
+ return res.resource.id;
2110
+ }
2111
+ });
2112
+ }
2113
+ return Promise.resolve(void 0);
2114
+ }
2115
+ /**
2116
+ * A fast way to retrieve resource ID for any resource definition.
2117
+ * @param resource - Any available resource definition
2118
+ *
2119
+ * @remarks
2120
+ * Similar with {@link NgwConnector.getResourceId | getResourceId} but rise error if resource is not exist.
2121
+ * To not make one more checks if the resource is definitely exists
2122
+ */
2123
+ getIdOrFail(resource, requestOptions) {
2124
+ return this.getId(resource, requestOptions).then((resp) => {
2125
+ if (resp === void 0) {
2126
+ throw new Error();
2127
+ }
2128
+ return resp;
2129
+ });
2130
+ }
2131
+ getMany(resource, requestOptions) {
2132
+ return this._resourceCacheFilter(resource).then((items) => {
2133
+ if (!items.length) {
2134
+ const query = {};
2135
+ if (resource.keyname) {
2136
+ query.keyname = resource.keyname;
2137
+ } else {
2138
+ Object.assign(query, resourceToQuery(resource));
2139
+ }
2140
+ return this.connector.get("resource.search", requestOptions, __spreadValues$u({
2141
+ serialization: "full"
2142
+ }, query)).then((resources) => {
2143
+ if ((requestOptions == null ? void 0 : requestOptions.cache) && resources) {
2144
+ for (const x of resources) {
2145
+ this.cache.add("resource.item", Promise.resolve(x), {
2146
+ id: x.resource.id
2147
+ });
2148
+ }
2149
+ }
2150
+ return resources;
2151
+ });
2152
+ }
2153
+ return items;
2154
+ });
2155
+ }
2156
+ getParent(resource, requestOptions) {
2157
+ return this.getOne(resource, requestOptions).then((child) => {
2158
+ if (child) {
2159
+ return this.getOne(child.resource.parent.id, requestOptions);
2160
+ }
2161
+ return Promise.resolve(void 0);
2162
+ });
2163
+ }
2164
+ getChildrenOf(resource, requestOptions) {
2165
+ return this.getIdOrFail(resource).then(
2166
+ (parent) => this._getChildrenOf(parent, requestOptions)
2167
+ );
2168
+ }
2169
+ update(resource, data) {
2170
+ return this.getId(resource).then((id) => {
2171
+ if (id !== void 0) {
2172
+ return this.connector.put("resource.item", { data }, { id });
2173
+ }
2174
+ });
2175
+ }
2176
+ /**
2177
+ * Fast way to delete resource from NGW and clean cache.
2178
+ * @param resource - Resource definition
2179
+ */
2180
+ delete(resource) {
2181
+ return this.getId(resource).then((id) => {
2182
+ if (id !== void 0) {
2183
+ return this.connector.delete("resource.item", null, { id }).then(() => {
2184
+ this._cleanResourceItemCache(id);
2185
+ return void 0;
2186
+ });
2187
+ }
2188
+ });
2189
+ }
2190
+ _getChildrenOf(parent, requestOptions, _items = []) {
2191
+ return this.connector.get("resource.collection", requestOptions, {
2192
+ parent
2193
+ }).then((items) => {
2194
+ const recursivePromises = [];
2195
+ for (const item of items) {
2196
+ if (requestOptions == null ? void 0 : requestOptions.cache) {
2197
+ this.cache.add("resource.item", Promise.resolve(item), {
2198
+ id: item.resource.id
2199
+ });
2200
+ }
2201
+ _items.push(item);
2202
+ if ((requestOptions == null ? void 0 : requestOptions.recursive) && item.resource.children) {
2203
+ recursivePromises.push(
2204
+ this._getChildrenOf(item.resource.id, requestOptions, _items)
2205
+ );
2206
+ }
2207
+ }
2208
+ if (recursivePromises.length) {
2209
+ return Promise.all(recursivePromises).then(() => {
2210
+ return _items;
2211
+ });
2212
+ }
2213
+ return _items;
2214
+ });
2215
+ }
2216
+ _cleanResourceItemCache(id) {
2217
+ return __async$r(this, null, function* () {
2218
+ var _a;
2219
+ const all = this.cache.all();
2220
+ const toDelete = [];
2221
+ for (const c of all) {
2222
+ const cid = (_a = c.props) == null ? void 0 : _a.id;
2223
+ if (["resource.item", "resource"].includes(c.key) && cid !== void 0) {
2224
+ if (typeof cid === "number") {
2225
+ if (cid === id) {
2226
+ toDelete.push(c);
2227
+ }
2228
+ } else {
2229
+ const rid = yield this.getId(cid);
2230
+ if (rid === id) {
2231
+ toDelete.push(c);
2232
+ }
2233
+ }
2234
+ }
2235
+ }
2236
+ for (const d of toDelete) {
2237
+ this.cache.delete(d);
2238
+ }
2239
+ });
2240
+ }
2241
+ _fetchResourceById(id, requestOptions) {
2242
+ return this.connector.get("resource.item", requestOptions, { id });
2243
+ }
2244
+ _fetchResourceBy(resource, requestOptions) {
2245
+ return this.getMany(resource, requestOptions).then((resources) => {
2246
+ return resources[0];
2247
+ });
2248
+ }
2249
+ _resourceCacheFilter(resource) {
2250
+ return Promise.all(this.cache.matchAll("resource.item")).then(
2251
+ (resources) => {
2252
+ const items = [];
2253
+ resources.filter((x) => {
2254
+ if (x) {
2255
+ if (resource.keyname && x.resource.keyname) {
2256
+ return resource.keyname === x.resource.keyname;
2257
+ }
2258
+ if (defined(resource.id) && defined(x.resource.id)) {
2259
+ return resource.id === x.resource.id;
2260
+ }
2261
+ return resourceCompare(resource, x.resource);
2262
+ }
2263
+ });
2264
+ return items;
2265
+ }
2266
+ );
2267
+ }
2268
+ }
2269
+
2270
+ var __defProp$F = Object.defineProperty;
2271
+ var __getOwnPropSymbols$u = Object.getOwnPropertySymbols;
2272
+ var __hasOwnProp$u = Object.prototype.hasOwnProperty;
2273
+ var __propIsEnum$u = Object.prototype.propertyIsEnumerable;
2274
+ var __defNormalProp$F = (obj, key, value) => key in obj ? __defProp$F(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
2275
+ var __spreadValues$t = (a, b) => {
2276
+ for (var prop in b || (b = {}))
2277
+ if (__hasOwnProp$u.call(b, prop))
2278
+ __defNormalProp$F(a, prop, b[prop]);
2279
+ if (__getOwnPropSymbols$u)
2280
+ for (var prop of __getOwnPropSymbols$u(b)) {
2281
+ if (__propIsEnum$u.call(b, prop))
2282
+ __defNormalProp$F(a, prop, b[prop]);
2283
+ }
2284
+ return a;
2285
+ };
2286
+ var __async$q = (__this, __arguments, generator) => {
2287
+ return new Promise((resolve, reject) => {
2288
+ var fulfilled = (value) => {
2289
+ try {
2290
+ step(generator.next(value));
2291
+ } catch (e) {
2292
+ reject(e);
2293
+ }
2294
+ };
2295
+ var rejected = (value) => {
2296
+ try {
2297
+ step(generator.throw(value));
2298
+ } catch (e) {
2299
+ reject(e);
2300
+ }
2301
+ };
2302
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
2303
+ step((generator = generator.apply(__this, __arguments)).next());
2304
+ });
2305
+ };
2306
+ function apiRequest(opt) {
2307
+ return __async$q(this, null, function* () {
2308
+ const { params, name, connector, requestOptions } = opt;
2309
+ const apiItems = yield connector.connect();
2310
+ let apiItem = apiItems && apiItems[name];
2311
+ if (apiItem) {
2312
+ apiItem = [...apiItem];
2313
+ let url = apiItem.shift();
2314
+ if (apiItem.length) {
2315
+ const replaceParams = {};
2316
+ for (let fry = 0; fry < apiItem.length; fry++) {
2317
+ const arg = apiItem[fry];
2318
+ replaceParams[fry] = `{${arg}}`;
2319
+ if (params[arg] === void 0) {
2320
+ throw new Error(`\`${arg}\` URL API argument is not specified`);
2321
+ }
2322
+ }
2323
+ if (url) {
2324
+ url = template(url, replaceParams);
2325
+ }
2326
+ }
2327
+ if (params) {
2328
+ const paramArray = [];
2329
+ const paramList = params.paramList;
2330
+ if (Array.isArray(paramList)) {
2331
+ paramList.forEach(([key, value]) => {
2332
+ paramArray.push(`${key}=${value}`);
2333
+ });
2334
+ }
2335
+ for (const p in params) {
2336
+ if (apiItem.indexOf(p) === -1) {
2337
+ paramArray.push(`${p}=${params[p]}`);
1708
2338
  }
1709
2339
  }
1710
- );
1711
- });
1712
- }
1713
- /**
1714
- * Quick way to change NextGIS Web user.
1715
- * @param credentials - New user credentials
1716
- */
1717
- login(credentials, options) {
1718
- this.logout();
1719
- addConnector(this);
1720
- return this._login(credentials, options);
1721
- }
1722
- /**
1723
- * Disconnecting a user. Aborting all current requests
1724
- */
1725
- logout() {
1726
- this.abort();
1727
- removeConnector(this);
1728
- this.options.auth = void 0;
1729
- this.user = void 0;
1730
- this.clearCache();
1731
- this.emitter.emit("logout");
1732
- }
1733
- getUserInfo(credentials, options) {
1734
- return __async$q(this, null, function* () {
1735
- if (this.user && this.user.id) {
1736
- return this.user;
2340
+ if (paramArray.length) {
2341
+ url = `${url}?${paramArray.join("&")}`;
2342
+ }
1737
2343
  }
1738
- if (credentials) {
1739
- this.options.auth = credentials;
2344
+ if (url) {
2345
+ return connector.makeQuery(url, params, __spreadValues$t({
2346
+ cacheName: name
2347
+ }, requestOptions));
2348
+ } else {
2349
+ throw new Error("Request URL is not set");
1740
2350
  }
1741
- const options_ = __spreadValues$t({
1742
- headers: this.getAuthorizationHeaders(credentials)
1743
- }, options);
1744
- return this.makeQuery(
1745
- "/api/component/auth/current_user",
1746
- {},
1747
- options_
1748
- );
2351
+ } else {
2352
+ return void 0;
2353
+ }
2354
+ });
2355
+ }
2356
+
2357
+ var __defProp$E = Object.defineProperty;
2358
+ var __defNormalProp$E = (obj, key, value) => key in obj ? __defProp$E(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
2359
+ var __publicField$q = (obj, key, value) => __defNormalProp$E(obj, key + "" , value);
2360
+ class NgwConnectorExtended extends NgwConnector {
2361
+ constructor(options) {
2362
+ super(options);
2363
+ __publicField$q(this, "resources");
2364
+ this.resources = new ResourcesControl({
2365
+ connector: this,
2366
+ cacheId: options.cacheId
1749
2367
  });
1750
2368
  }
1751
2369
  /**
1752
- * Obtaining the required Headers for authentication of requests in the NGW.
2370
+ * Clear the cache.
1753
2371
  */
1754
- getAuthorizationHeaders(credentials) {
1755
- const client = this.makeClientId(credentials);
1756
- if (client) {
1757
- return {
1758
- Authorization: `Basic ${client}`
1759
- };
1760
- }
1761
- }
1762
- makeClientId(credentials) {
1763
- credentials = credentials || this.options.auth;
1764
- if (credentials) {
1765
- const { login, password } = credentials;
1766
- const encodedStr = [login, password].map(encodeURIComponent).join(":");
1767
- {
1768
- return window.btoa(encodedStr);
1769
- }
1770
- }
1771
- }
1772
- /** Stop all api requests */
1773
- abort() {
1774
- for (const abortController of Object.values(this.activeRequests)) {
1775
- abortController.abort();
1776
- }
1777
- this.activeRequests = {};
1778
- }
1779
- getActiveApiRequests() {
1780
- return __spreadValues$t({}, this.activeRequests);
2372
+ clearCache() {
2373
+ super.clearCache();
2374
+ this.resources.cache.clean();
1781
2375
  }
1782
2376
  /**
1783
2377
  * Send request to NGW api router.
@@ -1896,75 +2490,6 @@ var NgwMap = (function (exports) {
1896
2490
  options
1897
2491
  );
1898
2492
  }
1899
- /**
1900
- * Send request to NGW.
1901
- * @param url - URL address to NGW
1902
- * @param params - Query params
1903
- * @param options - Request options
1904
- */
1905
- makeQuery(_0, _1) {
1906
- return __async$q(this, arguments, function* (url, params, options = {}) {
1907
- var _b;
1908
- url = (this.options.baseUrl ? this.options.baseUrl : "") + url;
1909
- if (!url) {
1910
- throw new Error("Empty `url` not allowed");
1911
- }
1912
- if (params) {
1913
- const _a = params, restParams = __objRest$3(_a, ["paramList"]);
1914
- url = template(url, restParams);
1915
- }
1916
- url = encodeURI(fixUrlStr(url));
1917
- options = __spreadValues$t({ withCredentials: this.withCredentials }, options);
1918
- const {
1919
- cache,
1920
- signal: externalSignal,
1921
- method = "GET",
1922
- headers,
1923
- cacheName,
1924
- cacheProps,
1925
- responseType,
1926
- withCredentials
1927
- } = options;
1928
- const internalAbortController = new AbortController();
1929
- const internalSignal = internalAbortController.signal;
1930
- if (externalSignal) {
1931
- if (externalSignal.aborted) {
1932
- throw new AbortError();
1933
- }
1934
- externalSignal.addEventListener("abort", () => {
1935
- internalAbortController.abort();
1936
- });
1937
- }
1938
- options.signal = internalSignal;
1939
- const createPromise = () => __async$q(this, null, function* () {
1940
- const id = REQUEST_ID++;
1941
- this.activeRequests[id] = internalAbortController;
1942
- try {
1943
- return this._loadData(url, options);
1944
- } finally {
1945
- this._cleanActiveRequest(id);
1946
- }
1947
- });
1948
- if (cache && method === "GET") {
1949
- const cacheOptions = cacheProps ? cacheProps : __spreadProps$g(__spreadValues$t({}, objectRemoveEmpty({
1950
- headers,
1951
- withCredentials,
1952
- responseType,
1953
- baseUrl: this.options.baseUrl,
1954
- userId: (_b = this.user) == null ? void 0 : _b.id
1955
- })), {
1956
- params
1957
- });
1958
- return this.cache.add(
1959
- cacheName || url,
1960
- createPromise,
1961
- cacheOptions,
1962
- false
1963
- );
1964
- }
1965
- return createPromise();
1966
- });
1967
- }
1968
2493
  // -------------------------------------------------------------------------
1969
2494
  // Resource Methods
1970
2495
  // -------------------------------------------------------------------------
@@ -2040,78 +2565,7 @@ var NgwMap = (function (exports) {
2040
2565
  deleteResource(resource) {
2041
2566
  return this.resources.delete(resource);
2042
2567
  }
2043
- /**
2044
- * @internal
2045
- */
2046
- _loadData(url, options) {
2047
- options.responseType = options.responseType || "json";
2048
- return new Promise((resolve, reject) => {
2049
- var _a;
2050
- if (this.user) {
2051
- options = options || {};
2052
- options.headers = __spreadValues$t(__spreadValues$t({}, this.getAuthorizationHeaders()), options.headers);
2053
- }
2054
- if (this.requestTransform) {
2055
- const [transUrl, transOptions] = this.requestTransform(url, options);
2056
- url = transUrl;
2057
- options = transOptions;
2058
- }
2059
- let runOnAbort = void 0;
2060
- loadData(url, resolve, options, reject, (handler) => {
2061
- runOnAbort = handler;
2062
- });
2063
- (_a = options.signal) == null ? void 0 : _a.addEventListener("abort", () => {
2064
- if (runOnAbort !== void 0) {
2065
- runOnAbort();
2066
- }
2067
- reject(new AbortError());
2068
- });
2069
- }).catch((httpError) => {
2070
- if (httpError.name !== "AbortError") {
2071
- {
2072
- console.warn("DEV WARN", httpError);
2073
- }
2074
- const er = this._handleHttpError(httpError);
2075
- if (er) {
2076
- throw er;
2077
- }
2078
- }
2079
- throw httpError;
2080
- });
2081
- }
2082
- _login(credentials, options) {
2083
- return __async$q(this, null, function* () {
2084
- try {
2085
- const data = yield this.getUserInfo(credentials, options);
2086
- this.user = data;
2087
- this.emitter.emit("login", data);
2088
- return data;
2089
- } catch (er) {
2090
- this.emitter.emit("login:error", er);
2091
- throw er;
2092
- }
2093
- });
2094
- }
2095
- _cleanActiveRequest(requestId) {
2096
- delete this.activeRequests[requestId];
2097
- }
2098
- _handleHttpError(er) {
2099
- if (er) {
2100
- if (er instanceof NgwError) {
2101
- if (er.exception === "nextgisweb.resource.exception.ResourceNotFound") {
2102
- throw new ResourceNotFoundError(er);
2103
- } else if (er.exception === "nextgisweb.core.exception.InsufficientPermissions") {
2104
- throw new InsufficientPermissionsError(er);
2105
- }
2106
- }
2107
- }
2108
- return er;
2109
- }
2110
2568
  }
2111
- __publicField$q(NgwConnector, "errors", {
2112
- NgwError,
2113
- ResourceNotFoundError
2114
- });
2115
2569
 
2116
2570
  var __defProp$D = Object.defineProperty;
2117
2571
  var __getOwnPropSymbols$t = Object.getOwnPropertySymbols;
@@ -5304,7 +5758,6 @@ var NgwMap = (function (exports) {
5304
5758
  __publicField$c(this, "emitter", new EventEmitter());
5305
5759
  __publicField$c(this, "_extent");
5306
5760
  __publicField$c(this, "response");
5307
- __publicField$c(this, "_webmapLayersIds");
5308
5761
  __publicField$c(this, "_webmapBaselayersIds", []);
5309
5762
  __publicField$c(this, "_lastActiveBaselayer");
5310
5763
  const r = options.resourceId;
@@ -5349,7 +5802,6 @@ var NgwMap = (function (exports) {
5349
5802
  }
5350
5803
  delete this.layer;
5351
5804
  delete this.response;
5352
- delete this._webmapLayersIds;
5353
5805
  }
5354
5806
  showLayer() {
5355
5807
  return __async$a(this, null, function* () {
@@ -5440,26 +5892,19 @@ var NgwMap = (function (exports) {
5440
5892
  getIdentificationIds(options) {
5441
5893
  return __async$a(this, null, function* () {
5442
5894
  const visibleLayers = [];
5443
- let ids = this._webmapLayersIds;
5444
- if (!ids) {
5445
- ids = yield this._getWebMapIds(options);
5446
- this._webmapLayersIds = ids;
5447
- }
5448
- if (ids && ids.length) {
5449
- let deps = this.getDependLayers();
5450
- deps = deps.sort((a, b) => b.id - a.id);
5451
- deps.forEach((x) => {
5452
- const item = x.item;
5453
- const parentId = item.parentId;
5454
- if (parentId !== void 0 && item.item_type === "layer" && item.layer_identifiable) {
5455
- const visible = x.properties.property("visibility");
5456
- const isVisible = visible.get() && !visible.isBlocked();
5457
- if (isVisible) {
5458
- visibleLayers.push(parentId);
5459
- }
5895
+ let deps = this.getDependLayers();
5896
+ deps = deps.sort((a, b) => b.id - a.id);
5897
+ deps.forEach((x) => {
5898
+ const item = x.item;
5899
+ const parentId = item.style_parent_id;
5900
+ if (parentId !== void 0 && item.item_type === "layer" && item.layer_identifiable) {
5901
+ const visible = x.properties.property("visibility");
5902
+ const isVisible = visible.get() && !visible.isBlocked();
5903
+ if (isVisible) {
5904
+ visibleLayers.push(parentId);
5460
5905
  }
5461
- });
5462
- }
5906
+ }
5907
+ });
5463
5908
  return visibleLayers;
5464
5909
  });
5465
5910
  }
@@ -5588,31 +6033,6 @@ var NgwMap = (function (exports) {
5588
6033
  }
5589
6034
  return item;
5590
6035
  }
5591
- _getWebMapIds(options) {
5592
- return __async$a(this, null, function* () {
5593
- const webMapItem = this.layer;
5594
- if (webMapItem && webMapItem.item.item_type === "root") {
5595
- const layers = webMapItem.tree.getDescendants();
5596
- const promises = [];
5597
- for (const x of layers) {
5598
- const item = x.item;
5599
- if (item.item_type === "layer") {
5600
- const id = item.layer_style_id;
5601
- const promise = this.options.connector.getResource(id, options).then((y) => {
5602
- if (y) {
5603
- const parentId = Number(y.resource.parent.id);
5604
- item.parentId = parentId;
5605
- return parentId;
5606
- }
5607
- });
5608
- promises.push(promise);
5609
- }
5610
- }
5611
- const ids = yield Promise.all(promises);
5612
- return ids.filter((x) => x !== void 0);
5613
- }
5614
- });
5615
- }
5616
6036
  }
5617
6037
 
5618
6038
  var __defProp$h = Object.defineProperty;
@@ -5667,7 +6087,7 @@ var NgwMap = (function (exports) {
5667
6087
  } else {
5668
6088
  throw new Error("url is not defined");
5669
6089
  }
5670
- this.connector = new NgwConnector({
6090
+ this.connector = new NgwConnectorExtended({
5671
6091
  baseUrl: this.url,
5672
6092
  auth: this.options.auth
5673
6093
  });
@@ -9179,7 +9599,7 @@ var NgwMap = (function (exports) {
9179
9599
  });
9180
9600
  }
9181
9601
  if (!options.connector) {
9182
- options.connector = new NgwConnector({
9602
+ options.connector = new NgwConnectorExtended({
9183
9603
  baseUrl: options.baseUrl || "",
9184
9604
  auth: options.auth,
9185
9605
  withCredentials: options.withCredentials
@@ -9453,7 +9873,7 @@ var NgwMap = (function (exports) {
9453
9873
  if (mem.layer.getDependLayers) {
9454
9874
  const dependLayers = mem.layer.getDependLayers();
9455
9875
  const dependFit = dependLayers.find((x) => {
9456
- return x.item && x.item.parentId === id;
9876
+ return x.item && x.item.style_parent_id === id;
9457
9877
  });
9458
9878
  if (dependFit) {
9459
9879
  return dependFit.layer;