@nextgis/ngw-connector 1.18.21 → 1.19.6

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,64 @@
1
- /** Bundle of @nextgis/ngw-connector; version: 1.18.21; author: NextGIS */
1
+ /** Bundle of @nextgis/ngw-connector; version: 1.19.6; 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
- * 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
- }
7
+ var pkg = {
8
+ name: "@nextgis/ngw-connector",
9
+ version: "1.19.6",
10
+ _priority: 12,
11
+ description: "A lightweight HTTP client optimized for use with NextGIS Web API",
12
+ main: "index.js",
13
+ module: "lib/ngw-connector.esm-bundler.js",
14
+ unpkg: "lib/ngw-connector.global.prod.js",
15
+ jsdelivr: "lib/ngw-connector.global.prod.js",
16
+ types: "lib/index.d.ts",
17
+ dependencies: {
18
+ "@nextgis/cache": "^1.19.0",
19
+ "@nextgis/cancelable-promise": "^1.19.0",
20
+ "@nextgis/utils": "^1.19.0",
21
+ "@types/events": "^3.0.0",
22
+ events: "^3.3.0",
23
+ "form-data": "^4.0.0"
24
+ },
25
+ devDependencies: {
26
+ "@nextgis/build-tools": "^1.19.0"
27
+ },
28
+ scripts: {
29
+ clean: "rimraf ./lib",
30
+ dev: "node ../build-tools/lib/build.js",
31
+ prod: "npm run dev -- --release",
32
+ lint: "eslint ./src/**/*.ts --fix --c ../../.eslintrc",
33
+ watch: "npm run dev -- --watch",
34
+ "gen:types": "node ./scripts/generator"
35
+ },
36
+ buildOptions: {
37
+ name: "NgwConnector",
38
+ formats: [
39
+ "esm-bundler",
40
+ "esm-browser",
41
+ "cjs",
42
+ "global"
43
+ ]
44
+ },
45
+ keywords: [
46
+ "NextGIS",
47
+ "MAP"
48
+ ],
49
+ author: "NextGIS",
50
+ files: [
51
+ "index.js",
52
+ "lib"
53
+ ],
54
+ license: "MIT",
55
+ homepage: "https://github.com/nextgis/nextgis_frontend/tree/master/packages/ngw-connector#readme",
56
+ repository: {
57
+ type: "git",
58
+ url: "git://github.com/nextgis/nextgis_frontend.git"
59
+ },
60
+ gitHead: "edc68033f914f6c95c4125b9738bba6d36e990b4"
61
+ };
19
62
 
20
63
  /**
21
64
  * Thrown when...
@@ -29,193 +72,6 @@ class NgwError extends Error {
29
72
  }
30
73
  }
31
74
 
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
75
  /**
220
76
  * Thrown when ...
221
77
  */
@@ -228,42 +84,12 @@ class ResourceNotFoundError extends NgwError {
228
84
  }
229
85
  }
230
86
 
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
- }
87
+ function isObject(val) {
88
+ return Object.prototype.toString.call(val) === '[object Object]';
241
89
  }
242
90
 
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
- }
91
+ function resourceCompare(res1, res2) {
92
+ return objectDeepEqual(res1, res2);
267
93
  }
268
94
 
269
95
  const exclude = ['description'];
@@ -299,10 +125,6 @@ function resourceToQuery(resource, prefix = '') {
299
125
  return query;
300
126
  }
301
127
 
302
- function resourceCompare(res1, res2) {
303
- return objectDeepEqual(res1, res2);
304
- }
305
-
306
128
  class ResourcesControl {
307
129
  constructor(connector) {
308
130
  this.connector = connector;
@@ -543,12 +365,247 @@ class ResourcesControl {
543
365
  }
544
366
  }
545
367
 
368
+ const CONNECTORS = [];
369
+ function addConnector(connector) {
370
+ CONNECTORS.push(connector);
371
+ }
372
+ function findConnector(options) {
373
+ return CONNECTORS.find((x) => {
374
+ if (x.options.baseUrl === options.baseUrl) {
375
+ if (options.auth) {
376
+ if (x.options.auth) {
377
+ return objectDeepEqual(x.options.auth, options.auth);
378
+ }
379
+ }
380
+ else {
381
+ // if auth not in find options; use first connector with same url
382
+ return true;
383
+ }
384
+ }
385
+ });
386
+ }
387
+ function removeConnector(connector) {
388
+ const index = CONNECTORS.indexOf(connector);
389
+ if (index !== -1) {
390
+ CONNECTORS.splice(index, 1);
391
+ }
392
+ }
393
+
394
+ /**
395
+ * Thrown when ...
396
+ */
397
+ class InsufficientPermissionsError extends NgwError {
398
+ constructor(obj) {
399
+ super(obj);
400
+ this.name = 'InsufficientPermissionsError';
401
+ this.exception = 'nextgisweb.core.exception.InsufficientPermissions';
402
+ Object.setPrototypeOf(this, InsufficientPermissionsError.prototype);
403
+ }
404
+ }
405
+
406
+ // https://github.com/Leaflet/Leaflet/blob/b507e21c510b53cd704fb8d3f89bb46ea925c8eb/src/core/Util.js#L165
407
+ const templateRe = /\{ *([\w_-]+) *\}/g;
408
+ function template(str, data) {
409
+ return str.replace(templateRe, (s, key) => {
410
+ let value = data[key];
411
+ if (value === undefined) {
412
+ throw new Error('No value provided for letiable ' + s);
413
+ }
414
+ else if (typeof value === 'function') {
415
+ value = value(data);
416
+ }
417
+ return value;
418
+ });
419
+ }
420
+
421
+ function apiRequest(opt) {
422
+ const params = opt.params;
423
+ return new CancelablePromise((resolve, reject) => {
424
+ opt.connector
425
+ .connect()
426
+ .then((apiItems) => {
427
+ let apiItem = apiItems && apiItems[opt.name];
428
+ if (apiItem) {
429
+ apiItem = [...apiItem];
430
+ let url = apiItem.shift();
431
+ if (apiItem.length) {
432
+ const replaceParams = {};
433
+ for (let fry = 0; fry < apiItem.length; fry++) {
434
+ const arg = apiItem[fry];
435
+ replaceParams[fry] = '{' + arg + '}';
436
+ if (params[arg] === undefined) {
437
+ throw new Error('`' + arg + '`' + ' url api argument is not specified');
438
+ }
439
+ }
440
+ if (url) {
441
+ url = template(url, replaceParams);
442
+ }
443
+ }
444
+ // Transfer part of the parameters from `params` to the URL string
445
+ if (params) {
446
+ const paramArray = [];
447
+ const paramList = params.paramList;
448
+ if (Array.isArray(paramList)) {
449
+ delete params.paramList;
450
+ paramList.forEach((x) => {
451
+ paramArray.push(`${x[0]}=${x[1]}`);
452
+ });
453
+ }
454
+ for (const p in params) {
455
+ if (apiItem.indexOf(p) === -1) {
456
+ paramArray.push(`${p}=${params[p]}`);
457
+ }
458
+ }
459
+ if (paramArray.length) {
460
+ url = url + '?' + paramArray.join('&');
461
+ }
462
+ }
463
+ if (url) {
464
+ resolve(opt.connector.makeQuery(url, params, opt.requestOptions));
465
+ }
466
+ else {
467
+ reject(new Error('request url is not set'));
468
+ }
469
+ }
470
+ else {
471
+ resolve(undefined);
472
+ }
473
+ })
474
+ .catch((er) => {
475
+ reject(er);
476
+ });
477
+ });
478
+ }
479
+
480
+ /**
481
+ * Thrown when...
482
+ */
483
+ class NetworkError extends Error {
484
+ constructor(url) {
485
+ super();
486
+ this.name = 'NetworkError';
487
+ Object.setPrototypeOf(this, NetworkError.prototype);
488
+ this.message = `Unable to request ${url}.
489
+ Possibly invalid NGW URL entered or CORS not configured to get request from ${location.origin}`; // /control-panel/cors
490
+ }
491
+ }
492
+
493
+ let loadData;
494
+ // @ts-ignore
495
+ {
496
+ loadData = (url, callback, options = {}, error, onCancel) => {
497
+ options.method = options.method || 'GET';
498
+ const xhr = new XMLHttpRequest();
499
+ xhr.open(options.method || 'GET', url, true); // true for asynchronous
500
+ if (options.responseType === 'blob') {
501
+ xhr.responseType = options.responseType;
502
+ }
503
+ const getResponseText = () => {
504
+ try {
505
+ return JSON.parse(xhr.responseText);
506
+ }
507
+ catch (er) {
508
+ return xhr.responseText;
509
+ }
510
+ };
511
+ const processingResponse = (forError = false) => {
512
+ const cb = forError ? error : callback;
513
+ if (options.responseType === 'blob') {
514
+ cb(xhr.response);
515
+ }
516
+ else {
517
+ if (xhr.responseText) {
518
+ cb(getResponseText());
519
+ }
520
+ else {
521
+ error({ message: '' });
522
+ }
523
+ }
524
+ };
525
+ xhr.onload = () => {
526
+ if ([401, 403, 404, 422, 500].indexOf(xhr.status) !== -1) {
527
+ error(new NgwError(getResponseText()));
528
+ }
529
+ processingResponse();
530
+ };
531
+ // xhr.onreadystatechange = () => {
532
+ // if (
533
+ // (xhr.readyState === 4 && xhr.status === 200) ||
534
+ // (xhr.readyState === 3 && xhr.status === 201)
535
+ // ) {
536
+ // processingResponse();
537
+ // } else if (xhr.readyState === 3 && xhr.status === 400) {
538
+ // processingResponse();
539
+ // } else if (xhr.readyState === 4 && xhr.status === 500) {
540
+ // processingResponse();
541
+ // } else if (xhr.readyState === 4 && xhr.status === 401) {
542
+ // error(xhr.statusText);
543
+ // } else if (xhr.readyState === 4) {
544
+ // error('request error');
545
+ // }
546
+ // };
547
+ xhr.onerror = (er) => {
548
+ if (xhr.status === 0) {
549
+ error(new NetworkError(url));
550
+ }
551
+ else {
552
+ error(er);
553
+ }
554
+ };
555
+ xhr.upload.onprogress = function (e) {
556
+ if (e.lengthComputable) {
557
+ const percentComplete = (e.loaded / e.total) * 100;
558
+ if (options.onProgress) {
559
+ options.onProgress(percentComplete, e);
560
+ }
561
+ // console.log(percentComplete + '% uploaded');
562
+ }
563
+ };
564
+ const headers = options.headers;
565
+ if (headers) {
566
+ for (const h in headers) {
567
+ const header = headers[h];
568
+ if (typeof header === 'string') {
569
+ xhr.setRequestHeader(h, header);
570
+ }
571
+ }
572
+ }
573
+ if (options.withCredentials !== undefined) {
574
+ xhr.withCredentials = options.withCredentials;
575
+ }
576
+ let data;
577
+ if (options.file) {
578
+ data = new FormData();
579
+ data.append('file', options.file);
580
+ if (options.data) {
581
+ for (const d in options.data) {
582
+ data.append(d, data[d]);
583
+ }
584
+ }
585
+ }
586
+ else {
587
+ data = options.data
588
+ ? typeof options.data === 'string'
589
+ ? options.data
590
+ : JSON.stringify(options.data)
591
+ : null;
592
+ }
593
+ if (onCancel) {
594
+ onCancel(() => {
595
+ xhr.abort();
596
+ });
597
+ }
598
+ xhr.send(data);
599
+ };
600
+ }
601
+
546
602
  let ID = 0;
547
603
  class NgwConnector$1 {
548
604
  constructor(options) {
549
605
  this.options = options;
550
606
  this.id = ID++;
551
607
  this.emitter = new EventEmitter();
608
+ this.client = `NextGIS-NGW-Connector/${pkg.version}`;
552
609
  this.routeStr = '/api/component/pyramid/route';
553
610
  this.activeRequests = [];
554
611
  const exist = findConnector(options);
@@ -600,7 +657,8 @@ class NgwConnector$1 {
600
657
  const auth = this.options.auth;
601
658
  const makeConnect = () => new CancelablePromise((resolve, reject) => {
602
659
  const makeQuery = () => {
603
- return this.makeQuery(this.routeStr, {}, {})
660
+ const routeUrl = `${this.routeStr}?client=${this.client}`;
661
+ return this.makeQuery(routeUrl.toString(), {}, {})
604
662
  .then((route) => {
605
663
  resolve(route);
606
664
  })