@goast/typescript 0.7.6 → 0.8.0-beta.1

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.
Files changed (100) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +52 -52
  3. package/assets/client/angular/angular-service.utils.ts +82 -82
  4. package/assets/client/angular/request-builder.ts +377 -377
  5. package/assets/client/fetch/fetch-client.utils.ts +52 -52
  6. package/assets/client/k6/request-builder.js +448 -448
  7. package/assets/client/k6/request-builder.ts +366 -366
  8. package/assets/stubs/easy-network-stub/easy-network-stub.utils.ts +257 -257
  9. package/esm/src/generators/services/angular-services/angular-service-generator.js +9 -9
  10. package/esm/src/generators/services/easy-network-stub/easy-network-stub-generator.js +9 -9
  11. package/esm/src/generators/services/fetch-clients/fetch-client-generator.js +4 -4
  12. package/esm/src/generators/services/k6-clients/k6-client-generator.js +11 -11
  13. package/package.json +2 -2
  14. package/script/src/generators/services/angular-services/angular-service-generator.js +9 -9
  15. package/script/src/generators/services/easy-network-stub/easy-network-stub-generator.js +9 -9
  16. package/script/src/generators/services/fetch-clients/fetch-client-generator.js +4 -4
  17. package/script/src/generators/services/k6-clients/k6-client-generator.js +11 -11
  18. package/src/mod.ts +0 -7
  19. package/src/src/assets.ts +0 -9
  20. package/src/src/ast/_index.ts +0 -54
  21. package/src/src/ast/common.ts +0 -1
  22. package/src/src/ast/index.ts +0 -1
  23. package/src/src/ast/node.ts +0 -10
  24. package/src/src/ast/nodes/argument.ts +0 -40
  25. package/src/src/ast/nodes/array-type.ts +0 -49
  26. package/src/src/ast/nodes/arrow-function.ts +0 -91
  27. package/src/src/ast/nodes/call.ts +0 -55
  28. package/src/src/ast/nodes/class.ts +0 -133
  29. package/src/src/ast/nodes/constructor-parameter.ts +0 -70
  30. package/src/src/ast/nodes/constructor.ts +0 -77
  31. package/src/src/ast/nodes/decorator.ts +0 -76
  32. package/src/src/ast/nodes/doc-tag.ts +0 -291
  33. package/src/src/ast/nodes/doc.ts +0 -102
  34. package/src/src/ast/nodes/enum-value.ts +0 -72
  35. package/src/src/ast/nodes/enum.ts +0 -77
  36. package/src/src/ast/nodes/export.ts +0 -54
  37. package/src/src/ast/nodes/function-type.ts +0 -60
  38. package/src/src/ast/nodes/function.ts +0 -107
  39. package/src/src/ast/nodes/generic-parameter.ts +0 -71
  40. package/src/src/ast/nodes/indexer.ts +0 -60
  41. package/src/src/ast/nodes/interface.ts +0 -117
  42. package/src/src/ast/nodes/intersection-type.ts +0 -67
  43. package/src/src/ast/nodes/lookup-type.ts +0 -53
  44. package/src/src/ast/nodes/method.ts +0 -138
  45. package/src/src/ast/nodes/object-type.ts +0 -52
  46. package/src/src/ast/nodes/object.ts +0 -48
  47. package/src/src/ast/nodes/parameter.ts +0 -85
  48. package/src/src/ast/nodes/property-accessor.ts +0 -173
  49. package/src/src/ast/nodes/property.ts +0 -192
  50. package/src/src/ast/nodes/reference.ts +0 -194
  51. package/src/src/ast/nodes/string.ts +0 -49
  52. package/src/src/ast/nodes/tuple.ts +0 -53
  53. package/src/src/ast/nodes/type-alias.ts +0 -75
  54. package/src/src/ast/nodes/typeof.ts +0 -42
  55. package/src/src/ast/nodes/types.ts +0 -40
  56. package/src/src/ast/nodes/union-type.ts +0 -62
  57. package/src/src/ast/nodes/variable.ts +0 -73
  58. package/src/src/ast/references/angular.ts +0 -24
  59. package/src/src/ast/references/easy-network-stub.ts +0 -21
  60. package/src/src/ast/references/index.ts +0 -5
  61. package/src/src/ast/references/k6.ts +0 -8
  62. package/src/src/ast/references/rxjs.ts +0 -7
  63. package/src/src/ast/references/typescript.ts +0 -35
  64. package/src/src/ast/utils/get-type-script-builder-options.ts +0 -19
  65. package/src/src/ast/utils/to-ts-node.ts +0 -27
  66. package/src/src/ast/utils/write-ts-generic-parameters.ts +0 -25
  67. package/src/src/ast/utils/write-ts-members.ts +0 -28
  68. package/src/src/ast/utils/write-ts-nodes.ts +0 -35
  69. package/src/src/ast/utils/write-ts-parameters.ts +0 -25
  70. package/src/src/common-results.ts +0 -26
  71. package/src/src/config.ts +0 -96
  72. package/src/src/file-builder.ts +0 -137
  73. package/src/src/generators/file-generator.ts +0 -15
  74. package/src/src/generators/index.ts +0 -6
  75. package/src/src/generators/models/index.ts +0 -3
  76. package/src/src/generators/models/model-generator.ts +0 -493
  77. package/src/src/generators/models/models-generator.ts +0 -108
  78. package/src/src/generators/models/models.ts +0 -121
  79. package/src/src/generators/services/angular-services/angular-service-generator.ts +0 -431
  80. package/src/src/generators/services/angular-services/angular-services-generator.ts +0 -295
  81. package/src/src/generators/services/angular-services/index.ts +0 -3
  82. package/src/src/generators/services/angular-services/models.ts +0 -277
  83. package/src/src/generators/services/angular-services/refs.ts +0 -54
  84. package/src/src/generators/services/easy-network-stub/easy-network-stub-generator.ts +0 -250
  85. package/src/src/generators/services/easy-network-stub/easy-network-stubs-generator.ts +0 -211
  86. package/src/src/generators/services/easy-network-stub/index.ts +0 -3
  87. package/src/src/generators/services/easy-network-stub/models.ts +0 -124
  88. package/src/src/generators/services/easy-network-stub/refs.ts +0 -47
  89. package/src/src/generators/services/fetch-clients/fetch-client-generator.ts +0 -305
  90. package/src/src/generators/services/fetch-clients/fetch-clients-generator.ts +0 -163
  91. package/src/src/generators/services/fetch-clients/index.ts +0 -3
  92. package/src/src/generators/services/fetch-clients/models.ts +0 -130
  93. package/src/src/generators/services/fetch-clients/refs.ts +0 -21
  94. package/src/src/generators/services/k6-clients/index.ts +0 -3
  95. package/src/src/generators/services/k6-clients/k6-client-generator.ts +0 -505
  96. package/src/src/generators/services/k6-clients/k6-clients-generator.ts +0 -171
  97. package/src/src/generators/services/k6-clients/models.ts +0 -247
  98. package/src/src/generators/services/k6-clients/refs.ts +0 -16
  99. package/src/src/import-collection.ts +0 -236
  100. package/src/src/utils.ts +0 -79
@@ -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
+ }