@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.
- package/lib/index.d.ts +13 -1
- package/lib/ngw-connector.cjs.js +254 -254
- 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 +753 -753
- 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 +240 -240
- 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 +796 -796
- 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 +6 -6
package/lib/index.d.ts
CHANGED
|
@@ -149,6 +149,12 @@ export declare interface GetRequestItemsResponseMap extends RequestItemKeys {
|
|
|
149
149
|
'feature_layer.feature.count': FeatureLayerCount;
|
|
150
150
|
'pyramid.company_logo': string;
|
|
151
151
|
'spatial_ref_sys.collection': SrsItem[];
|
|
152
|
+
'render.legend_symbols': RenderLegendSymbol[];
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
declare interface Icon {
|
|
156
|
+
format: string;
|
|
157
|
+
data: string;
|
|
152
158
|
}
|
|
153
159
|
|
|
154
160
|
export declare interface IdOnly {
|
|
@@ -471,6 +477,11 @@ export declare interface PyramidSettingsPyramidCompanyLogo {
|
|
|
471
477
|
enabled: boolean;
|
|
472
478
|
}
|
|
473
479
|
|
|
480
|
+
export declare interface RenderLegendSymbol {
|
|
481
|
+
display_name: string;
|
|
482
|
+
icon: Icon;
|
|
483
|
+
}
|
|
484
|
+
|
|
474
485
|
export declare interface RequestHeaders {
|
|
475
486
|
[header: string]: string | undefined;
|
|
476
487
|
Authorization?: string;
|
|
@@ -934,7 +945,7 @@ export declare interface Resource {
|
|
|
934
945
|
scopes: string[];
|
|
935
946
|
}
|
|
936
947
|
|
|
937
|
-
export declare type ResourceCls = 'resource_group' | 'postgis_layer' | 'wmsserver_service' | 'basemap_layer' | 'postgis_connection' | 'webmap' | 'wfsserver_service' | 'vector_layer' | 'raster_layer' | 'mapserver_style' | 'qgis_vector_style' | 'qgis_raster_style' | 'raster_style' | 'file_bucket' | 'lookup_table' | 'wmsclient_layer' | 'wmsclient_connection' | 'formbuilder_form' | 'file_bucket' | 'terrain_provider' | 'model_3d' | 'tmsclient_layer';
|
|
948
|
+
export declare type ResourceCls = 'resource_group' | 'postgis_layer' | 'wmsserver_service' | 'basemap_layer' | 'postgis_connection' | 'webmap' | 'wfsserver_service' | 'vector_layer' | 'raster_layer' | 'mapserver_style' | 'qgis_vector_style' | 'qgis_raster_style' | 'raster_style' | 'file_bucket' | 'lookup_table' | 'wmsclient_layer' | 'wmsclient_connection' | 'formbuilder_form' | 'file_bucket' | 'svg_marker_library' | 'ogcfserver_service' | 'tmsclient_connection' | 'tileset' | 'collector_project' | 'demo_project' | 'trackers_group' | 'tracker' | 'terrain_provider' | 'model_3d' | 'tmsclient_layer';
|
|
938
949
|
|
|
939
950
|
export declare type ResourceDefinition = ResourceIdKeynameDef | DeepPartial<Resource>;
|
|
940
951
|
|
|
@@ -1079,6 +1090,7 @@ export declare interface TreeLayer extends TreeItem {
|
|
|
1079
1090
|
layer_style_id: number;
|
|
1080
1091
|
style_parent_id: number;
|
|
1081
1092
|
layer_transparency: number;
|
|
1093
|
+
layer_identifiable: boolean;
|
|
1082
1094
|
}
|
|
1083
1095
|
|
|
1084
1096
|
export declare interface UserInfo {
|
package/lib/ngw-connector.cjs.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
/** Bundle of @nextgis/ngw-connector; version: 1.
|
|
1
|
+
/** Bundle of @nextgis/ngw-connector; version: 1.19.0; author: NextGIS */
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
4
|
var events = require('events');
|
|
5
|
+
var Cache = require('@nextgis/cache');
|
|
5
6
|
var CancelablePromise = require('@nextgis/cancelable-promise');
|
|
6
7
|
var utils = require('@nextgis/utils');
|
|
7
|
-
var Cache = require('@nextgis/cache');
|
|
8
8
|
|
|
9
9
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
|
|
10
10
|
|
|
11
|
-
var CancelablePromise__default = /*#__PURE__*/_interopDefaultLegacy(CancelablePromise);
|
|
12
11
|
var Cache__default = /*#__PURE__*/_interopDefaultLegacy(Cache);
|
|
12
|
+
var CancelablePromise__default = /*#__PURE__*/_interopDefaultLegacy(CancelablePromise);
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* Thrown when...
|
|
@@ -23,219 +23,6 @@ class NgwError extends Error {
|
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
function isError(error) {
|
|
27
|
-
if (utils.isObject(error)) {
|
|
28
|
-
return error.status_code && error.exception && error.title;
|
|
29
|
-
}
|
|
30
|
-
return false;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function isObject(val) {
|
|
34
|
-
return Object.prototype.toString.call(val) === '[object Object]';
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
let loadData;
|
|
38
|
-
// @ts-ignore
|
|
39
|
-
{
|
|
40
|
-
const url = require('url');
|
|
41
|
-
const http = require('http');
|
|
42
|
-
const https = require('https');
|
|
43
|
-
const FormData = require('form-data');
|
|
44
|
-
const adapterFor = (inputUrl) => {
|
|
45
|
-
const adapters = {
|
|
46
|
-
'http:': http,
|
|
47
|
-
'https:': https,
|
|
48
|
-
};
|
|
49
|
-
const protocol = url.parse(inputUrl).protocol || 'https:';
|
|
50
|
-
return adapters[protocol];
|
|
51
|
-
};
|
|
52
|
-
loadData = (url, callback, options = {}, error, onCancel) => {
|
|
53
|
-
const { file, headers, method, data, responseType } = options;
|
|
54
|
-
const request = new Promise((resolve, reject) => {
|
|
55
|
-
const adapter = adapterFor(url);
|
|
56
|
-
if (adapter) {
|
|
57
|
-
const requestOpt = {
|
|
58
|
-
headers: headers || {},
|
|
59
|
-
method,
|
|
60
|
-
};
|
|
61
|
-
const body = typeof data === 'string' ? data : JSON.stringify(data);
|
|
62
|
-
// https://stackoverflow.com/questions/35589109/node-http-delete-request-no-longer-works-after-upgrading-from-0-10-40
|
|
63
|
-
let form;
|
|
64
|
-
let uploadedFile = file;
|
|
65
|
-
if (file) {
|
|
66
|
-
const fileMeta = {};
|
|
67
|
-
if (isObject(file) &&
|
|
68
|
-
'file' in file &&
|
|
69
|
-
('filename' in file || 'name' in file)) {
|
|
70
|
-
const { file: file_, name, ...fileMeta_ } = file;
|
|
71
|
-
if (name && !fileMeta_.filename) {
|
|
72
|
-
fileMeta_.filename = name;
|
|
73
|
-
}
|
|
74
|
-
Object.assign(fileMeta, fileMeta_);
|
|
75
|
-
uploadedFile = file_;
|
|
76
|
-
}
|
|
77
|
-
form = new FormData();
|
|
78
|
-
form.append('file', uploadedFile, fileMeta);
|
|
79
|
-
if (data) {
|
|
80
|
-
for (const d in data) {
|
|
81
|
-
form.append(d, data[d]);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
Object.assign(requestOpt.headers, {
|
|
85
|
-
// 'content-length': form.getLengthSync(),
|
|
86
|
-
...form.getHeaders(),
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
if (body !== undefined) {
|
|
90
|
-
Object.assign(requestOpt.headers, {
|
|
91
|
-
'content-type': 'application/json',
|
|
92
|
-
'content-length': Buffer.byteLength(body),
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
const req = adapter.request(url, requestOpt, (resp) => {
|
|
96
|
-
let data = '';
|
|
97
|
-
resp.on('data', (chunk) => {
|
|
98
|
-
data += chunk;
|
|
99
|
-
});
|
|
100
|
-
resp.on('end', () => {
|
|
101
|
-
if (data) {
|
|
102
|
-
if (responseType === 'blob') {
|
|
103
|
-
resolve(data);
|
|
104
|
-
}
|
|
105
|
-
else {
|
|
106
|
-
let json;
|
|
107
|
-
try {
|
|
108
|
-
json = JSON.parse(data);
|
|
109
|
-
if (json && json.status_code && json.status_code) {
|
|
110
|
-
reject(json.message);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
catch (er) {
|
|
114
|
-
reject(er);
|
|
115
|
-
}
|
|
116
|
-
if (json !== undefined) {
|
|
117
|
-
if (isError(json)) {
|
|
118
|
-
reject('extractError(json)');
|
|
119
|
-
}
|
|
120
|
-
else {
|
|
121
|
-
resolve(json);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
reject('no data');
|
|
127
|
-
});
|
|
128
|
-
});
|
|
129
|
-
if (form) {
|
|
130
|
-
form.pipe(req);
|
|
131
|
-
}
|
|
132
|
-
req.on('error', (err) => {
|
|
133
|
-
reject(err);
|
|
134
|
-
});
|
|
135
|
-
if (body) {
|
|
136
|
-
req.write(body);
|
|
137
|
-
}
|
|
138
|
-
onCancel(() => {
|
|
139
|
-
req.abort();
|
|
140
|
-
});
|
|
141
|
-
req.end();
|
|
142
|
-
}
|
|
143
|
-
else {
|
|
144
|
-
throw new Error(`Given URL '${url}' is not correct`);
|
|
145
|
-
}
|
|
146
|
-
});
|
|
147
|
-
return request
|
|
148
|
-
.then((data) => {
|
|
149
|
-
if (callback) {
|
|
150
|
-
callback(data);
|
|
151
|
-
}
|
|
152
|
-
return data;
|
|
153
|
-
})
|
|
154
|
-
.catch((er) => {
|
|
155
|
-
if (error) {
|
|
156
|
-
error(er);
|
|
157
|
-
}
|
|
158
|
-
else {
|
|
159
|
-
throw new Error(er);
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// https://github.com/Leaflet/Leaflet/blob/b507e21c510b53cd704fb8d3f89bb46ea925c8eb/src/core/Util.js#L165
|
|
166
|
-
const templateRe = /\{ *([\w_-]+) *\}/g;
|
|
167
|
-
function template(str, data) {
|
|
168
|
-
return str.replace(templateRe, (s, key) => {
|
|
169
|
-
let value = data[key];
|
|
170
|
-
if (value === undefined) {
|
|
171
|
-
throw new Error('No value provided for letiable ' + s);
|
|
172
|
-
}
|
|
173
|
-
else if (typeof value === 'function') {
|
|
174
|
-
value = value(data);
|
|
175
|
-
}
|
|
176
|
-
return value;
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
function apiRequest(opt) {
|
|
181
|
-
const params = opt.params;
|
|
182
|
-
return new CancelablePromise__default((resolve, reject) => {
|
|
183
|
-
opt.connector
|
|
184
|
-
.connect()
|
|
185
|
-
.then((apiItems) => {
|
|
186
|
-
let apiItem = apiItems && apiItems[opt.name];
|
|
187
|
-
if (apiItem) {
|
|
188
|
-
apiItem = [...apiItem];
|
|
189
|
-
let url = apiItem.shift();
|
|
190
|
-
if (apiItem.length) {
|
|
191
|
-
const replaceParams = {};
|
|
192
|
-
for (let fry = 0; fry < apiItem.length; fry++) {
|
|
193
|
-
const arg = apiItem[fry];
|
|
194
|
-
replaceParams[fry] = '{' + arg + '}';
|
|
195
|
-
if (params[arg] === undefined) {
|
|
196
|
-
throw new Error('`' + arg + '`' + ' url api argument is not specified');
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
if (url) {
|
|
200
|
-
url = template(url, replaceParams);
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
// Transfer part of the parameters from `params` to the URL string
|
|
204
|
-
if (params) {
|
|
205
|
-
const paramArray = [];
|
|
206
|
-
const paramList = params.paramList;
|
|
207
|
-
if (Array.isArray(paramList)) {
|
|
208
|
-
delete params.paramList;
|
|
209
|
-
paramList.forEach((x) => {
|
|
210
|
-
paramArray.push(`${x[0]}=${x[1]}`);
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
for (const p in params) {
|
|
214
|
-
if (apiItem.indexOf(p) === -1) {
|
|
215
|
-
paramArray.push(`${p}=${params[p]}`);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
if (paramArray.length) {
|
|
219
|
-
url = url + '?' + paramArray.join('&');
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
if (url) {
|
|
223
|
-
resolve(opt.connector.makeQuery(url, params, opt.requestOptions));
|
|
224
|
-
}
|
|
225
|
-
else {
|
|
226
|
-
reject(new Error('request url is not set'));
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
else {
|
|
230
|
-
resolve(undefined);
|
|
231
|
-
}
|
|
232
|
-
})
|
|
233
|
-
.catch((er) => {
|
|
234
|
-
reject(er);
|
|
235
|
-
});
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
|
|
239
26
|
/**
|
|
240
27
|
* Thrown when ...
|
|
241
28
|
*/
|
|
@@ -248,42 +35,12 @@ class ResourceNotFoundError extends NgwError {
|
|
|
248
35
|
}
|
|
249
36
|
}
|
|
250
37
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
*/
|
|
254
|
-
class InsufficientPermissionsError extends NgwError {
|
|
255
|
-
constructor(obj) {
|
|
256
|
-
super(obj);
|
|
257
|
-
this.name = 'InsufficientPermissionsError';
|
|
258
|
-
this.exception = 'nextgisweb.core.exception.InsufficientPermissions';
|
|
259
|
-
Object.setPrototypeOf(this, InsufficientPermissionsError.prototype);
|
|
260
|
-
}
|
|
38
|
+
function isObject(val) {
|
|
39
|
+
return Object.prototype.toString.call(val) === '[object Object]';
|
|
261
40
|
}
|
|
262
41
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
CONNECTORS.push(connector);
|
|
266
|
-
}
|
|
267
|
-
function findConnector(options) {
|
|
268
|
-
return CONNECTORS.find((x) => {
|
|
269
|
-
if (x.options.baseUrl === options.baseUrl) {
|
|
270
|
-
if (options.auth) {
|
|
271
|
-
if (x.options.auth) {
|
|
272
|
-
return utils.objectDeepEqual(x.options.auth, options.auth);
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
else {
|
|
276
|
-
// if auth not in find options; use first connector with same url
|
|
277
|
-
return true;
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
});
|
|
281
|
-
}
|
|
282
|
-
function removeConnector(connector) {
|
|
283
|
-
const index = CONNECTORS.indexOf(connector);
|
|
284
|
-
if (index !== -1) {
|
|
285
|
-
CONNECTORS.splice(index, 1);
|
|
286
|
-
}
|
|
42
|
+
function resourceCompare(res1, res2) {
|
|
43
|
+
return utils.objectDeepEqual(res1, res2);
|
|
287
44
|
}
|
|
288
45
|
|
|
289
46
|
const exclude = ['description'];
|
|
@@ -319,10 +76,6 @@ function resourceToQuery(resource, prefix = '') {
|
|
|
319
76
|
return query;
|
|
320
77
|
}
|
|
321
78
|
|
|
322
|
-
function resourceCompare(res1, res2) {
|
|
323
|
-
return utils.objectDeepEqual(res1, res2);
|
|
324
|
-
}
|
|
325
|
-
|
|
326
79
|
class ResourcesControl {
|
|
327
80
|
constructor(connector) {
|
|
328
81
|
this.connector = connector;
|
|
@@ -563,6 +316,253 @@ class ResourcesControl {
|
|
|
563
316
|
}
|
|
564
317
|
}
|
|
565
318
|
|
|
319
|
+
const CONNECTORS = [];
|
|
320
|
+
function addConnector(connector) {
|
|
321
|
+
CONNECTORS.push(connector);
|
|
322
|
+
}
|
|
323
|
+
function findConnector(options) {
|
|
324
|
+
return CONNECTORS.find((x) => {
|
|
325
|
+
if (x.options.baseUrl === options.baseUrl) {
|
|
326
|
+
if (options.auth) {
|
|
327
|
+
if (x.options.auth) {
|
|
328
|
+
return utils.objectDeepEqual(x.options.auth, options.auth);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
else {
|
|
332
|
+
// if auth not in find options; use first connector with same url
|
|
333
|
+
return true;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
function removeConnector(connector) {
|
|
339
|
+
const index = CONNECTORS.indexOf(connector);
|
|
340
|
+
if (index !== -1) {
|
|
341
|
+
CONNECTORS.splice(index, 1);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Thrown when ...
|
|
347
|
+
*/
|
|
348
|
+
class InsufficientPermissionsError extends NgwError {
|
|
349
|
+
constructor(obj) {
|
|
350
|
+
super(obj);
|
|
351
|
+
this.name = 'InsufficientPermissionsError';
|
|
352
|
+
this.exception = 'nextgisweb.core.exception.InsufficientPermissions';
|
|
353
|
+
Object.setPrototypeOf(this, InsufficientPermissionsError.prototype);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// https://github.com/Leaflet/Leaflet/blob/b507e21c510b53cd704fb8d3f89bb46ea925c8eb/src/core/Util.js#L165
|
|
358
|
+
const templateRe = /\{ *([\w_-]+) *\}/g;
|
|
359
|
+
function template(str, data) {
|
|
360
|
+
return str.replace(templateRe, (s, key) => {
|
|
361
|
+
let value = data[key];
|
|
362
|
+
if (value === undefined) {
|
|
363
|
+
throw new Error('No value provided for letiable ' + s);
|
|
364
|
+
}
|
|
365
|
+
else if (typeof value === 'function') {
|
|
366
|
+
value = value(data);
|
|
367
|
+
}
|
|
368
|
+
return value;
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function apiRequest(opt) {
|
|
373
|
+
const params = opt.params;
|
|
374
|
+
return new CancelablePromise__default((resolve, reject) => {
|
|
375
|
+
opt.connector
|
|
376
|
+
.connect()
|
|
377
|
+
.then((apiItems) => {
|
|
378
|
+
let apiItem = apiItems && apiItems[opt.name];
|
|
379
|
+
if (apiItem) {
|
|
380
|
+
apiItem = [...apiItem];
|
|
381
|
+
let url = apiItem.shift();
|
|
382
|
+
if (apiItem.length) {
|
|
383
|
+
const replaceParams = {};
|
|
384
|
+
for (let fry = 0; fry < apiItem.length; fry++) {
|
|
385
|
+
const arg = apiItem[fry];
|
|
386
|
+
replaceParams[fry] = '{' + arg + '}';
|
|
387
|
+
if (params[arg] === undefined) {
|
|
388
|
+
throw new Error('`' + arg + '`' + ' url api argument is not specified');
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
if (url) {
|
|
392
|
+
url = template(url, replaceParams);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
// Transfer part of the parameters from `params` to the URL string
|
|
396
|
+
if (params) {
|
|
397
|
+
const paramArray = [];
|
|
398
|
+
const paramList = params.paramList;
|
|
399
|
+
if (Array.isArray(paramList)) {
|
|
400
|
+
delete params.paramList;
|
|
401
|
+
paramList.forEach((x) => {
|
|
402
|
+
paramArray.push(`${x[0]}=${x[1]}`);
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
for (const p in params) {
|
|
406
|
+
if (apiItem.indexOf(p) === -1) {
|
|
407
|
+
paramArray.push(`${p}=${params[p]}`);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
if (paramArray.length) {
|
|
411
|
+
url = url + '?' + paramArray.join('&');
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
if (url) {
|
|
415
|
+
resolve(opt.connector.makeQuery(url, params, opt.requestOptions));
|
|
416
|
+
}
|
|
417
|
+
else {
|
|
418
|
+
reject(new Error('request url is not set'));
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
else {
|
|
422
|
+
resolve(undefined);
|
|
423
|
+
}
|
|
424
|
+
})
|
|
425
|
+
.catch((er) => {
|
|
426
|
+
reject(er);
|
|
427
|
+
});
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
function isError(error) {
|
|
432
|
+
if (utils.isObject(error)) {
|
|
433
|
+
return error.status_code && error.exception && error.title;
|
|
434
|
+
}
|
|
435
|
+
return false;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
let loadData;
|
|
439
|
+
// @ts-ignore
|
|
440
|
+
{
|
|
441
|
+
const url = require('url');
|
|
442
|
+
const http = require('http');
|
|
443
|
+
const https = require('https');
|
|
444
|
+
const FormData = require('form-data');
|
|
445
|
+
const adapterFor = (inputUrl) => {
|
|
446
|
+
const adapters = {
|
|
447
|
+
'http:': http,
|
|
448
|
+
'https:': https,
|
|
449
|
+
};
|
|
450
|
+
const protocol = url.parse(inputUrl).protocol || 'https:';
|
|
451
|
+
return adapters[protocol];
|
|
452
|
+
};
|
|
453
|
+
loadData = (url, callback, options = {}, error, onCancel) => {
|
|
454
|
+
const { file, headers, method, data, responseType } = options;
|
|
455
|
+
const request = new Promise((resolve, reject) => {
|
|
456
|
+
const adapter = adapterFor(url);
|
|
457
|
+
if (adapter) {
|
|
458
|
+
const requestOpt = {
|
|
459
|
+
headers: headers || {},
|
|
460
|
+
method,
|
|
461
|
+
};
|
|
462
|
+
const body = typeof data === 'string' ? data : JSON.stringify(data);
|
|
463
|
+
// https://stackoverflow.com/questions/35589109/node-http-delete-request-no-longer-works-after-upgrading-from-0-10-40
|
|
464
|
+
let form;
|
|
465
|
+
let uploadedFile = file;
|
|
466
|
+
if (file) {
|
|
467
|
+
const fileMeta = {};
|
|
468
|
+
if (isObject(file) &&
|
|
469
|
+
'file' in file &&
|
|
470
|
+
('filename' in file || 'name' in file)) {
|
|
471
|
+
const { file: file_, name, ...fileMeta_ } = file;
|
|
472
|
+
if (name && !fileMeta_.filename) {
|
|
473
|
+
fileMeta_.filename = name;
|
|
474
|
+
}
|
|
475
|
+
Object.assign(fileMeta, fileMeta_);
|
|
476
|
+
uploadedFile = file_;
|
|
477
|
+
}
|
|
478
|
+
form = new FormData();
|
|
479
|
+
form.append('file', uploadedFile, fileMeta);
|
|
480
|
+
if (data) {
|
|
481
|
+
for (const d in data) {
|
|
482
|
+
form.append(d, data[d]);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
Object.assign(requestOpt.headers, {
|
|
486
|
+
// 'content-length': form.getLengthSync(),
|
|
487
|
+
...form.getHeaders(),
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
if (body !== undefined) {
|
|
491
|
+
Object.assign(requestOpt.headers, {
|
|
492
|
+
'content-type': 'application/json',
|
|
493
|
+
'content-length': Buffer.byteLength(body),
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
const req = adapter.request(url, requestOpt, (resp) => {
|
|
497
|
+
let data = '';
|
|
498
|
+
resp.on('data', (chunk) => {
|
|
499
|
+
data += chunk;
|
|
500
|
+
});
|
|
501
|
+
resp.on('end', () => {
|
|
502
|
+
if (data) {
|
|
503
|
+
if (responseType === 'blob') {
|
|
504
|
+
resolve(data);
|
|
505
|
+
}
|
|
506
|
+
else {
|
|
507
|
+
let json;
|
|
508
|
+
try {
|
|
509
|
+
json = JSON.parse(data);
|
|
510
|
+
if (json && json.status_code && json.status_code) {
|
|
511
|
+
reject(json.message);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
catch (er) {
|
|
515
|
+
reject(er);
|
|
516
|
+
}
|
|
517
|
+
if (json !== undefined) {
|
|
518
|
+
if (isError(json)) {
|
|
519
|
+
reject('extractError(json)');
|
|
520
|
+
}
|
|
521
|
+
else {
|
|
522
|
+
resolve(json);
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
reject('no data');
|
|
528
|
+
});
|
|
529
|
+
});
|
|
530
|
+
if (form) {
|
|
531
|
+
form.pipe(req);
|
|
532
|
+
}
|
|
533
|
+
req.on('error', (err) => {
|
|
534
|
+
reject(err);
|
|
535
|
+
});
|
|
536
|
+
if (body) {
|
|
537
|
+
req.write(body);
|
|
538
|
+
}
|
|
539
|
+
onCancel(() => {
|
|
540
|
+
req.abort();
|
|
541
|
+
});
|
|
542
|
+
req.end();
|
|
543
|
+
}
|
|
544
|
+
else {
|
|
545
|
+
throw new Error(`Given URL '${url}' is not correct`);
|
|
546
|
+
}
|
|
547
|
+
});
|
|
548
|
+
return request
|
|
549
|
+
.then((data) => {
|
|
550
|
+
if (callback) {
|
|
551
|
+
callback(data);
|
|
552
|
+
}
|
|
553
|
+
return data;
|
|
554
|
+
})
|
|
555
|
+
.catch((er) => {
|
|
556
|
+
if (error) {
|
|
557
|
+
error(er);
|
|
558
|
+
}
|
|
559
|
+
else {
|
|
560
|
+
throw new Error(er);
|
|
561
|
+
}
|
|
562
|
+
});
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
|
|
566
566
|
let ID = 0;
|
|
567
567
|
class NgwConnector$1 {
|
|
568
568
|
constructor(options) {
|