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