@grandlinex/swagger-mate 0.30.8
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/LICENSE +29 -0
- package/README.md +137 -0
- package/dist/cjs/PathHelp.d.ts +2 -0
- package/dist/cjs/PathHelp.js +47 -0
- package/dist/cjs/Swagger/Client/ClientUtil.d.ts +59 -0
- package/dist/cjs/Swagger/Client/ClientUtil.js +208 -0
- package/dist/cjs/Swagger/Client/FunctionProps.d.ts +4 -0
- package/dist/cjs/Swagger/Client/FunctionProps.js +60 -0
- package/dist/cjs/Swagger/Client/FunctionTemplate.d.ts +13 -0
- package/dist/cjs/Swagger/Client/FunctionTemplate.js +46 -0
- package/dist/cjs/Swagger/Client/InterfaceTemplate.d.ts +3 -0
- package/dist/cjs/Swagger/Client/InterfaceTemplate.js +14 -0
- package/dist/cjs/Swagger/Client/SwaggerClient.d.ts +4 -0
- package/dist/cjs/Swagger/Client/SwaggerClient.js +197 -0
- package/dist/cjs/Swagger/Meta/Swagger.d.ts +10 -0
- package/dist/cjs/Swagger/Meta/Swagger.js +40 -0
- package/dist/cjs/Swagger/Meta/SwaggerTypes.d.ts +107 -0
- package/dist/cjs/Swagger/Meta/SwaggerTypes.js +9 -0
- package/dist/cjs/Swagger/Meta/SwaggerTypesStatic.d.ts +4 -0
- package/dist/cjs/Swagger/Meta/SwaggerTypesStatic.js +3 -0
- package/dist/cjs/Swagger/Path/SPathUtil.d.ts +28 -0
- package/dist/cjs/Swagger/Path/SPathUtil.js +228 -0
- package/dist/cjs/Swagger/Path/SUtilMap.d.ts +192 -0
- package/dist/cjs/Swagger/Path/SUtilMap.js +194 -0
- package/dist/cjs/Swagger/SwaggerUtil.d.ts +12 -0
- package/dist/cjs/Swagger/SwaggerUtil.js +195 -0
- package/dist/cjs/Swagger/debug/BaseCon.d.ts +68 -0
- package/dist/cjs/Swagger/debug/BaseCon.js +277 -0
- package/dist/cjs/Swagger/debug/FetchCon.d.ts +3 -0
- package/dist/cjs/Swagger/debug/FetchCon.js +79 -0
- package/dist/cjs/Swagger/debug/NodeCon.d.ts +3 -0
- package/dist/cjs/Swagger/debug/NodeCon.js +142 -0
- package/dist/cjs/Swagger/debug/index.d.ts +5 -0
- package/dist/cjs/Swagger/debug/index.js +27 -0
- package/dist/cjs/cli.d.ts +5 -0
- package/dist/cjs/cli.js +89 -0
- package/dist/cjs/index.d.ts +8 -0
- package/dist/cjs/index.js +30 -0
- package/dist/cjs/package.json +3 -0
- package/dist/mjs/PathHelp.d.ts +2 -0
- package/dist/mjs/PathHelp.js +16 -0
- package/dist/mjs/Swagger/Client/ClientUtil.d.ts +59 -0
- package/dist/mjs/Swagger/Client/ClientUtil.js +195 -0
- package/dist/mjs/Swagger/Client/FunctionProps.d.ts +4 -0
- package/dist/mjs/Swagger/Client/FunctionProps.js +58 -0
- package/dist/mjs/Swagger/Client/FunctionTemplate.d.ts +13 -0
- package/dist/mjs/Swagger/Client/FunctionTemplate.js +40 -0
- package/dist/mjs/Swagger/Client/InterfaceTemplate.d.ts +3 -0
- package/dist/mjs/Swagger/Client/InterfaceTemplate.js +13 -0
- package/dist/mjs/Swagger/Client/SwaggerClient.d.ts +4 -0
- package/dist/mjs/Swagger/Client/SwaggerClient.js +168 -0
- package/dist/mjs/Swagger/Meta/Swagger.d.ts +10 -0
- package/dist/mjs/Swagger/Meta/Swagger.js +32 -0
- package/dist/mjs/Swagger/Meta/SwaggerTypes.d.ts +107 -0
- package/dist/mjs/Swagger/Meta/SwaggerTypes.js +5 -0
- package/dist/mjs/Swagger/Meta/SwaggerTypesStatic.d.ts +4 -0
- package/dist/mjs/Swagger/Meta/SwaggerTypesStatic.js +2 -0
- package/dist/mjs/Swagger/Path/SPathUtil.d.ts +28 -0
- package/dist/mjs/Swagger/Path/SPathUtil.js +222 -0
- package/dist/mjs/Swagger/Path/SUtilMap.d.ts +192 -0
- package/dist/mjs/Swagger/Path/SUtilMap.js +192 -0
- package/dist/mjs/Swagger/SwaggerUtil.d.ts +12 -0
- package/dist/mjs/Swagger/SwaggerUtil.js +166 -0
- package/dist/mjs/Swagger/debug/BaseCon.d.ts +68 -0
- package/dist/mjs/Swagger/debug/BaseCon.js +269 -0
- package/dist/mjs/Swagger/debug/FetchCon.d.ts +3 -0
- package/dist/mjs/Swagger/debug/FetchCon.js +77 -0
- package/dist/mjs/Swagger/debug/NodeCon.d.ts +3 -0
- package/dist/mjs/Swagger/debug/NodeCon.js +114 -0
- package/dist/mjs/Swagger/debug/index.d.ts +5 -0
- package/dist/mjs/Swagger/debug/index.js +5 -0
- package/dist/mjs/cli.d.ts +5 -0
- package/dist/mjs/cli.js +61 -0
- package/dist/mjs/index.d.ts +8 -0
- package/dist/mjs/index.js +8 -0
- package/dist/mjs/package.json +3 -0
- package/package.json +77 -0
- package/res/html/index.html +69 -0
- package/res/html/swagger-ui-bundle.js +3 -0
- package/res/html/swagger-ui-standalone-preset.js +3 -0
- package/res/html/swagger-ui.css +3 -0
- package/res/templates/class/ApiCon.ts +11 -0
- package/res/templates/class/AxiosCon.ts +49 -0
- package/res/templates/class/BaseCon.ts +377 -0
- package/res/templates/class/CApiCon.ts +11 -0
- package/res/templates/class/FetchCon.ts +93 -0
- package/res/templates/class/IApiCon.ts +10 -0
- package/res/templates/class/NodeCon.ts +164 -0
- package/res/templates/class/index.ts +18 -0
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* THIS FILE IS AUTOMATICALLY GENERATED
|
|
3
|
+
* DO NOT EDIT THIS FILE
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import FormData from 'form-data';
|
|
7
|
+
|
|
8
|
+
export type ErrorType = {
|
|
9
|
+
type: string;
|
|
10
|
+
global?: string[];
|
|
11
|
+
field?: { key: string; message: string }[];
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type HeaderType = string | string[] | number | undefined;
|
|
15
|
+
export type HandleRes<T> = {
|
|
16
|
+
success: boolean;
|
|
17
|
+
data: T | null;
|
|
18
|
+
code?: number;
|
|
19
|
+
error?: ErrorType;
|
|
20
|
+
headers?: Record<string, HeaderType>;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export type PathParam = { [key: string]: string | number | undefined };
|
|
24
|
+
export function isErrorType(x: any): x is ErrorType {
|
|
25
|
+
return x && typeof x === 'object' && x.type === 'error';
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface ConHandleConfig {
|
|
29
|
+
headers?: Record<string, string>;
|
|
30
|
+
param?: PathParam;
|
|
31
|
+
query?: PathParam;
|
|
32
|
+
}
|
|
33
|
+
export interface ConHandleResponse<T> {
|
|
34
|
+
code: number;
|
|
35
|
+
data: T | null;
|
|
36
|
+
headers: Record<string, HeaderType>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface ConHandle {
|
|
40
|
+
get<T>(url: string, config?: ConHandleConfig): Promise<ConHandleResponse<T>>;
|
|
41
|
+
post<T, J>(
|
|
42
|
+
url: string,
|
|
43
|
+
body?: J,
|
|
44
|
+
config?: ConHandleConfig
|
|
45
|
+
): Promise<ConHandleResponse<T>>;
|
|
46
|
+
patch<T, J>(
|
|
47
|
+
url: string,
|
|
48
|
+
body?: J,
|
|
49
|
+
config?: ConHandleConfig
|
|
50
|
+
): Promise<ConHandleResponse<T>>;
|
|
51
|
+
|
|
52
|
+
delete<T>(
|
|
53
|
+
url: string,
|
|
54
|
+
config?: ConHandleConfig
|
|
55
|
+
): Promise<ConHandleResponse<T>>;
|
|
56
|
+
}
|
|
57
|
+
export default class BaseCon {
|
|
58
|
+
api: string;
|
|
59
|
+
|
|
60
|
+
permanentHeader: undefined | Record<string, string>;
|
|
61
|
+
|
|
62
|
+
authorization: string | null;
|
|
63
|
+
|
|
64
|
+
disconnected: boolean;
|
|
65
|
+
|
|
66
|
+
failFlag: boolean;
|
|
67
|
+
|
|
68
|
+
logger: (arg: any) => void;
|
|
69
|
+
|
|
70
|
+
con: ConHandle;
|
|
71
|
+
|
|
72
|
+
reconnect: () => Promise<boolean>;
|
|
73
|
+
|
|
74
|
+
onReconnect: (con: BaseCon) => Promise<boolean>;
|
|
75
|
+
|
|
76
|
+
constructor(conf: {
|
|
77
|
+
con: ConHandle;
|
|
78
|
+
endpoint: string;
|
|
79
|
+
logger?: (arg: any) => void;
|
|
80
|
+
}) {
|
|
81
|
+
this.api = conf.endpoint;
|
|
82
|
+
this.logger = conf.logger || console.log;
|
|
83
|
+
this.disconnected = true;
|
|
84
|
+
this.authorization = null;
|
|
85
|
+
this.failFlag = false;
|
|
86
|
+
this.con = conf.con;
|
|
87
|
+
this.reconnect = async () => {
|
|
88
|
+
this.disconnected = true;
|
|
89
|
+
return false;
|
|
90
|
+
};
|
|
91
|
+
this.onReconnect = () => Promise.resolve(true);
|
|
92
|
+
this.handle = this.handle.bind(this);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Class helper functions
|
|
96
|
+
|
|
97
|
+
isConnected() {
|
|
98
|
+
return this.authorization !== null && !this.disconnected;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
token() {
|
|
102
|
+
return this.authorization || '';
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
p(path: string, config?: ConHandleConfig) {
|
|
106
|
+
let pp = path;
|
|
107
|
+
if (config?.param) {
|
|
108
|
+
const e = Object.keys(config.param);
|
|
109
|
+
for (const d of e) {
|
|
110
|
+
pp = pp.replace(`{${d}}`, `${config.param[d]}`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (config?.query) {
|
|
114
|
+
const e = Object.keys(config.query);
|
|
115
|
+
const slices = [];
|
|
116
|
+
for (const d of e) {
|
|
117
|
+
if (config.query[d]) {
|
|
118
|
+
slices.push(`${d}=${config.query[d]}`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (slices.length > 0) {
|
|
122
|
+
pp += `?${slices.join('&')}`;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return `${this.api}${pp}`;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async ping(): Promise<boolean> {
|
|
129
|
+
try {
|
|
130
|
+
const version = await this.con.get<{ api: number }>(this.p('/version'));
|
|
131
|
+
return version.data?.api === 1 && version.code === 200;
|
|
132
|
+
} catch (e) {
|
|
133
|
+
this.logger('ping failed');
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async test(email: string, password: string): Promise<boolean> {
|
|
139
|
+
const ping = await this.ping();
|
|
140
|
+
|
|
141
|
+
if (ping) {
|
|
142
|
+
try {
|
|
143
|
+
this.logger({ email, password });
|
|
144
|
+
const con = await this.con.post<
|
|
145
|
+
{ token: string },
|
|
146
|
+
{
|
|
147
|
+
username: string;
|
|
148
|
+
token: string;
|
|
149
|
+
}
|
|
150
|
+
>(this.p('/token'), {
|
|
151
|
+
username: email,
|
|
152
|
+
token: password,
|
|
153
|
+
});
|
|
154
|
+
return con.code === 200 || con.code === 201;
|
|
155
|
+
} catch (e) {
|
|
156
|
+
this.logger(e);
|
|
157
|
+
this.logger('cant connect to backend');
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
this.logger('test ping failed');
|
|
161
|
+
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async testToken(): Promise<boolean> {
|
|
166
|
+
const ping = await this.ping();
|
|
167
|
+
|
|
168
|
+
if (ping) {
|
|
169
|
+
try {
|
|
170
|
+
const con = await this.con.get<{ token: string }>(
|
|
171
|
+
this.p('/test/auth'),
|
|
172
|
+
{
|
|
173
|
+
headers: {
|
|
174
|
+
Authorization: this.token(),
|
|
175
|
+
},
|
|
176
|
+
}
|
|
177
|
+
);
|
|
178
|
+
return con.code === 200 || con.code === 201;
|
|
179
|
+
} catch (e) {
|
|
180
|
+
this.logger(e);
|
|
181
|
+
this.logger('cant connect to backend');
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
this.logger('test ping failed');
|
|
185
|
+
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
async connect(email: string, pw: string): Promise<boolean> {
|
|
190
|
+
const ping = await this.ping();
|
|
191
|
+
|
|
192
|
+
if (ping) {
|
|
193
|
+
try {
|
|
194
|
+
const token = await this.con.post<
|
|
195
|
+
{ token: string },
|
|
196
|
+
{
|
|
197
|
+
username: string;
|
|
198
|
+
token: string;
|
|
199
|
+
}
|
|
200
|
+
>(this.p('/token'), {
|
|
201
|
+
username: email,
|
|
202
|
+
token: pw,
|
|
203
|
+
});
|
|
204
|
+
// TODO check token
|
|
205
|
+
if (token.code === 200 || token.code === 201) {
|
|
206
|
+
this.authorization = `Bearer ${token.data?.token}`;
|
|
207
|
+
this.disconnected = false;
|
|
208
|
+
this.reconnect = async () => {
|
|
209
|
+
return (await this.connect(email, pw)) && (await this.testToken());
|
|
210
|
+
};
|
|
211
|
+
return true;
|
|
212
|
+
}
|
|
213
|
+
} catch (e) {
|
|
214
|
+
this.logger(e);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
this.logger('cant connect to backend');
|
|
218
|
+
this.authorization = null;
|
|
219
|
+
this.disconnected = true;
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Enable client before auth
|
|
225
|
+
*/
|
|
226
|
+
fakeEnableClient() {
|
|
227
|
+
this.authorization = 'DEBUG';
|
|
228
|
+
this.disconnected = false;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
async handle<T, J>(
|
|
232
|
+
type: 'POST' | 'GET' | 'PATCH' | 'DELETE',
|
|
233
|
+
path: string,
|
|
234
|
+
body?: J,
|
|
235
|
+
config?: ConHandleConfig
|
|
236
|
+
): Promise<HandleRes<T>> {
|
|
237
|
+
if (!this.authorization || this.disconnected) {
|
|
238
|
+
this.logger('Disconnected');
|
|
239
|
+
return {
|
|
240
|
+
success: false,
|
|
241
|
+
data: null,
|
|
242
|
+
code: -1,
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
let formHeader = null;
|
|
246
|
+
const cK = body as any;
|
|
247
|
+
if (
|
|
248
|
+
cK &&
|
|
249
|
+
(cK instanceof FormData || typeof cK?.getHeaders === 'function')
|
|
250
|
+
) {
|
|
251
|
+
formHeader = cK?.getHeaders?.() || {};
|
|
252
|
+
} else {
|
|
253
|
+
formHeader = {};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
let dat: ConHandleResponse<T> | null;
|
|
257
|
+
switch (type) {
|
|
258
|
+
case 'GET':
|
|
259
|
+
dat = await this.con.get<T>(this.p(path, config), {
|
|
260
|
+
...config,
|
|
261
|
+
headers: {
|
|
262
|
+
Authorization: this.token(),
|
|
263
|
+
...formHeader,
|
|
264
|
+
...config?.headers,
|
|
265
|
+
...this.permanentHeader,
|
|
266
|
+
},
|
|
267
|
+
});
|
|
268
|
+
break;
|
|
269
|
+
case 'POST':
|
|
270
|
+
dat = await this.con.post<T, J>(this.p(path, config), body, {
|
|
271
|
+
...config,
|
|
272
|
+
headers: {
|
|
273
|
+
Authorization: this.token(),
|
|
274
|
+
...formHeader,
|
|
275
|
+
...config?.headers,
|
|
276
|
+
...this.permanentHeader,
|
|
277
|
+
},
|
|
278
|
+
});
|
|
279
|
+
break;
|
|
280
|
+
case 'PATCH':
|
|
281
|
+
dat = await this.con.patch<T, J>(this.p(path, config), body, {
|
|
282
|
+
...config,
|
|
283
|
+
headers: {
|
|
284
|
+
Authorization: this.token(),
|
|
285
|
+
...formHeader,
|
|
286
|
+
...config?.headers,
|
|
287
|
+
...this.permanentHeader,
|
|
288
|
+
},
|
|
289
|
+
});
|
|
290
|
+
break;
|
|
291
|
+
case 'DELETE':
|
|
292
|
+
dat = await this.con.delete<T>(this.p(path, config), {
|
|
293
|
+
...config,
|
|
294
|
+
headers: {
|
|
295
|
+
Authorization: this.token(),
|
|
296
|
+
...formHeader,
|
|
297
|
+
...config?.headers,
|
|
298
|
+
...this.permanentHeader,
|
|
299
|
+
},
|
|
300
|
+
});
|
|
301
|
+
break;
|
|
302
|
+
default:
|
|
303
|
+
return {
|
|
304
|
+
success: false,
|
|
305
|
+
data: null,
|
|
306
|
+
code: -1,
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
let error: ErrorType | undefined;
|
|
311
|
+
if (isErrorType(dat.data)) {
|
|
312
|
+
error = dat.data;
|
|
313
|
+
}
|
|
314
|
+
let x = false;
|
|
315
|
+
switch (dat.code) {
|
|
316
|
+
case 200:
|
|
317
|
+
case 201:
|
|
318
|
+
return {
|
|
319
|
+
success: true,
|
|
320
|
+
data: dat.data,
|
|
321
|
+
code: dat.code,
|
|
322
|
+
error,
|
|
323
|
+
headers: dat.headers,
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
case 498:
|
|
327
|
+
x = await this.reconnect();
|
|
328
|
+
if (x) {
|
|
329
|
+
this.onReconnect(this).catch((dx) => {
|
|
330
|
+
this.logger(dx);
|
|
331
|
+
});
|
|
332
|
+
return this.handle(type, path, body, config);
|
|
333
|
+
}
|
|
334
|
+
this.reconnect = async () => {
|
|
335
|
+
this.disconnected = true;
|
|
336
|
+
return false;
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
this.disconnected = true;
|
|
340
|
+
return {
|
|
341
|
+
success: false,
|
|
342
|
+
data: null,
|
|
343
|
+
code: dat.code,
|
|
344
|
+
error,
|
|
345
|
+
headers: dat.headers,
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
case 401:
|
|
349
|
+
this.logger('AUTH NOT VALID');
|
|
350
|
+
this.disconnected = true;
|
|
351
|
+
return {
|
|
352
|
+
success: false,
|
|
353
|
+
data: null,
|
|
354
|
+
code: dat.code,
|
|
355
|
+
error,
|
|
356
|
+
headers: dat.headers,
|
|
357
|
+
};
|
|
358
|
+
case 403:
|
|
359
|
+
return {
|
|
360
|
+
success: false,
|
|
361
|
+
data: null,
|
|
362
|
+
error,
|
|
363
|
+
code: dat.code,
|
|
364
|
+
headers: dat.headers,
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
default:
|
|
368
|
+
return {
|
|
369
|
+
success: false,
|
|
370
|
+
data: null,
|
|
371
|
+
error,
|
|
372
|
+
code: dat.code,
|
|
373
|
+
headers: dat.headers,
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* THIS FILE IS AUTOMATICALLY GENERATED
|
|
3
|
+
* DO NOT EDIT THIS FILE
|
|
4
|
+
*/
|
|
5
|
+
import * as TX from './ApiTypes';
|
|
6
|
+
import { HandleRes } from './BaseCon';
|
|
7
|
+
import IApiCon from './IApiCon';
|
|
8
|
+
|
|
9
|
+
export default abstract class CApiCon implements IApiCon{
|
|
10
|
+
/* REPLACE_ME */
|
|
11
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { ConHandle, ConHandleResponse } from './BaseCon.js';
|
|
2
|
+
|
|
3
|
+
async function fcTransform<T>(
|
|
4
|
+
r: Promise<Response>
|
|
5
|
+
): Promise<ConHandleResponse<T>> {
|
|
6
|
+
const res = await r;
|
|
7
|
+
const head: Record<string, any> = {};
|
|
8
|
+
|
|
9
|
+
res.headers.forEach((val: any, key: any) => {
|
|
10
|
+
head[key] = val;
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
let data = null;
|
|
14
|
+
|
|
15
|
+
if (head['content-type']?.includes('application/json')) {
|
|
16
|
+
data = await res.json();
|
|
17
|
+
} else if (head['content-type']?.includes('form-data')) {
|
|
18
|
+
data = await res.formData();
|
|
19
|
+
} else if (head['content-type']?.includes('octet-stream')) {
|
|
20
|
+
data = await res.arrayBuffer();
|
|
21
|
+
} else if (head['content-type']?.includes('text/plain')) {
|
|
22
|
+
data = await res.text();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
code: res.status,
|
|
27
|
+
data,
|
|
28
|
+
headers: head,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function bodyTransform(r: any, headers?: Record<string, string>) {
|
|
33
|
+
if (!r) {
|
|
34
|
+
return {
|
|
35
|
+
headers,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
if (r instanceof FormData) {
|
|
39
|
+
return {
|
|
40
|
+
body: r,
|
|
41
|
+
headers: {
|
|
42
|
+
...headers,
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
body: JSON.stringify(r),
|
|
48
|
+
headers: {
|
|
49
|
+
...headers,
|
|
50
|
+
'content-type': 'application/json',
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const FetchCon: ConHandle = {
|
|
56
|
+
get: async (url, config) => {
|
|
57
|
+
return fcTransform(
|
|
58
|
+
fetch(url, {
|
|
59
|
+
method: 'GET',
|
|
60
|
+
headers: {
|
|
61
|
+
accept: 'application/json, text/plain, */*',
|
|
62
|
+
...config?.headers,
|
|
63
|
+
},
|
|
64
|
+
})
|
|
65
|
+
);
|
|
66
|
+
},
|
|
67
|
+
post: async (url, body, config) => {
|
|
68
|
+
return fcTransform(
|
|
69
|
+
fetch(url, {
|
|
70
|
+
method: 'POST',
|
|
71
|
+
...bodyTransform(body, config?.headers),
|
|
72
|
+
})
|
|
73
|
+
);
|
|
74
|
+
},
|
|
75
|
+
patch: async (url, body, config) => {
|
|
76
|
+
return fcTransform(
|
|
77
|
+
fetch(url, {
|
|
78
|
+
method: 'PATCH',
|
|
79
|
+
...bodyTransform(body, config?.headers),
|
|
80
|
+
})
|
|
81
|
+
);
|
|
82
|
+
},
|
|
83
|
+
delete: async (url, config) => {
|
|
84
|
+
return fcTransform(
|
|
85
|
+
fetch(url, {
|
|
86
|
+
method: 'DELETE',
|
|
87
|
+
headers: config?.headers,
|
|
88
|
+
})
|
|
89
|
+
);
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export default FetchCon;
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import FormData from 'form-data';
|
|
2
|
+
import * as http from 'http';
|
|
3
|
+
import * as https from 'https';
|
|
4
|
+
import { IncomingMessage } from 'http';
|
|
5
|
+
import { RequestOptions } from 'https';
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
ConHandle,
|
|
9
|
+
ConHandleConfig,
|
|
10
|
+
ConHandleResponse,
|
|
11
|
+
HeaderType,
|
|
12
|
+
} from './BaseCon.js';
|
|
13
|
+
|
|
14
|
+
function selectClient(url: string) {
|
|
15
|
+
if (url.startsWith('https')) {
|
|
16
|
+
return https;
|
|
17
|
+
}
|
|
18
|
+
return http;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async function fcTransform<T>(
|
|
22
|
+
message: IncomingMessage,
|
|
23
|
+
raw: any
|
|
24
|
+
): Promise<ConHandleResponse<T>> {
|
|
25
|
+
let data = null;
|
|
26
|
+
|
|
27
|
+
if (message.headers['content-type']?.includes('application/json')) {
|
|
28
|
+
data = JSON.parse(raw);
|
|
29
|
+
} else if (message.headers['content-type']?.includes('form-data')) {
|
|
30
|
+
data = null; // await res.formData()
|
|
31
|
+
} else if (message.headers['content-type']?.includes('octet-stream')) {
|
|
32
|
+
data = Buffer.from(raw);
|
|
33
|
+
} else if (message.headers['content-type']?.startsWith('text/')) {
|
|
34
|
+
data = Buffer.from(raw).toString('utf8');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
code: message.statusCode || -1,
|
|
39
|
+
data,
|
|
40
|
+
headers: message.headers,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function bodyTransform(
|
|
44
|
+
r: any,
|
|
45
|
+
headers?: Record<string, HeaderType>
|
|
46
|
+
): {
|
|
47
|
+
headers: Record<string, HeaderType>;
|
|
48
|
+
body: any;
|
|
49
|
+
} {
|
|
50
|
+
if (!r) {
|
|
51
|
+
return {
|
|
52
|
+
headers: headers || {},
|
|
53
|
+
body: undefined,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
if (r instanceof FormData) {
|
|
57
|
+
return {
|
|
58
|
+
body: r,
|
|
59
|
+
headers: {
|
|
60
|
+
...headers,
|
|
61
|
+
'content-type': 'multipart/form-data',
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
body: JSON.stringify(r),
|
|
67
|
+
headers: {
|
|
68
|
+
...headers,
|
|
69
|
+
'content-type': 'application/json',
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
async function makeRequest<T, J>(
|
|
74
|
+
url: string,
|
|
75
|
+
option: RequestOptions,
|
|
76
|
+
body?: J,
|
|
77
|
+
config?: ConHandleConfig
|
|
78
|
+
): Promise<ConHandleResponse<T>> {
|
|
79
|
+
return new Promise((resolve) => {
|
|
80
|
+
let headers: Record<string, any> = config?.headers || {};
|
|
81
|
+
let transForm = null;
|
|
82
|
+
if (body) {
|
|
83
|
+
transForm = bodyTransform(body, option.headers);
|
|
84
|
+
headers = {
|
|
85
|
+
...headers,
|
|
86
|
+
...transForm.headers,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const req = selectClient(url)
|
|
91
|
+
.request(
|
|
92
|
+
url,
|
|
93
|
+
{
|
|
94
|
+
headers,
|
|
95
|
+
...option,
|
|
96
|
+
},
|
|
97
|
+
(res) => {
|
|
98
|
+
let data = '';
|
|
99
|
+
|
|
100
|
+
// A chunk of data has been received.
|
|
101
|
+
res.on('data', (chunk) => {
|
|
102
|
+
data += chunk;
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// The whole response has been received. Print out the result.
|
|
106
|
+
res.on('end', () => {
|
|
107
|
+
resolve(fcTransform(res, data));
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
)
|
|
111
|
+
.on('error', (err) => {
|
|
112
|
+
console.log(`Error: ${err.message}`);
|
|
113
|
+
|
|
114
|
+
resolve({
|
|
115
|
+
code: -1,
|
|
116
|
+
data: null,
|
|
117
|
+
headers: {},
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
if (transForm && transForm.body) {
|
|
122
|
+
req.write(transForm.body);
|
|
123
|
+
}
|
|
124
|
+
req.end();
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const NodeCon: ConHandle = {
|
|
129
|
+
get: async (url, config) => {
|
|
130
|
+
return makeRequest(url, {}, undefined, config);
|
|
131
|
+
},
|
|
132
|
+
post: async (url, body, config) => {
|
|
133
|
+
return makeRequest(
|
|
134
|
+
url,
|
|
135
|
+
{
|
|
136
|
+
method: 'POST',
|
|
137
|
+
},
|
|
138
|
+
body,
|
|
139
|
+
config
|
|
140
|
+
);
|
|
141
|
+
},
|
|
142
|
+
patch: async (url, body, config) => {
|
|
143
|
+
return makeRequest(
|
|
144
|
+
url,
|
|
145
|
+
{
|
|
146
|
+
method: 'PATCH',
|
|
147
|
+
},
|
|
148
|
+
body,
|
|
149
|
+
config
|
|
150
|
+
);
|
|
151
|
+
},
|
|
152
|
+
delete: async (url, config) => {
|
|
153
|
+
return makeRequest(
|
|
154
|
+
url,
|
|
155
|
+
{
|
|
156
|
+
method: 'DELETE',
|
|
157
|
+
},
|
|
158
|
+
undefined,
|
|
159
|
+
config
|
|
160
|
+
);
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
export default NodeCon;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* THIS FILE IS AUTOMATICALLY GENERATED
|
|
3
|
+
* DO NOT EDIT THIS FILE
|
|
4
|
+
*/
|
|
5
|
+
import FormData from 'form-data';
|
|
6
|
+
import ApiCon from './ApiCon';
|
|
7
|
+
import BaseCon from './BaseCon';
|
|
8
|
+
import CApiCon from './CApiCon';
|
|
9
|
+
import IApiCon from './IApiCon';
|
|
10
|
+
|
|
11
|
+
import NodeCon from './NodeCon';
|
|
12
|
+
import FetchCon from './FetchCon';
|
|
13
|
+
import AxiosCon from './AxiosCon';
|
|
14
|
+
|
|
15
|
+
export * from './BaseCon';
|
|
16
|
+
export * from './ApiTypes';
|
|
17
|
+
export { ApiCon, BaseCon, CApiCon, IApiCon, FormData , NodeCon, FetchCon, AxiosCon };
|
|
18
|
+
export default ApiCon;
|