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