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