@nextgis/ngw-connector 3.0.0-alpha.12 → 3.0.0-alpha.13

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.
package/README.md CHANGED
@@ -46,6 +46,38 @@ We recommend linking to a specific version number `/ngw-connector@[version]`
46
46
  npm install @nextgis/ngw-connector
47
47
  ```
48
48
 
49
+ ## TypeScript Support
50
+
51
+ If you plan to use this library in a TypeScript project, it's recommended to install TypeScript declaration files for NextGIS Web by using the `@nextgis/ngw-types-loader` tool.
52
+
53
+ ### Installing NGW Types
54
+
55
+ Run the following command to download the latest TypeScript declaration files:
56
+
57
+ ```bash
58
+ npx @nextgis/ngw-types-loader
59
+ ```
60
+
61
+ This will download the declaration file from the default NextGIS Web instance (`https://demo.nextgis.com`) and update your `tsconfig.json` to include it.
62
+
63
+ If you are using a custom NextGIS Web deployment, you can specify the URL:
64
+
65
+ ```bash
66
+ npx @nextgis/ngw-types-loader https://your-custom-ngw-url.com
67
+ ```
68
+
69
+ After running the tool, ensure that your `tsconfig.json` includes the downloaded file:
70
+
71
+ ```json
72
+ {
73
+ "include": [
74
+ "./nextgisweb.d.ts"
75
+ ]
76
+ }
77
+ ```
78
+
79
+ For more details, refer to the [NGW Types Loader](https://www.npmjs.com/package/@nextgis/ngw-types-loader).
80
+
49
81
  ## Usage
50
82
 
51
83
  ```javascript
@@ -56,22 +88,25 @@ const ngwConnector = new NgwConnector({
56
88
  // auth: {login, password}
57
89
  });
58
90
 
59
- ngwConnector.get('resource.item', null, { id: 485 }).then((data) => {
60
- console.log(data);
61
- });
91
+ ngwConnector
92
+ .route('resource.item', { id: 485 })
93
+ .get()
94
+ .then((data) => {
95
+ console.log(data);
96
+ });
62
97
 
63
- ngwConnector.post('resource.collection', { data: [RESOURCE JSON] });
64
- ngwConnector.patch(
65
- 'feature_layer.feature.collection',
66
- { data: [ITEMS JSON] },
67
- {
68
- id,
98
+ ngwConnector.route('resource.collection').patch({ json: RESOURCE });
99
+ ngwConnector.route('feature_layer.feature.collection', { id }).patch({
100
+ json: ITEMS,
101
+ query: {
69
102
  srs: 4326,
70
103
  geom_format: 'geojson',
71
- }
72
- );
73
- ngwConnector.put('feature_layer.feature.item', { data: [ITEM JSON] }, { id, fid })
74
- ngwConnector.delete('feature_layer.feature.item', null, { id, fid })
104
+ },
105
+ });
106
+ ngwConnector
107
+ .route('feature_layer.feature.item', { id, fid })
108
+ .put({ json: ITEM });
109
+ ngwConnector.route('feature_layer.feature.item', { id, fid }).delete();
75
110
 
76
111
  // Shortcuts methods to find resources
77
112
  ngwConnector.getResource(2011); // by id
package/lib/index.d.ts CHANGED
@@ -1,10 +1,14 @@
1
1
  import { Routes } from '@nextgisweb/pyramid/type/route';
2
- import { EventEmitter } from 'events';
3
2
  import Cache from '@nextgis/cache';
3
+ import { EventEmitter } from 'events';
4
4
  import { FeatureProperties, DeepPartial } from '@nextgis/utils';
5
- import { GeoJsonObject, Geometry } from 'geojson';
6
5
  import { LegendSymbol } from '@nextgisweb/render/type/api';
7
- import { ResourceRead, CompositeRead } from '@nextgisweb/resource/type/api';
6
+ import { ResourceCls as ResourceCls$1, ResourceRead, CompositeRead } from '@nextgisweb/resource/type/api';
7
+ import { GeoJsonObject, Geometry } from 'geojson';
8
+ import { BasemapWebMapItemRead, BasemapWebMapRead } from '@nextgisweb/basemap/type/api';
9
+ import { FeatureLayerRead } from '@nextgisweb/feature-layer/type/api';
10
+ import { VectorLayerRead } from '@nextgisweb/vector-layer/type/api';
11
+ import { WebMapRead } from '@nextgisweb/webmap/type/api';
8
12
 
9
13
  declare class LunkwillParam {
10
14
  static VALUES: (string | null)[];
@@ -589,12 +593,16 @@ interface Permission {
589
593
  permission: string;
590
594
  propagate: boolean;
591
595
  }
596
+ /** @deprecated use ResourceCls from '@nextgisweb/resource/type/api' instead */
597
+ type ResourceCls = ResourceCls$1;
592
598
  interface ResourceHierarchy {
593
599
  id: number;
594
600
  parent: {
595
601
  id: any;
596
602
  };
597
603
  }
604
+ /** @deprecated use ResourceRead from '@nextgisweb/resource/type/api' instead */
605
+ type Resource = ResourceRead;
598
606
  interface BookmarkProperties {
599
607
  name: string;
600
608
  place: string;
@@ -610,6 +618,8 @@ interface NgwTimeFormat {
610
618
  second: number;
611
619
  }
612
620
  type NgwDateTimeFormat = NgwDateFormat & NgwTimeFormat;
621
+ /** @deprecated use WebMapRead from '@nextgisweb/resource/type/api' instead */
622
+ type WebmapResource = WebMapRead;
613
623
  interface BasemapResource {
614
624
  url: string;
615
625
  /**
@@ -617,6 +627,10 @@ interface BasemapResource {
617
627
  */
618
628
  qms: string;
619
629
  }
630
+ /** @deprecated use BasemapWebMapItemRead from '@nextgisweb/resource/type/api' instead */
631
+ type BasemapWebmapItem = BasemapWebMapItemRead;
632
+ /** @deprecated use BasemapWebMapRead from '@nextgisweb/resource/type/api' instead */
633
+ type BasemapWebmap = BasemapWebMapRead;
620
634
  interface LookupTableResource {
621
635
  items: Record<string, string>;
622
636
  }
@@ -632,7 +646,11 @@ interface FeatureLayerField {
632
646
  label_field: boolean;
633
647
  typemod?: unknown;
634
648
  }
649
+ /** @deprecated use FeatureLayerRead from '@nextgisweb/resource/type/api' instead */
650
+ type FeatureResource = FeatureLayerRead;
635
651
  type GeometryType = 'POINT' | 'LINESTRING' | 'POLYGON' | 'MULTIPOINT' | 'MULTILINESTRING' | 'MULTIPOLYGON' | 'POINTZ' | 'LINESTRINGZ' | 'POLYGONZ' | 'MULTIPOINTZ' | 'MULTILINESTRINGZ' | 'MULTIPOLYGONZ';
652
+ /** @deprecated use VectorLayerRead from '@nextgisweb/resource/type/api' instead */
653
+ type VectorLayer = VectorLayerRead;
636
654
  interface FileBucket {
637
655
  files: NgwFile[];
638
656
  tstamp?: any;
@@ -705,6 +723,8 @@ interface NgwFile {
705
723
  interface Resmeta {
706
724
  items: Record<string, any>;
707
725
  }
726
+ /** @deprecated use CompositeRead from '@nextgisweb/resource/type/api' instead */
727
+ type ResourceItem = CompositeRead;
708
728
 
709
729
  type ResourceStoreItemProperties = {
710
730
  [name: string]: any;
@@ -874,7 +894,7 @@ type RequestMethods = 'POST' | 'GET' | 'PATCH' | 'DELETE' | 'PUT';
874
894
  type NgwExceptions = 'nextgisweb.resource.exception.ResourceNotFound' | 'nextgisweb.core.exception.InsufficientPermissions';
875
895
  interface BaseRequestOptions {
876
896
  cache?: boolean;
877
- signal?: AbortSignal;
897
+ signal?: AbortSignal | null;
878
898
  }
879
899
  /**
880
900
  * Options for configuring requests to NGW.
@@ -916,7 +936,7 @@ interface RequestOptions<M extends RequestMethods = RequestMethods, K extends ke
916
936
  /**
917
937
  * Signal object that allows you to communicate with a DOM request (such as fetch) and abort it if desired via an AbortController.
918
938
  */
919
- signal?: AbortSignal;
939
+ signal?: AbortSignal | null;
920
940
  /**
921
941
  * Parameters to be included in the request URL.
922
942
  */
@@ -976,6 +996,28 @@ declare class NgwError extends Error {
976
996
  constructor(er?: Partial<NgwError>);
977
997
  }
978
998
 
999
+ /**
1000
+ * Thrown when ...
1001
+ */
1002
+ declare class NetworksResponseError extends NgwError {
1003
+ message: string;
1004
+ title: string;
1005
+ detail: string;
1006
+ constructor(obj?: Partial<NetworksResponseError>);
1007
+ }
1008
+
1009
+ declare function extractError(error: unknown): NetworksResponseError | {
1010
+ title: any;
1011
+ message: any;
1012
+ detail: any;
1013
+ data: any;
1014
+ } | {
1015
+ title: string;
1016
+ message: string;
1017
+ detail?: undefined;
1018
+ data?: undefined;
1019
+ } | undefined;
1020
+
979
1021
  /**
980
1022
  * Thrown when ...
981
1023
  */
@@ -985,6 +1027,8 @@ declare class InsufficientPermissionsError extends NgwError {
985
1027
  constructor(obj: InsufficientPermissionsError);
986
1028
  }
987
1029
 
1030
+ declare function isError(error: unknown): error is Error;
1031
+
988
1032
  /**
989
1033
  * Thrown when...
990
1034
  */
@@ -993,16 +1037,6 @@ declare class NetworkError extends Error {
993
1037
  constructor(url: string);
994
1038
  }
995
1039
 
996
- /**
997
- * Thrown when ...
998
- */
999
- declare class NetworksResponseError extends NgwError {
1000
- message: string;
1001
- title: string;
1002
- detail: string;
1003
- constructor(obj?: Partial<NetworksResponseError>);
1004
- }
1005
-
1006
1040
  /**
1007
1041
  * Thrown when ...
1008
1042
  */
@@ -1012,20 +1046,6 @@ declare class ResourceNotFoundError extends NgwError {
1012
1046
  constructor(obj?: ResourceNotFoundError);
1013
1047
  }
1014
1048
 
1015
- declare function isError(error: unknown): error is Error;
1016
-
1017
- declare function extractError(error: unknown): NetworksResponseError | {
1018
- title: any;
1019
- message: any;
1020
- detail: any;
1021
- data: any;
1022
- } | {
1023
- title: string;
1024
- message: string;
1025
- detail?: undefined;
1026
- data?: undefined;
1027
- } | undefined;
1028
-
1029
1049
  type errors_AbortError = AbortError;
1030
1050
  declare const errors_AbortError: typeof AbortError;
1031
1051
  type errors_InsufficientPermissionsError = InsufficientPermissionsError;
@@ -1081,7 +1101,7 @@ declare class NgwConnector {
1081
1101
  * .catch((er) => console.log('Connection problem', er));
1082
1102
  * ```
1083
1103
  */
1084
- connect(): Promise<PyramidRoute>;
1104
+ connect({ signal, }?: Pick<RequestOptions, 'signal'>): Promise<PyramidRoute>;
1085
1105
  /**
1086
1106
  * Quick way to change NextGIS Web user.
1087
1107
  * @param credentials - New user credentials
@@ -1309,4 +1329,4 @@ interface LayerLegend {
1309
1329
  setSymbolRender?: (symbolIndex: number, status: boolean) => void;
1310
1330
  }
1311
1331
 
1312
- export { type ApiRouteParams, type BaseRequestOptions, type BasemapResource, type BookmarkProperties, type CreatedResource, type Credentials, type DeleteRequestItemsResponseMap, type DeleteRequestOptions, type FeatureItem, type FeatureItemAttachment, type FeatureItemExtensions, type FeatureItemToNgw, type FeatureLayerCount, type FeatureLayerField, type FeatureLayersIdentify, type FeatureLayersIdentifyItems, type FileBucket, type FileMeta, type FileUploadResp, type GeometryType, type GetChildrenOfOptions, type GetRequestItemsResponseMap, type GetRequestOptions, type GetRouteParam, type IdOnly, type KeysWithMethodAndPath, type KeysWithMethods, type KeysWithPaths, type LayerFeature, type LayerLegend, type LoadingQueue, type LookupTableResource, type LunkwillData, type Method, type MethodAvailable, type MethodRequestOptions, type NgwConnectorOptions, type NgwDateFormat, type NgwDateTimeFormat, type NgwExceptions, type NgwExtent, type NgwFile, type NgwLayerIdentifyError, type NgwTimeFormat, type Params, type PatchRequestItemsResponseMap, type PatchRequestOptions, type Permission, type PostRequestItemsResponseMap, type PostRequestOptions, type PutRequestItemsResponseMap, type PutRequestOptions, type PyramidRoute, type PyramidSettings, type PyramidSettingsPyramid, type PyramidSettingsPyramidCompanyLogo, type RequestHeaders, type RequestItemAdditionalParams, type RequestItemKeys, type RequestItemsParams, type RequestItemsParamsMap, type RequestItemsResponseMap, type RequestMethod, type RequestMethods, type RequestOptions, type RequestOptionsBody, type RequestOptionsByMethod, type RequestOptionsJSON, type RequestTransformFunction, type Resmeta, type ResourceDefinition, type ResourceHierarchy, type ResourceIdKeynameDef, type ResourceItemDatatype, type ResourceStoreItem, type ResourceStoreItemDefault, type ResourceStoreItemProperties, type ResponseType, type RouteBody, type RouteMethods, type RouteName, type RouteParameters, type RouteQuery, type RouteRequestOptions, type RouteResp, type RouteResults, type Router, type SrsItem, type ToReturn, type TreeGroup, type TreeItem, type TreeLayer, type UserInfo, type VectorFieldDatatype, type WmsClientConnection, type WmsClientLayer, type WmsServerService, type WmsServerServiceLayer, NgwConnectorExtended as default };
1332
+ export { type ApiRouteParams, type BaseRequestOptions, type BasemapResource, type BasemapWebmap, type BasemapWebmapItem, type BookmarkProperties, type CreatedResource, type Credentials, type DeleteRequestItemsResponseMap, type DeleteRequestOptions, type FeatureItem, type FeatureItemAttachment, type FeatureItemExtensions, type FeatureItemToNgw, type FeatureLayerCount, type FeatureLayerField, type FeatureLayersIdentify, type FeatureLayersIdentifyItems, type FeatureResource, type FileBucket, type FileMeta, type FileUploadResp, type GeometryType, type GetChildrenOfOptions, type GetRequestItemsResponseMap, type GetRequestOptions, type GetRouteParam, type IdOnly, type KeysWithMethodAndPath, type KeysWithMethods, type KeysWithPaths, type LayerFeature, type LayerLegend, type LoadingQueue, type LookupTableResource, type LunkwillData, type Method, type MethodAvailable, type MethodRequestOptions, type NgwConnectorOptions, type NgwDateFormat, type NgwDateTimeFormat, type NgwExceptions, type NgwExtent, type NgwFile, type NgwLayerIdentifyError, type NgwTimeFormat, type Params, type PatchRequestItemsResponseMap, type PatchRequestOptions, type Permission, type PostRequestItemsResponseMap, type PostRequestOptions, type PutRequestItemsResponseMap, type PutRequestOptions, type PyramidRoute, type PyramidSettings, type PyramidSettingsPyramid, type PyramidSettingsPyramidCompanyLogo, type RequestHeaders, type RequestItemAdditionalParams, type RequestItemKeys, type RequestItemsParams, type RequestItemsParamsMap, type RequestItemsResponseMap, type RequestMethod, type RequestMethods, type RequestOptions, type RequestOptionsBody, type RequestOptionsByMethod, type RequestOptionsJSON, type RequestTransformFunction, type Resmeta, type Resource, type ResourceCls, type ResourceDefinition, type ResourceHierarchy, type ResourceIdKeynameDef, type ResourceItem, type ResourceItemDatatype, type ResourceStoreItem, type ResourceStoreItemDefault, type ResourceStoreItemProperties, type ResponseType, type RouteBody, type RouteMethods, type RouteName, type RouteParameters, type RouteQuery, type RouteRequestOptions, type RouteResp, type RouteResults, type Router, type SrsItem, type ToReturn, type TreeGroup, type TreeItem, type TreeLayer, type UserInfo, type VectorFieldDatatype, type VectorLayer, type WebmapResource, type WmsClientConnection, type WmsClientLayer, type WmsServerService, type WmsServerServiceLayer, NgwConnectorExtended as default };
@@ -1,13 +1,76 @@
1
- /** Bundle of @nextgis/ngw-connector; version: 3.0.0-alpha.8; author: NextGIS */
1
+ /** Bundle of @nextgis/ngw-connector; version: 3.0.0-alpha.13; 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.8",
73
+ version: "3.0.0-alpha.13",
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-alpha.13",
83
+ "@nextgis/cancelable-promise": "^3.0.0-alpha.13",
84
+ "@nextgis/utils": "^3.0.0-alpha.13",
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-alpha.13"
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);
@@ -128,49 +167,17 @@ class InsufficientPermissionsError extends NgwError {
128
167
 
129
168
  var __defProp$6 = Object.defineProperty;
130
169
  var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
131
- var __publicField$6 = (obj, key, value) => __defNormalProp$6(obj, key + "" , value);
132
- class NetworkError extends Error {
133
- constructor(url) {
134
- super();
135
- __publicField$6(this, "name", "NetworkError");
136
- Object.setPrototypeOf(this, NetworkError.prototype);
137
- this.message = `Unable to request ${url}.
138
- Possibly invalid NGW URL entered or CORS not configured to get request from ${location.origin}`;
139
- }
140
- }
141
-
142
- var __defProp$5 = Object.defineProperty;
143
- var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
144
- var __publicField$5 = (obj, key, value) => __defNormalProp$5(obj, typeof key !== "symbol" ? key + "" : key, value);
170
+ var __publicField$6 = (obj, key, value) => __defNormalProp$6(obj, typeof key !== "symbol" ? key + "" : key, value);
145
171
  let NetworksResponseError$1 = class NetworksResponseError extends NgwError {
146
172
  constructor(obj) {
147
173
  super(obj);
148
- __publicField$5(this, "message", "There is no response from the server or problem connecting to server.");
149
- __publicField$5(this, "title", "Network error");
150
- __publicField$5(this, "detail", "Check network connectivity and try again later.");
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.");
151
177
  Object.setPrototypeOf(this, NetworksResponseError.prototype);
152
178
  }
153
179
  };
154
180
 
155
- var __defProp$4 = Object.defineProperty;
156
- var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
157
- var __publicField$4 = (obj, key, value) => __defNormalProp$4(obj, typeof key !== "symbol" ? key + "" : key, value);
158
- class ResourceNotFoundError extends NgwError {
159
- constructor(obj) {
160
- super(obj);
161
- __publicField$4(this, "name", "ResourceNotFoundError");
162
- __publicField$4(this, "exception", "nextgisweb.resource.exception.ResourceNotFound");
163
- Object.setPrototypeOf(this, ResourceNotFoundError.prototype);
164
- }
165
- }
166
-
167
- function isError(error) {
168
- if (utils.isObject(error)) {
169
- return error.status_code && error.exception && error.title;
170
- }
171
- return false;
172
- }
173
-
174
181
  function extractError(error) {
175
182
  if (utils.isObject(error)) {
176
183
  if (error.name && error.message && error.title) {
@@ -196,6 +203,38 @@ function extractError(error) {
196
203
  }
197
204
  }
198
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
+
226
+ var __defProp$4 = Object.defineProperty;
227
+ var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
228
+ var __publicField$4 = (obj, key, value) => __defNormalProp$4(obj, typeof key !== "symbol" ? key + "" : key, value);
229
+ class ResourceNotFoundError extends NgwError {
230
+ constructor(obj) {
231
+ super(obj);
232
+ __publicField$4(this, "name", "ResourceNotFoundError");
233
+ __publicField$4(this, "exception", "nextgisweb.resource.exception.ResourceNotFound");
234
+ Object.setPrototypeOf(this, ResourceNotFoundError.prototype);
235
+ }
236
+ }
237
+
199
238
  var errors = /*#__PURE__*/Object.freeze({
200
239
  __proto__: null,
201
240
  AbortError: AbortError,
@@ -279,7 +318,7 @@ function lunkwillCheckResponse(lwResp) {
279
318
  async function responseJson(response) {
280
319
  try {
281
320
  return response.json();
282
- } catch (e) {
321
+ } catch {
283
322
  throw new Error();
284
323
  }
285
324
  }
@@ -300,7 +339,7 @@ async function lunkwillResponseUrl(lwResp) {
300
339
  try {
301
340
  lwResp2 = await fetch(sum, { credentials: "same-origin" });
302
341
  lwData2 = await lwResp2.json();
303
- } catch (e) {
342
+ } catch {
304
343
  failed = true;
305
344
  continue;
306
345
  }
@@ -328,7 +367,7 @@ async function lunkwillResponseUrl(lwResp) {
328
367
  async function lunkwillFetch(lwRespUrl) {
329
368
  try {
330
369
  return await window.fetch(lwRespUrl, { credentials: "same-origin" });
331
- } catch (e) {
370
+ } catch {
332
371
  throw new NetworksResponseError();
333
372
  }
334
373
  }
@@ -449,7 +488,7 @@ async function request(path, options, cacheEngine) {
449
488
  }
450
489
  return body;
451
490
  };
452
- if (cacheEngine && ((_a = opt.method) == null ? void 0 : _a.toUpperCase()) === "GET") {
491
+ if (cacheEngine && cache !== false && ((_a = opt.method) == null ? void 0 : _a.toUpperCase()) === "GET") {
453
492
  const props = cacheProps ? cacheProps : {
454
493
  ...utils.objectRemoveEmpty({
455
494
  withCredentials,
@@ -509,29 +548,34 @@ function route(name, connector, ...rest) {
509
548
  };
510
549
  const methods = ["get", "post", "put", "delete", "patch"];
511
550
  for (const method of methods) {
512
- const methodResp = async (requestOptions) => {
551
+ const methodResp = (requestOptions) => {
513
552
  var _a;
514
553
  const { headers: optHeaders, ...restOpt } = requestOptions || {};
515
- const routeData = await connector.connect();
516
- const template = routeURL(
517
- name,
518
- (_a = connector.options.baseUrl) != null ? _a : "",
519
- routeData,
520
- ...rest
521
- );
522
- const headers = utils.objectRemoveEmpty({
523
- ...connector.getAuthorizationHeaders(),
524
- ...optHeaders != null ? optHeaders : {}
554
+ if ((_a = requestOptions == null ? void 0 : requestOptions.signal) == null ? void 0 : _a.aborted) {
555
+ throw new AbortError();
556
+ }
557
+ return connector.connect().then((routeData) => {
558
+ var _a2;
559
+ const template = routeURL(
560
+ name,
561
+ (_a2 = connector.options.baseUrl) != null ? _a2 : "",
562
+ routeData,
563
+ ...rest
564
+ );
565
+ const headers = utils.objectRemoveEmpty({
566
+ ...connector.getAuthorizationHeaders(),
567
+ ...optHeaders != null ? optHeaders : {}
568
+ });
569
+ return request(
570
+ template,
571
+ {
572
+ headers,
573
+ ...restOpt,
574
+ method
575
+ },
576
+ connector.cache
577
+ );
525
578
  });
526
- return request(
527
- template,
528
- {
529
- headers,
530
- ...restOpt,
531
- method
532
- },
533
- connector.cache
534
- );
535
579
  };
536
580
  result[method] = methodResp;
537
581
  }
@@ -663,18 +707,29 @@ let loadData;
663
707
  };
664
708
  }
665
709
 
666
- const templateRe = /\{ *([\w_-]+) *\}/g;
667
- function template(str, data) {
668
- return str.replace(templateRe, (s, key) => {
669
- let value = data[key];
670
- if (value === void 0) {
671
- throw new Error("No value provided for letiable " + s);
672
- } else if (typeof value === "function") {
673
- value = value(data);
710
+ const CONNECTORS = [];
711
+ function addConnector(connector) {
712
+ CONNECTORS.push(connector);
713
+ }
714
+ function findConnector(options) {
715
+ return CONNECTORS.find((x) => {
716
+ if (x.options.baseUrl === options.baseUrl) {
717
+ if (options.auth) {
718
+ if (x.options.auth) {
719
+ return utils.objectDeepEqual(x.options.auth, options.auth);
720
+ }
721
+ } else {
722
+ return true;
723
+ }
674
724
  }
675
- return value;
676
725
  });
677
726
  }
727
+ function removeConnector(connector) {
728
+ const index = CONNECTORS.indexOf(connector);
729
+ if (index !== -1) {
730
+ CONNECTORS.splice(index, 1);
731
+ }
732
+ }
678
733
 
679
734
  var __defProp$2 = Object.defineProperty;
680
735
  var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
@@ -743,7 +798,9 @@ class NgwConnector {
743
798
  * .catch((er) => console.log('Connection problem', er));
744
799
  * ```
745
800
  */
746
- async connect() {
801
+ async connect({
802
+ signal
803
+ } = {}) {
747
804
  const auth = this.options.auth;
748
805
  if (auth) {
749
806
  const { login, password } = auth;
@@ -752,11 +809,12 @@ class NgwConnector {
752
809
  }
753
810
  }
754
811
  const routeUrl = `${this.routeStr}?client=${this.client}`;
755
- return await this.makeQuery(
812
+ return this.makeQuery(
756
813
  routeUrl,
757
814
  {},
758
815
  {
759
816
  cacheName: "route",
817
+ signal,
760
818
  cache: true,
761
819
  cacheProps: {
762
820
  id: this.id,
@@ -1245,56 +1303,6 @@ class ResourcesControl {
1245
1303
  }
1246
1304
  }
1247
1305
 
1248
- async function apiRequest(opt) {
1249
- const { params, name, connector, requestOptions } = opt;
1250
- const apiItems = await connector.connect();
1251
- let apiItem = apiItems && apiItems[name];
1252
- if (apiItem) {
1253
- apiItem = [...apiItem];
1254
- let url = apiItem.shift();
1255
- if (apiItem.length) {
1256
- const replaceParams = {};
1257
- for (let fry = 0; fry < apiItem.length; fry++) {
1258
- const arg = apiItem[fry];
1259
- replaceParams[fry] = `{${arg}}`;
1260
- if (params[arg] === void 0) {
1261
- throw new Error(`\`${arg}\` URL API argument is not specified`);
1262
- }
1263
- }
1264
- if (url) {
1265
- url = template(url, replaceParams);
1266
- }
1267
- }
1268
- if (params) {
1269
- const paramArray = [];
1270
- const paramList = params.paramList;
1271
- if (Array.isArray(paramList)) {
1272
- paramList.forEach(([key, value]) => {
1273
- paramArray.push(`${key}=${value}`);
1274
- });
1275
- }
1276
- for (const p in params) {
1277
- if (apiItem.indexOf(p) === -1) {
1278
- paramArray.push(`${p}=${params[p]}`);
1279
- }
1280
- }
1281
- if (paramArray.length) {
1282
- url = `${url}?${paramArray.join("&")}`;
1283
- }
1284
- }
1285
- if (url) {
1286
- return connector.makeQuery(url, params, {
1287
- cacheName: name,
1288
- ...requestOptions
1289
- });
1290
- } else {
1291
- throw new Error("Request URL is not set");
1292
- }
1293
- } else {
1294
- return void 0;
1295
- }
1296
- }
1297
-
1298
1306
  var __defProp = Object.defineProperty;
1299
1307
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1300
1308
  var __publicField = (obj, key, value) => __defNormalProp(obj, key + "" , value);