/
requestBuilder.handlebars
189 lines (167 loc) · 5.2 KB
/
requestBuilder.handlebars
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* tslint:disable */
import { HttpRequest, HttpParameterCodec, HttpParams, HttpHeaders } from '@angular/common/http';
/**
* Custom parameter codec to correctly handle the plus sign in parameter
* values. See https://github.com/angular/angular/issues/18261
*/
class ParameterCodec implements HttpParameterCodec {
encodeKey(key: string): string {
return encodeURIComponent(key);
}
encodeValue(value: string): string {
return encodeURIComponent(value);
}
decodeKey(key: string): string {
return decodeURIComponent(key);
}
decodeValue(value: string): string {
return decodeURIComponent(value);
}
}
const ParameterCodecInstance = new ParameterCodec();
/**
* Helper to build http requests from parameters
*/
export class {{ requestBuilderClass }} {
private _path = new Map<string, any>();
private _query = new Map<string, any>();
private _header = new Map<string, any>();
_bodyContent: any | null;
_bodyContentType?: string;
constructor(
public rootUrl: string,
public operationPath: string,
public method: string) {
}
/**
* Sets a path parameter
*/
path(name: string, value: any): void {
if (value !== null && value !== undefined) {
this._path.set(name, value);
}
}
/**
* Sets a query parameter
*/
query(name: string, value: any): void {
if (value !== null && value !== undefined) {
this._query.set(name, value);
}
}
/**
* Sets a header parameter
*/
header(name: string, value: any): void {
if (value !== null && value !== undefined) {
this._header.set(name, value);
}
}
/**
* Sets the body content, along with the content type
*/
body(value: any, contentType = 'application/json'): void {
if (value instanceof Blob) {
this._bodyContentType = value.type;
} else {
this._bodyContentType = contentType;
}
if ((this._bodyContentType || '').startsWith('multipart/form-data')) {
// Handle multipart form data
const formData = new FormData();
if (value != null) {
for (const key of Object.keys(value)) {
const val = value[key]
if (val instanceof Array) {
for (const v of val) {
const toAppend = this.formDataValue(v);
if (toAppend !== null) {
formData.append(key, toAppend);
}
}
} else {
const toAppend = this.formDataValue(val);
if (toAppend !== null) {
formData.set(key, toAppend);
}
}
}
}
this._bodyContent = formData;
} else {
// The body is the plain content
this._bodyContent = value;
}
}
private formDataValue(value: any): any {
if (value === null || value === undefined) {
return null;
}
if (value instanceof Blob) {
return value;
}
if (typeof value === 'object') {
return new Blob([JSON.stringify(value)], { type: 'application/json' });
}
return new Blob([String(value)], { type: 'text/plain' });
}
/**
* Builds the request with the current set parameters
*/
build<T=any>(options?: {
/** Which content types to accept */
accept?: string;
/** The expected response type */
responseType?: 'json' | 'text' | 'blob' | 'arraybuffer';
/** Whether to report progress on uploads / downloads */
reportProgress?: boolean;
}): HttpRequest<T> {
options = options || {};
// Path parameters
let path = this.operationPath;
for (const param of Array.from(this._path.keys())) {
path = path.replace(`{${param}}`, (this._path.get(param) || '').toString());
}
const url = this.rootUrl + path;
// Query parameters
let httpParams = new HttpParams({
encoder: ParameterCodecInstance
});
for (const param of Array.from(this._query.keys())) {
const value = this._query.get(param);
if (value instanceof Array) {
for (const item of value) {
httpParams = httpParams.append(param, (item !== undefined && item !== null ? item : '').toString());
}
} else {
httpParams = httpParams.set(param, (value !== undefined && value !== null ? value : '').toString());
}
}
// Header parameters
let httpHeaders = new HttpHeaders();
if (options.accept) {
httpHeaders = httpHeaders.append('Accept', options.accept);
}
for (const param of Array.from(this._header.keys())) {
const value = this._header.get(param);
if (value instanceof Array) {
for (const item of value) {
httpHeaders = httpHeaders.append(param, (item !== undefined && item !== null ? item : '').toString());
}
} else {
httpHeaders = httpHeaders.set(param, (value !== undefined && value !== null ? value : '').toString());
}
}
// Request content headers
if (this._bodyContentType && !(this._bodyContent instanceof FormData)) {
httpHeaders = httpHeaders.set('Content-Type', this._bodyContentType);
}
// Perform the request
return new HttpRequest<T>(this.method.toUpperCase(), url, this._bodyContent, {
params: httpParams,
headers: httpHeaders,
responseType: options.responseType,
reportProgress: options.reportProgress
});
}
}