@grandlinex/swagger-mate 1.3.4 → 1.3.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.
- package/dist/cjs/Swagger/Client/SwaggerClient.d.ts +0 -3
- package/dist/cjs/Swagger/Client/SwaggerClient.js +5 -38
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.js +1 -1
- package/dist/mjs/Swagger/Client/SwaggerClient.d.ts +0 -3
- package/dist/mjs/Swagger/Client/SwaggerClient.js +5 -38
- package/dist/mjs/index.d.ts +1 -1
- package/dist/mjs/index.js +1 -1
- package/package.json +7 -7
- package/res/templates/class/ApiCon.ts +1 -1
- package/res/templates/class/CApiCon.ts +1 -1
- package/res/templates/class/IApiCon.ts +1 -1
- package/res/templates/class/index.ts +2 -3
- package/dist/cjs/Swagger/debug/BaseCon.d.ts +0 -202
- package/dist/cjs/Swagger/debug/BaseCon.js +0 -432
- package/dist/cjs/Swagger/debug/FetchCon.d.ts +0 -3
- package/dist/cjs/Swagger/debug/FetchCon.js +0 -79
- package/dist/cjs/Swagger/debug/NodeCon.d.ts +0 -3
- package/dist/cjs/Swagger/debug/NodeCon.js +0 -158
- package/dist/cjs/Swagger/debug/index.d.ts +0 -5
- package/dist/cjs/Swagger/debug/index.js +0 -27
- package/dist/mjs/Swagger/debug/BaseCon.d.ts +0 -202
- package/dist/mjs/Swagger/debug/BaseCon.js +0 -425
- package/dist/mjs/Swagger/debug/FetchCon.d.ts +0 -3
- package/dist/mjs/Swagger/debug/FetchCon.js +0 -77
- package/dist/mjs/Swagger/debug/NodeCon.d.ts +0 -3
- package/dist/mjs/Swagger/debug/NodeCon.js +0 -120
- package/dist/mjs/Swagger/debug/index.d.ts +0 -5
- package/dist/mjs/Swagger/debug/index.js +0 -5
- package/res/templates/class/AxiosCon.ts +0 -49
- package/res/templates/class/BaseCon.ts +0 -533
- package/res/templates/class/FetchCon.ts +0 -93
- package/res/templates/class/NodeCon.ts +0 -172
|
@@ -1,432 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* THIS FILE IS AUTOMATICALLY GENERATED
|
|
4
|
-
* DO NOT EDIT THIS FILE
|
|
5
|
-
*/
|
|
6
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
7
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
8
|
-
};
|
|
9
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
exports.isErrorType = isErrorType;
|
|
11
|
-
const form_data_1 = __importDefault(require("form-data"));
|
|
12
|
-
function isErrorType(x) {
|
|
13
|
-
return x && typeof x === 'object' && x.type === 'error';
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* BaseCon provides a minimal client for interacting with an HTTP backend.
|
|
17
|
-
* It manages connection state, authentication tokens, and reconnection
|
|
18
|
-
* logic while delegating actual HTTP requests to a supplied {@link ConHandle}.
|
|
19
|
-
*
|
|
20
|
-
* @class
|
|
21
|
-
*/
|
|
22
|
-
class BaseCon {
|
|
23
|
-
/**
|
|
24
|
-
* Initializes a new instance of the client.
|
|
25
|
-
*
|
|
26
|
-
* @param {Object} conf Configuration object.
|
|
27
|
-
* @param {ConHandle} conf.con - Connection handle.
|
|
28
|
-
* @param {string} conf.endpoint - API endpoint URL.
|
|
29
|
-
* @param {(arg: any) => void} [conf.logger] - Optional logger function; defaults to console.log.
|
|
30
|
-
* @param {Record<string, string>} [conf.permanentHeader] - Optional permanent headers to include in requests.
|
|
31
|
-
* @return {void}
|
|
32
|
-
*/
|
|
33
|
-
constructor(conf) {
|
|
34
|
-
this.api = conf.endpoint;
|
|
35
|
-
this.logger = conf.logger ?? console.log;
|
|
36
|
-
this.disconnected = true;
|
|
37
|
-
this.noAuth = false;
|
|
38
|
-
this.authorization = null;
|
|
39
|
-
this.con = conf.con;
|
|
40
|
-
this.permanentHeader = conf.permanentHeader || {};
|
|
41
|
-
this.reconnect = async () => {
|
|
42
|
-
this.disconnected = true;
|
|
43
|
-
return false;
|
|
44
|
-
};
|
|
45
|
-
this.onReconnect = () => Promise.resolve(true);
|
|
46
|
-
this.handle = this.handle.bind(this);
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Retrieves the API endpoint.
|
|
50
|
-
*
|
|
51
|
-
* @return {string} The API endpoint string.
|
|
52
|
-
*/
|
|
53
|
-
getApiEndpoint() {
|
|
54
|
-
return this.api;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Sets the API endpoint URL used by the client.
|
|
58
|
-
*
|
|
59
|
-
* @param {string} endpoint - The full URL of the API endpoint.
|
|
60
|
-
* @returns {void}
|
|
61
|
-
*/
|
|
62
|
-
setApiEndpoint(endpoint) {
|
|
63
|
-
this.api = endpoint;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Indicates whether the instance is considered connected.
|
|
67
|
-
*
|
|
68
|
-
* The instance is regarded as connected when it either does not require authentication
|
|
69
|
-
* (`noAuth` is true) or it has an authorization token set (`authorization` is not null),
|
|
70
|
-
* and it is not currently marked as disconnected.
|
|
71
|
-
*
|
|
72
|
-
* @return {boolean} `true` if the instance is connected, `false` otherwise.
|
|
73
|
-
*/
|
|
74
|
-
isConnected() {
|
|
75
|
-
return (this.noAuth || this.authorization !== null) && !this.disconnected;
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Retrieves the current authorization token.
|
|
79
|
-
*
|
|
80
|
-
* @return {string|null} The token string used for authorization, or {@code null} if no token is set.
|
|
81
|
-
*/
|
|
82
|
-
token() {
|
|
83
|
-
return this.authorization;
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Returns the bearer token string if an authorization value has been set.
|
|
87
|
-
*
|
|
88
|
-
* @return {string|null} The bearer token prefixed with "Bearer ", or {@code null} when no authorization is present.
|
|
89
|
-
*/
|
|
90
|
-
getBearer() {
|
|
91
|
-
if (this.authorization) {
|
|
92
|
-
return `Bearer ${this.authorization}`;
|
|
93
|
-
}
|
|
94
|
-
return null;
|
|
95
|
-
}
|
|
96
|
-
p(path, config) {
|
|
97
|
-
let pp = path;
|
|
98
|
-
if (config?.param) {
|
|
99
|
-
const e = Object.keys(config.param);
|
|
100
|
-
for (const d of e) {
|
|
101
|
-
pp = pp.replace(`{${d}}`, `${config.param[d]}`);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
if (config?.query) {
|
|
105
|
-
const e = Object.keys(config.query);
|
|
106
|
-
const slices = [];
|
|
107
|
-
for (const d of e) {
|
|
108
|
-
if (config.query[d]) {
|
|
109
|
-
slices.push(`${d}=${config.query[d]}`);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
if (slices.length > 0) {
|
|
113
|
-
pp += `?${slices.join('&')}`;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
return `${this.api}${pp}`;
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Sends a ping request to the API to verify connectivity and version availability.
|
|
120
|
-
*
|
|
121
|
-
* @return {boolean} `true` if the API responded with a 200 status code and a valid version object; `false` otherwise.
|
|
122
|
-
*/
|
|
123
|
-
async ping() {
|
|
124
|
-
try {
|
|
125
|
-
const version = await this.con.get(this.p('/version'));
|
|
126
|
-
return version.data?.api !== undefined && version.code === 200;
|
|
127
|
-
}
|
|
128
|
-
catch (e) {
|
|
129
|
-
this.logger('ping failed');
|
|
130
|
-
return false;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* Builds the Authorization header for HTTP requests.
|
|
135
|
-
*
|
|
136
|
-
* If the instance has an authorization token, this method returns an object
|
|
137
|
-
* containing a single `Authorization` header with the Bearer token value.
|
|
138
|
-
* When no token is available, an empty header object is returned.
|
|
139
|
-
*
|
|
140
|
-
* @return {Record<string, string>} The headers object containing the
|
|
141
|
-
* `Authorization` header when applicable, otherwise an empty object.
|
|
142
|
-
*/
|
|
143
|
-
tokenHeader() {
|
|
144
|
-
if (this.authorization) {
|
|
145
|
-
return {
|
|
146
|
-
Authorization: this.getBearer(),
|
|
147
|
-
};
|
|
148
|
-
}
|
|
149
|
-
return {};
|
|
150
|
-
}
|
|
151
|
-
setPermanentHeader(header) {
|
|
152
|
-
this.permanentHeader = header;
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Validates the current authentication token by performing a ping and a test request
|
|
156
|
-
* to the backend. The method first ensures connectivity via {@link ping}. If the ping
|
|
157
|
-
* succeeds, it attempts to retrieve a token from the `/test/auth` endpoint using the
|
|
158
|
-
* current token in the `Authorization` header. The operation is considered successful
|
|
159
|
-
* if the response status code is 200 or 201.
|
|
160
|
-
*
|
|
161
|
-
* If any step fails, an error is logged and the method returns {@code false}. On
|
|
162
|
-
* success, it returns {@code true}.
|
|
163
|
-
*
|
|
164
|
-
* @return {Promise<boolean>} A promise that resolves to {@code true} if the token
|
|
165
|
-
* test succeeds, otherwise {@code false}.
|
|
166
|
-
*/
|
|
167
|
-
async testToken() {
|
|
168
|
-
const ping = await this.ping();
|
|
169
|
-
if (ping) {
|
|
170
|
-
try {
|
|
171
|
-
const con = await this.con.get(this.p('/test/auth'), {
|
|
172
|
-
headers: this.tokenHeader(),
|
|
173
|
-
});
|
|
174
|
-
return con.code === 200 || con.code === 201;
|
|
175
|
-
}
|
|
176
|
-
catch (e) {
|
|
177
|
-
this.logger(e);
|
|
178
|
-
this.logger('cant connect to backend');
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
this.logger('test ping failed');
|
|
182
|
-
return false;
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* Attempts to establish a connection to the backend without authentication.
|
|
186
|
-
*
|
|
187
|
-
* This method sends a ping request. If the ping succeeds, it clears any
|
|
188
|
-
* existing authorization data, marks the instance as connected,
|
|
189
|
-
* enables the no‑authentication mode, and returns `true`. If the ping
|
|
190
|
-
* fails, it logs a warning, clears authorization, marks the instance
|
|
191
|
-
* as disconnected, and returns `false`.
|
|
192
|
-
*
|
|
193
|
-
* @return {Promise<boolean>} `true` when a connection is successfully
|
|
194
|
-
* established without authentication, otherwise `false`.
|
|
195
|
-
*/
|
|
196
|
-
async connectNoAuth() {
|
|
197
|
-
const ping = await this.ping();
|
|
198
|
-
if (ping) {
|
|
199
|
-
this.authorization = null;
|
|
200
|
-
this.disconnected = false;
|
|
201
|
-
this.noAuth = true;
|
|
202
|
-
return true;
|
|
203
|
-
}
|
|
204
|
-
this.logger('cant connect to backend');
|
|
205
|
-
this.authorization = null;
|
|
206
|
-
this.disconnected = true;
|
|
207
|
-
this.noAuth = false;
|
|
208
|
-
return false;
|
|
209
|
-
}
|
|
210
|
-
/**
|
|
211
|
-
* Forces a connection using the provided bearer token.
|
|
212
|
-
*
|
|
213
|
-
* @param {string|null} token The token to be used for authentication.
|
|
214
|
-
* @returns {void}
|
|
215
|
-
*/
|
|
216
|
-
forceConnect(token) {
|
|
217
|
-
if (token) {
|
|
218
|
-
this.authorization = token.replace('Bearer ', '').replace('bearer ', '');
|
|
219
|
-
}
|
|
220
|
-
else {
|
|
221
|
-
this.authorization = null;
|
|
222
|
-
}
|
|
223
|
-
this.disconnected = false;
|
|
224
|
-
this.noAuth = token === null;
|
|
225
|
-
}
|
|
226
|
-
coppyFrom(con) {
|
|
227
|
-
this.authorization = con.authorization;
|
|
228
|
-
this.disconnected = con.disconnected;
|
|
229
|
-
this.noAuth = con.noAuth;
|
|
230
|
-
}
|
|
231
|
-
/**
|
|
232
|
-
* Establishes a connection to the backend using the supplied credentials.
|
|
233
|
-
* Performs a health‑check ping first; if successful, it requests an authentication
|
|
234
|
-
* token from the `/token` endpoint. When the token is obtained, the method
|
|
235
|
-
* updates internal state (authorization header, connection flags) and, unless
|
|
236
|
-
* a dry run is requested, sets up a reconnection routine. Any errors are
|
|
237
|
-
* logged and the method resolves to `false`.
|
|
238
|
-
*
|
|
239
|
-
* @param {string} email - The user's email address for authentication.
|
|
240
|
-
* @param {string} pw - The password (or token) for the specified user.
|
|
241
|
-
* @param {boolean} [dry=false] - If `true`, the method performs a dry run
|
|
242
|
-
* without persisting credentials or configuring reconnection logic.
|
|
243
|
-
*
|
|
244
|
-
* @returns Promise<boolean> `true` if the connection was successfully
|
|
245
|
-
* established, otherwise `false`.
|
|
246
|
-
*/
|
|
247
|
-
async connect(email, pw, dry = false) {
|
|
248
|
-
const ping = await this.ping();
|
|
249
|
-
if (ping) {
|
|
250
|
-
try {
|
|
251
|
-
const token = await this.con.post(this.p('/token'), {
|
|
252
|
-
username: email,
|
|
253
|
-
token: pw,
|
|
254
|
-
});
|
|
255
|
-
if (token.code === 200 || token.code === 201) {
|
|
256
|
-
if (!dry) {
|
|
257
|
-
this.authorization = token.data.token;
|
|
258
|
-
this.disconnected = false;
|
|
259
|
-
this.noAuth = false;
|
|
260
|
-
this.reconnect = async () => {
|
|
261
|
-
return ((await this.connect(email, pw)) && (await this.testToken()));
|
|
262
|
-
};
|
|
263
|
-
}
|
|
264
|
-
return true;
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
catch (e) {
|
|
268
|
-
this.logger(e);
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
this.logger('cant connect to backend');
|
|
272
|
-
this.authorization = null;
|
|
273
|
-
this.disconnected = true;
|
|
274
|
-
this.noAuth = false;
|
|
275
|
-
return false;
|
|
276
|
-
}
|
|
277
|
-
/**
|
|
278
|
-
* Performs an HTTP request using the client’s internal connection.
|
|
279
|
-
*
|
|
280
|
-
* The method verifies that the client is connected before attempting a request.
|
|
281
|
-
* It automatically injects the authorization token, permanent headers, and any
|
|
282
|
-
* headers supplied in `config`. If the request body is a `FormData` instance
|
|
283
|
-
* (or provides a `getHeaders` method), the appropriate form headers are added.
|
|
284
|
-
*
|
|
285
|
-
* Response handling:
|
|
286
|
-
* - `200` or `201`: returns `success: true` with the received data.
|
|
287
|
-
* - `498`: attempts to reconnect and retries the request once.
|
|
288
|
-
* - `401`: logs an authentication error, marks the client as disconnected.
|
|
289
|
-
* - `403` and other status codes: return `success: false` with the status code.
|
|
290
|
-
*
|
|
291
|
-
* @param {'POST'|'GET'|'PATCH'|'DELETE'} type The HTTP method to use.
|
|
292
|
-
* @param {string} path The endpoint path relative to the base URL.
|
|
293
|
-
* @param {J} [body] Optional request payload. May be `FormData` or a plain object.
|
|
294
|
-
* @param {ConHandleConfig} [config] Optional Axios-like configuration for the request.
|
|
295
|
-
* @returns {Promise<HandleRes<T>>} A promise that resolves to a `HandleRes` object
|
|
296
|
-
* containing the response data, status code, any error information, and headers.
|
|
297
|
-
*/
|
|
298
|
-
async handle(type, path, body, config) {
|
|
299
|
-
if (!this.isConnected()) {
|
|
300
|
-
this.logger('Disconnected');
|
|
301
|
-
return {
|
|
302
|
-
success: false,
|
|
303
|
-
data: null,
|
|
304
|
-
code: -1,
|
|
305
|
-
};
|
|
306
|
-
}
|
|
307
|
-
let formHeader = null;
|
|
308
|
-
const cK = body;
|
|
309
|
-
if (cK &&
|
|
310
|
-
(cK instanceof form_data_1.default || typeof cK?.getHeaders === 'function')) {
|
|
311
|
-
formHeader = cK?.getHeaders?.() || {};
|
|
312
|
-
}
|
|
313
|
-
else {
|
|
314
|
-
formHeader = {};
|
|
315
|
-
}
|
|
316
|
-
let dat;
|
|
317
|
-
switch (type) {
|
|
318
|
-
case 'GET':
|
|
319
|
-
dat = await this.con.get(this.p(path, config), {
|
|
320
|
-
...config,
|
|
321
|
-
headers: {
|
|
322
|
-
...this.tokenHeader(),
|
|
323
|
-
...formHeader,
|
|
324
|
-
...config?.headers,
|
|
325
|
-
...this.permanentHeader,
|
|
326
|
-
},
|
|
327
|
-
});
|
|
328
|
-
break;
|
|
329
|
-
case 'POST':
|
|
330
|
-
dat = await this.con.post(this.p(path, config), body, {
|
|
331
|
-
...config,
|
|
332
|
-
headers: {
|
|
333
|
-
...this.tokenHeader(),
|
|
334
|
-
...formHeader,
|
|
335
|
-
...config?.headers,
|
|
336
|
-
...this.permanentHeader,
|
|
337
|
-
},
|
|
338
|
-
});
|
|
339
|
-
break;
|
|
340
|
-
case 'PATCH':
|
|
341
|
-
dat = await this.con.patch(this.p(path, config), body, {
|
|
342
|
-
...config,
|
|
343
|
-
headers: {
|
|
344
|
-
...this.tokenHeader(),
|
|
345
|
-
...formHeader,
|
|
346
|
-
...config?.headers,
|
|
347
|
-
...this.permanentHeader,
|
|
348
|
-
},
|
|
349
|
-
});
|
|
350
|
-
break;
|
|
351
|
-
case 'DELETE':
|
|
352
|
-
dat = await this.con.delete(this.p(path, config), {
|
|
353
|
-
...config,
|
|
354
|
-
headers: {
|
|
355
|
-
...this.tokenHeader(),
|
|
356
|
-
...formHeader,
|
|
357
|
-
...config?.headers,
|
|
358
|
-
...this.permanentHeader,
|
|
359
|
-
},
|
|
360
|
-
});
|
|
361
|
-
break;
|
|
362
|
-
default:
|
|
363
|
-
return {
|
|
364
|
-
success: false,
|
|
365
|
-
data: null,
|
|
366
|
-
code: -1,
|
|
367
|
-
};
|
|
368
|
-
}
|
|
369
|
-
let error;
|
|
370
|
-
if (isErrorType(dat.data)) {
|
|
371
|
-
error = dat.data;
|
|
372
|
-
}
|
|
373
|
-
let x = false;
|
|
374
|
-
switch (dat.code) {
|
|
375
|
-
case 200:
|
|
376
|
-
case 201:
|
|
377
|
-
return {
|
|
378
|
-
success: true,
|
|
379
|
-
data: dat.data,
|
|
380
|
-
code: dat.code,
|
|
381
|
-
error,
|
|
382
|
-
headers: dat.headers,
|
|
383
|
-
};
|
|
384
|
-
case 498:
|
|
385
|
-
x = await this.reconnect();
|
|
386
|
-
if (x) {
|
|
387
|
-
this.onReconnect(this).catch((dx) => {
|
|
388
|
-
this.logger(dx);
|
|
389
|
-
});
|
|
390
|
-
return this.handle(type, path, body, config);
|
|
391
|
-
}
|
|
392
|
-
this.reconnect = async () => {
|
|
393
|
-
this.disconnected = true;
|
|
394
|
-
return false;
|
|
395
|
-
};
|
|
396
|
-
this.disconnected = true;
|
|
397
|
-
return {
|
|
398
|
-
success: false,
|
|
399
|
-
data: null,
|
|
400
|
-
code: dat.code,
|
|
401
|
-
error,
|
|
402
|
-
headers: dat.headers,
|
|
403
|
-
};
|
|
404
|
-
case 401:
|
|
405
|
-
this.logger('AUTH NOT VALID');
|
|
406
|
-
return {
|
|
407
|
-
success: false,
|
|
408
|
-
data: null,
|
|
409
|
-
code: dat.code,
|
|
410
|
-
error,
|
|
411
|
-
headers: dat.headers,
|
|
412
|
-
};
|
|
413
|
-
case 403:
|
|
414
|
-
return {
|
|
415
|
-
success: false,
|
|
416
|
-
data: null,
|
|
417
|
-
error,
|
|
418
|
-
code: dat.code,
|
|
419
|
-
headers: dat.headers,
|
|
420
|
-
};
|
|
421
|
-
default:
|
|
422
|
-
return {
|
|
423
|
-
success: false,
|
|
424
|
-
data: null,
|
|
425
|
-
error,
|
|
426
|
-
code: dat.code,
|
|
427
|
-
headers: dat.headers,
|
|
428
|
-
};
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
exports.default = BaseCon;
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
async function fcTransform(r) {
|
|
4
|
-
const res = await r;
|
|
5
|
-
const head = {};
|
|
6
|
-
res.headers.forEach((val, key) => {
|
|
7
|
-
head[key] = val;
|
|
8
|
-
});
|
|
9
|
-
let data = null;
|
|
10
|
-
if (head['content-type']?.includes('application/json')) {
|
|
11
|
-
data = await res.json();
|
|
12
|
-
}
|
|
13
|
-
else if (head['content-type']?.includes('form-data')) {
|
|
14
|
-
data = await res.formData();
|
|
15
|
-
}
|
|
16
|
-
else if (head['content-type']?.includes('octet-stream')) {
|
|
17
|
-
data = await res.arrayBuffer();
|
|
18
|
-
}
|
|
19
|
-
else if (head['content-type']?.includes('text/plain')) {
|
|
20
|
-
data = await res.text();
|
|
21
|
-
}
|
|
22
|
-
return {
|
|
23
|
-
code: res.status,
|
|
24
|
-
data,
|
|
25
|
-
headers: head,
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
function bodyTransform(r, headers) {
|
|
29
|
-
if (!r) {
|
|
30
|
-
return {
|
|
31
|
-
headers,
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
if (r instanceof FormData) {
|
|
35
|
-
return {
|
|
36
|
-
body: r,
|
|
37
|
-
headers: {
|
|
38
|
-
...headers,
|
|
39
|
-
},
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
return {
|
|
43
|
-
body: JSON.stringify(r),
|
|
44
|
-
headers: {
|
|
45
|
-
...headers,
|
|
46
|
-
'content-type': 'application/json',
|
|
47
|
-
},
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
const FetchCon = {
|
|
51
|
-
get: async (url, config) => {
|
|
52
|
-
return fcTransform(fetch(url, {
|
|
53
|
-
method: 'GET',
|
|
54
|
-
headers: {
|
|
55
|
-
accept: 'application/json, text/plain, */*',
|
|
56
|
-
...config?.headers,
|
|
57
|
-
},
|
|
58
|
-
}));
|
|
59
|
-
},
|
|
60
|
-
post: async (url, body, config) => {
|
|
61
|
-
return fcTransform(fetch(url, {
|
|
62
|
-
method: 'POST',
|
|
63
|
-
...bodyTransform(body, config?.headers),
|
|
64
|
-
}));
|
|
65
|
-
},
|
|
66
|
-
patch: async (url, body, config) => {
|
|
67
|
-
return fcTransform(fetch(url, {
|
|
68
|
-
method: 'PATCH',
|
|
69
|
-
...bodyTransform(body, config?.headers),
|
|
70
|
-
}));
|
|
71
|
-
},
|
|
72
|
-
delete: async (url, config) => {
|
|
73
|
-
return fcTransform(fetch(url, {
|
|
74
|
-
method: 'DELETE',
|
|
75
|
-
headers: config?.headers,
|
|
76
|
-
}));
|
|
77
|
-
},
|
|
78
|
-
};
|
|
79
|
-
exports.default = FetchCon;
|
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
-
};
|
|
38
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
const form_data_1 = __importDefault(require("form-data"));
|
|
40
|
-
const http = __importStar(require("http"));
|
|
41
|
-
const https = __importStar(require("https"));
|
|
42
|
-
function selectClient(url) {
|
|
43
|
-
if (url.startsWith('https')) {
|
|
44
|
-
return https;
|
|
45
|
-
}
|
|
46
|
-
return http;
|
|
47
|
-
}
|
|
48
|
-
async function fcTransform(message, raw) {
|
|
49
|
-
let data = null;
|
|
50
|
-
if (message.headers['content-type']?.includes('application/json')) {
|
|
51
|
-
data = JSON.parse(raw);
|
|
52
|
-
}
|
|
53
|
-
else if (message.headers['content-type']?.includes('form-data')) {
|
|
54
|
-
data = null; // await res.formData()
|
|
55
|
-
}
|
|
56
|
-
else if (message.headers['content-type']?.includes('octet-stream')) {
|
|
57
|
-
data = Buffer.from(raw);
|
|
58
|
-
}
|
|
59
|
-
else if (message.headers['content-type']?.startsWith('text/')) {
|
|
60
|
-
data = Buffer.from(raw).toString('utf8');
|
|
61
|
-
}
|
|
62
|
-
return {
|
|
63
|
-
code: message.statusCode || -1,
|
|
64
|
-
data,
|
|
65
|
-
headers: message.headers,
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
function bodyTransform(r, headers) {
|
|
69
|
-
if (!r) {
|
|
70
|
-
return {
|
|
71
|
-
headers: headers || {},
|
|
72
|
-
body: undefined,
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
if (r instanceof form_data_1.default) {
|
|
76
|
-
return {
|
|
77
|
-
body: r,
|
|
78
|
-
headers: {
|
|
79
|
-
...headers,
|
|
80
|
-
'content-type': 'multipart/form-data',
|
|
81
|
-
},
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
return {
|
|
85
|
-
body: JSON.stringify(r),
|
|
86
|
-
headers: {
|
|
87
|
-
...headers,
|
|
88
|
-
'content-type': 'application/json',
|
|
89
|
-
},
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
async function makeRequest(url, option, body, config) {
|
|
93
|
-
return new Promise((resolve) => {
|
|
94
|
-
let headers = config?.headers || {};
|
|
95
|
-
let transForm = null;
|
|
96
|
-
if (body) {
|
|
97
|
-
let safeHeaders;
|
|
98
|
-
if (option.headers &&
|
|
99
|
-
typeof option.headers === 'object' &&
|
|
100
|
-
!Array.isArray(option.headers)) {
|
|
101
|
-
safeHeaders = option.headers;
|
|
102
|
-
}
|
|
103
|
-
transForm = bodyTransform(body, safeHeaders);
|
|
104
|
-
headers = {
|
|
105
|
-
...headers,
|
|
106
|
-
...transForm.headers,
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
const req = selectClient(url)
|
|
110
|
-
.request(url, {
|
|
111
|
-
headers,
|
|
112
|
-
...option,
|
|
113
|
-
}, (res) => {
|
|
114
|
-
let data = '';
|
|
115
|
-
// A chunk of data has been received.
|
|
116
|
-
res.on('data', (chunk) => {
|
|
117
|
-
data += chunk;
|
|
118
|
-
});
|
|
119
|
-
// The whole response has been received. Print out the result.
|
|
120
|
-
res.on('end', () => {
|
|
121
|
-
resolve(fcTransform(res, data));
|
|
122
|
-
});
|
|
123
|
-
})
|
|
124
|
-
.on('error', (err) => {
|
|
125
|
-
console.log(`Error: ${err.message}`);
|
|
126
|
-
resolve({
|
|
127
|
-
code: -1,
|
|
128
|
-
data: null,
|
|
129
|
-
headers: {},
|
|
130
|
-
});
|
|
131
|
-
});
|
|
132
|
-
if (transForm && transForm.body) {
|
|
133
|
-
req.write(transForm.body);
|
|
134
|
-
}
|
|
135
|
-
req.end();
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
const NodeCon = {
|
|
139
|
-
get: async (url, config) => {
|
|
140
|
-
return makeRequest(url, {}, undefined, config);
|
|
141
|
-
},
|
|
142
|
-
post: async (url, body, config) => {
|
|
143
|
-
return makeRequest(url, {
|
|
144
|
-
method: 'POST',
|
|
145
|
-
}, body, config);
|
|
146
|
-
},
|
|
147
|
-
patch: async (url, body, config) => {
|
|
148
|
-
return makeRequest(url, {
|
|
149
|
-
method: 'PATCH',
|
|
150
|
-
}, body, config);
|
|
151
|
-
},
|
|
152
|
-
delete: async (url, config) => {
|
|
153
|
-
return makeRequest(url, {
|
|
154
|
-
method: 'DELETE',
|
|
155
|
-
}, undefined, config);
|
|
156
|
-
},
|
|
157
|
-
};
|
|
158
|
-
exports.default = NodeCon;
|