@goast/typescript 0.8.0-beta.1 → 0.8.0-beta.2
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 +21 -21
- package/README.md +52 -52
- package/assets/client/angular/angular-service.utils.ts +82 -82
- package/assets/client/angular/request-builder.ts +377 -377
- package/assets/client/fetch/fetch-client.utils.ts +52 -52
- package/assets/client/k6/request-builder.js +448 -448
- package/assets/client/k6/request-builder.ts +366 -366
- package/assets/stubs/easy-network-stub/easy-network-stub.utils.ts +257 -257
- package/esm/src/generators/models/model-generator.d.ts +22 -0
- package/esm/src/generators/models/model-generator.d.ts.map +1 -1
- package/esm/src/generators/models/model-generator.js +37 -5
- package/esm/src/generators/services/angular-services/angular-service-generator.js +9 -9
- package/esm/src/generators/services/angular-services/angular-services-generator.js +2 -2
- package/esm/src/generators/services/easy-network-stub/easy-network-stub-generator.js +9 -9
- package/esm/src/generators/services/fetch-clients/fetch-client-generator.js +4 -4
- package/esm/src/generators/services/k6-clients/k6-client-generator.d.ts.map +1 -1
- package/esm/src/generators/services/k6-clients/k6-client-generator.js +23 -13
- package/esm/src/import-collection.d.ts.map +1 -1
- package/esm/src/import-collection.js +2 -2
- package/package.json +2 -2
- package/script/src/generators/models/model-generator.d.ts +22 -0
- package/script/src/generators/models/model-generator.d.ts.map +1 -1
- package/script/src/generators/models/model-generator.js +37 -5
- package/script/src/generators/services/angular-services/angular-service-generator.js +9 -9
- package/script/src/generators/services/angular-services/angular-services-generator.js +2 -2
- package/script/src/generators/services/easy-network-stub/easy-network-stub-generator.js +9 -9
- package/script/src/generators/services/fetch-clients/fetch-client-generator.js +4 -4
- package/script/src/generators/services/k6-clients/k6-client-generator.d.ts.map +1 -1
- package/script/src/generators/services/k6-clients/k6-client-generator.js +23 -13
- package/script/src/import-collection.d.ts.map +1 -1
- package/script/src/import-collection.js +1 -1
|
@@ -1,366 +1,366 @@
|
|
|
1
|
-
// @deno-types="npm:@types/k6/http"
|
|
2
|
-
import http, { Params, Response } from 'k6/http';
|
|
3
|
-
// @ts-ignore
|
|
4
|
-
import { FormData } from 'https://jslib.k6.io/formdata/0.0.2/index.js';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Defines the options for appending a parameter
|
|
8
|
-
*/
|
|
9
|
-
type ParameterOptions = {
|
|
10
|
-
style?: string;
|
|
11
|
-
explode?: boolean;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
type BuildOptions = {
|
|
15
|
-
accept?: string;
|
|
16
|
-
params?: Params;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Base class for a parameter
|
|
21
|
-
*/
|
|
22
|
-
abstract class Parameter {
|
|
23
|
-
public readonly name: string;
|
|
24
|
-
public readonly value: any;
|
|
25
|
-
public readonly options: ParameterOptions;
|
|
26
|
-
|
|
27
|
-
constructor(name: string, value: any, options: ParameterOptions, defaultStyle: string, defaultExplode: boolean) {
|
|
28
|
-
this.name = name;
|
|
29
|
-
this.value = value;
|
|
30
|
-
this.options = options || {};
|
|
31
|
-
if (this.options.style === null || this.options.style === undefined) {
|
|
32
|
-
this.options.style = defaultStyle;
|
|
33
|
-
}
|
|
34
|
-
if (this.options.explode === null || this.options.explode === undefined) {
|
|
35
|
-
this.options.explode = defaultExplode;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
public serializeValue(value: any, separator = ','): string {
|
|
40
|
-
if (value === null || value === undefined) {
|
|
41
|
-
return '';
|
|
42
|
-
} else if (value instanceof Array) {
|
|
43
|
-
return value
|
|
44
|
-
.map((v) => this.serializeValue(v).split(separator).join(encodeURIComponent(separator)))
|
|
45
|
-
.join(separator);
|
|
46
|
-
} else if (typeof value === 'object') {
|
|
47
|
-
const array: string[] = [];
|
|
48
|
-
for (const key of Object.keys(value)) {
|
|
49
|
-
let propVal = value[key];
|
|
50
|
-
if (propVal !== null && propVal !== undefined) {
|
|
51
|
-
propVal = this.serializeValue(propVal).split(separator).join(encodeURIComponent(separator));
|
|
52
|
-
if (this.options.explode) {
|
|
53
|
-
array.push(`${key}=${propVal}`);
|
|
54
|
-
} else {
|
|
55
|
-
array.push(key);
|
|
56
|
-
array.push(propVal);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
return array.join(separator);
|
|
61
|
-
} else {
|
|
62
|
-
return String(value);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* A parameter in the operation path
|
|
69
|
-
*/
|
|
70
|
-
class PathParameter extends Parameter {
|
|
71
|
-
constructor(name: string, value: any, options: ParameterOptions) {
|
|
72
|
-
super(name, value, options, 'simple', false);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
public append(path: string): string {
|
|
76
|
-
let value = this.value;
|
|
77
|
-
if (value === null || value === undefined) {
|
|
78
|
-
value = '';
|
|
79
|
-
}
|
|
80
|
-
let prefix = this.options.style === 'label' ? '.' : '';
|
|
81
|
-
const separator = this.options.explode ? (prefix === '' ? ',' : prefix) : ',';
|
|
82
|
-
let alreadySerialized = false;
|
|
83
|
-
if (this.options.style === 'matrix') {
|
|
84
|
-
// The parameter name is just used as prefix, except in some cases...
|
|
85
|
-
prefix = `;${this.name}=`;
|
|
86
|
-
if (this.options.explode && typeof value === 'object') {
|
|
87
|
-
prefix = ';';
|
|
88
|
-
if (value instanceof Array) {
|
|
89
|
-
// For arrays we have to repeat the name for each element
|
|
90
|
-
value = value.map((v) => `${this.name}=${this.serializeValue(v, ';')}`);
|
|
91
|
-
value = value.join(';');
|
|
92
|
-
alreadySerialized = true;
|
|
93
|
-
} else {
|
|
94
|
-
// For objects we have to put each the key / value pairs
|
|
95
|
-
value = this.serializeValue(value, ';');
|
|
96
|
-
alreadySerialized = true;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
value = prefix + (alreadySerialized ? value : this.serializeValue(value, separator));
|
|
101
|
-
// Replace both the plain variable and the corresponding variant taking in the prefix and explode into account
|
|
102
|
-
path = path.replace(`{${this.name}}`, value);
|
|
103
|
-
path = path.replace(`{${prefix}${this.name}${this.options.explode ? '*' : ''}}`, value);
|
|
104
|
-
return path;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
public override serializeValue(value: any, separator = ','): string {
|
|
108
|
-
let result = typeof value === 'string' ? encodeURIComponent(value) : super.serializeValue(value, separator);
|
|
109
|
-
result = result.replace(/%3D/g, '=');
|
|
110
|
-
result = result.replace(/%3B/g, ';');
|
|
111
|
-
result = result.replace(/%2C/g, ',');
|
|
112
|
-
return result;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* A parameter in the query
|
|
118
|
-
*/
|
|
119
|
-
class QueryParameter extends Parameter {
|
|
120
|
-
constructor(name: string, value: any, options: ParameterOptions) {
|
|
121
|
-
super(name, value, options, 'form', true);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
public append(params: string): string {
|
|
125
|
-
if (this.value instanceof Array) {
|
|
126
|
-
// Array serialization
|
|
127
|
-
if (this.options.explode) {
|
|
128
|
-
for (const v of this.value) {
|
|
129
|
-
params = `${params}${params ? '&' : '?'}${encodeURIComponent(this.name)}=${this.serializeValue(v)}`;
|
|
130
|
-
}
|
|
131
|
-
} else {
|
|
132
|
-
const separator = this.options.style === 'spaceDelimited'
|
|
133
|
-
? ' '
|
|
134
|
-
: this.options.style === 'pipeDelimited'
|
|
135
|
-
? '|'
|
|
136
|
-
: ',';
|
|
137
|
-
return `${params}${params ? '&' : '?'}${encodeURIComponent(this.name)}=${
|
|
138
|
-
this.serializeValue(this.value, separator)
|
|
139
|
-
}`;
|
|
140
|
-
}
|
|
141
|
-
} else if (this.value !== null && typeof this.value === 'object') {
|
|
142
|
-
// Object serialization
|
|
143
|
-
if (this.options.style === 'deepObject') {
|
|
144
|
-
// Append a parameter for each key, in the form `name[key]`
|
|
145
|
-
for (const key of Object.keys(this.value)) {
|
|
146
|
-
const propVal = this.value[key];
|
|
147
|
-
if (propVal !== null && propVal !== undefined) {
|
|
148
|
-
params = `${params}${params ? '&' : '?'}${encodeURIComponent(this.name)}[${
|
|
149
|
-
encodeURIComponent(
|
|
150
|
-
key,
|
|
151
|
-
)
|
|
152
|
-
}]=${this.serializeValue(propVal)}`;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
} else if (this.options.explode) {
|
|
156
|
-
// Append a parameter for each key without using the parameter name
|
|
157
|
-
for (const key of Object.keys(this.value)) {
|
|
158
|
-
const propVal = this.value[key];
|
|
159
|
-
if (propVal !== null && propVal !== undefined) {
|
|
160
|
-
params = `${params}${params ? '&' : '?'}${encodeURIComponent(key)}=${this.serializeValue(propVal)}`;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
} else {
|
|
164
|
-
// Append a single parameter whose values are a comma-separated list of key,value,key,value...
|
|
165
|
-
const array: unknown[] = [];
|
|
166
|
-
for (const key of Object.keys(this.value)) {
|
|
167
|
-
const propVal = this.value[key];
|
|
168
|
-
if (propVal !== null && propVal !== undefined) {
|
|
169
|
-
array.push(key);
|
|
170
|
-
array.push(propVal);
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
params = `${params}${params ? '&' : '?'}${encodeURIComponent(this.name)}=${this.serializeValue(array)}`;
|
|
174
|
-
}
|
|
175
|
-
} else if (this.value !== null && this.value !== undefined) {
|
|
176
|
-
// Plain value
|
|
177
|
-
params = `${params}${params ? '&' : '?'}${encodeURIComponent(this.name)}=${this.serializeValue(this.value)}`;
|
|
178
|
-
}
|
|
179
|
-
return params;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* A parameter in the HTTP request header
|
|
185
|
-
*/
|
|
186
|
-
class HeaderParameter extends Parameter {
|
|
187
|
-
constructor(name: string, value: any, options: ParameterOptions) {
|
|
188
|
-
super(name, value, options, 'simple', false);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
public append(headers: Record<string, string>): Record<string, string> {
|
|
192
|
-
if (this.value !== null && this.value !== undefined) {
|
|
193
|
-
if (this.value instanceof Array) {
|
|
194
|
-
throw new Error('Array header values are not supported');
|
|
195
|
-
} else {
|
|
196
|
-
headers[this.name] = this.serializeValue(this.value);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
return headers;
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* Helper to build http requests from parameters
|
|
205
|
-
*/
|
|
206
|
-
export class RequestBuilder {
|
|
207
|
-
private readonly _path: Map<string, PathParameter>;
|
|
208
|
-
private readonly _query: Map<string, QueryParameter>;
|
|
209
|
-
private readonly _header: Map<string, HeaderParameter>;
|
|
210
|
-
private _bodyContent: any;
|
|
211
|
-
private _bodyContentType: string | undefined;
|
|
212
|
-
|
|
213
|
-
public readonly rootUrl: string;
|
|
214
|
-
public readonly operationPath: string;
|
|
215
|
-
public readonly method: string;
|
|
216
|
-
|
|
217
|
-
constructor(rootUrl: string, operationPath: string, method: string) {
|
|
218
|
-
this.rootUrl = rootUrl;
|
|
219
|
-
this.operationPath = operationPath;
|
|
220
|
-
this.method = method;
|
|
221
|
-
this._path = new Map();
|
|
222
|
-
this._query = new Map();
|
|
223
|
-
this._header = new Map();
|
|
224
|
-
this._bodyContent = null;
|
|
225
|
-
this._bodyContentType = undefined;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* Sets a path parameter
|
|
230
|
-
*/
|
|
231
|
-
public path(name: string, value: any, options: ParameterOptions) {
|
|
232
|
-
this._path.set(name, new PathParameter(name, value, options || {}));
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
/**
|
|
236
|
-
* Sets a query parameter
|
|
237
|
-
*/
|
|
238
|
-
public query(name: string, value: any, options: ParameterOptions) {
|
|
239
|
-
this._query.set(name, new QueryParameter(name, value, options || {}));
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
/**
|
|
243
|
-
* Sets a header parameter
|
|
244
|
-
*/
|
|
245
|
-
public header(name: string, value: any, options: ParameterOptions) {
|
|
246
|
-
this._header.set(name, new HeaderParameter(name, value, options || {}));
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
/**
|
|
250
|
-
* Sets the body content, along with the content type
|
|
251
|
-
*/
|
|
252
|
-
public body(value: any, contentType = 'application/json') {
|
|
253
|
-
this._bodyContentType = contentType;
|
|
254
|
-
if (this._bodyContentType === 'application/x-www-form-urlencoded' && value !== null && typeof value === 'object') {
|
|
255
|
-
// Handle URL-encoded data
|
|
256
|
-
const pairs: [string, string][] = [];
|
|
257
|
-
for (const key of Object.keys(value)) {
|
|
258
|
-
let val = value[key];
|
|
259
|
-
if (!(val instanceof Array)) {
|
|
260
|
-
val = [val];
|
|
261
|
-
}
|
|
262
|
-
for (const v of val) {
|
|
263
|
-
const formValue = this.formDataValue(v);
|
|
264
|
-
if (formValue !== null) {
|
|
265
|
-
pairs.push([key, formValue]);
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
this._bodyContent = pairs.map((p) => `${encodeURIComponent(p[0])}=${encodeURIComponent(p[1])}`).join('&');
|
|
270
|
-
} else if (this._bodyContentType === 'multipart/form-data' && value !== null && typeof value === 'object') {
|
|
271
|
-
const fd = new FormData();
|
|
272
|
-
for (const key in value) {
|
|
273
|
-
fd.append(key, this.formDataValue(value[key]));
|
|
274
|
-
}
|
|
275
|
-
this._bodyContent = fd.body();
|
|
276
|
-
this._bodyContentType = 'multipart/form-data; boundary=' + fd.boundary;
|
|
277
|
-
} else if (this._bodyContentType.indexOf('json') !== -1 && value !== null && typeof value === 'object') {
|
|
278
|
-
this._bodyContent = JSON.stringify(value);
|
|
279
|
-
} else {
|
|
280
|
-
// The body is the plain content
|
|
281
|
-
this._bodyContent = value;
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
private formDataValue(value: any): any {
|
|
286
|
-
if (value === null || value === undefined) {
|
|
287
|
-
return null;
|
|
288
|
-
}
|
|
289
|
-
if (this.isFile(value)) {
|
|
290
|
-
return value;
|
|
291
|
-
}
|
|
292
|
-
return { data: JSON.stringify(value), content_type: 'application/json' };
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
private isFile(value: any): value is http.FileData {
|
|
296
|
-
return (
|
|
297
|
-
typeof value === 'object' &&
|
|
298
|
-
value !== null &&
|
|
299
|
-
'data' in value &&
|
|
300
|
-
(typeof value.data === 'string' || value.data instanceof ArrayBuffer) &&
|
|
301
|
-
(!('filename' in value) || typeof value.filename === 'string') &&
|
|
302
|
-
(!('content_type' in value) || typeof value.content_type === 'string') &&
|
|
303
|
-
Object.keys(value).every((key) => key === 'data' || key === 'filename' || key === 'content_type')
|
|
304
|
-
);
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
/**
|
|
308
|
-
* Builds the request with the current set parameters
|
|
309
|
-
*/
|
|
310
|
-
public build(options?: BuildOptions): Response {
|
|
311
|
-
// Perform the request
|
|
312
|
-
return http.request(...this.buildRequestParams(options));
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
/**
|
|
316
|
-
* Builds the request with the current set parameters
|
|
317
|
-
*/
|
|
318
|
-
public buildAsync(options?: BuildOptions): Promise<Response> {
|
|
319
|
-
// Perform the request
|
|
320
|
-
return http.asyncRequest(...this.buildRequestParams(options));
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
private buildRequestParams(options?: BuildOptions): Parameters<typeof http.request> {
|
|
324
|
-
options = options || {};
|
|
325
|
-
|
|
326
|
-
// Path parameters
|
|
327
|
-
let path = this.operationPath;
|
|
328
|
-
for (const pathParam of this._path.values()) {
|
|
329
|
-
path = pathParam.append(path);
|
|
330
|
-
}
|
|
331
|
-
const url = this.rootUrl + path;
|
|
332
|
-
|
|
333
|
-
// Query parameters
|
|
334
|
-
let httpParams = '';
|
|
335
|
-
for (const queryParam of this._query.values()) {
|
|
336
|
-
httpParams = queryParam.append(httpParams);
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
// Header parameters
|
|
340
|
-
let httpHeaders: Record<string, string> = {};
|
|
341
|
-
if (options.accept) {
|
|
342
|
-
httpHeaders['Accept'] = options.accept;
|
|
343
|
-
}
|
|
344
|
-
for (const headerParam of this._header.values()) {
|
|
345
|
-
httpHeaders = headerParam.append(httpHeaders);
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
// Request content headers
|
|
349
|
-
if (this._bodyContentType) {
|
|
350
|
-
httpHeaders['Content-Type'] = this._bodyContentType;
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
return [
|
|
354
|
-
this.method.toUpperCase(),
|
|
355
|
-
url + httpParams,
|
|
356
|
-
this._bodyContent,
|
|
357
|
-
Object.assign({}, options.params, {
|
|
358
|
-
headers: Object.assign({}, httpHeaders, options.params ? options.params.headers : {}),
|
|
359
|
-
tags: {
|
|
360
|
-
name: this.operationPath,
|
|
361
|
-
...options.params?.tags,
|
|
362
|
-
},
|
|
363
|
-
}),
|
|
364
|
-
];
|
|
365
|
-
}
|
|
366
|
-
}
|
|
1
|
+
// @deno-types="npm:@types/k6/http"
|
|
2
|
+
import http, { Params, Response } from 'k6/http';
|
|
3
|
+
// @ts-ignore
|
|
4
|
+
import { FormData } from 'https://jslib.k6.io/formdata/0.0.2/index.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Defines the options for appending a parameter
|
|
8
|
+
*/
|
|
9
|
+
type ParameterOptions = {
|
|
10
|
+
style?: string;
|
|
11
|
+
explode?: boolean;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
type BuildOptions = {
|
|
15
|
+
accept?: string;
|
|
16
|
+
params?: Params;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Base class for a parameter
|
|
21
|
+
*/
|
|
22
|
+
abstract class Parameter {
|
|
23
|
+
public readonly name: string;
|
|
24
|
+
public readonly value: any;
|
|
25
|
+
public readonly options: ParameterOptions;
|
|
26
|
+
|
|
27
|
+
constructor(name: string, value: any, options: ParameterOptions, defaultStyle: string, defaultExplode: boolean) {
|
|
28
|
+
this.name = name;
|
|
29
|
+
this.value = value;
|
|
30
|
+
this.options = options || {};
|
|
31
|
+
if (this.options.style === null || this.options.style === undefined) {
|
|
32
|
+
this.options.style = defaultStyle;
|
|
33
|
+
}
|
|
34
|
+
if (this.options.explode === null || this.options.explode === undefined) {
|
|
35
|
+
this.options.explode = defaultExplode;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public serializeValue(value: any, separator = ','): string {
|
|
40
|
+
if (value === null || value === undefined) {
|
|
41
|
+
return '';
|
|
42
|
+
} else if (value instanceof Array) {
|
|
43
|
+
return value
|
|
44
|
+
.map((v) => this.serializeValue(v).split(separator).join(encodeURIComponent(separator)))
|
|
45
|
+
.join(separator);
|
|
46
|
+
} else if (typeof value === 'object') {
|
|
47
|
+
const array: string[] = [];
|
|
48
|
+
for (const key of Object.keys(value)) {
|
|
49
|
+
let propVal = value[key];
|
|
50
|
+
if (propVal !== null && propVal !== undefined) {
|
|
51
|
+
propVal = this.serializeValue(propVal).split(separator).join(encodeURIComponent(separator));
|
|
52
|
+
if (this.options.explode) {
|
|
53
|
+
array.push(`${key}=${propVal}`);
|
|
54
|
+
} else {
|
|
55
|
+
array.push(key);
|
|
56
|
+
array.push(propVal);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return array.join(separator);
|
|
61
|
+
} else {
|
|
62
|
+
return String(value);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* A parameter in the operation path
|
|
69
|
+
*/
|
|
70
|
+
class PathParameter extends Parameter {
|
|
71
|
+
constructor(name: string, value: any, options: ParameterOptions) {
|
|
72
|
+
super(name, value, options, 'simple', false);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public append(path: string): string {
|
|
76
|
+
let value = this.value;
|
|
77
|
+
if (value === null || value === undefined) {
|
|
78
|
+
value = '';
|
|
79
|
+
}
|
|
80
|
+
let prefix = this.options.style === 'label' ? '.' : '';
|
|
81
|
+
const separator = this.options.explode ? (prefix === '' ? ',' : prefix) : ',';
|
|
82
|
+
let alreadySerialized = false;
|
|
83
|
+
if (this.options.style === 'matrix') {
|
|
84
|
+
// The parameter name is just used as prefix, except in some cases...
|
|
85
|
+
prefix = `;${this.name}=`;
|
|
86
|
+
if (this.options.explode && typeof value === 'object') {
|
|
87
|
+
prefix = ';';
|
|
88
|
+
if (value instanceof Array) {
|
|
89
|
+
// For arrays we have to repeat the name for each element
|
|
90
|
+
value = value.map((v) => `${this.name}=${this.serializeValue(v, ';')}`);
|
|
91
|
+
value = value.join(';');
|
|
92
|
+
alreadySerialized = true;
|
|
93
|
+
} else {
|
|
94
|
+
// For objects we have to put each the key / value pairs
|
|
95
|
+
value = this.serializeValue(value, ';');
|
|
96
|
+
alreadySerialized = true;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
value = prefix + (alreadySerialized ? value : this.serializeValue(value, separator));
|
|
101
|
+
// Replace both the plain variable and the corresponding variant taking in the prefix and explode into account
|
|
102
|
+
path = path.replace(`{${this.name}}`, value);
|
|
103
|
+
path = path.replace(`{${prefix}${this.name}${this.options.explode ? '*' : ''}}`, value);
|
|
104
|
+
return path;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public override serializeValue(value: any, separator = ','): string {
|
|
108
|
+
let result = typeof value === 'string' ? encodeURIComponent(value) : super.serializeValue(value, separator);
|
|
109
|
+
result = result.replace(/%3D/g, '=');
|
|
110
|
+
result = result.replace(/%3B/g, ';');
|
|
111
|
+
result = result.replace(/%2C/g, ',');
|
|
112
|
+
return result;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* A parameter in the query
|
|
118
|
+
*/
|
|
119
|
+
class QueryParameter extends Parameter {
|
|
120
|
+
constructor(name: string, value: any, options: ParameterOptions) {
|
|
121
|
+
super(name, value, options, 'form', true);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
public append(params: string): string {
|
|
125
|
+
if (this.value instanceof Array) {
|
|
126
|
+
// Array serialization
|
|
127
|
+
if (this.options.explode) {
|
|
128
|
+
for (const v of this.value) {
|
|
129
|
+
params = `${params}${params ? '&' : '?'}${encodeURIComponent(this.name)}=${this.serializeValue(v)}`;
|
|
130
|
+
}
|
|
131
|
+
} else {
|
|
132
|
+
const separator = this.options.style === 'spaceDelimited'
|
|
133
|
+
? ' '
|
|
134
|
+
: this.options.style === 'pipeDelimited'
|
|
135
|
+
? '|'
|
|
136
|
+
: ',';
|
|
137
|
+
return `${params}${params ? '&' : '?'}${encodeURIComponent(this.name)}=${
|
|
138
|
+
this.serializeValue(this.value, separator)
|
|
139
|
+
}`;
|
|
140
|
+
}
|
|
141
|
+
} else if (this.value !== null && typeof this.value === 'object') {
|
|
142
|
+
// Object serialization
|
|
143
|
+
if (this.options.style === 'deepObject') {
|
|
144
|
+
// Append a parameter for each key, in the form `name[key]`
|
|
145
|
+
for (const key of Object.keys(this.value)) {
|
|
146
|
+
const propVal = this.value[key];
|
|
147
|
+
if (propVal !== null && propVal !== undefined) {
|
|
148
|
+
params = `${params}${params ? '&' : '?'}${encodeURIComponent(this.name)}[${
|
|
149
|
+
encodeURIComponent(
|
|
150
|
+
key,
|
|
151
|
+
)
|
|
152
|
+
}]=${this.serializeValue(propVal)}`;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
} else if (this.options.explode) {
|
|
156
|
+
// Append a parameter for each key without using the parameter name
|
|
157
|
+
for (const key of Object.keys(this.value)) {
|
|
158
|
+
const propVal = this.value[key];
|
|
159
|
+
if (propVal !== null && propVal !== undefined) {
|
|
160
|
+
params = `${params}${params ? '&' : '?'}${encodeURIComponent(key)}=${this.serializeValue(propVal)}`;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
} else {
|
|
164
|
+
// Append a single parameter whose values are a comma-separated list of key,value,key,value...
|
|
165
|
+
const array: unknown[] = [];
|
|
166
|
+
for (const key of Object.keys(this.value)) {
|
|
167
|
+
const propVal = this.value[key];
|
|
168
|
+
if (propVal !== null && propVal !== undefined) {
|
|
169
|
+
array.push(key);
|
|
170
|
+
array.push(propVal);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
params = `${params}${params ? '&' : '?'}${encodeURIComponent(this.name)}=${this.serializeValue(array)}`;
|
|
174
|
+
}
|
|
175
|
+
} else if (this.value !== null && this.value !== undefined) {
|
|
176
|
+
// Plain value
|
|
177
|
+
params = `${params}${params ? '&' : '?'}${encodeURIComponent(this.name)}=${this.serializeValue(this.value)}`;
|
|
178
|
+
}
|
|
179
|
+
return params;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* A parameter in the HTTP request header
|
|
185
|
+
*/
|
|
186
|
+
class HeaderParameter extends Parameter {
|
|
187
|
+
constructor(name: string, value: any, options: ParameterOptions) {
|
|
188
|
+
super(name, value, options, 'simple', false);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
public append(headers: Record<string, string>): Record<string, string> {
|
|
192
|
+
if (this.value !== null && this.value !== undefined) {
|
|
193
|
+
if (this.value instanceof Array) {
|
|
194
|
+
throw new Error('Array header values are not supported');
|
|
195
|
+
} else {
|
|
196
|
+
headers[this.name] = this.serializeValue(this.value);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return headers;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Helper to build http requests from parameters
|
|
205
|
+
*/
|
|
206
|
+
export class RequestBuilder {
|
|
207
|
+
private readonly _path: Map<string, PathParameter>;
|
|
208
|
+
private readonly _query: Map<string, QueryParameter>;
|
|
209
|
+
private readonly _header: Map<string, HeaderParameter>;
|
|
210
|
+
private _bodyContent: any;
|
|
211
|
+
private _bodyContentType: string | undefined;
|
|
212
|
+
|
|
213
|
+
public readonly rootUrl: string;
|
|
214
|
+
public readonly operationPath: string;
|
|
215
|
+
public readonly method: string;
|
|
216
|
+
|
|
217
|
+
constructor(rootUrl: string, operationPath: string, method: string) {
|
|
218
|
+
this.rootUrl = rootUrl;
|
|
219
|
+
this.operationPath = operationPath;
|
|
220
|
+
this.method = method;
|
|
221
|
+
this._path = new Map();
|
|
222
|
+
this._query = new Map();
|
|
223
|
+
this._header = new Map();
|
|
224
|
+
this._bodyContent = null;
|
|
225
|
+
this._bodyContentType = undefined;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Sets a path parameter
|
|
230
|
+
*/
|
|
231
|
+
public path(name: string, value: any, options: ParameterOptions) {
|
|
232
|
+
this._path.set(name, new PathParameter(name, value, options || {}));
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Sets a query parameter
|
|
237
|
+
*/
|
|
238
|
+
public query(name: string, value: any, options: ParameterOptions) {
|
|
239
|
+
this._query.set(name, new QueryParameter(name, value, options || {}));
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Sets a header parameter
|
|
244
|
+
*/
|
|
245
|
+
public header(name: string, value: any, options: ParameterOptions) {
|
|
246
|
+
this._header.set(name, new HeaderParameter(name, value, options || {}));
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Sets the body content, along with the content type
|
|
251
|
+
*/
|
|
252
|
+
public body(value: any, contentType = 'application/json') {
|
|
253
|
+
this._bodyContentType = contentType;
|
|
254
|
+
if (this._bodyContentType === 'application/x-www-form-urlencoded' && value !== null && typeof value === 'object') {
|
|
255
|
+
// Handle URL-encoded data
|
|
256
|
+
const pairs: [string, string][] = [];
|
|
257
|
+
for (const key of Object.keys(value)) {
|
|
258
|
+
let val = value[key];
|
|
259
|
+
if (!(val instanceof Array)) {
|
|
260
|
+
val = [val];
|
|
261
|
+
}
|
|
262
|
+
for (const v of val) {
|
|
263
|
+
const formValue = this.formDataValue(v);
|
|
264
|
+
if (formValue !== null) {
|
|
265
|
+
pairs.push([key, formValue]);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
this._bodyContent = pairs.map((p) => `${encodeURIComponent(p[0])}=${encodeURIComponent(p[1])}`).join('&');
|
|
270
|
+
} else if (this._bodyContentType === 'multipart/form-data' && value !== null && typeof value === 'object') {
|
|
271
|
+
const fd = new FormData();
|
|
272
|
+
for (const key in value) {
|
|
273
|
+
fd.append(key, this.formDataValue(value[key]));
|
|
274
|
+
}
|
|
275
|
+
this._bodyContent = fd.body();
|
|
276
|
+
this._bodyContentType = 'multipart/form-data; boundary=' + fd.boundary;
|
|
277
|
+
} else if (this._bodyContentType.indexOf('json') !== -1 && value !== null && typeof value === 'object') {
|
|
278
|
+
this._bodyContent = JSON.stringify(value);
|
|
279
|
+
} else {
|
|
280
|
+
// The body is the plain content
|
|
281
|
+
this._bodyContent = value;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
private formDataValue(value: any): any {
|
|
286
|
+
if (value === null || value === undefined) {
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
if (this.isFile(value)) {
|
|
290
|
+
return value;
|
|
291
|
+
}
|
|
292
|
+
return { data: JSON.stringify(value), content_type: 'application/json' };
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
private isFile(value: any): value is http.FileData {
|
|
296
|
+
return (
|
|
297
|
+
typeof value === 'object' &&
|
|
298
|
+
value !== null &&
|
|
299
|
+
'data' in value &&
|
|
300
|
+
(typeof value.data === 'string' || value.data instanceof ArrayBuffer) &&
|
|
301
|
+
(!('filename' in value) || typeof value.filename === 'string') &&
|
|
302
|
+
(!('content_type' in value) || typeof value.content_type === 'string') &&
|
|
303
|
+
Object.keys(value).every((key) => key === 'data' || key === 'filename' || key === 'content_type')
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Builds the request with the current set parameters
|
|
309
|
+
*/
|
|
310
|
+
public build(options?: BuildOptions): Response {
|
|
311
|
+
// Perform the request
|
|
312
|
+
return http.request(...this.buildRequestParams(options));
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Builds the request with the current set parameters
|
|
317
|
+
*/
|
|
318
|
+
public buildAsync(options?: BuildOptions): Promise<Response> {
|
|
319
|
+
// Perform the request
|
|
320
|
+
return http.asyncRequest(...this.buildRequestParams(options));
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
private buildRequestParams(options?: BuildOptions): Parameters<typeof http.request> {
|
|
324
|
+
options = options || {};
|
|
325
|
+
|
|
326
|
+
// Path parameters
|
|
327
|
+
let path = this.operationPath;
|
|
328
|
+
for (const pathParam of this._path.values()) {
|
|
329
|
+
path = pathParam.append(path);
|
|
330
|
+
}
|
|
331
|
+
const url = this.rootUrl + path;
|
|
332
|
+
|
|
333
|
+
// Query parameters
|
|
334
|
+
let httpParams = '';
|
|
335
|
+
for (const queryParam of this._query.values()) {
|
|
336
|
+
httpParams = queryParam.append(httpParams);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// Header parameters
|
|
340
|
+
let httpHeaders: Record<string, string> = {};
|
|
341
|
+
if (options.accept) {
|
|
342
|
+
httpHeaders['Accept'] = options.accept;
|
|
343
|
+
}
|
|
344
|
+
for (const headerParam of this._header.values()) {
|
|
345
|
+
httpHeaders = headerParam.append(httpHeaders);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// Request content headers
|
|
349
|
+
if (this._bodyContentType) {
|
|
350
|
+
httpHeaders['Content-Type'] = this._bodyContentType;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
return [
|
|
354
|
+
this.method.toUpperCase(),
|
|
355
|
+
url + httpParams,
|
|
356
|
+
this._bodyContent,
|
|
357
|
+
Object.assign({}, options.params, {
|
|
358
|
+
headers: Object.assign({}, httpHeaders, options.params ? options.params.headers : {}),
|
|
359
|
+
tags: {
|
|
360
|
+
name: this.operationPath,
|
|
361
|
+
...options.params?.tags,
|
|
362
|
+
},
|
|
363
|
+
}),
|
|
364
|
+
];
|
|
365
|
+
}
|
|
366
|
+
}
|