@nextgis/ngw-connector 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,11 +1,11 @@
1
- /** Bundle of @nextgis/ngw-connector; version: 3.0.0-alpha.4; author: NextGIS */
1
+ /** Bundle of @nextgis/ngw-connector; version: 3.0.0-alpha.6; author: NextGIS */
2
+ import { objectDeepEqual, objectRemoveEmpty, fixUrlStr, defined } from '@nextgis/utils';
2
3
  import { EventEmitter } from 'events';
3
4
  import Cache from '@nextgis/cache';
4
- import { objectDeepEqual, defined, objectRemoveEmpty, fixUrlStr } from '@nextgis/utils';
5
5
 
6
6
  var pkg = {
7
7
  name: "@nextgis/ngw-connector",
8
- version: "3.0.0-alpha.4",
8
+ version: "3.0.0-alpha.6",
9
9
  _priority: 12,
10
10
  description: "A lightweight HTTP client optimized for use with NextGIS Web API",
11
11
  main: "index.js",
@@ -14,15 +14,15 @@ var pkg = {
14
14
  jsdelivr: "lib/ngw-connector.global.prod.js",
15
15
  types: "lib/index.d.ts",
16
16
  dependencies: {
17
- "@nextgis/cache": "^3.0.0-alpha.4",
18
- "@nextgis/cancelable-promise": "^3.0.0-alpha.3",
19
- "@nextgis/utils": "^3.0.0-alpha.4",
17
+ "@nextgis/cache": "^3.0.0-alpha.5",
18
+ "@nextgis/cancelable-promise": "^3.0.0-alpha.5",
19
+ "@nextgis/utils": "^3.0.0-alpha.5",
20
20
  "@types/events": "^3.0.0",
21
21
  events: "*",
22
22
  "form-data": "^4.0.0"
23
23
  },
24
24
  devDependencies: {
25
- "@nextgis/build-tools": "^3.0.0-alpha.3"
25
+ "@nextgis/build-tools": "^3.0.0-alpha.5"
26
26
  },
27
27
  scripts: {
28
28
  clean: "rimraf ./lib",
@@ -56,87 +56,179 @@ var pkg = {
56
56
  type: "git",
57
57
  url: "git://github.com/nextgis/nextgis_frontend.git"
58
58
  },
59
+ engines: {
60
+ node: ">=18.20.4"
61
+ },
59
62
  gitHead: "edc68033f914f6c95c4125b9738bba6d36e990b4"
60
63
  };
61
64
 
62
- var __defProp$6 = Object.defineProperty;
63
- var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
64
- var __publicField$5 = (obj, key, value) => __defNormalProp$6(obj, typeof key !== "symbol" ? key + "" : key, value);
65
+ const CONNECTORS = [];
66
+ function addConnector(connector) {
67
+ CONNECTORS.push(connector);
68
+ }
69
+ function findConnector(options) {
70
+ return CONNECTORS.find((x) => {
71
+ if (x.options.baseUrl === options.baseUrl) {
72
+ if (options.auth) {
73
+ if (x.options.auth) {
74
+ return objectDeepEqual(x.options.auth, options.auth);
75
+ }
76
+ } else {
77
+ return true;
78
+ }
79
+ }
80
+ });
81
+ }
82
+ function removeConnector(connector) {
83
+ const index = CONNECTORS.indexOf(connector);
84
+ if (index !== -1) {
85
+ CONNECTORS.splice(index, 1);
86
+ }
87
+ }
88
+
89
+ class AbortError extends Error {
90
+ constructor(message = "AbortError") {
91
+ super(message);
92
+ this.name = "AbortError";
93
+ }
94
+ }
95
+
96
+ var __defProp$a = Object.defineProperty;
97
+ var __defNormalProp$a = (obj, key, value) => key in obj ? __defProp$a(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
98
+ var __publicField$7 = (obj, key, value) => __defNormalProp$a(obj, typeof key !== "symbol" ? key + "" : key, value);
65
99
  class NgwError extends Error {
66
100
  constructor(er) {
67
101
  super();
68
- __publicField$5(this, "name", "NgwError");
69
- __publicField$5(this, "title");
70
- __publicField$5(this, "message");
71
- __publicField$5(this, "detail");
72
- __publicField$5(this, "exception");
73
- __publicField$5(this, "status_code");
74
- __publicField$5(this, "data");
75
- __publicField$5(this, "guru_meditation");
102
+ __publicField$7(this, "name", "NgwError");
103
+ __publicField$7(this, "title");
104
+ __publicField$7(this, "message");
105
+ __publicField$7(this, "detail");
106
+ __publicField$7(this, "exception");
107
+ __publicField$7(this, "status_code");
108
+ __publicField$7(this, "data");
109
+ __publicField$7(this, "guru_meditation");
76
110
  Object.assign(this, er);
77
111
  Object.setPrototypeOf(this, NgwError.prototype);
78
112
  }
79
113
  }
80
114
 
81
- var __defProp$5 = Object.defineProperty;
82
- var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
83
- var __publicField$4 = (obj, key, value) => __defNormalProp$5(obj, typeof key !== "symbol" ? key + "" : key, value);
115
+ var __defProp$9 = Object.defineProperty;
116
+ var __defNormalProp$9 = (obj, key, value) => key in obj ? __defProp$9(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
117
+ var __publicField$6 = (obj, key, value) => __defNormalProp$9(obj, typeof key !== "symbol" ? key + "" : key, value);
118
+ class InsufficientPermissionsError extends NgwError {
119
+ constructor(obj) {
120
+ super(obj);
121
+ __publicField$6(this, "name", "InsufficientPermissionsError");
122
+ __publicField$6(this, "exception", "nextgisweb.core.exception.InsufficientPermissions");
123
+ Object.setPrototypeOf(this, InsufficientPermissionsError.prototype);
124
+ }
125
+ }
126
+
127
+ var __defProp$8 = Object.defineProperty;
128
+ var __defNormalProp$8 = (obj, key, value) => key in obj ? __defProp$8(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
129
+ var __publicField$5 = (obj, key, value) => __defNormalProp$8(obj, typeof key !== "symbol" ? key + "" : key, value);
84
130
  class ResourceNotFoundError extends NgwError {
85
131
  constructor(obj) {
86
132
  super(obj);
87
- __publicField$4(this, "name", "ResourceNotFoundError");
88
- __publicField$4(this, "exception", "nextgisweb.resource.exception.ResourceNotFound");
133
+ __publicField$5(this, "name", "ResourceNotFoundError");
134
+ __publicField$5(this, "exception", "nextgisweb.resource.exception.ResourceNotFound");
89
135
  Object.setPrototypeOf(this, ResourceNotFoundError.prototype);
90
136
  }
91
137
  }
92
138
 
93
- function isObject(val) {
94
- return Object.prototype.toString.call(val) === "[object Object]";
139
+ var __defProp$7 = Object.defineProperty;
140
+ var __defNormalProp$7 = (obj, key, value) => key in obj ? __defProp$7(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
141
+ var __publicField$4 = (obj, key, value) => __defNormalProp$7(obj, typeof key !== "symbol" ? key + "" : key, value);
142
+ class BaseAPIError extends Error {
143
+ constructor(message) {
144
+ super(message || "Something went wrong.");
145
+ __publicField$4(this, "title");
146
+ this.name = "BaseAPIError";
147
+ this.title = "Unknown API error";
148
+ if (Error.captureStackTrace) {
149
+ Error.captureStackTrace(this, BaseAPIError);
150
+ }
151
+ }
95
152
  }
96
-
97
- function resourceCompare(res1, res2) {
98
- return objectDeepEqual(res1, res2);
153
+ class NetworksResponseError extends BaseAPIError {
154
+ // prettier-ignore
155
+ constructor(message) {
156
+ super(message || "There is no response from the server or problem connecting to server.");
157
+ __publicField$4(this, "detail");
158
+ this.title = "Network error";
159
+ this.detail = "Check network connectivity and try again later.";
160
+ }
99
161
  }
100
-
101
- const exclude = ["description"];
102
- function resourceToQuery(resource, prefix = "") {
103
- prefix = prefix ? prefix + "__" : "";
104
- const query = {};
105
- for (const [key, value] of Object.entries(resource)) {
106
- if (exclude.indexOf(key) === -1) {
107
- if (isObject(value)) {
108
- if (key === "owner_user") {
109
- const children = resourceToQuery(value, key);
110
- Object.assign(query, children);
111
- } else if (key === "parent" && "id" in value) {
112
- query.parent_id = value.id;
113
- }
114
- } else if (defined(value)) {
115
- query[prefix + key] = value;
116
- }
162
+ class InvalidResponseError extends BaseAPIError {
163
+ constructor(message) {
164
+ super(message || "Something went wrong.");
165
+ this.title = "Unexpected server response";
166
+ }
167
+ }
168
+ class ServerResponseError extends BaseAPIError {
169
+ constructor(data) {
170
+ super(data.message);
171
+ __publicField$4(this, "detail");
172
+ __publicField$4(this, "data");
173
+ this.title = data.title || this.title;
174
+ this.detail = data.detail || null;
175
+ this.data = data;
176
+ }
177
+ }
178
+ class LunkwillError extends Error {
179
+ // prettier-ignore
180
+ constructor(message, data = {}) {
181
+ super(message || "Unexpected error while processing long-running request.");
182
+ __publicField$4(this, "title");
183
+ __publicField$4(this, "data");
184
+ this.name = "LunkwillError";
185
+ this.title = "Long-running request error";
186
+ this.data = data;
187
+ if (Error.captureStackTrace) {
188
+ Error.captureStackTrace(this, LunkwillError);
117
189
  }
118
190
  }
119
- return query;
191
+ }
192
+ class LunkwillRequestCancelled extends LunkwillError {
193
+ constructor(data) {
194
+ super("Long-running request was cancelled.", data);
195
+ }
196
+ }
197
+ class LunkwillRequestFailed extends LunkwillError {
198
+ constructor(data) {
199
+ super(void 0, data);
200
+ }
120
201
  }
121
202
 
122
- var __defProp$4 = Object.defineProperty;
123
- var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
124
- var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
125
- var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
126
- var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
127
- var __spreadValues$2 = (a, b) => {
203
+ var __defProp$6 = Object.defineProperty;
204
+ var __getOwnPropSymbols$4 = Object.getOwnPropertySymbols;
205
+ var __hasOwnProp$4 = Object.prototype.hasOwnProperty;
206
+ var __propIsEnum$4 = Object.prototype.propertyIsEnumerable;
207
+ var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
208
+ var __spreadValues$4 = (a, b) => {
128
209
  for (var prop in b || (b = {}))
129
- if (__hasOwnProp$2.call(b, prop))
130
- __defNormalProp$4(a, prop, b[prop]);
131
- if (__getOwnPropSymbols$2)
132
- for (var prop of __getOwnPropSymbols$2(b)) {
133
- if (__propIsEnum$2.call(b, prop))
134
- __defNormalProp$4(a, prop, b[prop]);
210
+ if (__hasOwnProp$4.call(b, prop))
211
+ __defNormalProp$6(a, prop, b[prop]);
212
+ if (__getOwnPropSymbols$4)
213
+ for (var prop of __getOwnPropSymbols$4(b)) {
214
+ if (__propIsEnum$4.call(b, prop))
215
+ __defNormalProp$6(a, prop, b[prop]);
135
216
  }
136
217
  return a;
137
218
  };
138
- var __publicField$3 = (obj, key, value) => __defNormalProp$4(obj, typeof key !== "symbol" ? key + "" : key, value);
139
- var __async$2 = (__this, __arguments, generator) => {
219
+ var __objRest$2 = (source, exclude) => {
220
+ var target = {};
221
+ for (var prop in source)
222
+ if (__hasOwnProp$4.call(source, prop) && exclude.indexOf(prop) < 0)
223
+ target[prop] = source[prop];
224
+ if (source != null && __getOwnPropSymbols$4)
225
+ for (var prop of __getOwnPropSymbols$4(source)) {
226
+ if (exclude.indexOf(prop) < 0 && __propIsEnum$4.call(source, prop))
227
+ target[prop] = source[prop];
228
+ }
229
+ return target;
230
+ };
231
+ var __async$4 = (__this, __arguments, generator) => {
140
232
  return new Promise((resolve, reject) => {
141
233
  var fulfilled = (value) => {
142
234
  try {
@@ -156,307 +248,238 @@ var __async$2 = (__this, __arguments, generator) => {
156
248
  step((generator = generator.apply(__this, __arguments)).next());
157
249
  });
158
250
  };
159
- class ResourcesControl {
160
- constructor({
161
- connector,
162
- cacheId
163
- }) {
164
- __publicField$3(this, "cache");
165
- __publicField$3(this, "connector");
166
- this.connector = connector;
167
- this.cache = new Cache({ namespace: cacheId });
168
- }
169
- // -------------------------------------------------------------------------
170
- // Resource Methods
171
- // -------------------------------------------------------------------------
172
- /**
173
- * Receive resource from NGW by id, keyname or search-object parameter.
174
- * @param resource - Resource id, keyname or search-object
175
- *
176
- * @remarks
177
- * Fetching resource would be cached to speed up next call
178
- */
179
- getOne(resource, requestOptions) {
180
- const opt = __spreadValues$2({}, requestOptions);
181
- if (typeof resource === "string") ; else if (typeof resource === "number") ; else if (isObject(resource)) {
182
- if (resource.id !== void 0) {
183
- resource.id;
184
- } else {
185
- if (resource.keyname) {
186
- resource.keyname;
187
- }
188
- if (resource.display_name) {
189
- resource.display_name;
190
- }
251
+ function lunkwillCheckResponse(lwResp) {
252
+ const ct = lwResp.headers.get("content-type");
253
+ return ct !== void 0 && ct !== null && ct.includes("application/vnd.lunkwill.request-summary+json");
254
+ }
255
+ function responseJson(response) {
256
+ return __async$4(this, null, function* () {
257
+ try {
258
+ return response.json();
259
+ } catch (e) {
260
+ throw new Error();
261
+ }
262
+ });
263
+ }
264
+ function lunkwillResponseUrl(lwResp) {
265
+ return __async$4(this, null, function* () {
266
+ const lwData = yield responseJson(lwResp);
267
+ let delay = lwData.delay_ms;
268
+ const retry = lwData.retry_ms !== void 0 ? lwData.retry_ms : 2e3;
269
+ const sum = `/api/lunkwill/${lwData.id}/summary`;
270
+ const res = `/api/lunkwill/${lwData.id}/response`;
271
+ const sleep = (msec) => new Promise((resolve) => setTimeout(resolve, msec));
272
+ let failed = false;
273
+ let ready = false;
274
+ while (!ready) {
275
+ yield sleep(failed ? retry : delay);
276
+ failed = false;
277
+ let lwResp2;
278
+ let lwData2;
279
+ try {
280
+ lwResp2 = yield fetch(sum, { credentials: "same-origin" });
281
+ lwData2 = yield lwResp2.json();
282
+ } catch (e) {
283
+ failed = true;
284
+ continue;
285
+ }
286
+ switch (lwData2.status) {
287
+ case void 0:
288
+ throw new LunkwillError(void 0, lwData2);
289
+ case "ready":
290
+ ready = true;
291
+ break;
292
+ case "cancelled":
293
+ throw new LunkwillRequestCancelled(lwData2);
294
+ case "failed":
295
+ throw new LunkwillRequestFailed(lwData2);
296
+ case "spooled":
297
+ case "processing":
298
+ case "buffering":
299
+ delay = lwData2.delay_ms;
300
+ break;
301
+ default:
302
+ throw new LunkwillError(void 0, lwData2);
191
303
  }
192
304
  }
193
- if (typeof resource === "string") {
194
- return this._fetchResourceBy({ keyname: resource }, opt);
195
- } else if (typeof resource === "number") {
196
- return this._fetchResourceById(resource, opt);
197
- } else if (isObject(resource)) {
198
- return this._fetchResourceBy(resource, opt);
305
+ return res;
306
+ });
307
+ }
308
+ function lunkwillFetch(lwRespUrl) {
309
+ return __async$4(this, null, function* () {
310
+ try {
311
+ return yield window.fetch(lwRespUrl, { credentials: "same-origin" });
312
+ } catch (e) {
313
+ throw new NetworksResponseError();
199
314
  }
200
- return Promise.resolve(void 0);
201
- }
202
- getOneOrFail(resource, requestOptions) {
203
- return this.getOne(resource, requestOptions).then((res) => {
204
- if (res) {
205
- return res;
206
- }
207
- throw new ResourceNotFoundError();
208
- });
209
- }
210
- /**
211
- * A fast way to retrieve resource ID for any resource definition.
212
- * @param resource - Any available resource definition
213
- *
214
- * @remarks
215
- * There are situations when exactly the resource id is needed
216
- * (for example, to compose the correct request to the api)
217
- * then this method will come in handy to facilitate the extraction of the identifier
218
- * if the resource is specified through a keyname or other parameters.
219
- */
220
- getId(resource, requestOptions) {
221
- if (typeof resource === "number") {
222
- return Promise.resolve(resource);
223
- } else if (typeof resource === "string" || isObject(resource)) {
224
- return this.getOne(resource, requestOptions).then((res) => {
225
- if (res) {
226
- return res.resource.id;
315
+ });
316
+ }
317
+ const mediaContentTypes = [
318
+ "application/pdf",
319
+ "image/png",
320
+ "image/jpeg",
321
+ "image/tiff",
322
+ "text/csv"
323
+ ];
324
+ const mediaContentTypesRegex = new RegExp(mediaContentTypes.join("|"));
325
+ function isMediaContentType(contentType) {
326
+ return mediaContentTypesRegex.test(contentType);
327
+ }
328
+ function encodeQueryParams(value) {
329
+ const result = [];
330
+ for (const [k, v] of Object.entries(value)) {
331
+ if (typeof v === "string" || typeof v === "number" || typeof v === "boolean") {
332
+ result.push(`${k}=${encodeURIComponent(v)}`);
333
+ } else if (Array.isArray(v)) {
334
+ result.push(`${k}=${v.map(encodeURIComponent).join(",")}`);
335
+ } else {
336
+ for (const [sk, sv] of Object.entries(v)) {
337
+ const ske = `${k}[${encodeURIComponent(sk)}]`;
338
+ if (typeof sv === "string" || typeof sv === "number" || typeof sv === "boolean") {
339
+ result.push(`${ske}=${encodeURIComponent(sv)}`);
340
+ } else if (Array.isArray(sv)) {
341
+ const sve = sv.map(encodeURIComponent);
342
+ result.push(`${ske}=${sve.join(",")}`);
227
343
  }
228
- });
344
+ }
229
345
  }
230
- return Promise.resolve(void 0);
231
346
  }
232
- /**
233
- * A fast way to retrieve resource ID for any resource definition.
234
- * @param resource - Any available resource definition
235
- *
236
- * @remarks
237
- * Similar with {@link NgwConnector.getResourceId | getResourceId} but rise error if resource is not exist.
238
- * To not make one more checks if the resource is definitely exists
239
- */
240
- getIdOrFail(resource, requestOptions) {
241
- return this.getId(resource, requestOptions).then((resp) => {
242
- if (resp === void 0) {
243
- throw new Error();
244
- }
245
- return resp;
246
- });
347
+ return result.join("&");
348
+ }
349
+ function generateUrl(path, query) {
350
+ let urlParams = "";
351
+ if (query !== void 0) {
352
+ urlParams = "?" + encodeQueryParams(query);
247
353
  }
248
- getMany(resource, requestOptions) {
249
- return this._resourceCacheFilter(resource).then((items) => {
250
- if (!items.length) {
251
- const query = {};
252
- if (resource.keyname) {
253
- query.keyname = resource.keyname;
254
- } else {
255
- Object.assign(query, resourceToQuery(resource));
354
+ return path + urlParams;
355
+ }
356
+ function request(path, options, cacheEngine) {
357
+ return __async$4(this, null, function* () {
358
+ var _b;
359
+ const defaults = {
360
+ method: "GET",
361
+ credentials: "same-origin",
362
+ headers: {}
363
+ };
364
+ const _a = __spreadValues$4(__spreadValues$4({}, defaults), options), {
365
+ withCredentials,
366
+ responseType,
367
+ cacheProps,
368
+ cacheName,
369
+ lunkwill,
370
+ cache,
371
+ query,
372
+ json
373
+ } = _a, opt = __objRest$2(_a, [
374
+ "withCredentials",
375
+ "responseType",
376
+ "cacheProps",
377
+ "cacheName",
378
+ "lunkwill",
379
+ "cache",
380
+ "query",
381
+ "json"
382
+ ]);
383
+ if (withCredentials) {
384
+ opt.credentials = "include";
385
+ }
386
+ let useLunkwill = false;
387
+ if (lunkwill !== void 0) {
388
+ lunkwill.toHeaders(opt.headers || {});
389
+ useLunkwill = true;
390
+ }
391
+ const lunkwillReturnUrl = !!opt.lunkwillReturnUrl;
392
+ delete opt.lunkwillReturnUrl;
393
+ if (json !== void 0) {
394
+ opt.body = JSON.stringify(json);
395
+ const headers = opt.headers || {};
396
+ headers["Content-Type"] = "application/json";
397
+ opt.headers = headers;
398
+ }
399
+ const url = generateUrl(path, query);
400
+ const makeRequest = () => __async$4(this, null, function* () {
401
+ let response;
402
+ try {
403
+ response = yield fetch(url, opt);
404
+ } catch (e) {
405
+ if (e.name === "AbortError") {
406
+ throw e;
256
407
  }
257
- return this.connector.get("resource.search", requestOptions, __spreadValues$2({
258
- serialization: "full"
259
- }, query)).then((resources) => {
260
- if ((requestOptions == null ? void 0 : requestOptions.cache) && resources) {
261
- for (const x of resources) {
262
- this.cache.add("resource.item", Promise.resolve(x), {
263
- id: x.resource.id
264
- });
265
- }
266
- }
267
- return resources;
268
- });
408
+ throw new NetworksResponseError();
269
409
  }
270
- return items;
271
- });
272
- }
273
- getParent(resource, requestOptions) {
274
- return this.getOne(resource, requestOptions).then((child) => {
275
- if (child) {
276
- return this.getOne(child.resource.parent.id, requestOptions);
410
+ if (useLunkwill && lunkwillCheckResponse(response)) {
411
+ const lwRespUrl = yield lunkwillResponseUrl(response);
412
+ if (lunkwillReturnUrl) {
413
+ return lwRespUrl;
414
+ }
415
+ response = yield lunkwillFetch(lwRespUrl);
277
416
  }
278
- return Promise.resolve(void 0);
279
- });
280
- }
281
- getChildrenOf(resource, requestOptions) {
282
- return this.getIdOrFail(resource).then(
283
- (parent) => this._getChildrenOf(parent, requestOptions)
284
- );
285
- }
286
- update(resource, data) {
287
- return this.getId(resource).then((id) => {
288
- if (id !== void 0) {
289
- return this.connector.put("resource.item", { data }, { id });
417
+ const respCType = response.headers.get("content-type");
418
+ const respJSON = respCType && (respCType.includes("application/json") || respCType.includes("application/vnd.lunkwill.request-summary+json"));
419
+ let body;
420
+ try {
421
+ const respMedia = respCType && isMediaContentType(respCType);
422
+ if (responseType === "blob" || respMedia) {
423
+ body = yield response.blob();
424
+ } else if (respJSON) {
425
+ body = yield response.json();
426
+ } else {
427
+ throw new InvalidResponseError();
428
+ }
429
+ } catch (e) {
430
+ if (e.name === "AbortError" || e instanceof InvalidResponseError) {
431
+ throw e;
432
+ }
433
+ throw new InvalidResponseError();
290
434
  }
291
- });
292
- }
293
- /**
294
- * Fast way to delete resource from NGW and clean cache.
295
- * @param resource - Resource definition
296
- */
297
- delete(resource) {
298
- return this.getId(resource).then((id) => {
299
- if (id !== void 0) {
300
- return this.connector.delete("resource.item", null, { id }).then(() => {
301
- this._cleanResourceItemCache(id);
302
- return void 0;
303
- });
435
+ if (400 <= response.status && response.status <= 599) {
436
+ throw new ServerResponseError(body);
304
437
  }
438
+ return body;
305
439
  });
306
- }
307
- _getChildrenOf(parent, requestOptions, _items = []) {
308
- return this.connector.get("resource.collection", requestOptions, {
309
- parent
310
- }).then((items) => {
311
- const recursivePromises = [];
312
- for (const item of items) {
313
- if (requestOptions == null ? void 0 : requestOptions.cache) {
314
- this.cache.add("resource.item", Promise.resolve(item), {
315
- id: item.resource.id
316
- });
317
- }
318
- _items.push(item);
319
- if ((requestOptions == null ? void 0 : requestOptions.recursive) && item.resource.children) {
320
- recursivePromises.push(
321
- this._getChildrenOf(item.resource.id, requestOptions, _items)
322
- );
323
- }
324
- }
325
- if (recursivePromises.length) {
326
- return Promise.all(recursivePromises).then(() => {
327
- return _items;
328
- });
329
- }
330
- return _items;
331
- });
332
- }
333
- _cleanResourceItemCache(id) {
334
- return __async$2(this, null, function* () {
335
- var _a;
336
- const all = this.cache.all();
337
- const toDelete = [];
338
- for (const c of all) {
339
- const cid = (_a = c.props) == null ? void 0 : _a.id;
340
- if (["resource.item", "resource"].includes(c.key) && cid !== void 0) {
341
- if (typeof cid === "number") {
342
- if (cid === id) {
343
- toDelete.push(c);
344
- }
345
- } else {
346
- const rid = yield this.getId(cid);
347
- if (rid === id) {
348
- toDelete.push(c);
349
- }
350
- }
351
- }
352
- }
353
- for (const d of toDelete) {
354
- this.cache.delete(d);
355
- }
356
- });
357
- }
358
- _fetchResourceById(id, requestOptions) {
359
- return this.connector.get("resource.item", requestOptions, { id });
360
- }
361
- _fetchResourceBy(resource, requestOptions) {
362
- return this.getMany(resource, requestOptions).then((resources) => {
363
- return resources[0];
364
- });
365
- }
366
- _resourceCacheFilter(resource) {
367
- return Promise.all(this.cache.matchAll("resource.item")).then(
368
- (resources) => {
369
- const items = [];
370
- resources.filter((x) => {
371
- if (x) {
372
- if (resource.keyname && x.resource.keyname) {
373
- return resource.keyname === x.resource.keyname;
374
- }
375
- if (defined(resource.id) && defined(x.resource.id)) {
376
- return resource.id === x.resource.id;
377
- }
378
- return resourceCompare(resource, x.resource);
379
- }
380
- });
381
- return items;
382
- }
383
- );
384
- }
385
- }
386
-
387
- const CONNECTORS = [];
388
- function addConnector(connector) {
389
- CONNECTORS.push(connector);
390
- }
391
- function findConnector(options) {
392
- return CONNECTORS.find((x) => {
393
- if (x.options.baseUrl === options.baseUrl) {
394
- if (options.auth) {
395
- if (x.options.auth) {
396
- return objectDeepEqual(x.options.auth, options.auth);
397
- }
398
- } else {
399
- return true;
400
- }
401
- }
402
- });
403
- }
404
- function removeConnector(connector) {
405
- const index = CONNECTORS.indexOf(connector);
406
- if (index !== -1) {
407
- CONNECTORS.splice(index, 1);
408
- }
409
- }
410
-
411
- class AbortError extends Error {
412
- constructor(message = "AbortError") {
413
- super(message);
414
- this.name = "AbortError";
415
- }
416
- }
417
-
418
- var __defProp$3 = Object.defineProperty;
419
- var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
420
- var __publicField$2 = (obj, key, value) => __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
421
- class InsufficientPermissionsError extends NgwError {
422
- constructor(obj) {
423
- super(obj);
424
- __publicField$2(this, "name", "InsufficientPermissionsError");
425
- __publicField$2(this, "exception", "nextgisweb.core.exception.InsufficientPermissions");
426
- Object.setPrototypeOf(this, InsufficientPermissionsError.prototype);
427
- }
428
- }
429
-
430
- const templateRe = /\{ *([\w_-]+) *\}/g;
431
- function template(str, data) {
432
- return str.replace(templateRe, (s, key) => {
433
- let value = data[key];
434
- if (value === void 0) {
435
- throw new Error("No value provided for letiable " + s);
436
- } else if (typeof value === "function") {
437
- value = value(data);
440
+ if (cacheEngine && cache && ((_b = opt.method) == null ? void 0 : _b.toUpperCase()) === "GET") {
441
+ const cacheOptions = cacheProps ? cacheProps : __spreadValues$4({}, objectRemoveEmpty({
442
+ withCredentials,
443
+ responseType
444
+ }));
445
+ return cacheEngine.add(cacheName || url, makeRequest, cacheOptions, false);
438
446
  }
439
- return value;
447
+ return makeRequest();
440
448
  });
441
449
  }
442
450
 
443
- var __defProp$2 = Object.defineProperty;
444
- var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
445
- var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
446
- var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
447
- var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
448
- var __spreadValues$1 = (a, b) => {
451
+ var __defProp$5 = Object.defineProperty;
452
+ var __defProps$1 = Object.defineProperties;
453
+ var __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors;
454
+ var __getOwnPropSymbols$3 = Object.getOwnPropertySymbols;
455
+ var __hasOwnProp$3 = Object.prototype.hasOwnProperty;
456
+ var __propIsEnum$3 = Object.prototype.propertyIsEnumerable;
457
+ var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
458
+ var __spreadValues$3 = (a, b) => {
449
459
  for (var prop in b || (b = {}))
450
- if (__hasOwnProp$1.call(b, prop))
451
- __defNormalProp$2(a, prop, b[prop]);
452
- if (__getOwnPropSymbols$1)
453
- for (var prop of __getOwnPropSymbols$1(b)) {
454
- if (__propIsEnum$1.call(b, prop))
455
- __defNormalProp$2(a, prop, b[prop]);
460
+ if (__hasOwnProp$3.call(b, prop))
461
+ __defNormalProp$5(a, prop, b[prop]);
462
+ if (__getOwnPropSymbols$3)
463
+ for (var prop of __getOwnPropSymbols$3(b)) {
464
+ if (__propIsEnum$3.call(b, prop))
465
+ __defNormalProp$5(a, prop, b[prop]);
456
466
  }
457
467
  return a;
458
468
  };
459
- var __async$1 = (__this, __arguments, generator) => {
469
+ var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
470
+ var __objRest$1 = (source, exclude) => {
471
+ var target = {};
472
+ for (var prop in source)
473
+ if (__hasOwnProp$3.call(source, prop) && exclude.indexOf(prop) < 0)
474
+ target[prop] = source[prop];
475
+ if (source != null && __getOwnPropSymbols$3)
476
+ for (var prop of __getOwnPropSymbols$3(source)) {
477
+ if (exclude.indexOf(prop) < 0 && __propIsEnum$3.call(source, prop))
478
+ target[prop] = source[prop];
479
+ }
480
+ return target;
481
+ };
482
+ var __async$3 = (__this, __arguments, generator) => {
460
483
  return new Promise((resolve, reject) => {
461
484
  var fulfilled = (value) => {
462
485
  try {
@@ -476,70 +499,94 @@ var __async$1 = (__this, __arguments, generator) => {
476
499
  step((generator = generator.apply(__this, __arguments)).next());
477
500
  });
478
501
  };
479
- function apiRequest(opt) {
480
- return __async$1(this, null, function* () {
481
- const { params, name, connector, requestOptions } = opt;
482
- const apiItems = yield connector.connect();
483
- let apiItem = apiItems && apiItems[name];
484
- if (apiItem) {
485
- apiItem = [...apiItem];
486
- let url = apiItem.shift();
487
- if (apiItem.length) {
488
- const replaceParams = {};
489
- for (let fry = 0; fry < apiItem.length; fry++) {
490
- const arg = apiItem[fry];
491
- replaceParams[fry] = `{${arg}}`;
492
- if (params[arg] === void 0) {
493
- throw new Error(`\`${arg}\` URL API argument is not specified`);
494
- }
495
- }
496
- if (url) {
497
- url = template(url, replaceParams);
498
- }
499
- }
500
- if (params) {
501
- const paramArray = [];
502
- const paramList = params.paramList;
503
- if (Array.isArray(paramList)) {
504
- paramList.forEach(([key, value]) => {
505
- paramArray.push(`${key}=${value}`);
506
- });
507
- }
508
- for (const p in params) {
509
- if (apiItem.indexOf(p) === -1) {
510
- paramArray.push(`${p}=${params[p]}`);
511
- }
512
- }
513
- if (paramArray.length) {
514
- url = `${url}?${paramArray.join("&")}`;
515
- }
516
- }
517
- if (url) {
518
- return connector.makeQuery(url, params, __spreadValues$1({
519
- cacheName: name
520
- }, requestOptions));
521
- } else {
522
- throw new Error("Request URL is not set");
523
- }
524
- } else {
525
- return void 0;
502
+ function routeURL(name, baseUrl, routeData, ...rest) {
503
+ const [template, ...params] = routeData[name];
504
+ const first = rest[0];
505
+ let sub;
506
+ if (first === void 0) {
507
+ sub = [];
508
+ } else if (typeof first === "object" && first !== null) {
509
+ if (rest.length > 1) {
510
+ throw new Error("Too many arguments for route(name, object)!");
526
511
  }
527
- });
512
+ sub = [];
513
+ for (const [p, v] of Object.entries(first)) {
514
+ sub[params.indexOf(p)] = String(v);
515
+ }
516
+ } else {
517
+ sub = rest.map((v) => String(v));
518
+ }
519
+ return fixUrlStr(
520
+ baseUrl + template.replace(/\{(\w+)\}/g, function(m, a) {
521
+ const idx = parseInt(a);
522
+ const value = sub[idx];
523
+ if (value === void 0) {
524
+ const msg = `Undefined parameter ${idx} in "${template}".`;
525
+ throw new Error(msg);
526
+ }
527
+ return String(value);
528
+ })
529
+ );
530
+ }
531
+ function route(name, connector, ...rest) {
532
+ const result = {
533
+ url: (opt) => __async$3(this, null, function* () {
534
+ var _a;
535
+ const routeData = yield connector.connect();
536
+ const template = routeURL(
537
+ name,
538
+ (_a = connector.options.baseUrl) != null ? _a : "",
539
+ routeData,
540
+ ...rest
541
+ );
542
+ return generateUrl(template, opt == null ? void 0 : opt.query);
543
+ })
544
+ };
545
+ const methods = ["get", "post", "put", "delete", "patch"];
546
+ for (const method of methods) {
547
+ const methodResp = (requestOptions) => __async$3(this, null, function* () {
548
+ var _b;
549
+ const _a = requestOptions || {}, { headers: optHeaders } = _a, restOpt = __objRest$1(_a, ["headers"]);
550
+ const routeData = yield connector.connect();
551
+ const template = routeURL(
552
+ name,
553
+ (_b = connector.options.baseUrl) != null ? _b : "",
554
+ routeData,
555
+ ...rest
556
+ );
557
+ const headers = objectRemoveEmpty(__spreadValues$3(__spreadValues$3({}, connector.getAuthorizationHeaders()), optHeaders != null ? optHeaders : {}));
558
+ return request(
559
+ template,
560
+ __spreadProps$1(__spreadValues$3({
561
+ headers
562
+ }, restOpt), {
563
+ method
564
+ }),
565
+ connector.cache
566
+ );
567
+ });
568
+ result[method] = methodResp;
569
+ }
570
+ return result;
528
571
  }
529
572
 
530
- var __defProp$1 = Object.defineProperty;
531
- var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
532
- var __publicField$1 = (obj, key, value) => __defNormalProp$1(obj, key + "" , value);
573
+ var __defProp$4 = Object.defineProperty;
574
+ var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
575
+ var __publicField$3 = (obj, key, value) => __defNormalProp$4(obj, key + "" , value);
533
576
  class NetworkError extends Error {
534
577
  constructor(url) {
535
578
  super();
536
- __publicField$1(this, "name", "NetworkError");
579
+ __publicField$3(this, "name", "NetworkError");
537
580
  Object.setPrototypeOf(this, NetworkError.prototype);
538
581
  this.message = `Unable to request ${url}.
539
582
  Possibly invalid NGW URL entered or CORS not configured to get request from ${location.origin}`;
540
583
  }
541
584
  }
542
585
 
586
+ function isObject(val) {
587
+ return Object.prototype.toString.call(val) === "[object Object]";
588
+ }
589
+
543
590
  let loadData;
544
591
  {
545
592
  loadData = (url, callback, options = {}, error, onCancel) => {
@@ -622,21 +669,34 @@ let loadData;
622
669
  };
623
670
  }
624
671
 
625
- var __defProp = Object.defineProperty;
672
+ const templateRe = /\{ *([\w_-]+) *\}/g;
673
+ function template(str, data) {
674
+ return str.replace(templateRe, (s, key) => {
675
+ let value = data[key];
676
+ if (value === void 0) {
677
+ throw new Error("No value provided for letiable " + s);
678
+ } else if (typeof value === "function") {
679
+ value = value(data);
680
+ }
681
+ return value;
682
+ });
683
+ }
684
+
685
+ var __defProp$3 = Object.defineProperty;
626
686
  var __defProps = Object.defineProperties;
627
687
  var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
628
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
629
- var __hasOwnProp = Object.prototype.hasOwnProperty;
630
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
631
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
632
- var __spreadValues = (a, b) => {
688
+ var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
689
+ var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
690
+ var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
691
+ var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
692
+ var __spreadValues$2 = (a, b) => {
633
693
  for (var prop in b || (b = {}))
634
- if (__hasOwnProp.call(b, prop))
635
- __defNormalProp(a, prop, b[prop]);
636
- if (__getOwnPropSymbols)
637
- for (var prop of __getOwnPropSymbols(b)) {
638
- if (__propIsEnum.call(b, prop))
639
- __defNormalProp(a, prop, b[prop]);
694
+ if (__hasOwnProp$2.call(b, prop))
695
+ __defNormalProp$3(a, prop, b[prop]);
696
+ if (__getOwnPropSymbols$2)
697
+ for (var prop of __getOwnPropSymbols$2(b)) {
698
+ if (__propIsEnum$2.call(b, prop))
699
+ __defNormalProp$3(a, prop, b[prop]);
640
700
  }
641
701
  return a;
642
702
  };
@@ -644,17 +704,17 @@ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
644
704
  var __objRest = (source, exclude) => {
645
705
  var target = {};
646
706
  for (var prop in source)
647
- if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
707
+ if (__hasOwnProp$2.call(source, prop) && exclude.indexOf(prop) < 0)
648
708
  target[prop] = source[prop];
649
- if (source != null && __getOwnPropSymbols)
650
- for (var prop of __getOwnPropSymbols(source)) {
651
- if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
709
+ if (source != null && __getOwnPropSymbols$2)
710
+ for (var prop of __getOwnPropSymbols$2(source)) {
711
+ if (exclude.indexOf(prop) < 0 && __propIsEnum$2.call(source, prop))
652
712
  target[prop] = source[prop];
653
713
  }
654
714
  return target;
655
715
  };
656
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
657
- var __async = (__this, __arguments, generator) => {
716
+ var __publicField$2 = (obj, key, value) => __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
717
+ var __async$2 = (__this, __arguments, generator) => {
658
718
  return new Promise((resolve, reject) => {
659
719
  var fulfilled = (value) => {
660
720
  try {
@@ -679,24 +739,23 @@ let REQUEST_ID = 0;
679
739
  class NgwConnector {
680
740
  constructor(options) {
681
741
  this.options = options;
682
- __publicField(this, "id", ID++);
683
- __publicField(this, "emitter", new EventEmitter());
684
- __publicField(this, "user");
685
- __publicField(this, "resources");
686
- __publicField(this, "cache");
687
- __publicField(this, "withCredentials");
688
- __publicField(this, "client", `NextGIS-NGW-Connector/${pkg.version}`);
689
- __publicField(this, "routeStr", "/api/component/pyramid/route");
690
- __publicField(this, "activeRequests", {});
691
- __publicField(this, "requestTransform");
742
+ __publicField$2(this, "id", ID++);
743
+ __publicField$2(this, "emitter", new EventEmitter());
744
+ __publicField$2(this, "user");
745
+ __publicField$2(this, "cache");
746
+ __publicField$2(this, "withCredentials");
747
+ __publicField$2(this, "client", `NextGIS-NGW-Connector/${pkg.version}`);
748
+ __publicField$2(this, "routeStr", "/api/component/pyramid/route");
749
+ __publicField$2(this, "activeRequests", {});
750
+ __publicField$2(this, "requestTransform");
692
751
  const exist = findConnector(options);
693
752
  this.cache = new Cache({ namespace: options.cacheId });
694
753
  if (exist) {
695
754
  return exist;
696
755
  } else {
697
- const { route, requestTransform, withCredentials } = this.options;
698
- if (route) {
699
- this.routeStr = route;
756
+ const { route: route2, requestTransform, withCredentials } = this.options;
757
+ if (route2) {
758
+ this.routeStr = route2;
700
759
  }
701
760
  if (requestTransform) {
702
761
  this.requestTransform = requestTransform;
@@ -704,10 +763,6 @@ class NgwConnector {
704
763
  if (withCredentials !== void 0) {
705
764
  this.withCredentials = withCredentials;
706
765
  }
707
- this.resources = new ResourcesControl({
708
- connector: this,
709
- cacheId: options.cacheId
710
- });
711
766
  addConnector(this);
712
767
  }
713
768
  }
@@ -719,7 +774,6 @@ class NgwConnector {
719
774
  */
720
775
  clearCache() {
721
776
  this.cache.clean();
722
- this.resources.cache.clean();
723
777
  }
724
778
  setRequestTransform(requestTransform) {
725
779
  this.requestTransform = requestTransform;
@@ -741,14 +795,14 @@ class NgwConnector {
741
795
  * Can be used to check connection.
742
796
  * @example
743
797
  * ```javascript
744
- * const connector = new NgwConnector({baseUrl: 'https://demo.nextgis.com'});
798
+ * const connector = new NgwConnector({ baseUrl: 'https://demo.nextgis.com' });
745
799
  * connector.connect()
746
800
  * .then(() => console.log('Ok'))
747
801
  * .catch((er) => console.log('Connection problem', er));
748
802
  * ```
749
803
  */
750
804
  connect() {
751
- return __async(this, null, function* () {
805
+ return __async$2(this, null, function* () {
752
806
  const auth = this.options.auth;
753
807
  if (auth) {
754
808
  const { login, password } = auth;
@@ -756,90 +810,630 @@ class NgwConnector {
756
810
  yield this._login({ login, password });
757
811
  }
758
812
  }
759
- const routeUrl = `${this.routeStr}?client=${this.client}`;
760
- return yield this.makeQuery(
761
- routeUrl,
762
- {},
763
- {
764
- cacheName: "route",
765
- cache: true,
766
- cacheProps: {
767
- id: this.id,
768
- auth,
769
- baseUrl: this.options.baseUrl
813
+ const routeUrl = `${this.routeStr}?client=${this.client}`;
814
+ return yield this.makeQuery(
815
+ routeUrl,
816
+ {},
817
+ {
818
+ cacheName: "route",
819
+ cache: true,
820
+ cacheProps: {
821
+ id: this.id,
822
+ auth,
823
+ baseUrl: this.options.baseUrl
824
+ }
825
+ }
826
+ );
827
+ });
828
+ }
829
+ /**
830
+ * Quick way to change NextGIS Web user.
831
+ * @param credentials - New user credentials
832
+ */
833
+ login(credentials, options) {
834
+ this.logout();
835
+ addConnector(this);
836
+ return this._login(credentials, options);
837
+ }
838
+ /**
839
+ * Disconnecting a user. Aborting all current requests
840
+ */
841
+ logout() {
842
+ this.abort();
843
+ removeConnector(this);
844
+ this.options.auth = void 0;
845
+ this.user = void 0;
846
+ this.clearCache();
847
+ this.emitter.emit("logout");
848
+ }
849
+ getUserInfo(credentials, options) {
850
+ return __async$2(this, null, function* () {
851
+ if (this.user && this.user.id) {
852
+ return this.user;
853
+ }
854
+ if (credentials) {
855
+ this.options.auth = credentials;
856
+ }
857
+ const options_ = __spreadValues$2({
858
+ headers: this.getAuthorizationHeaders(credentials),
859
+ cache: true
860
+ }, options);
861
+ return this.makeQuery(
862
+ "/api/component/auth/current_user",
863
+ {},
864
+ options_
865
+ );
866
+ });
867
+ }
868
+ /**
869
+ * Obtaining the required Headers for authentication of requests in the NGW.
870
+ */
871
+ getAuthorizationHeaders(credentials) {
872
+ const client = this.makeClientId(credentials);
873
+ if (client) {
874
+ return {
875
+ Authorization: `Basic ${client}`
876
+ };
877
+ }
878
+ return {};
879
+ }
880
+ makeClientId(credentials) {
881
+ credentials = credentials || this.options.auth;
882
+ if (credentials) {
883
+ const { login, password } = credentials;
884
+ const encodedStr = [login, password].map(encodeURIComponent).join(":");
885
+ {
886
+ return window.btoa(encodedStr);
887
+ }
888
+ }
889
+ }
890
+ /** Stop all api requests */
891
+ abort() {
892
+ for (const abortController of Object.values(this.activeRequests)) {
893
+ abortController.abort();
894
+ }
895
+ this.activeRequests = {};
896
+ }
897
+ getActiveApiRequests() {
898
+ return __spreadValues$2({}, this.activeRequests);
899
+ }
900
+ route(name, ...rest) {
901
+ return route(name, this, ...rest);
902
+ }
903
+ /**
904
+ * Send request to NGW.
905
+ * @param url - URL address to NGW
906
+ * @param params - Query params
907
+ * @param options - Request options
908
+ */
909
+ makeQuery(_0, _1) {
910
+ return __async$2(this, arguments, function* (url, params, options = {}) {
911
+ var _b;
912
+ url = (this.options.baseUrl ? this.options.baseUrl : "") + url;
913
+ if (!url) {
914
+ throw new Error("Empty `url` not allowed");
915
+ }
916
+ if (params) {
917
+ const _a = params, restParams = __objRest(_a, ["paramList"]);
918
+ url = template(url, restParams);
919
+ }
920
+ url = encodeURI(fixUrlStr(url));
921
+ options = __spreadValues$2({ withCredentials: this.withCredentials }, options);
922
+ const {
923
+ cache,
924
+ signal: externalSignal,
925
+ method = "GET",
926
+ headers,
927
+ cacheName,
928
+ cacheProps,
929
+ responseType,
930
+ withCredentials
931
+ } = options;
932
+ const internalAbortController = new AbortController();
933
+ const internalSignal = internalAbortController.signal;
934
+ if (externalSignal) {
935
+ if (externalSignal.aborted) {
936
+ throw new AbortError();
937
+ }
938
+ externalSignal.addEventListener("abort", () => {
939
+ internalAbortController.abort();
940
+ });
941
+ }
942
+ options.signal = internalSignal;
943
+ const createPromise = () => __async$2(this, null, function* () {
944
+ const id = REQUEST_ID++;
945
+ this.activeRequests[id] = internalAbortController;
946
+ try {
947
+ return this._loadData(url, options);
948
+ } finally {
949
+ this._cleanActiveRequest(id);
950
+ }
951
+ });
952
+ if (cache && method === "GET") {
953
+ const cacheOptions = cacheProps ? cacheProps : __spreadProps(__spreadValues$2({}, objectRemoveEmpty({
954
+ headers,
955
+ withCredentials,
956
+ responseType,
957
+ baseUrl: this.options.baseUrl,
958
+ userId: (_b = this.user) == null ? void 0 : _b.id
959
+ })), {
960
+ params
961
+ });
962
+ return this.cache.add(
963
+ cacheName || url,
964
+ createPromise,
965
+ cacheOptions,
966
+ false
967
+ );
968
+ }
969
+ return createPromise();
970
+ });
971
+ }
972
+ _loadData(url, options) {
973
+ options.responseType = options.responseType || "json";
974
+ return new Promise((resolve, reject) => {
975
+ var _a;
976
+ if (this.user) {
977
+ options = options || {};
978
+ options.headers = __spreadValues$2(__spreadValues$2({}, this.getAuthorizationHeaders()), options.headers);
979
+ }
980
+ if (this.requestTransform) {
981
+ const [transUrl, transOptions] = this.requestTransform(url, options);
982
+ url = transUrl;
983
+ options = transOptions;
984
+ }
985
+ let runOnAbort = void 0;
986
+ loadData(url, resolve, options, reject, (handler) => {
987
+ runOnAbort = handler;
988
+ });
989
+ (_a = options.signal) == null ? void 0 : _a.addEventListener("abort", () => {
990
+ if (runOnAbort !== void 0) {
991
+ runOnAbort();
992
+ }
993
+ reject(new AbortError());
994
+ });
995
+ }).catch((httpError) => {
996
+ if (httpError.name !== "AbortError") {
997
+ if (!!(process.env.NODE_ENV !== "production")) {
998
+ console.warn("DEV WARN", httpError);
999
+ }
1000
+ const er = this._handleHttpError(httpError);
1001
+ if (er) {
1002
+ throw er;
1003
+ }
1004
+ }
1005
+ throw httpError;
1006
+ });
1007
+ }
1008
+ _login(credentials, options) {
1009
+ return __async$2(this, null, function* () {
1010
+ try {
1011
+ const data = yield this.getUserInfo(credentials, options);
1012
+ this.user = data;
1013
+ this.emitter.emit("login", data);
1014
+ return data;
1015
+ } catch (er) {
1016
+ this.emitter.emit("login:error", er);
1017
+ throw er;
1018
+ }
1019
+ });
1020
+ }
1021
+ _cleanActiveRequest(requestId) {
1022
+ delete this.activeRequests[requestId];
1023
+ }
1024
+ _handleHttpError(er) {
1025
+ if (er) {
1026
+ if (er instanceof NgwError) {
1027
+ if (er.exception === "nextgisweb.resource.exception.ResourceNotFound") {
1028
+ throw new ResourceNotFoundError(er);
1029
+ } else if (er.exception === "nextgisweb.core.exception.InsufficientPermissions") {
1030
+ throw new InsufficientPermissionsError(er);
1031
+ }
1032
+ }
1033
+ }
1034
+ return er;
1035
+ }
1036
+ }
1037
+ __publicField$2(NgwConnector, "errors", {
1038
+ NgwError,
1039
+ ResourceNotFoundError
1040
+ });
1041
+
1042
+ function resourceCompare(res1, res2) {
1043
+ return objectDeepEqual(res1, res2);
1044
+ }
1045
+
1046
+ const exclude = ["description"];
1047
+ function resourceToQuery(resource, prefix = "") {
1048
+ prefix = prefix ? prefix + "__" : "";
1049
+ const query = {};
1050
+ for (const [key, value] of Object.entries(resource)) {
1051
+ if (exclude.indexOf(key) === -1) {
1052
+ if (isObject(value)) {
1053
+ if (key === "owner_user") {
1054
+ const children = resourceToQuery(value, key);
1055
+ Object.assign(query, children);
1056
+ } else if (key === "parent" && "id" in value) {
1057
+ query.parent_id = value.id;
1058
+ }
1059
+ } else if (defined(value)) {
1060
+ query[prefix + key] = value;
1061
+ }
1062
+ }
1063
+ }
1064
+ return query;
1065
+ }
1066
+
1067
+ var __defProp$2 = Object.defineProperty;
1068
+ var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
1069
+ var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
1070
+ var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
1071
+ var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1072
+ var __spreadValues$1 = (a, b) => {
1073
+ for (var prop in b || (b = {}))
1074
+ if (__hasOwnProp$1.call(b, prop))
1075
+ __defNormalProp$2(a, prop, b[prop]);
1076
+ if (__getOwnPropSymbols$1)
1077
+ for (var prop of __getOwnPropSymbols$1(b)) {
1078
+ if (__propIsEnum$1.call(b, prop))
1079
+ __defNormalProp$2(a, prop, b[prop]);
1080
+ }
1081
+ return a;
1082
+ };
1083
+ var __publicField$1 = (obj, key, value) => __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
1084
+ var __async$1 = (__this, __arguments, generator) => {
1085
+ return new Promise((resolve, reject) => {
1086
+ var fulfilled = (value) => {
1087
+ try {
1088
+ step(generator.next(value));
1089
+ } catch (e) {
1090
+ reject(e);
1091
+ }
1092
+ };
1093
+ var rejected = (value) => {
1094
+ try {
1095
+ step(generator.throw(value));
1096
+ } catch (e) {
1097
+ reject(e);
1098
+ }
1099
+ };
1100
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
1101
+ step((generator = generator.apply(__this, __arguments)).next());
1102
+ });
1103
+ };
1104
+ class ResourcesControl {
1105
+ constructor({
1106
+ connector,
1107
+ cacheId
1108
+ }) {
1109
+ __publicField$1(this, "cache");
1110
+ __publicField$1(this, "connector");
1111
+ this.connector = connector;
1112
+ this.cache = new Cache({ namespace: cacheId });
1113
+ }
1114
+ // -------------------------------------------------------------------------
1115
+ // Resource Methods
1116
+ // -------------------------------------------------------------------------
1117
+ /**
1118
+ * Receive resource from NGW by id, keyname or search-object parameter.
1119
+ * @param resource - Resource id, keyname or search-object
1120
+ *
1121
+ * @remarks
1122
+ * Fetching resource would be cached to speed up next call
1123
+ */
1124
+ getOne(resource, requestOptions) {
1125
+ const opt = __spreadValues$1({}, requestOptions);
1126
+ if (typeof resource === "string") ; else if (typeof resource === "number") ; else if (isObject(resource)) {
1127
+ if (resource.id !== void 0) {
1128
+ resource.id;
1129
+ } else {
1130
+ if (resource.keyname) {
1131
+ resource.keyname;
1132
+ }
1133
+ if (resource.display_name) {
1134
+ resource.display_name;
1135
+ }
1136
+ }
1137
+ }
1138
+ if (typeof resource === "string") {
1139
+ return this._fetchResourceBy({ keyname: resource }, opt);
1140
+ } else if (typeof resource === "number") {
1141
+ return this._fetchResourceById(resource, opt);
1142
+ } else if (isObject(resource)) {
1143
+ return this._fetchResourceBy(resource, opt);
1144
+ }
1145
+ return Promise.resolve(void 0);
1146
+ }
1147
+ getOneOrFail(resource, requestOptions) {
1148
+ return this.getOne(resource, requestOptions).then((res) => {
1149
+ if (res) {
1150
+ return res;
1151
+ }
1152
+ throw new ResourceNotFoundError();
1153
+ });
1154
+ }
1155
+ /**
1156
+ * A fast way to retrieve resource ID for any resource definition.
1157
+ * @param resource - Any available resource definition
1158
+ *
1159
+ * @remarks
1160
+ * There are situations when exactly the resource id is needed
1161
+ * (for example, to compose the correct request to the api)
1162
+ * then this method will come in handy to facilitate the extraction of the identifier
1163
+ * if the resource is specified through a keyname or other parameters.
1164
+ */
1165
+ getId(resource, requestOptions) {
1166
+ if (typeof resource === "number") {
1167
+ return Promise.resolve(resource);
1168
+ } else if (typeof resource === "string" || isObject(resource)) {
1169
+ return this.getOne(resource, requestOptions).then((res) => {
1170
+ if (res) {
1171
+ return res.resource.id;
1172
+ }
1173
+ });
1174
+ }
1175
+ return Promise.resolve(void 0);
1176
+ }
1177
+ /**
1178
+ * A fast way to retrieve resource ID for any resource definition.
1179
+ * @param resource - Any available resource definition
1180
+ *
1181
+ * @remarks
1182
+ * Similar with {@link NgwConnector.getResourceId | getResourceId} but rise error if resource is not exist.
1183
+ * To not make one more checks if the resource is definitely exists
1184
+ */
1185
+ getIdOrFail(resource, requestOptions) {
1186
+ return this.getId(resource, requestOptions).then((resp) => {
1187
+ if (resp === void 0) {
1188
+ throw new Error();
1189
+ }
1190
+ return resp;
1191
+ });
1192
+ }
1193
+ getMany(resource, requestOptions) {
1194
+ return this._resourceCacheFilter(resource).then((items) => {
1195
+ if (!items.length) {
1196
+ const query = {};
1197
+ if (resource.keyname) {
1198
+ query.keyname = resource.keyname;
1199
+ } else {
1200
+ Object.assign(query, resourceToQuery(resource));
1201
+ }
1202
+ return this.connector.get("resource.search", requestOptions, __spreadValues$1({
1203
+ serialization: "full"
1204
+ }, query)).then((resources) => {
1205
+ if ((requestOptions == null ? void 0 : requestOptions.cache) && resources) {
1206
+ for (const x of resources) {
1207
+ this.cache.add("resource.item", Promise.resolve(x), {
1208
+ id: x.resource.id
1209
+ });
1210
+ }
1211
+ }
1212
+ return resources;
1213
+ });
1214
+ }
1215
+ return items;
1216
+ });
1217
+ }
1218
+ getParent(resource, requestOptions) {
1219
+ return this.getOne(resource, requestOptions).then((child) => {
1220
+ if (child) {
1221
+ return this.getOne(child.resource.parent.id, requestOptions);
1222
+ }
1223
+ return Promise.resolve(void 0);
1224
+ });
1225
+ }
1226
+ getChildrenOf(resource, requestOptions) {
1227
+ return this.getIdOrFail(resource).then(
1228
+ (parent) => this._getChildrenOf(parent, requestOptions)
1229
+ );
1230
+ }
1231
+ update(resource, data) {
1232
+ return this.getId(resource).then((id) => {
1233
+ if (id !== void 0) {
1234
+ return this.connector.put("resource.item", { data }, { id });
1235
+ }
1236
+ });
1237
+ }
1238
+ /**
1239
+ * Fast way to delete resource from NGW and clean cache.
1240
+ * @param resource - Resource definition
1241
+ */
1242
+ delete(resource) {
1243
+ return this.getId(resource).then((id) => {
1244
+ if (id !== void 0) {
1245
+ return this.connector.delete("resource.item", null, { id }).then(() => {
1246
+ this._cleanResourceItemCache(id);
1247
+ return void 0;
1248
+ });
1249
+ }
1250
+ });
1251
+ }
1252
+ _getChildrenOf(parent, requestOptions, _items = []) {
1253
+ return this.connector.get("resource.collection", requestOptions, {
1254
+ parent
1255
+ }).then((items) => {
1256
+ const recursivePromises = [];
1257
+ for (const item of items) {
1258
+ if (requestOptions == null ? void 0 : requestOptions.cache) {
1259
+ this.cache.add("resource.item", Promise.resolve(item), {
1260
+ id: item.resource.id
1261
+ });
1262
+ }
1263
+ _items.push(item);
1264
+ if ((requestOptions == null ? void 0 : requestOptions.recursive) && item.resource.children) {
1265
+ recursivePromises.push(
1266
+ this._getChildrenOf(item.resource.id, requestOptions, _items)
1267
+ );
1268
+ }
1269
+ }
1270
+ if (recursivePromises.length) {
1271
+ return Promise.all(recursivePromises).then(() => {
1272
+ return _items;
1273
+ });
1274
+ }
1275
+ return _items;
1276
+ });
1277
+ }
1278
+ _cleanResourceItemCache(id) {
1279
+ return __async$1(this, null, function* () {
1280
+ var _a;
1281
+ const all = this.cache.all();
1282
+ const toDelete = [];
1283
+ for (const c of all) {
1284
+ const cid = (_a = c.props) == null ? void 0 : _a.id;
1285
+ if (["resource.item", "resource"].includes(c.key) && cid !== void 0) {
1286
+ if (typeof cid === "number") {
1287
+ if (cid === id) {
1288
+ toDelete.push(c);
1289
+ }
1290
+ } else {
1291
+ const rid = yield this.getId(cid);
1292
+ if (rid === id) {
1293
+ toDelete.push(c);
1294
+ }
1295
+ }
1296
+ }
1297
+ }
1298
+ for (const d of toDelete) {
1299
+ this.cache.delete(d);
1300
+ }
1301
+ });
1302
+ }
1303
+ _fetchResourceById(id, requestOptions) {
1304
+ return this.connector.get("resource.item", requestOptions, { id });
1305
+ }
1306
+ _fetchResourceBy(resource, requestOptions) {
1307
+ return this.getMany(resource, requestOptions).then((resources) => {
1308
+ return resources[0];
1309
+ });
1310
+ }
1311
+ _resourceCacheFilter(resource) {
1312
+ return Promise.all(this.cache.matchAll("resource.item")).then(
1313
+ (resources) => {
1314
+ const items = [];
1315
+ resources.filter((x) => {
1316
+ if (x) {
1317
+ if (resource.keyname && x.resource.keyname) {
1318
+ return resource.keyname === x.resource.keyname;
1319
+ }
1320
+ if (defined(resource.id) && defined(x.resource.id)) {
1321
+ return resource.id === x.resource.id;
1322
+ }
1323
+ return resourceCompare(resource, x.resource);
1324
+ }
1325
+ });
1326
+ return items;
1327
+ }
1328
+ );
1329
+ }
1330
+ }
1331
+
1332
+ var __defProp$1 = Object.defineProperty;
1333
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
1334
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
1335
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
1336
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1337
+ var __spreadValues = (a, b) => {
1338
+ for (var prop in b || (b = {}))
1339
+ if (__hasOwnProp.call(b, prop))
1340
+ __defNormalProp$1(a, prop, b[prop]);
1341
+ if (__getOwnPropSymbols)
1342
+ for (var prop of __getOwnPropSymbols(b)) {
1343
+ if (__propIsEnum.call(b, prop))
1344
+ __defNormalProp$1(a, prop, b[prop]);
1345
+ }
1346
+ return a;
1347
+ };
1348
+ var __async = (__this, __arguments, generator) => {
1349
+ return new Promise((resolve, reject) => {
1350
+ var fulfilled = (value) => {
1351
+ try {
1352
+ step(generator.next(value));
1353
+ } catch (e) {
1354
+ reject(e);
1355
+ }
1356
+ };
1357
+ var rejected = (value) => {
1358
+ try {
1359
+ step(generator.throw(value));
1360
+ } catch (e) {
1361
+ reject(e);
1362
+ }
1363
+ };
1364
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
1365
+ step((generator = generator.apply(__this, __arguments)).next());
1366
+ });
1367
+ };
1368
+ function apiRequest(opt) {
1369
+ return __async(this, null, function* () {
1370
+ const { params, name, connector, requestOptions } = opt;
1371
+ const apiItems = yield connector.connect();
1372
+ let apiItem = apiItems && apiItems[name];
1373
+ if (apiItem) {
1374
+ apiItem = [...apiItem];
1375
+ let url = apiItem.shift();
1376
+ if (apiItem.length) {
1377
+ const replaceParams = {};
1378
+ for (let fry = 0; fry < apiItem.length; fry++) {
1379
+ const arg = apiItem[fry];
1380
+ replaceParams[fry] = `{${arg}}`;
1381
+ if (params[arg] === void 0) {
1382
+ throw new Error(`\`${arg}\` URL API argument is not specified`);
1383
+ }
1384
+ }
1385
+ if (url) {
1386
+ url = template(url, replaceParams);
1387
+ }
1388
+ }
1389
+ if (params) {
1390
+ const paramArray = [];
1391
+ const paramList = params.paramList;
1392
+ if (Array.isArray(paramList)) {
1393
+ paramList.forEach(([key, value]) => {
1394
+ paramArray.push(`${key}=${value}`);
1395
+ });
1396
+ }
1397
+ for (const p in params) {
1398
+ if (apiItem.indexOf(p) === -1) {
1399
+ paramArray.push(`${p}=${params[p]}`);
770
1400
  }
771
1401
  }
772
- );
773
- });
774
- }
775
- /**
776
- * Quick way to change NextGIS Web user.
777
- * @param credentials - New user credentials
778
- */
779
- login(credentials, options) {
780
- this.logout();
781
- addConnector(this);
782
- return this._login(credentials, options);
783
- }
784
- /**
785
- * Disconnecting a user. Aborting all current requests
786
- */
787
- logout() {
788
- this.abort();
789
- removeConnector(this);
790
- this.options.auth = void 0;
791
- this.user = void 0;
792
- this.clearCache();
793
- this.emitter.emit("logout");
794
- }
795
- getUserInfo(credentials, options) {
796
- return __async(this, null, function* () {
797
- if (this.user && this.user.id) {
798
- return this.user;
1402
+ if (paramArray.length) {
1403
+ url = `${url}?${paramArray.join("&")}`;
1404
+ }
799
1405
  }
800
- if (credentials) {
801
- this.options.auth = credentials;
1406
+ if (url) {
1407
+ return connector.makeQuery(url, params, __spreadValues({
1408
+ cacheName: name
1409
+ }, requestOptions));
1410
+ } else {
1411
+ throw new Error("Request URL is not set");
802
1412
  }
803
- const options_ = __spreadValues({
804
- headers: this.getAuthorizationHeaders(credentials)
805
- }, options);
806
- return this.makeQuery(
807
- "/api/component/auth/current_user",
808
- {},
809
- options_
810
- );
1413
+ } else {
1414
+ return void 0;
1415
+ }
1416
+ });
1417
+ }
1418
+
1419
+ var __defProp = Object.defineProperty;
1420
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1421
+ var __publicField = (obj, key, value) => __defNormalProp(obj, key + "" , value);
1422
+ class NgwConnectorExtended extends NgwConnector {
1423
+ constructor(options) {
1424
+ super(options);
1425
+ __publicField(this, "resources");
1426
+ this.resources = new ResourcesControl({
1427
+ connector: this,
1428
+ cacheId: options.cacheId
811
1429
  });
812
1430
  }
813
1431
  /**
814
- * Obtaining the required Headers for authentication of requests in the NGW.
1432
+ * Clear the cache.
815
1433
  */
816
- getAuthorizationHeaders(credentials) {
817
- const client = this.makeClientId(credentials);
818
- if (client) {
819
- return {
820
- Authorization: `Basic ${client}`
821
- };
822
- }
823
- }
824
- makeClientId(credentials) {
825
- credentials = credentials || this.options.auth;
826
- if (credentials) {
827
- const { login, password } = credentials;
828
- const encodedStr = [login, password].map(encodeURIComponent).join(":");
829
- {
830
- return window.btoa(encodedStr);
831
- }
832
- }
833
- }
834
- /** Stop all api requests */
835
- abort() {
836
- for (const abortController of Object.values(this.activeRequests)) {
837
- abortController.abort();
838
- }
839
- this.activeRequests = {};
840
- }
841
- getActiveApiRequests() {
842
- return __spreadValues({}, this.activeRequests);
1434
+ clearCache() {
1435
+ super.clearCache();
1436
+ this.resources.cache.clean();
843
1437
  }
844
1438
  /**
845
1439
  * Send request to NGW api router.
@@ -958,75 +1552,6 @@ class NgwConnector {
958
1552
  options
959
1553
  );
960
1554
  }
961
- /**
962
- * Send request to NGW.
963
- * @param url - URL address to NGW
964
- * @param params - Query params
965
- * @param options - Request options
966
- */
967
- makeQuery(_0, _1) {
968
- return __async(this, arguments, function* (url, params, options = {}) {
969
- var _b;
970
- url = (this.options.baseUrl ? this.options.baseUrl : "") + url;
971
- if (!url) {
972
- throw new Error("Empty `url` not allowed");
973
- }
974
- if (params) {
975
- const _a = params, restParams = __objRest(_a, ["paramList"]);
976
- url = template(url, restParams);
977
- }
978
- url = encodeURI(fixUrlStr(url));
979
- options = __spreadValues({ withCredentials: this.withCredentials }, options);
980
- const {
981
- cache,
982
- signal: externalSignal,
983
- method = "GET",
984
- headers,
985
- cacheName,
986
- cacheProps,
987
- responseType,
988
- withCredentials
989
- } = options;
990
- const internalAbortController = new AbortController();
991
- const internalSignal = internalAbortController.signal;
992
- if (externalSignal) {
993
- if (externalSignal.aborted) {
994
- throw new AbortError();
995
- }
996
- externalSignal.addEventListener("abort", () => {
997
- internalAbortController.abort();
998
- });
999
- }
1000
- options.signal = internalSignal;
1001
- const createPromise = () => __async(this, null, function* () {
1002
- const id = REQUEST_ID++;
1003
- this.activeRequests[id] = internalAbortController;
1004
- try {
1005
- return this._loadData(url, options);
1006
- } finally {
1007
- this._cleanActiveRequest(id);
1008
- }
1009
- });
1010
- if (cache && method === "GET") {
1011
- const cacheOptions = cacheProps ? cacheProps : __spreadProps(__spreadValues({}, objectRemoveEmpty({
1012
- headers,
1013
- withCredentials,
1014
- responseType,
1015
- baseUrl: this.options.baseUrl,
1016
- userId: (_b = this.user) == null ? void 0 : _b.id
1017
- })), {
1018
- params
1019
- });
1020
- return this.cache.add(
1021
- cacheName || url,
1022
- createPromise,
1023
- cacheOptions,
1024
- false
1025
- );
1026
- }
1027
- return createPromise();
1028
- });
1029
- }
1030
1555
  // -------------------------------------------------------------------------
1031
1556
  // Resource Methods
1032
1557
  // -------------------------------------------------------------------------
@@ -1102,78 +1627,7 @@ class NgwConnector {
1102
1627
  deleteResource(resource) {
1103
1628
  return this.resources.delete(resource);
1104
1629
  }
1105
- /**
1106
- * @internal
1107
- */
1108
- _loadData(url, options) {
1109
- options.responseType = options.responseType || "json";
1110
- return new Promise((resolve, reject) => {
1111
- var _a;
1112
- if (this.user) {
1113
- options = options || {};
1114
- options.headers = __spreadValues(__spreadValues({}, this.getAuthorizationHeaders()), options.headers);
1115
- }
1116
- if (this.requestTransform) {
1117
- const [transUrl, transOptions] = this.requestTransform(url, options);
1118
- url = transUrl;
1119
- options = transOptions;
1120
- }
1121
- let runOnAbort = void 0;
1122
- loadData(url, resolve, options, reject, (handler) => {
1123
- runOnAbort = handler;
1124
- });
1125
- (_a = options.signal) == null ? void 0 : _a.addEventListener("abort", () => {
1126
- if (runOnAbort !== void 0) {
1127
- runOnAbort();
1128
- }
1129
- reject(new AbortError());
1130
- });
1131
- }).catch((httpError) => {
1132
- if (httpError.name !== "AbortError") {
1133
- if (!!(process.env.NODE_ENV !== "production")) {
1134
- console.warn("DEV WARN", httpError);
1135
- }
1136
- const er = this._handleHttpError(httpError);
1137
- if (er) {
1138
- throw er;
1139
- }
1140
- }
1141
- throw httpError;
1142
- });
1143
- }
1144
- _login(credentials, options) {
1145
- return __async(this, null, function* () {
1146
- try {
1147
- const data = yield this.getUserInfo(credentials, options);
1148
- this.user = data;
1149
- this.emitter.emit("login", data);
1150
- return data;
1151
- } catch (er) {
1152
- this.emitter.emit("login:error", er);
1153
- throw er;
1154
- }
1155
- });
1156
- }
1157
- _cleanActiveRequest(requestId) {
1158
- delete this.activeRequests[requestId];
1159
- }
1160
- _handleHttpError(er) {
1161
- if (er) {
1162
- if (er instanceof NgwError) {
1163
- if (er.exception === "nextgisweb.resource.exception.ResourceNotFound") {
1164
- throw new ResourceNotFoundError(er);
1165
- } else if (er.exception === "nextgisweb.core.exception.InsufficientPermissions") {
1166
- throw new InsufficientPermissionsError(er);
1167
- }
1168
- }
1169
- }
1170
- return er;
1171
- }
1172
1630
  }
1173
- __publicField(NgwConnector, "errors", {
1174
- NgwError,
1175
- ResourceNotFoundError
1176
- });
1177
1631
 
1178
- export { NgwConnector as default };
1632
+ export { NgwConnectorExtended as default };
1179
1633
  //# sourceMappingURL=ngw-connector.esm-bundler.js.map