@nextgis/ngw-connector 3.0.0-alpha.7 → 3.0.0

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,13 +1,76 @@
1
- /** Bundle of @nextgis/ngw-connector; version: 3.0.0-alpha.7; author: NextGIS */
1
+ /** Bundle of @nextgis/ngw-connector; version: 3.0.0; author: NextGIS */
2
2
  'use strict';
3
3
 
4
4
  var utils = require('@nextgis/utils');
5
- var events = require('events');
6
5
  var Cache = require('@nextgis/cache');
6
+ var events = require('events');
7
+
8
+ const templateRe = /\{ *([\w_-]+) *\}/g;
9
+ function template(str, data) {
10
+ return str.replace(templateRe, (s, key) => {
11
+ let value = data[key];
12
+ if (value === void 0) {
13
+ throw new Error("No value provided for letiable " + s);
14
+ } else if (typeof value === "function") {
15
+ value = value(data);
16
+ }
17
+ return value;
18
+ });
19
+ }
20
+
21
+ async function apiRequest(opt) {
22
+ const { params, name, connector, requestOptions } = opt;
23
+ const apiItems = await connector.connect();
24
+ let apiItem = apiItems && apiItems[name];
25
+ if (apiItem) {
26
+ apiItem = [...apiItem];
27
+ let url = apiItem.shift();
28
+ if (apiItem.length) {
29
+ const replaceParams = {};
30
+ for (let fry = 0; fry < apiItem.length; fry++) {
31
+ const arg = apiItem[fry];
32
+ replaceParams[fry] = `{${arg}}`;
33
+ if (params[arg] === void 0) {
34
+ throw new Error(`\`${arg}\` URL API argument is not specified`);
35
+ }
36
+ }
37
+ if (url) {
38
+ url = template(url, replaceParams);
39
+ }
40
+ }
41
+ if (params) {
42
+ const paramArray = [];
43
+ const paramList = params.paramList;
44
+ if (Array.isArray(paramList)) {
45
+ paramList.forEach(([key, value]) => {
46
+ paramArray.push(`${key}=${value}`);
47
+ });
48
+ }
49
+ for (const p in params) {
50
+ if (apiItem.indexOf(p) === -1) {
51
+ paramArray.push(`${p}=${params[p]}`);
52
+ }
53
+ }
54
+ if (paramArray.length) {
55
+ url = `${url}?${paramArray.join("&")}`;
56
+ }
57
+ }
58
+ if (url) {
59
+ return connector.makeQuery(url, params, {
60
+ cacheName: name,
61
+ ...requestOptions
62
+ });
63
+ } else {
64
+ throw new Error("Request URL is not set");
65
+ }
66
+ } else {
67
+ return void 0;
68
+ }
69
+ }
7
70
 
8
71
  var pkg = {
9
72
  name: "@nextgis/ngw-connector",
10
- version: "3.0.0-alpha.7",
73
+ version: "3.0.0",
11
74
  _priority: 12,
12
75
  description: "A lightweight HTTP client optimized for use with NextGIS Web API",
13
76
  main: "index.js",
@@ -16,15 +79,15 @@ var pkg = {
16
79
  jsdelivr: "lib/ngw-connector.global.prod.js",
17
80
  types: "lib/index.d.ts",
18
81
  dependencies: {
19
- "@nextgis/cache": "^3.0.0-alpha.7",
20
- "@nextgis/cancelable-promise": "^3.0.0-alpha.5",
21
- "@nextgis/utils": "^3.0.0-alpha.5",
82
+ "@nextgis/cache": "3.0.0",
83
+ "@nextgis/cancelable-promise": "3.0.0",
84
+ "@nextgis/utils": "3.0.0",
22
85
  "@types/events": "^3.0.0",
23
86
  events: "*",
24
87
  "form-data": "^4.0.0"
25
88
  },
26
89
  devDependencies: {
27
- "@nextgis/build-tools": "^3.0.0-alpha.5"
90
+ "@nextgis/build-tools": "3.0.0"
28
91
  },
29
92
  scripts: {
30
93
  clean: "rimraf ./lib",
@@ -64,30 +127,6 @@ var pkg = {
64
127
  gitHead: "edc68033f914f6c95c4125b9738bba6d36e990b4"
65
128
  };
66
129
 
67
- const CONNECTORS = [];
68
- function addConnector(connector) {
69
- CONNECTORS.push(connector);
70
- }
71
- function findConnector(options) {
72
- return CONNECTORS.find((x) => {
73
- if (x.options.baseUrl === options.baseUrl) {
74
- if (options.auth) {
75
- if (x.options.auth) {
76
- return utils.objectDeepEqual(x.options.auth, options.auth);
77
- }
78
- } else {
79
- return true;
80
- }
81
- }
82
- });
83
- }
84
- function removeConnector(connector) {
85
- const index = CONNECTORS.indexOf(connector);
86
- if (index !== -1) {
87
- CONNECTORS.splice(index, 1);
88
- }
89
- }
90
-
91
130
  class AbortError extends Error {
92
131
  constructor(message = "AbortError") {
93
132
  super(message);
@@ -95,37 +134,95 @@ class AbortError extends Error {
95
134
  }
96
135
  }
97
136
 
98
- var __defProp$6 = Object.defineProperty;
99
- var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
100
- var __publicField$6 = (obj, key, value) => __defNormalProp$6(obj, typeof key !== "symbol" ? key + "" : key, value);
137
+ var __defProp$8 = Object.defineProperty;
138
+ var __defNormalProp$8 = (obj, key, value) => key in obj ? __defProp$8(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
139
+ var __publicField$8 = (obj, key, value) => __defNormalProp$8(obj, typeof key !== "symbol" ? key + "" : key, value);
101
140
  class NgwError extends Error {
102
141
  constructor(er) {
103
142
  super();
104
- __publicField$6(this, "name", "NgwError");
105
- __publicField$6(this, "title");
106
- __publicField$6(this, "message");
107
- __publicField$6(this, "detail");
108
- __publicField$6(this, "exception");
109
- __publicField$6(this, "status_code");
110
- __publicField$6(this, "data");
111
- __publicField$6(this, "guru_meditation");
143
+ __publicField$8(this, "name", "NgwError");
144
+ __publicField$8(this, "title");
145
+ __publicField$8(this, "message");
146
+ __publicField$8(this, "detail");
147
+ __publicField$8(this, "exception");
148
+ __publicField$8(this, "status_code");
149
+ __publicField$8(this, "data");
150
+ __publicField$8(this, "guru_meditation");
112
151
  Object.assign(this, er);
113
152
  Object.setPrototypeOf(this, NgwError.prototype);
114
153
  }
115
154
  }
116
155
 
117
- var __defProp$5 = Object.defineProperty;
118
- var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
119
- var __publicField$5 = (obj, key, value) => __defNormalProp$5(obj, typeof key !== "symbol" ? key + "" : key, value);
156
+ var __defProp$7 = Object.defineProperty;
157
+ var __defNormalProp$7 = (obj, key, value) => key in obj ? __defProp$7(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
158
+ var __publicField$7 = (obj, key, value) => __defNormalProp$7(obj, typeof key !== "symbol" ? key + "" : key, value);
120
159
  class InsufficientPermissionsError extends NgwError {
121
160
  constructor(obj) {
122
161
  super(obj);
123
- __publicField$5(this, "name", "InsufficientPermissionsError");
124
- __publicField$5(this, "exception", "nextgisweb.core.exception.InsufficientPermissions");
162
+ __publicField$7(this, "name", "InsufficientPermissionsError");
163
+ __publicField$7(this, "exception", "nextgisweb.core.exception.InsufficientPermissions");
125
164
  Object.setPrototypeOf(this, InsufficientPermissionsError.prototype);
126
165
  }
127
166
  }
128
167
 
168
+ var __defProp$6 = Object.defineProperty;
169
+ var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
170
+ var __publicField$6 = (obj, key, value) => __defNormalProp$6(obj, typeof key !== "symbol" ? key + "" : key, value);
171
+ let NetworksResponseError$1 = class NetworksResponseError extends NgwError {
172
+ constructor(obj) {
173
+ super(obj);
174
+ __publicField$6(this, "message", "There is no response from the server or problem connecting to server.");
175
+ __publicField$6(this, "title", "Network error");
176
+ __publicField$6(this, "detail", "Check network connectivity and try again later.");
177
+ Object.setPrototypeOf(this, NetworksResponseError.prototype);
178
+ }
179
+ };
180
+
181
+ function extractError(error) {
182
+ if (utils.isObject(error)) {
183
+ if (error.name && error.message && error.title) {
184
+ return {
185
+ title: error.title,
186
+ message: error.message,
187
+ detail: error.detail || null,
188
+ data: error.data && error.data.data ? error.data.data : null
189
+ };
190
+ } else if (error.exception) {
191
+ if (error.status === void 0 || error.status === 0 || error.data === void 0) {
192
+ return new NetworksResponseError$1({
193
+ title: error.title,
194
+ status_code: error.status_code,
195
+ exception: error.exception
196
+ });
197
+ }
198
+ }
199
+ return {
200
+ title: typeof error.title === "string" ? error.title : "Unexpected error",
201
+ message: typeof error.message === "string" ? error.message : "Something went wrong."
202
+ };
203
+ }
204
+ }
205
+
206
+ function isError(error) {
207
+ if (utils.isObject(error)) {
208
+ return error.status_code && error.exception && error.title;
209
+ }
210
+ return false;
211
+ }
212
+
213
+ var __defProp$5 = Object.defineProperty;
214
+ var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
215
+ var __publicField$5 = (obj, key, value) => __defNormalProp$5(obj, key + "" , value);
216
+ class NetworkError extends Error {
217
+ constructor(url) {
218
+ super();
219
+ __publicField$5(this, "name", "NetworkError");
220
+ Object.setPrototypeOf(this, NetworkError.prototype);
221
+ this.message = `Unable to request ${url}.
222
+ Possibly invalid NGW URL entered or CORS not configured to get request from ${location.origin}`;
223
+ }
224
+ }
225
+
129
226
  var __defProp$4 = Object.defineProperty;
130
227
  var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
131
228
  var __publicField$4 = (obj, key, value) => __defNormalProp$4(obj, typeof key !== "symbol" ? key + "" : key, value);
@@ -138,6 +235,18 @@ class ResourceNotFoundError extends NgwError {
138
235
  }
139
236
  }
140
237
 
238
+ var errors = /*#__PURE__*/Object.freeze({
239
+ __proto__: null,
240
+ AbortError: AbortError,
241
+ InsufficientPermissionsError: InsufficientPermissionsError,
242
+ NetworkError: NetworkError,
243
+ NetworksResponseError: NetworksResponseError$1,
244
+ NgwError: NgwError,
245
+ ResourceNotFoundError: ResourceNotFoundError,
246
+ extractError: extractError,
247
+ isError: isError
248
+ });
249
+
141
250
  var __defProp$3 = Object.defineProperty;
142
251
  var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
143
252
  var __publicField$3 = (obj, key, value) => __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
@@ -209,7 +318,7 @@ function lunkwillCheckResponse(lwResp) {
209
318
  async function responseJson(response) {
210
319
  try {
211
320
  return response.json();
212
- } catch (e) {
321
+ } catch {
213
322
  throw new Error();
214
323
  }
215
324
  }
@@ -230,7 +339,7 @@ async function lunkwillResponseUrl(lwResp) {
230
339
  try {
231
340
  lwResp2 = await fetch(sum, { credentials: "same-origin" });
232
341
  lwData2 = await lwResp2.json();
233
- } catch (e) {
342
+ } catch {
234
343
  failed = true;
235
344
  continue;
236
345
  }
@@ -258,7 +367,7 @@ async function lunkwillResponseUrl(lwResp) {
258
367
  async function lunkwillFetch(lwRespUrl) {
259
368
  try {
260
369
  return await window.fetch(lwRespUrl, { credentials: "same-origin" });
261
- } catch (e) {
370
+ } catch {
262
371
  throw new NetworksResponseError();
263
372
  }
264
373
  }
@@ -302,7 +411,7 @@ function generateUrl(path, query) {
302
411
  return path + urlParams;
303
412
  }
304
413
  async function request(path, options, cacheEngine) {
305
- var _a;
414
+ var _a, _b;
306
415
  const defaults = {
307
416
  method: "GET",
308
417
  credentials: "same-origin",
@@ -322,6 +431,7 @@ async function request(path, options, cacheEngine) {
322
431
  ...defaults,
323
432
  ...options
324
433
  };
434
+ opt.method = (_a = opt.method) == null ? void 0 : _a.toUpperCase();
325
435
  if (withCredentials) {
326
436
  opt.credentials = "include";
327
437
  }
@@ -344,7 +454,7 @@ async function request(path, options, cacheEngine) {
344
454
  try {
345
455
  response = await fetch(url, opt);
346
456
  } catch (e) {
347
- if (e.name === "AbortError") {
457
+ if (opt.signal && opt.signal.aborted) {
348
458
  throw e;
349
459
  }
350
460
  throw new NetworksResponseError();
@@ -379,14 +489,26 @@ async function request(path, options, cacheEngine) {
379
489
  }
380
490
  return body;
381
491
  };
382
- if (cacheEngine && cache && ((_a = opt.method) == null ? void 0 : _a.toUpperCase()) === "GET") {
383
- const cacheOptions = cacheProps ? cacheProps : {
384
- ...utils.objectRemoveEmpty({
385
- withCredentials,
386
- responseType
387
- })
388
- };
389
- return cacheEngine.add(cacheName || url, makeRequest, cacheOptions, false);
492
+ if (cacheEngine) {
493
+ if (((_b = opt.method) == null ? void 0 : _b.toUpperCase()) === "GET") {
494
+ if (cache !== false) {
495
+ const props = cacheProps ? cacheProps : {
496
+ ...utils.objectRemoveEmpty({
497
+ withCredentials,
498
+ responseType
499
+ })
500
+ };
501
+ return cacheEngine.add(cacheName || url, makeRequest, {
502
+ props,
503
+ expirationTime: cache ? void 0 : 500
504
+ });
505
+ }
506
+ } else {
507
+ const ignoredForClean = ["api/feature_layer/identify"];
508
+ if (ignoredForClean.every((part) => !path.includes(part))) {
509
+ cacheEngine.clean();
510
+ }
511
+ }
390
512
  }
391
513
  return makeRequest();
392
514
  }
@@ -436,42 +558,40 @@ function route(name, connector, ...rest) {
436
558
  };
437
559
  const methods = ["get", "post", "put", "delete", "patch"];
438
560
  for (const method of methods) {
439
- const methodResp = async (requestOptions) => {
561
+ const methodResp = (requestOptions) => {
440
562
  var _a;
441
563
  const { headers: optHeaders, ...restOpt } = requestOptions || {};
442
- const routeData = await connector.connect();
443
- const template = routeURL(
444
- name,
445
- (_a = connector.options.baseUrl) != null ? _a : "",
446
- routeData,
447
- ...rest
448
- );
449
- const headers = utils.objectRemoveEmpty({
450
- ...connector.getAuthorizationHeaders(),
451
- ...optHeaders != null ? optHeaders : {}
564
+ if ((_a = requestOptions == null ? void 0 : requestOptions.signal) == null ? void 0 : _a.aborted) {
565
+ throw new AbortError();
566
+ }
567
+ return connector.connect().then((routeData) => {
568
+ var _a2;
569
+ const template = routeURL(
570
+ name,
571
+ (_a2 = connector.options.baseUrl) != null ? _a2 : "",
572
+ routeData,
573
+ ...rest
574
+ );
575
+ const headers = utils.objectRemoveEmpty({
576
+ ...connector.getAuthorizationHeaders(),
577
+ ...optHeaders != null ? optHeaders : {}
578
+ });
579
+ return request(
580
+ template,
581
+ {
582
+ headers,
583
+ ...restOpt,
584
+ method
585
+ },
586
+ connector.cache
587
+ );
452
588
  });
453
- return request(
454
- template,
455
- {
456
- headers,
457
- ...restOpt,
458
- method
459
- },
460
- connector.cache
461
- );
462
589
  };
463
590
  result[method] = methodResp;
464
591
  }
465
592
  return result;
466
593
  }
467
594
 
468
- function isError(error) {
469
- if (utils.isObject(error)) {
470
- return error.status_code && error.exception && error.title;
471
- }
472
- return false;
473
- }
474
-
475
595
  function isObject(val) {
476
596
  return Object.prototype.toString.call(val) === "[object Object]";
477
597
  }
@@ -597,18 +717,29 @@ let loadData;
597
717
  };
598
718
  }
599
719
 
600
- const templateRe = /\{ *([\w_-]+) *\}/g;
601
- function template(str, data) {
602
- return str.replace(templateRe, (s, key) => {
603
- let value = data[key];
604
- if (value === void 0) {
605
- throw new Error("No value provided for letiable " + s);
606
- } else if (typeof value === "function") {
607
- value = value(data);
720
+ const CONNECTORS = [];
721
+ function addConnector(connector) {
722
+ CONNECTORS.push(connector);
723
+ }
724
+ function findConnector(options) {
725
+ return CONNECTORS.find((x) => {
726
+ if (x.options.baseUrl === options.baseUrl) {
727
+ if (options.auth) {
728
+ if (x.options.auth) {
729
+ return utils.objectDeepEqual(x.options.auth, options.auth);
730
+ }
731
+ } else {
732
+ return true;
733
+ }
608
734
  }
609
- return value;
610
735
  });
611
736
  }
737
+ function removeConnector(connector) {
738
+ const index = CONNECTORS.indexOf(connector);
739
+ if (index !== -1) {
740
+ CONNECTORS.splice(index, 1);
741
+ }
742
+ }
612
743
 
613
744
  var __defProp$2 = Object.defineProperty;
614
745
  var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
@@ -623,12 +754,16 @@ class NgwConnector {
623
754
  __publicField$2(this, "user");
624
755
  __publicField$2(this, "cache");
625
756
  __publicField$2(this, "withCredentials");
757
+ // Dedicated cache for route requests, as these are executed before any other requests.
758
+ // This cache does not need to be cleared during the session.
759
+ __publicField$2(this, "routeCache");
626
760
  __publicField$2(this, "client", `NextGIS-NGW-Connector/${pkg.version}`);
627
761
  __publicField$2(this, "routeStr", "/api/component/pyramid/route");
628
762
  __publicField$2(this, "activeRequests", {});
629
763
  __publicField$2(this, "requestTransform");
630
764
  const exist = findConnector(options);
631
765
  this.cache = new Cache({ namespace: options.cacheId });
766
+ this.routeCache = new Cache({ namespace: "routecache" });
632
767
  if (exist) {
633
768
  return exist;
634
769
  } else {
@@ -677,7 +812,9 @@ class NgwConnector {
677
812
  * .catch((er) => console.log('Connection problem', er));
678
813
  * ```
679
814
  */
680
- async connect() {
815
+ async connect({
816
+ signal
817
+ } = {}) {
681
818
  const auth = this.options.auth;
682
819
  if (auth) {
683
820
  const { login, password } = auth;
@@ -686,18 +823,9 @@ class NgwConnector {
686
823
  }
687
824
  }
688
825
  const routeUrl = `${this.routeStr}?client=${this.client}`;
689
- return await this.makeQuery(
690
- routeUrl,
691
- {},
692
- {
693
- cacheName: "route",
694
- cache: true,
695
- cacheProps: {
696
- id: this.id,
697
- auth,
698
- baseUrl: this.options.baseUrl
699
- }
700
- }
826
+ return this.routeCache.add(
827
+ this.options.baseUrl || String(this.id),
828
+ () => this.makeQuery(routeUrl, null, { signal, cache: false })
701
829
  );
702
830
  }
703
831
  /**
@@ -717,6 +845,7 @@ class NgwConnector {
717
845
  removeConnector(this);
718
846
  this.options.auth = void 0;
719
847
  this.user = void 0;
848
+ this.routeCache.clean();
720
849
  this.clearCache();
721
850
  this.emitter.emit("logout");
722
851
  }
@@ -821,7 +950,7 @@ class NgwConnector {
821
950
  this._cleanActiveRequest(id);
822
951
  }
823
952
  };
824
- if (method === "GET") {
953
+ if (method === "GET" && cache !== false) {
825
954
  const cacheOptions = cacheProps ? cacheProps : {
826
955
  ...utils.objectRemoveEmpty({
827
956
  headers,
@@ -905,10 +1034,7 @@ class NgwConnector {
905
1034
  return er;
906
1035
  }
907
1036
  }
908
- __publicField$2(NgwConnector, "errors", {
909
- NgwError,
910
- ResourceNotFoundError
911
- });
1037
+ __publicField$2(NgwConnector, "errors", errors);
912
1038
 
913
1039
  function resourceCompare(res1, res2) {
914
1040
  return utils.objectDeepEqual(res1, res2);
@@ -922,7 +1048,10 @@ function resourceToQuery(resource, prefix = "") {
922
1048
  if (exclude.indexOf(key) === -1) {
923
1049
  if (isObject(value)) {
924
1050
  if (key === "owner_user") {
925
- const children = resourceToQuery(value, key);
1051
+ const children = resourceToQuery(
1052
+ value,
1053
+ key
1054
+ );
926
1055
  Object.assign(query, children);
927
1056
  } else if (key === "parent" && "id" in value) {
928
1057
  query.parent_id = value.id;
@@ -1036,9 +1165,12 @@ class ResourcesControl {
1036
1165
  } else {
1037
1166
  Object.assign(query, resourceToQuery(resource));
1038
1167
  }
1039
- return this.connector.get("resource.search", requestOptions, {
1040
- serialization: "full",
1041
- ...query
1168
+ return this.connector.route("resource.search").get({
1169
+ ...requestOptions,
1170
+ query: {
1171
+ serialization: "full",
1172
+ ...query
1173
+ }
1042
1174
  }).then((resources) => {
1043
1175
  if ((requestOptions == null ? void 0 : requestOptions.cache) && resources) {
1044
1176
  for (const x of resources) {
@@ -1055,7 +1187,8 @@ class ResourcesControl {
1055
1187
  }
1056
1188
  getParent(resource, requestOptions) {
1057
1189
  return this.getOne(resource, requestOptions).then((child) => {
1058
- if (child) {
1190
+ var _a, _b;
1191
+ if ((_b = (_a = child == null ? void 0 : child.resource) == null ? void 0 : _a.parent) == null ? void 0 : _b.id) {
1059
1192
  return this.getOne(child.resource.parent.id, requestOptions);
1060
1193
  }
1061
1194
  return Promise.resolve(void 0);
@@ -1087,31 +1220,41 @@ class ResourcesControl {
1087
1220
  }
1088
1221
  });
1089
1222
  }
1090
- _getChildrenOf(parent, requestOptions, _items = []) {
1091
- return this.connector.get("resource.collection", requestOptions, {
1092
- parent
1093
- }).then((items) => {
1094
- const recursivePromises = [];
1095
- for (const item of items) {
1096
- if (requestOptions == null ? void 0 : requestOptions.cache) {
1097
- this.cache.add("resource.item", Promise.resolve(item), {
1098
- id: item.resource.id
1099
- });
1100
- }
1101
- _items.push(item);
1102
- if ((requestOptions == null ? void 0 : requestOptions.recursive) && item.resource.children) {
1103
- recursivePromises.push(
1104
- this._getChildrenOf(item.resource.id, requestOptions, _items)
1105
- );
1106
- }
1223
+ async _getChildrenOf(parentDef, requestOptions, _items = []) {
1224
+ let parent = void 0;
1225
+ if (typeof parentDef === "string") {
1226
+ parent = await this.getId(parentDef, requestOptions);
1227
+ } else if (typeof parentDef === "object") {
1228
+ parent = parentDef.id;
1229
+ } else {
1230
+ parent = parentDef;
1231
+ }
1232
+ const items = await this.connector.route("resource.collection").get({
1233
+ ...requestOptions,
1234
+ query: {
1235
+ parent
1107
1236
  }
1108
- if (recursivePromises.length) {
1109
- return Promise.all(recursivePromises).then(() => {
1110
- return _items;
1237
+ });
1238
+ const recursivePromises = [];
1239
+ for (const item of items) {
1240
+ if (requestOptions == null ? void 0 : requestOptions.cache) {
1241
+ this.cache.add("resource.item", Promise.resolve(item), {
1242
+ id: item.resource.id
1111
1243
  });
1112
1244
  }
1113
- return _items;
1114
- });
1245
+ _items.push(item);
1246
+ if ((requestOptions == null ? void 0 : requestOptions.recursive) && item.resource.children) {
1247
+ recursivePromises.push(
1248
+ this._getChildrenOf(item.resource.id, requestOptions, _items)
1249
+ );
1250
+ }
1251
+ }
1252
+ if (recursivePromises.length) {
1253
+ return Promise.all(recursivePromises).then(() => {
1254
+ return _items;
1255
+ });
1256
+ }
1257
+ return _items;
1115
1258
  }
1116
1259
  async _cleanResourceItemCache(id) {
1117
1260
  var _a;
@@ -1137,7 +1280,7 @@ class ResourcesControl {
1137
1280
  }
1138
1281
  }
1139
1282
  _fetchResourceById(id, requestOptions) {
1140
- return this.connector.get("resource.item", requestOptions, { id });
1283
+ return this.connector.route("resource.item", { id }).get(requestOptions);
1141
1284
  }
1142
1285
  _fetchResourceBy(resource, requestOptions) {
1143
1286
  return this.getMany(resource, requestOptions).then((resources) => {
@@ -1165,56 +1308,6 @@ class ResourcesControl {
1165
1308
  }
1166
1309
  }
1167
1310
 
1168
- async function apiRequest(opt) {
1169
- const { params, name, connector, requestOptions } = opt;
1170
- const apiItems = await connector.connect();
1171
- let apiItem = apiItems && apiItems[name];
1172
- if (apiItem) {
1173
- apiItem = [...apiItem];
1174
- let url = apiItem.shift();
1175
- if (apiItem.length) {
1176
- const replaceParams = {};
1177
- for (let fry = 0; fry < apiItem.length; fry++) {
1178
- const arg = apiItem[fry];
1179
- replaceParams[fry] = `{${arg}}`;
1180
- if (params[arg] === void 0) {
1181
- throw new Error(`\`${arg}\` URL API argument is not specified`);
1182
- }
1183
- }
1184
- if (url) {
1185
- url = template(url, replaceParams);
1186
- }
1187
- }
1188
- if (params) {
1189
- const paramArray = [];
1190
- const paramList = params.paramList;
1191
- if (Array.isArray(paramList)) {
1192
- paramList.forEach(([key, value]) => {
1193
- paramArray.push(`${key}=${value}`);
1194
- });
1195
- }
1196
- for (const p in params) {
1197
- if (apiItem.indexOf(p) === -1) {
1198
- paramArray.push(`${p}=${params[p]}`);
1199
- }
1200
- }
1201
- if (paramArray.length) {
1202
- url = `${url}?${paramArray.join("&")}`;
1203
- }
1204
- }
1205
- if (url) {
1206
- return connector.makeQuery(url, params, {
1207
- cacheName: name,
1208
- ...requestOptions
1209
- });
1210
- } else {
1211
- throw new Error("Request URL is not set");
1212
- }
1213
- } else {
1214
- return void 0;
1215
- }
1216
- }
1217
-
1218
1311
  var __defProp = Object.defineProperty;
1219
1312
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1220
1313
  var __publicField = (obj, key, value) => __defNormalProp(obj, key + "" , value);