@itwin/core-mobile 4.0.0-dev.7 → 4.0.0-dev.72
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/CHANGELOG.md +48 -1
- package/README.md +1 -1
- package/lib/cjs/MobileBackend.d.ts +7 -7
- package/lib/cjs/MobileBackend.js +28 -24
- package/lib/cjs/MobileBackend.js.map +1 -1
- package/lib/cjs/MobileFrontend.d.ts +5 -5
- package/lib/cjs/MobileFrontend.js +25 -21
- package/lib/cjs/MobileFrontend.js.map +1 -1
- package/lib/cjs/__DOC_ONLY__.d.ts +33 -33
- package/lib/cjs/__DOC_ONLY__.js +56 -52
- package/lib/cjs/__DOC_ONLY__.js.map +1 -1
- package/lib/cjs/backend/MobileAuthorizationBackend.d.ts +17 -17
- package/lib/cjs/backend/MobileAuthorizationBackend.js +46 -46
- package/lib/cjs/backend/MobileAuthorizationBackend.js.map +1 -1
- package/lib/cjs/backend/MobileFileHandler.d.ts +112 -112
- package/lib/cjs/backend/MobileFileHandler.js +263 -263
- package/lib/cjs/backend/MobileHost.d.ts +72 -72
- package/lib/cjs/backend/MobileHost.d.ts.map +1 -1
- package/lib/cjs/backend/MobileHost.js +155 -156
- package/lib/cjs/backend/MobileHost.js.map +1 -1
- package/lib/cjs/backend/MobileRpcServer.d.ts +19 -19
- package/lib/cjs/backend/MobileRpcServer.js +156 -156
- package/lib/cjs/backend/MobileRpcServer.js.map +1 -1
- package/lib/cjs/backend/Request.d.ts +117 -149
- package/lib/cjs/backend/Request.d.ts.map +1 -1
- package/lib/cjs/backend/Request.js +255 -267
- package/lib/cjs/backend/Request.js.map +1 -1
- package/lib/cjs/common/MobileAppChannel.d.ts +4 -4
- package/lib/cjs/common/MobileAppChannel.js +11 -11
- package/lib/cjs/common/MobileAppProps.d.ts +35 -35
- package/lib/cjs/common/MobileAppProps.d.ts.map +1 -1
- package/lib/cjs/common/MobileAppProps.js +26 -26
- package/lib/cjs/common/MobileEventLoop.d.ts +11 -11
- package/lib/cjs/common/MobileEventLoop.js +30 -30
- package/lib/cjs/common/MobileEventLoop.js.map +1 -1
- package/lib/cjs/common/MobileIpc.d.ts +17 -17
- package/lib/cjs/common/MobileIpc.js +70 -70
- package/lib/cjs/common/MobilePush.d.ts +20 -20
- package/lib/cjs/common/MobilePush.js +53 -53
- package/lib/cjs/common/MobilePush.js.map +1 -1
- package/lib/cjs/common/MobileRpcManager.d.ts +40 -40
- package/lib/cjs/common/MobileRpcManager.js +110 -110
- package/lib/cjs/common/MobileRpcManager.js.map +1 -1
- package/lib/cjs/common/MobileRpcProtocol.d.ts +58 -58
- package/lib/cjs/common/MobileRpcProtocol.d.ts.map +1 -1
- package/lib/cjs/common/MobileRpcProtocol.js +277 -277
- package/lib/cjs/common/MobileRpcProtocol.js.map +1 -1
- package/lib/cjs/common/MobileRpcRequest.d.ts +20 -20
- package/lib/cjs/common/MobileRpcRequest.js +50 -50
- package/lib/cjs/frontend/MobileApp.d.ts +23 -23
- package/lib/cjs/frontend/MobileApp.d.ts.map +1 -1
- package/lib/cjs/frontend/MobileApp.js +79 -80
- package/lib/cjs/frontend/MobileApp.js.map +1 -1
- package/lib/cjs/frontend/MobileAuthorizationFrontend.d.ts +17 -17
- package/lib/cjs/frontend/MobileAuthorizationFrontend.d.ts.map +1 -1
- package/lib/cjs/frontend/MobileAuthorizationFrontend.js +54 -46
- package/lib/cjs/frontend/MobileAuthorizationFrontend.js.map +1 -1
- package/lib/cjs/test/ios/MobilePlatform.test.d.ts +1 -1
- package/lib/cjs/test/ios/MobilePlatform.test.js +84 -84
- package/package.json +20 -24
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Request.d.ts","sourceRoot":"","sources":["../../../src/backend/Request.ts"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"Request.d.ts","sourceRoot":"","sources":["../../../src/backend/Request.ts"],"names":[],"mappings":";AAQA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,OAAO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Json = exports.request = exports.ResponseError = exports.requestIdHeaderName = void 0;
|
|
4
|
-
/*---------------------------------------------------------------------------------------------
|
|
5
|
-
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
|
|
6
|
-
* See LICENSE.md in the project root for license terms and full copyright notice.
|
|
7
|
-
*--------------------------------------------------------------------------------------------*/
|
|
8
|
-
/** @packageDocumentation
|
|
9
|
-
* @module iTwinServiceClients
|
|
10
|
-
*/
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
*
|
|
27
|
-
* @
|
|
28
|
-
* @
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
error
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
error.
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
error._data
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
error.
|
|
54
|
-
error.
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
*
|
|
62
|
-
* @param
|
|
63
|
-
* @
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
*
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
const
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
* @param
|
|
123
|
-
* @
|
|
124
|
-
* @
|
|
125
|
-
* @
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
if (options.
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
sareq = sareq.
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
if (options.
|
|
149
|
-
sareq = sareq.
|
|
150
|
-
if (options.
|
|
151
|
-
sareq = sareq.
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
if (options.
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
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
|
-
.on("
|
|
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
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
* @param url server URL to address the request
|
|
257
|
-
* @internal
|
|
258
|
-
*/
|
|
259
|
-
async function getJson(url) {
|
|
260
|
-
const options = {
|
|
261
|
-
method: "GET",
|
|
262
|
-
responseType: "json",
|
|
263
|
-
};
|
|
264
|
-
const data = await request(url, options);
|
|
265
|
-
return data.body;
|
|
266
|
-
}
|
|
267
|
-
exports.getJson = getJson;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getJson = exports.request = exports.ResponseError = exports.requestIdHeaderName = void 0;
|
|
4
|
+
/*---------------------------------------------------------------------------------------------
|
|
5
|
+
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
|
|
6
|
+
* See LICENSE.md in the project root for license terms and full copyright notice.
|
|
7
|
+
*--------------------------------------------------------------------------------------------*/
|
|
8
|
+
/** @packageDocumentation
|
|
9
|
+
* @module iTwinServiceClients
|
|
10
|
+
*/
|
|
11
|
+
const _ = require("lodash");
|
|
12
|
+
const sarequest = require("superagent");
|
|
13
|
+
const core_bentley_1 = require("@itwin/core-bentley");
|
|
14
|
+
const loggerCategory = "core-mobile-backend.Request";
|
|
15
|
+
/** @internal */
|
|
16
|
+
exports.requestIdHeaderName = "X-Correlation-Id";
|
|
17
|
+
/** Error object that's thrown/rejected if the Request fails due to a network error, or if the status is *not* in the range of 200-299 (inclusive)
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
20
|
+
class ResponseError extends core_bentley_1.BentleyError {
|
|
21
|
+
constructor(errorNumber, message, getMetaData) {
|
|
22
|
+
super(errorNumber, message, getMetaData);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Parses error from server's response
|
|
26
|
+
* @param response Http response from the server.
|
|
27
|
+
* @returns Parsed error.
|
|
28
|
+
* @internal
|
|
29
|
+
*/
|
|
30
|
+
static parse(response, log = true) {
|
|
31
|
+
const error = new ResponseError(ResponseError.parseHttpStatus(response.statusType));
|
|
32
|
+
if (!response) {
|
|
33
|
+
error.message = "Couldn't get response object.";
|
|
34
|
+
return error;
|
|
35
|
+
}
|
|
36
|
+
if (response.response) {
|
|
37
|
+
if (response.response.error) {
|
|
38
|
+
error.name = response.response.error.name || error.name;
|
|
39
|
+
error.description = response.response.error.message;
|
|
40
|
+
}
|
|
41
|
+
if (response.response.res) {
|
|
42
|
+
error.message = response.response.res.statusMessage;
|
|
43
|
+
}
|
|
44
|
+
if (response.response.body && Object.keys(response.response.body).length > 0) {
|
|
45
|
+
error._data = {};
|
|
46
|
+
_.merge(error._data, response.response.body);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
error._data = response.response.text;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
error.status = response.status || response.statusCode;
|
|
53
|
+
error.name = response.code || response.name || error.name;
|
|
54
|
+
error.message = error.message || response.message || response.statusMessage;
|
|
55
|
+
if (log)
|
|
56
|
+
error.log();
|
|
57
|
+
return error;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Decides whether request should be retried or not
|
|
61
|
+
* @param error Error returned by request
|
|
62
|
+
* @param response Response returned by request
|
|
63
|
+
* @internal
|
|
64
|
+
*/
|
|
65
|
+
static shouldRetry(error, response) {
|
|
66
|
+
if (error !== undefined && error !== null) {
|
|
67
|
+
if ((error.status === undefined || error.status === null) && (error.res === undefined || error.res === null)) {
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return (response !== undefined && response.statusType === core_bentley_1.HttpStatus.ServerError);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* @internal
|
|
75
|
+
*/
|
|
76
|
+
static parseHttpStatus(statusType) {
|
|
77
|
+
switch (statusType) {
|
|
78
|
+
case 1:
|
|
79
|
+
return core_bentley_1.HttpStatus.Info;
|
|
80
|
+
case 2:
|
|
81
|
+
return core_bentley_1.HttpStatus.Success;
|
|
82
|
+
case 3:
|
|
83
|
+
return core_bentley_1.HttpStatus.Redirection;
|
|
84
|
+
case 4:
|
|
85
|
+
return core_bentley_1.HttpStatus.ClientError;
|
|
86
|
+
case 5:
|
|
87
|
+
return core_bentley_1.HttpStatus.ServerError;
|
|
88
|
+
default:
|
|
89
|
+
return core_bentley_1.HttpStatus.Success;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* @internal
|
|
94
|
+
*/
|
|
95
|
+
logMessage() {
|
|
96
|
+
return `${this.status} ${this.name}: ${this.message}`;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Logs this error
|
|
100
|
+
* @internal
|
|
101
|
+
*/
|
|
102
|
+
log() {
|
|
103
|
+
core_bentley_1.Logger.logError(loggerCategory, this.logMessage(), () => this.getMetaData());
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
exports.ResponseError = ResponseError;
|
|
107
|
+
const logResponse = (req, startTime) => (res) => {
|
|
108
|
+
const elapsed = new Date().getTime() - startTime;
|
|
109
|
+
const elapsedTime = `${elapsed}ms`;
|
|
110
|
+
core_bentley_1.Logger.logTrace(loggerCategory, `${req.method.toUpperCase()} ${res.status} ${req.url} (${elapsedTime})`);
|
|
111
|
+
};
|
|
112
|
+
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
113
|
+
const logRequest = (req) => {
|
|
114
|
+
const startTime = new Date().getTime();
|
|
115
|
+
return req.on("response", logResponse(req, startTime));
|
|
116
|
+
};
|
|
117
|
+
/** Wrapper around making HTTP requests with the specific options.
|
|
118
|
+
*
|
|
119
|
+
* Usable in both a browser and node based environment.
|
|
120
|
+
*
|
|
121
|
+
* @param url Server URL to address the request
|
|
122
|
+
* @param options Options to pass to the request
|
|
123
|
+
* @returns Resolves to the response from the server
|
|
124
|
+
* @throws ResponseError if the request fails due to network issues, or if the returned status is *outside* the range of 200-299 (inclusive)
|
|
125
|
+
* @internal
|
|
126
|
+
*/
|
|
127
|
+
async function request(url, options) {
|
|
128
|
+
let sareq = sarequest(options.method, url);
|
|
129
|
+
if (options.retries)
|
|
130
|
+
sareq = sareq.retry(options.retries, options.retryCallback);
|
|
131
|
+
if (core_bentley_1.Logger.isEnabled(loggerCategory, core_bentley_1.LogLevel.Trace))
|
|
132
|
+
sareq = sareq.use(logRequest);
|
|
133
|
+
if (options.headers)
|
|
134
|
+
sareq = sareq.set(options.headers);
|
|
135
|
+
core_bentley_1.Logger.logInfo(loggerCategory, url);
|
|
136
|
+
if (options.accept)
|
|
137
|
+
sareq = sareq.accept(options.accept);
|
|
138
|
+
if (options.body)
|
|
139
|
+
sareq = sareq.send(options.body);
|
|
140
|
+
if (options.timeout)
|
|
141
|
+
sareq = sareq.timeout(options.timeout);
|
|
142
|
+
if (options.responseType)
|
|
143
|
+
sareq = sareq.responseType(options.responseType);
|
|
144
|
+
if (options.redirects)
|
|
145
|
+
sareq = sareq.redirects(options.redirects);
|
|
146
|
+
else
|
|
147
|
+
sareq = sareq.redirects(0);
|
|
148
|
+
if (options.buffer)
|
|
149
|
+
sareq = sareq.buffer(options.buffer);
|
|
150
|
+
if (options.parser)
|
|
151
|
+
sareq = sareq.parse(options.parser);
|
|
152
|
+
/** Default to any globally supplied proxy, unless an agent is specified in this call */
|
|
153
|
+
if (options.agent)
|
|
154
|
+
sareq = sareq.agent(options.agent);
|
|
155
|
+
if (options.progressCallback) {
|
|
156
|
+
sareq = sareq.on("progress", (event) => {
|
|
157
|
+
if (event) {
|
|
158
|
+
options.progressCallback({
|
|
159
|
+
loaded: event.loaded,
|
|
160
|
+
total: event.total,
|
|
161
|
+
percent: event.percent,
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
const errorCallback = options.errorCallback ? options.errorCallback : ResponseError.parse;
|
|
167
|
+
if (options.readStream) {
|
|
168
|
+
if (typeof window !== "undefined")
|
|
169
|
+
throw new Error("This option is not supported on browsers");
|
|
170
|
+
return new Promise((resolve, reject) => {
|
|
171
|
+
sareq = sareq.type("blob");
|
|
172
|
+
options
|
|
173
|
+
.readStream
|
|
174
|
+
.pipe(sareq)
|
|
175
|
+
.on("error", (error) => {
|
|
176
|
+
const parsedError = errorCallback(error);
|
|
177
|
+
reject(parsedError);
|
|
178
|
+
})
|
|
179
|
+
.on("end", () => {
|
|
180
|
+
const retResponse = {
|
|
181
|
+
status: 201,
|
|
182
|
+
header: undefined,
|
|
183
|
+
body: undefined,
|
|
184
|
+
text: undefined,
|
|
185
|
+
};
|
|
186
|
+
resolve(retResponse);
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
if (options.stream) {
|
|
191
|
+
if (typeof window !== "undefined")
|
|
192
|
+
throw new Error("This option is not supported on browsers");
|
|
193
|
+
return new Promise((resolve, reject) => {
|
|
194
|
+
sareq
|
|
195
|
+
.on("response", (res) => {
|
|
196
|
+
if (res.statusCode !== 200) {
|
|
197
|
+
const parsedError = errorCallback(res);
|
|
198
|
+
reject(parsedError);
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
})
|
|
202
|
+
.pipe(options.stream)
|
|
203
|
+
.on("error", (error) => {
|
|
204
|
+
const parsedError = errorCallback(error);
|
|
205
|
+
reject(parsedError);
|
|
206
|
+
})
|
|
207
|
+
.on("finish", () => {
|
|
208
|
+
const retResponse = {
|
|
209
|
+
status: 200,
|
|
210
|
+
header: undefined,
|
|
211
|
+
body: undefined,
|
|
212
|
+
text: undefined,
|
|
213
|
+
};
|
|
214
|
+
resolve(retResponse);
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
// console.log("%s %s %s", url, options.method, queryStr);
|
|
219
|
+
/**
|
|
220
|
+
* Note:
|
|
221
|
+
* Javascript's fetch returns status.OK if error is between 200-299 inclusive, and doesn't reject in this case.
|
|
222
|
+
* Fetch only rejects if there's some network issue (permissions issue or similar)
|
|
223
|
+
* Superagent rejects network issues, and errors outside the range of 200-299. We are currently using
|
|
224
|
+
* superagent, but may eventually switch to JavaScript's fetch library.
|
|
225
|
+
*/
|
|
226
|
+
try {
|
|
227
|
+
const response = await sareq;
|
|
228
|
+
const retResponse = {
|
|
229
|
+
body: response.body,
|
|
230
|
+
text: response.text,
|
|
231
|
+
header: response.header,
|
|
232
|
+
status: response.status,
|
|
233
|
+
};
|
|
234
|
+
return retResponse;
|
|
235
|
+
}
|
|
236
|
+
catch (error) {
|
|
237
|
+
const parsedError = errorCallback(error);
|
|
238
|
+
throw parsedError;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
exports.request = request;
|
|
242
|
+
/**
|
|
243
|
+
* fetch json from HTTP request
|
|
244
|
+
* @param url server URL to address the request
|
|
245
|
+
* @internal
|
|
246
|
+
*/
|
|
247
|
+
async function getJson(url) {
|
|
248
|
+
const options = {
|
|
249
|
+
method: "GET",
|
|
250
|
+
responseType: "json",
|
|
251
|
+
};
|
|
252
|
+
const data = await request(url, options);
|
|
253
|
+
return data.body;
|
|
254
|
+
}
|
|
255
|
+
exports.getJson = getJson;
|
|
268
256
|
//# sourceMappingURL=Request.js.map
|