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