@nextgis/ngw-connector 1.18.21 → 2.0.0-alpha.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.
- package/README.md +3 -3
- package/lib/index.d.ts +1100 -1181
- package/lib/ngw-connector.cjs.js +955 -957
- package/lib/ngw-connector.cjs.js.map +1 -1
- package/lib/ngw-connector.cjs.prod.js +1 -1
- package/lib/ngw-connector.cjs.prod.js.map +1 -1
- package/lib/ngw-connector.esm-browser.js +1664 -1739
- package/lib/ngw-connector.esm-browser.js.map +1 -1
- package/lib/ngw-connector.esm-browser.prod.js +1 -1
- package/lib/ngw-connector.esm-browser.prod.js.map +1 -1
- package/lib/ngw-connector.esm-bundler.js +957 -925
- package/lib/ngw-connector.esm-bundler.js.map +1 -1
- package/lib/ngw-connector.esm-bundler.prod.js +1 -1
- package/lib/ngw-connector.esm-bundler.prod.js.map +1 -1
- package/lib/ngw-connector.global.js +1844 -2095
- package/lib/ngw-connector.global.js.map +1 -1
- package/lib/ngw-connector.global.prod.js +1 -1
- package/lib/ngw-connector.global.prod.js.map +1 -1
- package/package.json +7 -7
|
@@ -1,1000 +1,1032 @@
|
|
|
1
|
-
/** Bundle of @nextgis/ngw-connector; version:
|
|
1
|
+
/** Bundle of @nextgis/ngw-connector; version: 2.0.0-alpha.0; author: NextGIS */
|
|
2
2
|
import { EventEmitter } from 'events';
|
|
3
|
+
import Cache from '@nextgis/cache';
|
|
3
4
|
import CancelablePromise from '@nextgis/cancelable-promise';
|
|
4
5
|
import { objectDeepEqual, defined, objectRemoveEmpty, fixUrlStr } from '@nextgis/utils';
|
|
5
|
-
import Cache from '@nextgis/cache';
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
7
|
+
var __defProp$5 = Object.defineProperty;
|
|
8
|
+
var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
9
|
+
var __publicField$5 = (obj, key, value) => {
|
|
10
|
+
__defNormalProp$5(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
11
|
+
return value;
|
|
12
|
+
};
|
|
13
|
+
class NgwError extends Error {
|
|
14
|
+
constructor(er) {
|
|
15
|
+
super();
|
|
16
|
+
__publicField$5(this, "name", "NgwError");
|
|
17
|
+
__publicField$5(this, "title");
|
|
18
|
+
__publicField$5(this, "message");
|
|
19
|
+
__publicField$5(this, "detail");
|
|
20
|
+
__publicField$5(this, "exception");
|
|
21
|
+
__publicField$5(this, "status_code");
|
|
22
|
+
__publicField$5(this, "data");
|
|
23
|
+
__publicField$5(this, "guru_meditation");
|
|
24
|
+
Object.assign(this, er);
|
|
25
|
+
Object.setPrototypeOf(this, NgwError.prototype);
|
|
26
|
+
}
|
|
18
27
|
}
|
|
19
28
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
var __defProp$4 = Object.defineProperty;
|
|
30
|
+
var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
31
|
+
var __publicField$4 = (obj, key, value) => {
|
|
32
|
+
__defNormalProp$4(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
33
|
+
return value;
|
|
34
|
+
};
|
|
35
|
+
class ResourceNotFoundError extends NgwError {
|
|
36
|
+
constructor(obj) {
|
|
37
|
+
super(obj);
|
|
38
|
+
__publicField$4(this, "name", "ResourceNotFoundError");
|
|
39
|
+
__publicField$4(this, "exception", "nextgisweb.resource.exception.ResourceNotFound");
|
|
40
|
+
Object.setPrototypeOf(this, ResourceNotFoundError.prototype);
|
|
41
|
+
}
|
|
30
42
|
}
|
|
31
43
|
|
|
32
44
|
function isObject(val) {
|
|
33
|
-
|
|
45
|
+
return Object.prototype.toString.call(val) === "[object Object]";
|
|
34
46
|
}
|
|
35
47
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
const processingResponse = (forError = false) => {
|
|
55
|
-
const cb = forError ? error : callback;
|
|
56
|
-
if (options.responseType === 'blob') {
|
|
57
|
-
cb(xhr.response);
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
if (xhr.responseText) {
|
|
61
|
-
cb(getResponseText());
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
64
|
-
error({ message: '' });
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
xhr.onload = () => {
|
|
69
|
-
if ([401, 403, 404, 422, 500].indexOf(xhr.status) !== -1) {
|
|
70
|
-
error(new NgwError(getResponseText()));
|
|
71
|
-
}
|
|
72
|
-
processingResponse();
|
|
73
|
-
};
|
|
74
|
-
// xhr.onreadystatechange = () => {
|
|
75
|
-
// if (
|
|
76
|
-
// (xhr.readyState === 4 && xhr.status === 200) ||
|
|
77
|
-
// (xhr.readyState === 3 && xhr.status === 201)
|
|
78
|
-
// ) {
|
|
79
|
-
// processingResponse();
|
|
80
|
-
// } else if (xhr.readyState === 3 && xhr.status === 400) {
|
|
81
|
-
// processingResponse();
|
|
82
|
-
// } else if (xhr.readyState === 4 && xhr.status === 500) {
|
|
83
|
-
// processingResponse();
|
|
84
|
-
// } else if (xhr.readyState === 4 && xhr.status === 401) {
|
|
85
|
-
// error(xhr.statusText);
|
|
86
|
-
// } else if (xhr.readyState === 4) {
|
|
87
|
-
// error('request error');
|
|
88
|
-
// }
|
|
89
|
-
// };
|
|
90
|
-
xhr.onerror = (er) => {
|
|
91
|
-
if (xhr.status === 0) {
|
|
92
|
-
error(new NetworkError(url));
|
|
93
|
-
}
|
|
94
|
-
else {
|
|
95
|
-
error(er);
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
|
-
xhr.upload.onprogress = function (e) {
|
|
99
|
-
if (e.lengthComputable) {
|
|
100
|
-
const percentComplete = (e.loaded / e.total) * 100;
|
|
101
|
-
if (options.onProgress) {
|
|
102
|
-
options.onProgress(percentComplete, e);
|
|
103
|
-
}
|
|
104
|
-
// console.log(percentComplete + '% uploaded');
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
const headers = options.headers;
|
|
108
|
-
if (headers) {
|
|
109
|
-
for (const h in headers) {
|
|
110
|
-
const header = headers[h];
|
|
111
|
-
if (typeof header === 'string') {
|
|
112
|
-
xhr.setRequestHeader(h, header);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
48
|
+
function resourceCompare(res1, res2) {
|
|
49
|
+
return objectDeepEqual(res1, res2);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const exclude = ["description"];
|
|
53
|
+
function resourceToQuery(resource, prefix = "") {
|
|
54
|
+
prefix = prefix ? prefix + "__" : "";
|
|
55
|
+
const query = {};
|
|
56
|
+
for (const [key, value] of Object.entries(resource)) {
|
|
57
|
+
if (exclude.indexOf(key) === -1) {
|
|
58
|
+
if (isObject(value)) {
|
|
59
|
+
if (key === "owner_user") {
|
|
60
|
+
const children = resourceToQuery(value, key);
|
|
61
|
+
Object.assign(query, children);
|
|
62
|
+
} else if (key === "parent" && "id" in value) {
|
|
63
|
+
query.parent_id = value.id;
|
|
115
64
|
}
|
|
116
|
-
|
|
117
|
-
|
|
65
|
+
} else if (defined(value)) {
|
|
66
|
+
query[prefix + key] = value;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return query;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
var __defProp$3 = Object.defineProperty;
|
|
74
|
+
var __defProps = Object.defineProperties;
|
|
75
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
76
|
+
var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
|
|
77
|
+
var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
|
|
78
|
+
var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
|
|
79
|
+
var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
80
|
+
var __spreadValues$1 = (a, b) => {
|
|
81
|
+
for (var prop in b || (b = {}))
|
|
82
|
+
if (__hasOwnProp$1.call(b, prop))
|
|
83
|
+
__defNormalProp$3(a, prop, b[prop]);
|
|
84
|
+
if (__getOwnPropSymbols$1)
|
|
85
|
+
for (var prop of __getOwnPropSymbols$1(b)) {
|
|
86
|
+
if (__propIsEnum$1.call(b, prop))
|
|
87
|
+
__defNormalProp$3(a, prop, b[prop]);
|
|
88
|
+
}
|
|
89
|
+
return a;
|
|
90
|
+
};
|
|
91
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
92
|
+
var __publicField$3 = (obj, key, value) => {
|
|
93
|
+
__defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
94
|
+
return value;
|
|
95
|
+
};
|
|
96
|
+
var __async = (__this, __arguments, generator) => {
|
|
97
|
+
return new Promise((resolve, reject) => {
|
|
98
|
+
var fulfilled = (value) => {
|
|
99
|
+
try {
|
|
100
|
+
step(generator.next(value));
|
|
101
|
+
} catch (e) {
|
|
102
|
+
reject(e);
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
var rejected = (value) => {
|
|
106
|
+
try {
|
|
107
|
+
step(generator.throw(value));
|
|
108
|
+
} catch (e) {
|
|
109
|
+
reject(e);
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
113
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
114
|
+
});
|
|
115
|
+
};
|
|
116
|
+
class ResourcesControl {
|
|
117
|
+
constructor(connector) {
|
|
118
|
+
this.connector = connector;
|
|
119
|
+
__publicField$3(this, "cache", new Cache());
|
|
120
|
+
}
|
|
121
|
+
// -------------------------------------------------------------------------
|
|
122
|
+
// Resource Methods
|
|
123
|
+
// -------------------------------------------------------------------------
|
|
124
|
+
/**
|
|
125
|
+
* Receive resource from NGW by id, keyname or search-object parameter.
|
|
126
|
+
* @param resource - Resource id, keyname or search-object
|
|
127
|
+
*
|
|
128
|
+
* @remarks
|
|
129
|
+
* Fetching resource would be cached to speed up next call
|
|
130
|
+
*/
|
|
131
|
+
getOne(resource, requestOptions) {
|
|
132
|
+
const cache = new Cache();
|
|
133
|
+
const forCache = {};
|
|
134
|
+
const opt = __spreadProps(__spreadValues$1({}, requestOptions), { cache: false });
|
|
135
|
+
if (typeof resource === "string") {
|
|
136
|
+
forCache.keyname = resource;
|
|
137
|
+
} else if (typeof resource === "number") {
|
|
138
|
+
forCache.id = resource;
|
|
139
|
+
} else if (isObject(resource)) {
|
|
140
|
+
if (resource.id !== void 0) {
|
|
141
|
+
forCache.id = resource.id;
|
|
142
|
+
} else {
|
|
143
|
+
if (resource.keyname) {
|
|
144
|
+
forCache.keyname = resource.keyname;
|
|
118
145
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
data = new FormData();
|
|
122
|
-
data.append('file', options.file);
|
|
123
|
-
if (options.data) {
|
|
124
|
-
for (const d in options.data) {
|
|
125
|
-
data.append(d, data[d]);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
146
|
+
if (resource.display_name) {
|
|
147
|
+
forCache.display_name = resource.display_name;
|
|
128
148
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
const makeRequest = () => {
|
|
152
|
+
if (typeof resource === "string") {
|
|
153
|
+
return this._fetchResourceBy({ keyname: resource }, opt);
|
|
154
|
+
} else if (typeof resource === "number") {
|
|
155
|
+
return this._fetchResourceById(resource, opt);
|
|
156
|
+
} else if (isObject(resource)) {
|
|
157
|
+
return this._fetchResourceBy(resource, opt);
|
|
158
|
+
}
|
|
159
|
+
return CancelablePromise.resolve(void 0);
|
|
160
|
+
};
|
|
161
|
+
return cache.addFull("resource", makeRequest, forCache);
|
|
162
|
+
}
|
|
163
|
+
getOneOrFail(resource, requestOptions) {
|
|
164
|
+
return this.getOne(resource, requestOptions).then((res) => {
|
|
165
|
+
if (res) {
|
|
166
|
+
return res;
|
|
167
|
+
}
|
|
168
|
+
throw new ResourceNotFoundError();
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* A fast way to retrieve resource ID for any resource definition.
|
|
173
|
+
* @param resource - Any available resource definition
|
|
174
|
+
*
|
|
175
|
+
* @remarks
|
|
176
|
+
* There are situations when exactly the resource id is needed
|
|
177
|
+
* (for example, to compose the correct request to the api)
|
|
178
|
+
* then this method will come in handy to facilitate the extraction of the identifier
|
|
179
|
+
* if the resource is specified through a keyname or other parameters.
|
|
180
|
+
*/
|
|
181
|
+
getId(resource, requestOptions) {
|
|
182
|
+
if (typeof resource === "number") {
|
|
183
|
+
return CancelablePromise.resolve(resource);
|
|
184
|
+
} else if (typeof resource === "string" || isObject(resource)) {
|
|
185
|
+
return this.getOne(resource, requestOptions).then((res) => {
|
|
186
|
+
if (res) {
|
|
187
|
+
return res.resource.id;
|
|
135
188
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
return CancelablePromise.resolve(void 0);
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* A fast way to retrieve resource ID for any resource definition.
|
|
195
|
+
* @param resource - Any available resource definition
|
|
196
|
+
*
|
|
197
|
+
* @remarks
|
|
198
|
+
* Similar with {@link NgwConnector.getResourceId | getResourceId} but rise error if resource is not exist.
|
|
199
|
+
* To not make one more checks if the resource is definitely exists
|
|
200
|
+
*/
|
|
201
|
+
getIdOrFail(resource, requestOptions) {
|
|
202
|
+
return this.getId(resource, requestOptions).then((resp) => {
|
|
203
|
+
if (resp === void 0) {
|
|
204
|
+
throw new Error();
|
|
205
|
+
}
|
|
206
|
+
return resp;
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
getMany(resource, requestOptions) {
|
|
210
|
+
return this._resourceCacheFilter(resource).then((items) => {
|
|
211
|
+
if (!items.length) {
|
|
212
|
+
const query = {};
|
|
213
|
+
if (resource.keyname) {
|
|
214
|
+
query.keyname = resource.keyname;
|
|
215
|
+
} else {
|
|
216
|
+
Object.assign(query, resourceToQuery(resource));
|
|
140
217
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
218
|
+
return this.connector.get("resource.search", requestOptions, __spreadValues$1({
|
|
219
|
+
serialization: "full"
|
|
220
|
+
}, query)).then((resources) => {
|
|
221
|
+
if (resources) {
|
|
222
|
+
for (const x of resources) {
|
|
223
|
+
this.cache.add("resource.item", CancelablePromise.resolve(x), {
|
|
224
|
+
id: x.resource.id
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return resources;
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
return items;
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
getParent(resource, requestOptions) {
|
|
235
|
+
return this.getOne(resource, requestOptions).then((child) => {
|
|
236
|
+
if (child) {
|
|
237
|
+
return this.getOne(child.resource.parent.id, requestOptions);
|
|
238
|
+
}
|
|
239
|
+
return CancelablePromise.resolve(void 0);
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
getChildrenOf(resource, requestOptions) {
|
|
243
|
+
return this.getIdOrFail(resource).then(
|
|
244
|
+
(parent) => this._getChildrenOf(parent, requestOptions)
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
update(resource, data) {
|
|
248
|
+
return this.getId(resource).then((id) => {
|
|
249
|
+
if (id !== void 0) {
|
|
250
|
+
return this.connector.put("resource.item", { data }, { id });
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Fast way to delete resource from NGW and clean cache.
|
|
256
|
+
* @param resource - Resource definition
|
|
257
|
+
*/
|
|
258
|
+
delete(resource) {
|
|
259
|
+
return this.getId(resource).then((id) => {
|
|
260
|
+
if (id !== void 0) {
|
|
261
|
+
return this.connector.delete("resource.item", null, { id }).then(() => {
|
|
262
|
+
this._cleanResourceItemCache(id);
|
|
263
|
+
return void 0;
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
_getChildrenOf(parent, requestOptions, _items = []) {
|
|
269
|
+
return this.connector.get(
|
|
270
|
+
"resource.collection",
|
|
271
|
+
__spreadValues$1({ cache: true }, requestOptions),
|
|
272
|
+
{
|
|
273
|
+
parent
|
|
274
|
+
}
|
|
275
|
+
).then((items) => {
|
|
276
|
+
const recursivePromises = [];
|
|
277
|
+
for (const item of items) {
|
|
278
|
+
this.cache.add("resource.item", CancelablePromise.resolve(item), {
|
|
279
|
+
id: item.resource.id
|
|
280
|
+
});
|
|
281
|
+
_items.push(item);
|
|
282
|
+
if ((requestOptions == null ? void 0 : requestOptions.recursive) && item.resource.children) {
|
|
283
|
+
recursivePromises.push(
|
|
284
|
+
this._getChildrenOf(item.resource.id, requestOptions, _items)
|
|
285
|
+
);
|
|
152
286
|
}
|
|
153
|
-
|
|
154
|
-
|
|
287
|
+
}
|
|
288
|
+
if (recursivePromises.length) {
|
|
289
|
+
return CancelablePromise.all(recursivePromises).then(() => {
|
|
290
|
+
return _items;
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
return _items;
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
_cleanResourceItemCache(id) {
|
|
297
|
+
return __async(this, null, function* () {
|
|
298
|
+
const all = this.cache.all();
|
|
299
|
+
const toDelete = [];
|
|
300
|
+
for (const c of all) {
|
|
301
|
+
const cid = c.options && c.options.id;
|
|
302
|
+
if (["resource.item", "resource"].includes(c.key) && cid !== void 0) {
|
|
303
|
+
if (typeof cid === "number") {
|
|
304
|
+
if (cid === id) {
|
|
305
|
+
toDelete.push(c);
|
|
306
|
+
}
|
|
307
|
+
} else {
|
|
308
|
+
const rid = yield this.getId(cid);
|
|
309
|
+
if (rid === id) {
|
|
310
|
+
toDelete.push(c);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
155
313
|
}
|
|
156
|
-
|
|
314
|
+
}
|
|
315
|
+
for (const d of toDelete) {
|
|
316
|
+
this.cache.delete(d);
|
|
317
|
+
}
|
|
157
318
|
});
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
// Transfer part of the parameters from `params` to the URL string
|
|
184
|
-
if (params) {
|
|
185
|
-
const paramArray = [];
|
|
186
|
-
const paramList = params.paramList;
|
|
187
|
-
if (Array.isArray(paramList)) {
|
|
188
|
-
delete params.paramList;
|
|
189
|
-
paramList.forEach((x) => {
|
|
190
|
-
paramArray.push(`${x[0]}=${x[1]}`);
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
for (const p in params) {
|
|
194
|
-
if (apiItem.indexOf(p) === -1) {
|
|
195
|
-
paramArray.push(`${p}=${params[p]}`);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
if (paramArray.length) {
|
|
199
|
-
url = url + '?' + paramArray.join('&');
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
if (url) {
|
|
203
|
-
resolve(opt.connector.makeQuery(url, params, opt.requestOptions));
|
|
204
|
-
}
|
|
205
|
-
else {
|
|
206
|
-
reject(new Error('request url is not set'));
|
|
207
|
-
}
|
|
319
|
+
}
|
|
320
|
+
_fetchResourceById(id, requestOptions) {
|
|
321
|
+
const promise = () => this.connector.get("resource.item", requestOptions, { id });
|
|
322
|
+
return this.cache.add("resource.item", promise, {
|
|
323
|
+
id
|
|
324
|
+
}).catch((er) => {
|
|
325
|
+
if (!(er instanceof ResourceNotFoundError)) {
|
|
326
|
+
throw er;
|
|
327
|
+
}
|
|
328
|
+
return void 0;
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
_fetchResourceBy(resource, requestOptions) {
|
|
332
|
+
return this.getMany(resource, requestOptions).then((resources) => {
|
|
333
|
+
return resources[0];
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
_resourceCacheFilter(resource) {
|
|
337
|
+
return CancelablePromise.all(this.cache.matchAll("resource.item")).then(
|
|
338
|
+
(resources) => {
|
|
339
|
+
const items = [];
|
|
340
|
+
resources.filter((x) => {
|
|
341
|
+
if (x) {
|
|
342
|
+
if (resource.keyname && x.resource.keyname) {
|
|
343
|
+
return resource.keyname === x.resource.keyname;
|
|
208
344
|
}
|
|
209
|
-
|
|
210
|
-
|
|
345
|
+
if (defined(resource.id) && defined(x.resource.id)) {
|
|
346
|
+
return resource.id === x.resource.id;
|
|
211
347
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
reject(er);
|
|
348
|
+
return resourceCompare(resource, x.resource);
|
|
349
|
+
}
|
|
215
350
|
});
|
|
216
|
-
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
* Thrown when ...
|
|
221
|
-
*/
|
|
222
|
-
class ResourceNotFoundError extends NgwError {
|
|
223
|
-
constructor(obj) {
|
|
224
|
-
super(obj);
|
|
225
|
-
this.name = 'ResourceNotFoundError';
|
|
226
|
-
this.exception = 'nextgisweb.resource.exception.ResourceNotFound';
|
|
227
|
-
Object.setPrototypeOf(this, ResourceNotFoundError.prototype);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* Thrown when ...
|
|
233
|
-
*/
|
|
234
|
-
class InsufficientPermissionsError extends NgwError {
|
|
235
|
-
constructor(obj) {
|
|
236
|
-
super(obj);
|
|
237
|
-
this.name = 'InsufficientPermissionsError';
|
|
238
|
-
this.exception = 'nextgisweb.core.exception.InsufficientPermissions';
|
|
239
|
-
Object.setPrototypeOf(this, InsufficientPermissionsError.prototype);
|
|
240
|
-
}
|
|
351
|
+
return items;
|
|
352
|
+
}
|
|
353
|
+
);
|
|
354
|
+
}
|
|
241
355
|
}
|
|
242
356
|
|
|
243
357
|
const CONNECTORS = [];
|
|
244
358
|
function addConnector(connector) {
|
|
245
|
-
|
|
359
|
+
CONNECTORS.push(connector);
|
|
246
360
|
}
|
|
247
361
|
function findConnector(options) {
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
else {
|
|
256
|
-
// if auth not in find options; use first connector with same url
|
|
257
|
-
return true;
|
|
258
|
-
}
|
|
362
|
+
return CONNECTORS.find((x) => {
|
|
363
|
+
if (x.options.baseUrl === options.baseUrl) {
|
|
364
|
+
if (options.auth) {
|
|
365
|
+
if (x.options.auth) {
|
|
366
|
+
return objectDeepEqual(x.options.auth, options.auth);
|
|
259
367
|
}
|
|
260
|
-
|
|
368
|
+
} else {
|
|
369
|
+
return true;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
});
|
|
261
373
|
}
|
|
262
374
|
function removeConnector(connector) {
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
375
|
+
const index = CONNECTORS.indexOf(connector);
|
|
376
|
+
if (index !== -1) {
|
|
377
|
+
CONNECTORS.splice(index, 1);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
var __defProp$2 = Object.defineProperty;
|
|
382
|
+
var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
383
|
+
var __publicField$2 = (obj, key, value) => {
|
|
384
|
+
__defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
385
|
+
return value;
|
|
386
|
+
};
|
|
387
|
+
class InsufficientPermissionsError extends NgwError {
|
|
388
|
+
constructor(obj) {
|
|
389
|
+
super(obj);
|
|
390
|
+
__publicField$2(this, "name", "InsufficientPermissionsError");
|
|
391
|
+
__publicField$2(this, "exception", "nextgisweb.core.exception.InsufficientPermissions");
|
|
392
|
+
Object.setPrototypeOf(this, InsufficientPermissionsError.prototype);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
const templateRe = /\{ *([\w_-]+) *\}/g;
|
|
397
|
+
function template(str, data) {
|
|
398
|
+
return str.replace(templateRe, (s, key) => {
|
|
399
|
+
let value = data[key];
|
|
400
|
+
if (value === void 0) {
|
|
401
|
+
throw new Error("No value provided for letiable " + s);
|
|
402
|
+
} else if (typeof value === "function") {
|
|
403
|
+
value = value(data);
|
|
266
404
|
}
|
|
405
|
+
return value;
|
|
406
|
+
});
|
|
267
407
|
}
|
|
268
408
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
if (key === 'owner_user') {
|
|
287
|
-
const children = resourceToQuery(value, key);
|
|
288
|
-
Object.assign(query, children);
|
|
289
|
-
}
|
|
290
|
-
else if (key === 'parent' && 'id' in value) {
|
|
291
|
-
query.parent_id = value.id;
|
|
292
|
-
}
|
|
409
|
+
function apiRequest(opt) {
|
|
410
|
+
const params = opt.params;
|
|
411
|
+
return new CancelablePromise((resolve, reject) => {
|
|
412
|
+
opt.connector.connect().then((apiItems) => {
|
|
413
|
+
let apiItem = apiItems && apiItems[opt.name];
|
|
414
|
+
if (apiItem) {
|
|
415
|
+
apiItem = [...apiItem];
|
|
416
|
+
let url = apiItem.shift();
|
|
417
|
+
if (apiItem.length) {
|
|
418
|
+
const replaceParams = {};
|
|
419
|
+
for (let fry = 0; fry < apiItem.length; fry++) {
|
|
420
|
+
const arg = apiItem[fry];
|
|
421
|
+
replaceParams[fry] = "{" + arg + "}";
|
|
422
|
+
if (params[arg] === void 0) {
|
|
423
|
+
throw new Error(
|
|
424
|
+
"`" + arg + "` url api argument is not specified"
|
|
425
|
+
);
|
|
293
426
|
}
|
|
294
|
-
|
|
295
|
-
|
|
427
|
+
}
|
|
428
|
+
if (url) {
|
|
429
|
+
url = template(url, replaceParams);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
if (params) {
|
|
433
|
+
const paramArray = [];
|
|
434
|
+
const paramList = params.paramList;
|
|
435
|
+
if (Array.isArray(paramList)) {
|
|
436
|
+
delete params.paramList;
|
|
437
|
+
paramList.forEach((x) => {
|
|
438
|
+
paramArray.push(`${x[0]}=${x[1]}`);
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
for (const p in params) {
|
|
442
|
+
if (apiItem.indexOf(p) === -1) {
|
|
443
|
+
paramArray.push(`${p}=${params[p]}`);
|
|
296
444
|
}
|
|
445
|
+
}
|
|
446
|
+
if (paramArray.length) {
|
|
447
|
+
url = url + "?" + paramArray.join("&");
|
|
448
|
+
}
|
|
297
449
|
}
|
|
298
|
-
|
|
299
|
-
|
|
450
|
+
if (url) {
|
|
451
|
+
resolve(opt.connector.makeQuery(url, params, opt.requestOptions));
|
|
452
|
+
} else {
|
|
453
|
+
reject(new Error("request url is not set"));
|
|
454
|
+
}
|
|
455
|
+
} else {
|
|
456
|
+
resolve(void 0);
|
|
457
|
+
}
|
|
458
|
+
}).catch((er) => {
|
|
459
|
+
reject(er);
|
|
460
|
+
});
|
|
461
|
+
});
|
|
300
462
|
}
|
|
301
463
|
|
|
302
|
-
|
|
303
|
-
|
|
464
|
+
var __defProp$1 = Object.defineProperty;
|
|
465
|
+
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
466
|
+
var __publicField$1 = (obj, key, value) => {
|
|
467
|
+
__defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
468
|
+
return value;
|
|
469
|
+
};
|
|
470
|
+
class NetworkError extends Error {
|
|
471
|
+
constructor(url) {
|
|
472
|
+
super();
|
|
473
|
+
__publicField$1(this, "name", "NetworkError");
|
|
474
|
+
Object.setPrototypeOf(this, NetworkError.prototype);
|
|
475
|
+
this.message = `Unable to request ${url}.
|
|
476
|
+
Possibly invalid NGW URL entered or CORS not configured to get request from ${location.origin}`;
|
|
477
|
+
}
|
|
304
478
|
}
|
|
305
479
|
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
480
|
+
let loadData;
|
|
481
|
+
{
|
|
482
|
+
loadData = (url, callback, options = {}, error, onCancel) => {
|
|
483
|
+
options.method = options.method || "GET";
|
|
484
|
+
const xhr = new XMLHttpRequest();
|
|
485
|
+
xhr.open(options.method || "GET", url, true);
|
|
486
|
+
if (options.responseType === "blob") {
|
|
487
|
+
xhr.responseType = options.responseType;
|
|
310
488
|
}
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
}
|
|
328
|
-
else if (typeof resource === 'number') {
|
|
329
|
-
forCache.id = resource;
|
|
330
|
-
}
|
|
331
|
-
else if (isObject(resource)) {
|
|
332
|
-
if (resource.id !== undefined) {
|
|
333
|
-
forCache.id = resource.id;
|
|
334
|
-
}
|
|
335
|
-
else if (resource.keyname) {
|
|
336
|
-
forCache.keyname = resource.keyname;
|
|
337
|
-
}
|
|
489
|
+
const getResponseText = () => {
|
|
490
|
+
try {
|
|
491
|
+
return JSON.parse(xhr.responseText);
|
|
492
|
+
} catch (er) {
|
|
493
|
+
return xhr.responseText;
|
|
494
|
+
}
|
|
495
|
+
};
|
|
496
|
+
const processingResponse = (forError = false) => {
|
|
497
|
+
const cb = forError ? error : callback;
|
|
498
|
+
if (options.responseType === "blob") {
|
|
499
|
+
cb(xhr.response);
|
|
500
|
+
} else {
|
|
501
|
+
if (xhr.responseText) {
|
|
502
|
+
cb(getResponseText());
|
|
503
|
+
} else {
|
|
504
|
+
error({ message: "" });
|
|
338
505
|
}
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
});
|
|
360
|
-
}
|
|
361
|
-
/**
|
|
362
|
-
* A fast way to retrieve resource ID for any resource definition.
|
|
363
|
-
* @param resource - Any available resource definition
|
|
364
|
-
*
|
|
365
|
-
* @remarks
|
|
366
|
-
* There are situations when exactly the resource id is needed
|
|
367
|
-
* (for example, to compose the correct request to the api)
|
|
368
|
-
* then this method will come in handy to facilitate the extraction of the identifier
|
|
369
|
-
* if the resource is specified through a keyname or other parameters.
|
|
370
|
-
*/
|
|
371
|
-
getId(resource, requestOptions) {
|
|
372
|
-
if (typeof resource === 'number') {
|
|
373
|
-
return CancelablePromise.resolve(resource);
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
xhr.onload = () => {
|
|
509
|
+
if ([401, 403, 404, 422, 500].indexOf(xhr.status) !== -1) {
|
|
510
|
+
error(new NgwError(getResponseText()));
|
|
511
|
+
}
|
|
512
|
+
processingResponse();
|
|
513
|
+
};
|
|
514
|
+
xhr.onerror = (er) => {
|
|
515
|
+
if (xhr.status === 0) {
|
|
516
|
+
error(new NetworkError(url));
|
|
517
|
+
} else {
|
|
518
|
+
error(er);
|
|
519
|
+
}
|
|
520
|
+
};
|
|
521
|
+
xhr.upload.onprogress = function(e) {
|
|
522
|
+
if (e.lengthComputable) {
|
|
523
|
+
const percentComplete = e.loaded / e.total * 100;
|
|
524
|
+
if (options.onProgress) {
|
|
525
|
+
options.onProgress(percentComplete, e);
|
|
374
526
|
}
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
527
|
+
}
|
|
528
|
+
};
|
|
529
|
+
const headers = options.headers;
|
|
530
|
+
if (headers) {
|
|
531
|
+
for (const h in headers) {
|
|
532
|
+
const header = headers[h];
|
|
533
|
+
if (typeof header === "string") {
|
|
534
|
+
xhr.setRequestHeader(h, header);
|
|
381
535
|
}
|
|
382
|
-
|
|
383
|
-
}
|
|
384
|
-
/**
|
|
385
|
-
* A fast way to retrieve resource ID for any resource definition.
|
|
386
|
-
* @param resource - Any available resource definition
|
|
387
|
-
*
|
|
388
|
-
* @remarks
|
|
389
|
-
* Similar with {@link NgwConnector.getResourceId | getResourceId} but rise error if resource is not exist.
|
|
390
|
-
* To not make one more checks if the resource is definitely exists
|
|
391
|
-
*/
|
|
392
|
-
getIdOrFail(resource, requestOptions) {
|
|
393
|
-
return this.getId(resource, requestOptions).then((resp) => {
|
|
394
|
-
if (resp === undefined) {
|
|
395
|
-
throw new Error();
|
|
396
|
-
}
|
|
397
|
-
return resp;
|
|
398
|
-
});
|
|
399
|
-
}
|
|
400
|
-
getMany(resource, requestOptions) {
|
|
401
|
-
return this._resourceCacheFilter(resource).then((items) => {
|
|
402
|
-
if (!items.length) {
|
|
403
|
-
const query = {};
|
|
404
|
-
if (resource.keyname) {
|
|
405
|
-
query.keyname = resource.keyname;
|
|
406
|
-
}
|
|
407
|
-
else {
|
|
408
|
-
Object.assign(query, resourceToQuery(resource));
|
|
409
|
-
}
|
|
410
|
-
return this.connector
|
|
411
|
-
.get('resource.search', requestOptions, {
|
|
412
|
-
serialization: 'full',
|
|
413
|
-
...query,
|
|
414
|
-
})
|
|
415
|
-
.then((resources) => {
|
|
416
|
-
if (resources) {
|
|
417
|
-
for (const x of resources) {
|
|
418
|
-
this.cache.add('resource.item', CancelablePromise.resolve(x), {
|
|
419
|
-
id: x.resource.id,
|
|
420
|
-
});
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
return resources;
|
|
424
|
-
});
|
|
425
|
-
}
|
|
426
|
-
return items;
|
|
427
|
-
});
|
|
428
|
-
}
|
|
429
|
-
getParent(resource, requestOptions) {
|
|
430
|
-
return this.getOne(resource, requestOptions).then((child) => {
|
|
431
|
-
if (child) {
|
|
432
|
-
return this.getOne(child.resource.parent.id, requestOptions);
|
|
433
|
-
}
|
|
434
|
-
return CancelablePromise.resolve(undefined);
|
|
435
|
-
});
|
|
436
|
-
}
|
|
437
|
-
getChildrenOf(resource, requestOptions) {
|
|
438
|
-
return this.getIdOrFail(resource).then((parent) => this._getChildrenOf(parent, requestOptions));
|
|
439
|
-
}
|
|
440
|
-
update(resource, data) {
|
|
441
|
-
return this.getId(resource).then((id) => {
|
|
442
|
-
if (id !== undefined) {
|
|
443
|
-
return this.connector.put('resource.item', { data }, { id });
|
|
444
|
-
}
|
|
445
|
-
});
|
|
446
|
-
}
|
|
447
|
-
/**
|
|
448
|
-
* Fast way to delete resource from NGW and clean cache.
|
|
449
|
-
* @param resource - Resource definition
|
|
450
|
-
*/
|
|
451
|
-
delete(resource) {
|
|
452
|
-
return this.getId(resource).then((id) => {
|
|
453
|
-
if (id !== undefined) {
|
|
454
|
-
return this.connector.delete('resource.item', null, { id }).then(() => {
|
|
455
|
-
this._cleanResourceItemCache(id);
|
|
456
|
-
return undefined;
|
|
457
|
-
});
|
|
458
|
-
}
|
|
459
|
-
});
|
|
536
|
+
}
|
|
460
537
|
}
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
.get('resource.collection', { cache: true, ...requestOptions }, {
|
|
464
|
-
parent,
|
|
465
|
-
})
|
|
466
|
-
.then((items) => {
|
|
467
|
-
const recursivePromises = [];
|
|
468
|
-
for (const item of items) {
|
|
469
|
-
this.cache.add('resource.item', CancelablePromise.resolve(item), {
|
|
470
|
-
id: item.resource.id,
|
|
471
|
-
});
|
|
472
|
-
_items.push(item);
|
|
473
|
-
if ((requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.recursive) && item.resource.children) {
|
|
474
|
-
recursivePromises.push(this._getChildrenOf(item.resource.id, requestOptions, _items));
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
if (recursivePromises.length) {
|
|
478
|
-
return CancelablePromise.all(recursivePromises).then(() => {
|
|
479
|
-
return _items;
|
|
480
|
-
});
|
|
481
|
-
}
|
|
482
|
-
return _items;
|
|
483
|
-
});
|
|
538
|
+
if (options.withCredentials !== void 0) {
|
|
539
|
+
xhr.withCredentials = options.withCredentials;
|
|
484
540
|
}
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
if (cid === id) {
|
|
493
|
-
toDelete.push(c);
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
else {
|
|
497
|
-
const rid = await this.getId(cid);
|
|
498
|
-
if (rid === id) {
|
|
499
|
-
toDelete.push(c);
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
}
|
|
504
|
-
for (const d of toDelete) {
|
|
505
|
-
this.cache.delete(d);
|
|
541
|
+
let data;
|
|
542
|
+
if (options.file) {
|
|
543
|
+
data = new FormData();
|
|
544
|
+
data.append("file", options.file);
|
|
545
|
+
if (options.data) {
|
|
546
|
+
for (const d in options.data) {
|
|
547
|
+
data.append(d, data[d]);
|
|
506
548
|
}
|
|
549
|
+
}
|
|
550
|
+
} else {
|
|
551
|
+
data = options.data ? typeof options.data === "string" ? options.data : JSON.stringify(options.data) : null;
|
|
507
552
|
}
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
id,
|
|
513
|
-
})
|
|
514
|
-
.catch((er) => {
|
|
515
|
-
if (!(er instanceof ResourceNotFoundError)) {
|
|
516
|
-
throw er;
|
|
517
|
-
}
|
|
518
|
-
return undefined;
|
|
519
|
-
});
|
|
520
|
-
}
|
|
521
|
-
_fetchResourceBy(resource, requestOptions) {
|
|
522
|
-
return this.getMany(resource, requestOptions).then((resources) => {
|
|
523
|
-
return resources[0];
|
|
524
|
-
});
|
|
525
|
-
}
|
|
526
|
-
_resourceCacheFilter(resource) {
|
|
527
|
-
return CancelablePromise.all(this.cache.matchAll('resource.item')).then((resources) => {
|
|
528
|
-
const items = [];
|
|
529
|
-
resources.filter((x) => {
|
|
530
|
-
if (x) {
|
|
531
|
-
// identical by uniq props
|
|
532
|
-
if (resource.keyname && x.resource.keyname) {
|
|
533
|
-
return resource.keyname === x.resource.keyname;
|
|
534
|
-
}
|
|
535
|
-
if (defined(resource.id) && defined(x.resource.id)) {
|
|
536
|
-
return resource.id === x.resource.id;
|
|
537
|
-
}
|
|
538
|
-
return resourceCompare(resource, x.resource);
|
|
539
|
-
}
|
|
540
|
-
});
|
|
541
|
-
return items;
|
|
542
|
-
});
|
|
553
|
+
if (onCancel) {
|
|
554
|
+
onCancel(() => {
|
|
555
|
+
xhr.abort();
|
|
556
|
+
});
|
|
543
557
|
}
|
|
558
|
+
xhr.send(data);
|
|
559
|
+
};
|
|
544
560
|
}
|
|
545
561
|
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
if (this.options.route) {
|
|
560
|
-
this.routeStr = this.options.route;
|
|
561
|
-
}
|
|
562
|
-
if (this.options.requestTransform) {
|
|
563
|
-
this.requestTransform = this.options.requestTransform;
|
|
564
|
-
}
|
|
565
|
-
this.resources = new ResourcesControl(this);
|
|
566
|
-
addConnector(this);
|
|
567
|
-
}
|
|
568
|
-
}
|
|
569
|
-
static create(options) {
|
|
570
|
-
return new this(options);
|
|
571
|
-
}
|
|
572
|
-
setRequestTransform(requestTransform) {
|
|
573
|
-
this.requestTransform = requestTransform;
|
|
562
|
+
var __defProp = Object.defineProperty;
|
|
563
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
564
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
565
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
566
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
567
|
+
var __spreadValues = (a, b) => {
|
|
568
|
+
for (var prop in b || (b = {}))
|
|
569
|
+
if (__hasOwnProp.call(b, prop))
|
|
570
|
+
__defNormalProp(a, prop, b[prop]);
|
|
571
|
+
if (__getOwnPropSymbols)
|
|
572
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
573
|
+
if (__propIsEnum.call(b, prop))
|
|
574
|
+
__defNormalProp(a, prop, b[prop]);
|
|
574
575
|
}
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
576
|
+
return a;
|
|
577
|
+
};
|
|
578
|
+
var __publicField = (obj, key, value) => {
|
|
579
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
580
|
+
return value;
|
|
581
|
+
};
|
|
582
|
+
let ID = 0;
|
|
583
|
+
class NgwConnector {
|
|
584
|
+
constructor(options) {
|
|
585
|
+
this.options = options;
|
|
586
|
+
__publicField(this, "id", ID++);
|
|
587
|
+
__publicField(this, "emitter", new EventEmitter());
|
|
588
|
+
__publicField(this, "user");
|
|
589
|
+
__publicField(this, "resources");
|
|
590
|
+
__publicField(this, "routeStr", "/api/component/pyramid/route");
|
|
591
|
+
__publicField(this, "activeRequests", []);
|
|
592
|
+
__publicField(this, "requestTransform");
|
|
593
|
+
const exist = findConnector(options);
|
|
594
|
+
if (exist) {
|
|
595
|
+
return exist;
|
|
596
|
+
} else {
|
|
597
|
+
if (this.options.route) {
|
|
598
|
+
this.routeStr = this.options.route;
|
|
599
|
+
}
|
|
600
|
+
if (this.options.requestTransform) {
|
|
601
|
+
this.requestTransform = this.options.requestTransform;
|
|
602
|
+
}
|
|
603
|
+
this.resources = new ResourcesControl(this);
|
|
604
|
+
addConnector(this);
|
|
584
605
|
}
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
})
|
|
623
|
-
|
|
624
|
-
id: this.id,
|
|
625
|
-
auth,
|
|
626
|
-
baseUrl: this.options.baseUrl,
|
|
606
|
+
}
|
|
607
|
+
static create(options) {
|
|
608
|
+
return new this(options);
|
|
609
|
+
}
|
|
610
|
+
setRequestTransform(requestTransform) {
|
|
611
|
+
this.requestTransform = requestTransform;
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* Fast way to specify the connection address to NextGIS Web.
|
|
615
|
+
* The current connection will be severed.
|
|
616
|
+
* @param baseUrl - NGW url
|
|
617
|
+
*/
|
|
618
|
+
setNgw(baseUrl) {
|
|
619
|
+
this.logout();
|
|
620
|
+
this.options.baseUrl = baseUrl;
|
|
621
|
+
addConnector(this);
|
|
622
|
+
}
|
|
623
|
+
/**
|
|
624
|
+
* Establishing a connection with NextGIS Web to fulfill all other requests.
|
|
625
|
+
* @remarks
|
|
626
|
+
* This method need not be called manually as it is used when forming a request in {@link apiRequest | apiRequest}.
|
|
627
|
+
* Can be used to check connection.
|
|
628
|
+
* @example
|
|
629
|
+
* ```javascript
|
|
630
|
+
* const connector = new NgwConnector({baseUrl: 'https://demo.nextgis.com'});
|
|
631
|
+
* connector.connect()
|
|
632
|
+
* .then(() => console.log('Ok'))
|
|
633
|
+
* .catch((er) => console.log('Connection problem', er));
|
|
634
|
+
* ```
|
|
635
|
+
*/
|
|
636
|
+
connect() {
|
|
637
|
+
const cache = new Cache();
|
|
638
|
+
const auth = this.options.auth;
|
|
639
|
+
const makeConnect = () => new CancelablePromise((resolve, reject) => {
|
|
640
|
+
const makeQuery = () => {
|
|
641
|
+
return this.makeQuery(this.routeStr, {}, {}).then((route) => {
|
|
642
|
+
resolve(route);
|
|
643
|
+
}).catch((er) => {
|
|
644
|
+
reject(er);
|
|
627
645
|
});
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
addConnector(this);
|
|
636
|
-
return this._login(credentials, options);
|
|
637
|
-
}
|
|
638
|
-
/**
|
|
639
|
-
* Disconnecting a user. Aborting all current requests
|
|
640
|
-
*/
|
|
641
|
-
logout() {
|
|
642
|
-
this.abort();
|
|
643
|
-
removeConnector(this);
|
|
644
|
-
this.options.auth = undefined;
|
|
645
|
-
this.user = undefined;
|
|
646
|
-
this.emitter.emit('logout');
|
|
647
|
-
this.resources.cache.clean();
|
|
648
|
-
}
|
|
649
|
-
getUserInfo(credentials, options) {
|
|
650
|
-
if (this.user && this.user.id) {
|
|
651
|
-
return CancelablePromise.resolve(this.user);
|
|
652
|
-
}
|
|
653
|
-
if (credentials) {
|
|
654
|
-
this.options.auth = credentials;
|
|
655
|
-
}
|
|
656
|
-
const options_ = {
|
|
657
|
-
headers: this.getAuthorizationHeaders(credentials),
|
|
658
|
-
// withCredentials: true
|
|
659
|
-
...options,
|
|
660
|
-
};
|
|
661
|
-
// Do not use apiRequest('auth.current_user') to avoid circular-references
|
|
662
|
-
return this.makeQuery('/api/component/auth/current_user', {}, options_);
|
|
663
|
-
}
|
|
664
|
-
/**
|
|
665
|
-
* Obtaining the required Headers for authentication of requests in the NGW.
|
|
666
|
-
*/
|
|
667
|
-
getAuthorizationHeaders(credentials) {
|
|
668
|
-
const client = this.makeClientId(credentials);
|
|
669
|
-
if (client) {
|
|
670
|
-
return {
|
|
671
|
-
Authorization: 'Basic ' + client,
|
|
672
|
-
};
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
makeClientId(credentials) {
|
|
676
|
-
credentials = credentials || this.options.auth;
|
|
677
|
-
if (credentials) {
|
|
678
|
-
const { login, password } = credentials;
|
|
679
|
-
const str = unescape(encodeURIComponent(`${login}:${password}`));
|
|
680
|
-
// @ts-ignore
|
|
681
|
-
{
|
|
682
|
-
return window.btoa(str);
|
|
683
|
-
}
|
|
646
|
+
};
|
|
647
|
+
if (auth) {
|
|
648
|
+
const { login, password } = auth;
|
|
649
|
+
if (login && password) {
|
|
650
|
+
return this._login({ login, password }).then(() => {
|
|
651
|
+
return makeQuery();
|
|
652
|
+
}).catch((er) => reject(er));
|
|
684
653
|
}
|
|
654
|
+
}
|
|
655
|
+
return makeQuery();
|
|
656
|
+
});
|
|
657
|
+
return cache.add("route", makeConnect, {
|
|
658
|
+
id: this.id,
|
|
659
|
+
auth,
|
|
660
|
+
baseUrl: this.options.baseUrl
|
|
661
|
+
});
|
|
662
|
+
}
|
|
663
|
+
/**
|
|
664
|
+
* Quick way to change NextGIS Web user.
|
|
665
|
+
* @param credentials - New user credentials
|
|
666
|
+
*/
|
|
667
|
+
login(credentials, options) {
|
|
668
|
+
this.logout();
|
|
669
|
+
addConnector(this);
|
|
670
|
+
return this._login(credentials, options);
|
|
671
|
+
}
|
|
672
|
+
/**
|
|
673
|
+
* Disconnecting a user. Aborting all current requests
|
|
674
|
+
*/
|
|
675
|
+
logout() {
|
|
676
|
+
this.abort();
|
|
677
|
+
removeConnector(this);
|
|
678
|
+
this.options.auth = void 0;
|
|
679
|
+
this.user = void 0;
|
|
680
|
+
this.emitter.emit("logout");
|
|
681
|
+
this.resources.cache.clean();
|
|
682
|
+
}
|
|
683
|
+
getUserInfo(credentials, options) {
|
|
684
|
+
if (this.user && this.user.id) {
|
|
685
|
+
return CancelablePromise.resolve(this.user);
|
|
685
686
|
}
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
for (const req of this.activeRequests) {
|
|
689
|
-
req.cancel();
|
|
690
|
-
}
|
|
691
|
-
this.activeRequests = [];
|
|
687
|
+
if (credentials) {
|
|
688
|
+
this.options.auth = credentials;
|
|
692
689
|
}
|
|
693
|
-
|
|
694
|
-
|
|
690
|
+
const options_ = __spreadValues({
|
|
691
|
+
headers: this.getAuthorizationHeaders(credentials)
|
|
692
|
+
}, options);
|
|
693
|
+
return this.makeQuery(
|
|
694
|
+
"/api/component/auth/current_user",
|
|
695
|
+
{},
|
|
696
|
+
options_
|
|
697
|
+
);
|
|
698
|
+
}
|
|
699
|
+
/**
|
|
700
|
+
* Obtaining the required Headers for authentication of requests in the NGW.
|
|
701
|
+
*/
|
|
702
|
+
getAuthorizationHeaders(credentials) {
|
|
703
|
+
const client = this.makeClientId(credentials);
|
|
704
|
+
if (client) {
|
|
705
|
+
return {
|
|
706
|
+
Authorization: "Basic " + client
|
|
707
|
+
};
|
|
695
708
|
}
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
* // there is such an NGW route item
|
|
706
|
-
* // "feature_layer.feature.item": [
|
|
707
|
-
* // "/api/resource/{0}/feature/{1}",
|
|
708
|
-
* // "id",
|
|
709
|
-
* // "fid"
|
|
710
|
-
* // ],
|
|
711
|
-
*
|
|
712
|
-
* const connector = new NgwConnector({ baseUrl: 'https://example.nextgis.com' });
|
|
713
|
-
* connector.apiRequest('feature_layer.feature.item', {
|
|
714
|
-
* // request params for {0} and {1}
|
|
715
|
-
* 'id': 2011,
|
|
716
|
-
* 'fid': 101,
|
|
717
|
-
* // query params
|
|
718
|
-
* 'srs': 4326,
|
|
719
|
-
* 'geom_format': 'geojson',
|
|
720
|
-
* }, { method: 'GET' });
|
|
721
|
-
* // send get-request to 'https://example.nextgis.com/api/resource/2011/feature/101?srs=4326&geom_format=geojson'
|
|
722
|
-
*
|
|
723
|
-
* ```
|
|
724
|
-
*/
|
|
725
|
-
apiRequest(name, params_ = {}, requestOptions = {}) {
|
|
726
|
-
var _a, _b;
|
|
727
|
-
const { method, headers, withCredentials, responseType } = requestOptions;
|
|
728
|
-
params_ = (_a = requestOptions.params) !== null && _a !== void 0 ? _a : params_;
|
|
729
|
-
const params = objectRemoveEmpty(params_);
|
|
730
|
-
const makeApiRequest = () => apiRequest({ name, params, requestOptions, connector: this });
|
|
731
|
-
if (requestOptions.cache && method === 'GET') {
|
|
732
|
-
const cache = new Cache();
|
|
733
|
-
return cache.add(name, makeApiRequest, {
|
|
734
|
-
params,
|
|
735
|
-
...objectRemoveEmpty({
|
|
736
|
-
headers,
|
|
737
|
-
withCredentials,
|
|
738
|
-
responseType,
|
|
739
|
-
baseUrl: this.options.baseUrl,
|
|
740
|
-
userId: (_b = this.user) === null || _b === void 0 ? void 0 : _b.id,
|
|
741
|
-
}),
|
|
742
|
-
});
|
|
743
|
-
}
|
|
744
|
-
return makeApiRequest();
|
|
709
|
+
}
|
|
710
|
+
makeClientId(credentials) {
|
|
711
|
+
credentials = credentials || this.options.auth;
|
|
712
|
+
if (credentials) {
|
|
713
|
+
const { login, password } = credentials;
|
|
714
|
+
const str = unescape(encodeURIComponent(`${login}:${password}`));
|
|
715
|
+
{
|
|
716
|
+
return window.btoa(str);
|
|
717
|
+
}
|
|
745
718
|
}
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
*
|
|
752
|
-
* @example
|
|
753
|
-
* ```javascript
|
|
754
|
-
* connector.post('resource.collection', { data: POST_PAYLOAD })
|
|
755
|
-
* .then((newResource) => console.log(newResource))
|
|
756
|
-
* .catch((error) => console.warn(error));
|
|
757
|
-
* ```
|
|
758
|
-
*/
|
|
759
|
-
post(name, options, params) {
|
|
760
|
-
options = options || {};
|
|
761
|
-
options.method = 'POST';
|
|
762
|
-
return this.apiRequest(name, params, options);
|
|
719
|
+
}
|
|
720
|
+
/** Stop all api requests */
|
|
721
|
+
abort() {
|
|
722
|
+
for (const req of this.activeRequests) {
|
|
723
|
+
req.cancel();
|
|
763
724
|
}
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
725
|
+
this.activeRequests = [];
|
|
726
|
+
}
|
|
727
|
+
getActiveApiRequests() {
|
|
728
|
+
return [...this.activeRequests];
|
|
729
|
+
}
|
|
730
|
+
/**
|
|
731
|
+
* Send request to NGW api router.
|
|
732
|
+
* @param name - NGW route name from {@link https://docs.nextgis.com/docs_ngweb_dev/doc/developer/resource.html#routes | routes}
|
|
733
|
+
* @param params - Request item params or query params
|
|
734
|
+
* @param requestOptions - Request options
|
|
735
|
+
*
|
|
736
|
+
* @example
|
|
737
|
+
* ```javascript
|
|
738
|
+
*
|
|
739
|
+
* // there is such an NGW route item
|
|
740
|
+
* // "feature_layer.feature.item": [
|
|
741
|
+
* // "/api/resource/{0}/feature/{1}",
|
|
742
|
+
* // "id",
|
|
743
|
+
* // "fid"
|
|
744
|
+
* // ],
|
|
745
|
+
*
|
|
746
|
+
* const connector = new NgwConnector({ baseUrl: 'https://example.nextgis.com' });
|
|
747
|
+
* connector.apiRequest('feature_layer.feature.item', {
|
|
748
|
+
* // request params for {0} and {1}
|
|
749
|
+
* 'id': 2011,
|
|
750
|
+
* 'fid': 101,
|
|
751
|
+
* // query params
|
|
752
|
+
* 'srs': 4326,
|
|
753
|
+
* 'geom_format': 'geojson',
|
|
754
|
+
* }, { method: 'GET' });
|
|
755
|
+
* // send get-request to 'https://example.nextgis.com/api/resource/2011/feature/101?srs=4326&geom_format=geojson'
|
|
756
|
+
*
|
|
757
|
+
* ```
|
|
758
|
+
*/
|
|
759
|
+
apiRequest(name, params_ = {}, requestOptions = {}) {
|
|
760
|
+
var _a, _b;
|
|
761
|
+
const { method, headers, withCredentials, responseType } = requestOptions;
|
|
762
|
+
params_ = (_a = requestOptions.params) != null ? _a : params_;
|
|
763
|
+
const params = objectRemoveEmpty(params_);
|
|
764
|
+
const makeApiRequest = () => apiRequest({ name, params, requestOptions, connector: this });
|
|
765
|
+
if (requestOptions.cache && method === "GET") {
|
|
766
|
+
const cache = new Cache();
|
|
767
|
+
return cache.add(name, makeApiRequest, __spreadValues({
|
|
768
|
+
params
|
|
769
|
+
}, objectRemoveEmpty({
|
|
770
|
+
headers,
|
|
771
|
+
withCredentials,
|
|
772
|
+
responseType,
|
|
773
|
+
baseUrl: this.options.baseUrl,
|
|
774
|
+
userId: (_b = this.user) == null ? void 0 : _b.id
|
|
775
|
+
})));
|
|
774
776
|
}
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
777
|
+
return makeApiRequest();
|
|
778
|
+
}
|
|
779
|
+
/**
|
|
780
|
+
* Shortcut method for send POST request to NGW.
|
|
781
|
+
* @param name - NGW route name from {@link https://docs.nextgis.com/docs_ngweb_dev/doc/developer/resource.html#routes | routes}
|
|
782
|
+
* @param options - Request options
|
|
783
|
+
* @param params - Request item params or query params
|
|
784
|
+
*
|
|
785
|
+
* @example
|
|
786
|
+
* ```javascript
|
|
787
|
+
* connector.post('resource.collection', { data: POST_PAYLOAD })
|
|
788
|
+
* .then((newResource) => console.log(newResource))
|
|
789
|
+
* .catch((error) => console.warn(error));
|
|
790
|
+
* ```
|
|
791
|
+
*/
|
|
792
|
+
post(name, options, params) {
|
|
793
|
+
options = options || {};
|
|
794
|
+
options.method = "POST";
|
|
795
|
+
return this.apiRequest(
|
|
796
|
+
name,
|
|
797
|
+
params,
|
|
798
|
+
options
|
|
799
|
+
);
|
|
800
|
+
}
|
|
801
|
+
/**
|
|
802
|
+
* Shortcut method for send GET request to NGW.
|
|
803
|
+
* @param name - NGW route name from {@link https://docs.nextgis.com/docs_ngweb_dev/doc/developer/resource.html#routes | routes}
|
|
804
|
+
* @param options - Request options
|
|
805
|
+
* @param params - Request item params or query params
|
|
806
|
+
*/
|
|
807
|
+
get(name, options, params) {
|
|
808
|
+
options = options || {};
|
|
809
|
+
options.method = "GET";
|
|
810
|
+
return this.apiRequest(
|
|
811
|
+
name,
|
|
812
|
+
params,
|
|
813
|
+
options
|
|
814
|
+
);
|
|
815
|
+
}
|
|
816
|
+
/**
|
|
817
|
+
* Shortcut method for send PATCH request to NGW.
|
|
818
|
+
* @param name - NGW route name from {@link https://docs.nextgis.com/docs_ngweb_dev/doc/developer/resource.html#routes | routes}
|
|
819
|
+
* @param options - Request options
|
|
820
|
+
* @param params - Request item params or query params
|
|
821
|
+
*/
|
|
822
|
+
patch(name, options, params) {
|
|
823
|
+
options = options || {};
|
|
824
|
+
options.method = "PATCH";
|
|
825
|
+
return this.apiRequest(
|
|
826
|
+
name,
|
|
827
|
+
params,
|
|
828
|
+
options
|
|
829
|
+
);
|
|
830
|
+
}
|
|
831
|
+
/**
|
|
832
|
+
* Shortcut method for send PUT request to NGW.
|
|
833
|
+
* @param name - NGW route name from {@link https://docs.nextgis.com/docs_ngweb_dev/doc/developer/resource.html#routes | routes}
|
|
834
|
+
* @param options - Request options
|
|
835
|
+
* @param params - Request item params or query params
|
|
836
|
+
*/
|
|
837
|
+
put(name, options, params) {
|
|
838
|
+
options = options || {};
|
|
839
|
+
options.method = "PUT";
|
|
840
|
+
return this.apiRequest(
|
|
841
|
+
name,
|
|
842
|
+
params,
|
|
843
|
+
options
|
|
844
|
+
);
|
|
845
|
+
}
|
|
846
|
+
/**
|
|
847
|
+
* Shortcut method for send DELETE request to NGW.
|
|
848
|
+
* @param name - NGW route name from {@link https://docs.nextgis.com/docs_ngweb_dev/doc/developer/resource.html#routes | routes}
|
|
849
|
+
* @param options - Request options
|
|
850
|
+
* @param params - Request item params or query params
|
|
851
|
+
*/
|
|
852
|
+
delete(name, options, params) {
|
|
853
|
+
options = options || {};
|
|
854
|
+
options.method = "DELETE";
|
|
855
|
+
return this.apiRequest(
|
|
856
|
+
name,
|
|
857
|
+
params,
|
|
858
|
+
options
|
|
859
|
+
);
|
|
860
|
+
}
|
|
861
|
+
/**
|
|
862
|
+
* Send request to NGW.
|
|
863
|
+
* @param url - URL address to NGW
|
|
864
|
+
* @param params - Query params
|
|
865
|
+
* @param options - Request options
|
|
866
|
+
*/
|
|
867
|
+
makeQuery(url, params, options = {}) {
|
|
868
|
+
url = (this.options.baseUrl ? this.options.baseUrl : "") + url;
|
|
869
|
+
if (url) {
|
|
870
|
+
if (params) {
|
|
871
|
+
url = template(url, params);
|
|
872
|
+
}
|
|
873
|
+
url = encodeURI(fixUrlStr(url));
|
|
874
|
+
return this._loadData(url, options);
|
|
875
|
+
} else {
|
|
876
|
+
throw new Error("Empty `url` not allowed");
|
|
785
877
|
}
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
878
|
+
}
|
|
879
|
+
// -------------------------------------------------------------------------
|
|
880
|
+
// Resource Methods
|
|
881
|
+
// -------------------------------------------------------------------------
|
|
882
|
+
/**
|
|
883
|
+
* {@link ResourcesControl.getOne}
|
|
884
|
+
*/
|
|
885
|
+
getResource(resource, requestOptions) {
|
|
886
|
+
return this.resources.getOne(resource, requestOptions);
|
|
887
|
+
}
|
|
888
|
+
/**
|
|
889
|
+
* {@link ResourcesControl.getOneOrFail}
|
|
890
|
+
*/
|
|
891
|
+
getResourceOrFail(resource, requestOptions) {
|
|
892
|
+
return this.resources.getOneOrFail(resource, requestOptions);
|
|
893
|
+
}
|
|
894
|
+
/**
|
|
895
|
+
* @deprecated - use {@link getResource}
|
|
896
|
+
*/
|
|
897
|
+
getResourceBy(resource) {
|
|
898
|
+
return this.resources.getOne(resource);
|
|
899
|
+
}
|
|
900
|
+
/**
|
|
901
|
+
* @deprecated - use {@link getResource}
|
|
902
|
+
*/
|
|
903
|
+
getResourceByKeyname(keyname) {
|
|
904
|
+
return this.resources.getOne(keyname);
|
|
905
|
+
}
|
|
906
|
+
/**
|
|
907
|
+
* @deprecated - use {@link getResource}
|
|
908
|
+
*/
|
|
909
|
+
getResourceById(id) {
|
|
910
|
+
return this.resources.getOne(id);
|
|
911
|
+
}
|
|
912
|
+
/**
|
|
913
|
+
* {@link ResourcesControl.getId}
|
|
914
|
+
*/
|
|
915
|
+
getResourceId(resource, requestOptions) {
|
|
916
|
+
return this.resources.getId(resource, requestOptions);
|
|
917
|
+
}
|
|
918
|
+
/**
|
|
919
|
+
* {@link ResourcesControl.getIdOrFail}
|
|
920
|
+
*/
|
|
921
|
+
getResourceIdOrFail(resource, requestOptions) {
|
|
922
|
+
return this.resources.getIdOrFail(resource, requestOptions);
|
|
923
|
+
}
|
|
924
|
+
/**
|
|
925
|
+
* {@link ResourcesControl.getMany}
|
|
926
|
+
*/
|
|
927
|
+
getResourcesBy(resource, requestOptions) {
|
|
928
|
+
return this.resources.getMany(resource, requestOptions);
|
|
929
|
+
}
|
|
930
|
+
/**
|
|
931
|
+
* {@link ResourcesControl.getParent}
|
|
932
|
+
*/
|
|
933
|
+
getResourceParent(resource, requestOptions) {
|
|
934
|
+
return this.resources.getParent(resource, requestOptions);
|
|
935
|
+
}
|
|
936
|
+
/**
|
|
937
|
+
* {@link ResourcesControl.getChildrenOf}
|
|
938
|
+
*/
|
|
939
|
+
getResourceChildren(resource, requestOptions) {
|
|
940
|
+
return this.resources.getChildrenOf(resource, requestOptions);
|
|
941
|
+
}
|
|
942
|
+
/**
|
|
943
|
+
* {@link ResourcesControl.update}
|
|
944
|
+
*/
|
|
945
|
+
updateResource(resource, data) {
|
|
946
|
+
return this.resources.update(resource, data);
|
|
947
|
+
}
|
|
948
|
+
/**
|
|
949
|
+
* {@link ResourcesControl.delete}
|
|
950
|
+
*/
|
|
951
|
+
deleteResource(resource) {
|
|
952
|
+
return this.resources.delete(resource);
|
|
953
|
+
}
|
|
954
|
+
/**
|
|
955
|
+
* @internal
|
|
956
|
+
*/
|
|
957
|
+
_loadData(url, options) {
|
|
958
|
+
options.responseType = options.responseType || "json";
|
|
959
|
+
if (options.signal && options.signal.aborted) {
|
|
960
|
+
return CancelablePromise.reject(new CancelablePromise.CancelError());
|
|
796
961
|
}
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
* @param name - NGW route name from {@link https://docs.nextgis.com/docs_ngweb_dev/doc/developer/resource.html#routes | routes}
|
|
800
|
-
* @param options - Request options
|
|
801
|
-
* @param params - Request item params or query params
|
|
802
|
-
*/
|
|
803
|
-
delete(name, options, params) {
|
|
962
|
+
const request = new CancelablePromise((resolve, reject, onCancel) => {
|
|
963
|
+
if (this.user) {
|
|
804
964
|
options = options || {};
|
|
805
|
-
options.
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
return this._loadData(url, options);
|
|
822
|
-
}
|
|
823
|
-
else {
|
|
824
|
-
throw new Error('Empty `url` not allowed');
|
|
965
|
+
options.headers = __spreadValues(__spreadValues({}, this.getAuthorizationHeaders()), options.headers);
|
|
966
|
+
}
|
|
967
|
+
if (this.requestTransform) {
|
|
968
|
+
const [transUrl, transOptions] = this.requestTransform(url, options);
|
|
969
|
+
url = transUrl;
|
|
970
|
+
options = transOptions;
|
|
971
|
+
}
|
|
972
|
+
loadData(url, resolve, options, reject, onCancel);
|
|
973
|
+
}).then((resp) => {
|
|
974
|
+
this._cleanActiveRequests(request);
|
|
975
|
+
return resp;
|
|
976
|
+
}).catch((httpError) => {
|
|
977
|
+
this._cleanActiveRequests(request);
|
|
978
|
+
if (httpError instanceof CancelablePromise.CancelError) ; else {
|
|
979
|
+
if (!!(process.env.NODE_ENV !== "production")) {
|
|
980
|
+
console.warn("DEV WARN", httpError);
|
|
825
981
|
}
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
// -------------------------------------------------------------------------
|
|
830
|
-
/**
|
|
831
|
-
* {@link ResourcesControl.getOne}
|
|
832
|
-
*/
|
|
833
|
-
getResource(resource, requestOptions) {
|
|
834
|
-
return this.resources.getOne(resource, requestOptions);
|
|
835
|
-
}
|
|
836
|
-
/**
|
|
837
|
-
* {@link ResourcesControl.getOneOrFail}
|
|
838
|
-
*/
|
|
839
|
-
getResourceOrFail(resource, requestOptions) {
|
|
840
|
-
return this.resources.getOneOrFail(resource, requestOptions);
|
|
841
|
-
}
|
|
842
|
-
/**
|
|
843
|
-
* @deprecated - use {@link getResource}
|
|
844
|
-
*/
|
|
845
|
-
getResourceBy(resource) {
|
|
846
|
-
return this.resources.getOne(resource);
|
|
847
|
-
}
|
|
848
|
-
/**
|
|
849
|
-
* @deprecated - use {@link getResource}
|
|
850
|
-
*/
|
|
851
|
-
getResourceByKeyname(keyname) {
|
|
852
|
-
return this.resources.getOne(keyname);
|
|
853
|
-
}
|
|
854
|
-
/**
|
|
855
|
-
* @deprecated - use {@link getResource}
|
|
856
|
-
*/
|
|
857
|
-
getResourceById(id) {
|
|
858
|
-
return this.resources.getOne(id);
|
|
859
|
-
}
|
|
860
|
-
/**
|
|
861
|
-
* {@link ResourcesControl.getId}
|
|
862
|
-
*/
|
|
863
|
-
getResourceId(resource, requestOptions) {
|
|
864
|
-
return this.resources.getId(resource, requestOptions);
|
|
865
|
-
}
|
|
866
|
-
/**
|
|
867
|
-
* {@link ResourcesControl.getIdOrFail}
|
|
868
|
-
*/
|
|
869
|
-
getResourceIdOrFail(resource, requestOptions) {
|
|
870
|
-
return this.resources.getIdOrFail(resource, requestOptions);
|
|
871
|
-
}
|
|
872
|
-
/**
|
|
873
|
-
* {@link ResourcesControl.getMany}
|
|
874
|
-
*/
|
|
875
|
-
getResourcesBy(resource, requestOptions) {
|
|
876
|
-
return this.resources.getMany(resource, requestOptions);
|
|
877
|
-
}
|
|
878
|
-
/**
|
|
879
|
-
* {@link ResourcesControl.getParent}
|
|
880
|
-
*/
|
|
881
|
-
getResourceParent(resource, requestOptions) {
|
|
882
|
-
return this.resources.getParent(resource, requestOptions);
|
|
883
|
-
}
|
|
884
|
-
/**
|
|
885
|
-
* {@link ResourcesControl.getChildrenOf}
|
|
886
|
-
*/
|
|
887
|
-
getResourceChildren(resource, requestOptions) {
|
|
888
|
-
return this.resources.getChildrenOf(resource, requestOptions);
|
|
889
|
-
}
|
|
890
|
-
/**
|
|
891
|
-
* {@link ResourcesControl.update}
|
|
892
|
-
*/
|
|
893
|
-
updateResource(resource, data) {
|
|
894
|
-
return this.resources.update(resource, data);
|
|
895
|
-
}
|
|
896
|
-
/**
|
|
897
|
-
* {@link ResourcesControl.delete}
|
|
898
|
-
*/
|
|
899
|
-
deleteResource(resource) {
|
|
900
|
-
return this.resources.delete(resource);
|
|
901
|
-
}
|
|
902
|
-
/**
|
|
903
|
-
* @internal
|
|
904
|
-
*/
|
|
905
|
-
_loadData(url, options) {
|
|
906
|
-
options.responseType = options.responseType || 'json';
|
|
907
|
-
const request = new CancelablePromise((resolve, reject, onCancel) => {
|
|
908
|
-
if (this.user) {
|
|
909
|
-
options = options || {};
|
|
910
|
-
// options.withCredentials = true;
|
|
911
|
-
options.headers = {
|
|
912
|
-
...this.getAuthorizationHeaders(),
|
|
913
|
-
...options.headers,
|
|
914
|
-
};
|
|
915
|
-
}
|
|
916
|
-
if (this.requestTransform) {
|
|
917
|
-
const [transUrl, transOptions] = this.requestTransform(url, options);
|
|
918
|
-
url = transUrl;
|
|
919
|
-
options = transOptions;
|
|
920
|
-
}
|
|
921
|
-
loadData(url, resolve, options, reject, onCancel);
|
|
922
|
-
})
|
|
923
|
-
.then((resp) => {
|
|
924
|
-
this._cleanActiveRequests(request);
|
|
925
|
-
return resp;
|
|
926
|
-
})
|
|
927
|
-
.catch((httpError) => {
|
|
928
|
-
this._cleanActiveRequests(request);
|
|
929
|
-
if (httpError instanceof CancelablePromise.CancelError) ;
|
|
930
|
-
else {
|
|
931
|
-
// @ts-ignore
|
|
932
|
-
if ((process.env.NODE_ENV !== 'production')) {
|
|
933
|
-
console.warn('DEV WARN', httpError);
|
|
934
|
-
}
|
|
935
|
-
const er = this._handleHttpError(httpError);
|
|
936
|
-
if (er) {
|
|
937
|
-
throw er;
|
|
938
|
-
}
|
|
939
|
-
}
|
|
940
|
-
});
|
|
941
|
-
if (options.signal &&
|
|
942
|
-
typeof options.signal.addEventListener === 'function') {
|
|
943
|
-
options.signal.addEventListener('abort', () => {
|
|
944
|
-
request.cancel();
|
|
945
|
-
this._cleanActiveRequests(request);
|
|
946
|
-
});
|
|
982
|
+
const er = this._handleHttpError(httpError);
|
|
983
|
+
if (er) {
|
|
984
|
+
throw er;
|
|
947
985
|
}
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
this.emitter.emit('login', data);
|
|
956
|
-
return data;
|
|
957
|
-
})
|
|
958
|
-
.catch((er) => {
|
|
959
|
-
this.emitter.emit('login:error', er);
|
|
960
|
-
throw er;
|
|
961
|
-
});
|
|
986
|
+
}
|
|
987
|
+
});
|
|
988
|
+
if (options.signal && typeof options.signal.addEventListener === "function") {
|
|
989
|
+
options.signal.addEventListener("abort", () => {
|
|
990
|
+
request.cancel();
|
|
991
|
+
this._cleanActiveRequests(request);
|
|
992
|
+
});
|
|
962
993
|
}
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
994
|
+
this.activeRequests.push(request);
|
|
995
|
+
return request;
|
|
996
|
+
}
|
|
997
|
+
_login(credentials, options) {
|
|
998
|
+
return this.getUserInfo(credentials, options).then((data) => {
|
|
999
|
+
this.user = data;
|
|
1000
|
+
this.emitter.emit("login", data);
|
|
1001
|
+
return data;
|
|
1002
|
+
}).catch((er) => {
|
|
1003
|
+
this.emitter.emit("login:error", er);
|
|
1004
|
+
throw er;
|
|
1005
|
+
});
|
|
1006
|
+
}
|
|
1007
|
+
_cleanActiveRequests(request) {
|
|
1008
|
+
const activeRequestIndex = this.activeRequests.indexOf(request);
|
|
1009
|
+
if (activeRequestIndex !== -1) {
|
|
1010
|
+
this.activeRequests.splice(activeRequestIndex, 1);
|
|
968
1011
|
}
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
}
|
|
978
|
-
}
|
|
1012
|
+
}
|
|
1013
|
+
_handleHttpError(er) {
|
|
1014
|
+
if (er) {
|
|
1015
|
+
if (er instanceof NgwError) {
|
|
1016
|
+
if (er.exception === "nextgisweb.resource.exception.ResourceNotFound") {
|
|
1017
|
+
throw new ResourceNotFoundError(er);
|
|
1018
|
+
} else if (er.exception === "nextgisweb.core.exception.InsufficientPermissions") {
|
|
1019
|
+
throw new InsufficientPermissionsError(er);
|
|
979
1020
|
}
|
|
980
|
-
|
|
1021
|
+
}
|
|
981
1022
|
}
|
|
1023
|
+
return er;
|
|
1024
|
+
}
|
|
982
1025
|
}
|
|
983
|
-
NgwConnector
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
};
|
|
987
|
-
|
|
988
|
-
/**
|
|
989
|
-
* A lightweight HTTP client optimized for use with {@link http://docs.nextgis.ru/docs_ngweb_dev/doc/developer/toc.html | NextGIS Web} API
|
|
990
|
-
*
|
|
991
|
-
* @remarks
|
|
992
|
-
* Make sure CORS is registered in the {@link https://docs.nextgis.com/docs_ngcom/source/CORS.html | NextGIS Web settings } to be able to send requests.
|
|
993
|
-
*
|
|
994
|
-
* @packageDocumentation
|
|
995
|
-
* @module ngw-connector
|
|
996
|
-
*/
|
|
997
|
-
var NgwConnector = NgwConnector$1;
|
|
1026
|
+
__publicField(NgwConnector, "errors", {
|
|
1027
|
+
NgwError,
|
|
1028
|
+
ResourceNotFoundError
|
|
1029
|
+
});
|
|
998
1030
|
|
|
999
1031
|
export { NgwConnector as default };
|
|
1000
1032
|
//# sourceMappingURL=ngw-connector.esm-bundler.js.map
|