@nextgis/ngw-connector 1.18.12 → 1.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,21 +1,8 @@
1
- /** Bundle of @nextgis/ngw-connector; version: 1.18.12; author: NextGIS */
1
+ /** Bundle of @nextgis/ngw-connector; version: 1.19.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
-
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
- }
18
- }
19
6
 
20
7
  /**
21
8
  * Thrown when...
@@ -29,193 +16,6 @@ class NgwError extends Error {
29
16
  }
30
17
  }
31
18
 
32
- function isObject(val) {
33
- return Object.prototype.toString.call(val) === '[object Object]';
34
- }
35
-
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
- };
143
- }
144
-
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
- });
158
- }
159
-
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
- });
217
- }
218
-
219
19
  /**
220
20
  * Thrown when ...
221
21
  */
@@ -228,42 +28,12 @@ class ResourceNotFoundError extends NgwError {
228
28
  }
229
29
  }
230
30
 
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
- }
31
+ function isObject(val) {
32
+ return Object.prototype.toString.call(val) === '[object Object]';
241
33
  }
242
34
 
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
- }
35
+ function resourceCompare(res1, res2) {
36
+ return objectDeepEqual(res1, res2);
267
37
  }
268
38
 
269
39
  const exclude = ['description'];
@@ -299,10 +69,6 @@ function resourceToQuery(resource, prefix = '') {
299
69
  return query;
300
70
  }
301
71
 
302
- function resourceCompare(res1, res2) {
303
- return objectDeepEqual(res1, res2);
304
- }
305
-
306
72
  class ResourcesControl {
307
73
  constructor(connector) {
308
74
  this.connector = connector;
@@ -543,6 +309,240 @@ class ResourcesControl {
543
309
  }
544
310
  }
545
311
 
312
+ const CONNECTORS = [];
313
+ function addConnector(connector) {
314
+ CONNECTORS.push(connector);
315
+ }
316
+ function findConnector(options) {
317
+ return CONNECTORS.find((x) => {
318
+ if (x.options.baseUrl === options.baseUrl) {
319
+ if (options.auth) {
320
+ if (x.options.auth) {
321
+ return objectDeepEqual(x.options.auth, options.auth);
322
+ }
323
+ }
324
+ else {
325
+ // if auth not in find options; use first connector with same url
326
+ return true;
327
+ }
328
+ }
329
+ });
330
+ }
331
+ function removeConnector(connector) {
332
+ const index = CONNECTORS.indexOf(connector);
333
+ if (index !== -1) {
334
+ CONNECTORS.splice(index, 1);
335
+ }
336
+ }
337
+
338
+ /**
339
+ * Thrown when ...
340
+ */
341
+ class InsufficientPermissionsError extends NgwError {
342
+ constructor(obj) {
343
+ super(obj);
344
+ this.name = 'InsufficientPermissionsError';
345
+ this.exception = 'nextgisweb.core.exception.InsufficientPermissions';
346
+ Object.setPrototypeOf(this, InsufficientPermissionsError.prototype);
347
+ }
348
+ }
349
+
350
+ // https://github.com/Leaflet/Leaflet/blob/b507e21c510b53cd704fb8d3f89bb46ea925c8eb/src/core/Util.js#L165
351
+ const templateRe = /\{ *([\w_-]+) *\}/g;
352
+ function template(str, data) {
353
+ return str.replace(templateRe, (s, key) => {
354
+ let value = data[key];
355
+ if (value === undefined) {
356
+ throw new Error('No value provided for letiable ' + s);
357
+ }
358
+ else if (typeof value === 'function') {
359
+ value = value(data);
360
+ }
361
+ return value;
362
+ });
363
+ }
364
+
365
+ function apiRequest(opt) {
366
+ const params = opt.params;
367
+ return new CancelablePromise((resolve, reject) => {
368
+ opt.connector
369
+ .connect()
370
+ .then((apiItems) => {
371
+ let apiItem = apiItems && apiItems[opt.name];
372
+ if (apiItem) {
373
+ apiItem = [...apiItem];
374
+ let url = apiItem.shift();
375
+ if (apiItem.length) {
376
+ const replaceParams = {};
377
+ for (let fry = 0; fry < apiItem.length; fry++) {
378
+ const arg = apiItem[fry];
379
+ replaceParams[fry] = '{' + arg + '}';
380
+ if (params[arg] === undefined) {
381
+ throw new Error('`' + arg + '`' + ' url api argument is not specified');
382
+ }
383
+ }
384
+ if (url) {
385
+ url = template(url, replaceParams);
386
+ }
387
+ }
388
+ // Transfer part of the parameters from `params` to the URL string
389
+ if (params) {
390
+ const paramArray = [];
391
+ const paramList = params.paramList;
392
+ if (Array.isArray(paramList)) {
393
+ delete params.paramList;
394
+ paramList.forEach((x) => {
395
+ paramArray.push(`${x[0]}=${x[1]}`);
396
+ });
397
+ }
398
+ for (const p in params) {
399
+ if (apiItem.indexOf(p) === -1) {
400
+ paramArray.push(`${p}=${params[p]}`);
401
+ }
402
+ }
403
+ if (paramArray.length) {
404
+ url = url + '?' + paramArray.join('&');
405
+ }
406
+ }
407
+ if (url) {
408
+ resolve(opt.connector.makeQuery(url, params, opt.requestOptions));
409
+ }
410
+ else {
411
+ reject(new Error('request url is not set'));
412
+ }
413
+ }
414
+ else {
415
+ resolve(undefined);
416
+ }
417
+ })
418
+ .catch((er) => {
419
+ reject(er);
420
+ });
421
+ });
422
+ }
423
+
424
+ /**
425
+ * Thrown when...
426
+ */
427
+ class NetworkError extends Error {
428
+ constructor(url) {
429
+ super();
430
+ this.name = 'NetworkError';
431
+ Object.setPrototypeOf(this, NetworkError.prototype);
432
+ this.message = `Unable to request ${url}.
433
+ Possibly invalid NGW URL entered or CORS not configured to get request from ${location.origin}`; // /control-panel/cors
434
+ }
435
+ }
436
+
437
+ let loadData;
438
+ // @ts-ignore
439
+ {
440
+ loadData = (url, callback, options = {}, error, onCancel) => {
441
+ options.method = options.method || 'GET';
442
+ const xhr = new XMLHttpRequest();
443
+ xhr.open(options.method || 'GET', url, true); // true for asynchronous
444
+ if (options.responseType === 'blob') {
445
+ xhr.responseType = options.responseType;
446
+ }
447
+ const getResponseText = () => {
448
+ try {
449
+ return JSON.parse(xhr.responseText);
450
+ }
451
+ catch (er) {
452
+ return xhr.responseText;
453
+ }
454
+ };
455
+ const processingResponse = (forError = false) => {
456
+ const cb = forError ? error : callback;
457
+ if (options.responseType === 'blob') {
458
+ cb(xhr.response);
459
+ }
460
+ else {
461
+ if (xhr.responseText) {
462
+ cb(getResponseText());
463
+ }
464
+ else {
465
+ error({ message: '' });
466
+ }
467
+ }
468
+ };
469
+ xhr.onload = () => {
470
+ if ([401, 403, 404, 422, 500].indexOf(xhr.status) !== -1) {
471
+ error(new NgwError(getResponseText()));
472
+ }
473
+ processingResponse();
474
+ };
475
+ // xhr.onreadystatechange = () => {
476
+ // if (
477
+ // (xhr.readyState === 4 && xhr.status === 200) ||
478
+ // (xhr.readyState === 3 && xhr.status === 201)
479
+ // ) {
480
+ // processingResponse();
481
+ // } else if (xhr.readyState === 3 && xhr.status === 400) {
482
+ // processingResponse();
483
+ // } else if (xhr.readyState === 4 && xhr.status === 500) {
484
+ // processingResponse();
485
+ // } else if (xhr.readyState === 4 && xhr.status === 401) {
486
+ // error(xhr.statusText);
487
+ // } else if (xhr.readyState === 4) {
488
+ // error('request error');
489
+ // }
490
+ // };
491
+ xhr.onerror = (er) => {
492
+ if (xhr.status === 0) {
493
+ error(new NetworkError(url));
494
+ }
495
+ else {
496
+ error(er);
497
+ }
498
+ };
499
+ xhr.upload.onprogress = function (e) {
500
+ if (e.lengthComputable) {
501
+ const percentComplete = (e.loaded / e.total) * 100;
502
+ if (options.onProgress) {
503
+ options.onProgress(percentComplete, e);
504
+ }
505
+ // console.log(percentComplete + '% uploaded');
506
+ }
507
+ };
508
+ const headers = options.headers;
509
+ if (headers) {
510
+ for (const h in headers) {
511
+ const header = headers[h];
512
+ if (typeof header === 'string') {
513
+ xhr.setRequestHeader(h, header);
514
+ }
515
+ }
516
+ }
517
+ if (options.withCredentials !== undefined) {
518
+ xhr.withCredentials = options.withCredentials;
519
+ }
520
+ let data;
521
+ if (options.file) {
522
+ data = new FormData();
523
+ data.append('file', options.file);
524
+ if (options.data) {
525
+ for (const d in options.data) {
526
+ data.append(d, data[d]);
527
+ }
528
+ }
529
+ }
530
+ else {
531
+ data = options.data
532
+ ? typeof options.data === 'string'
533
+ ? options.data
534
+ : JSON.stringify(options.data)
535
+ : null;
536
+ }
537
+ if (onCancel) {
538
+ onCancel(() => {
539
+ xhr.abort();
540
+ });
541
+ }
542
+ xhr.send(data);
543
+ };
544
+ }
545
+
546
546
  let ID = 0;
547
547
  class NgwConnector$1 {
548
548
  constructor(options) {