forked from hellosign/hellosign-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-single.mustache
343 lines (296 loc) · 11.3 KB
/
api-single.mustache
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
{{>licenseInfo}}
import axios, { AxiosError, AxiosRequestConfig } from 'axios';
/* tslint:disable:no-unused-locals */
{{#imports}}
import { {{classname}} } from '{{filename}}';
{{/imports}}
import { ObjectSerializer, Authentication, VoidAuth, Interceptor } from '../model/models';
{{#hasAuthMethods}}
import { HttpBasicAuth, HttpBearerAuth, ApiKeyAuth, OAuth } from '../model/models';
{{/hasAuthMethods}}
import { HttpError, optionsI, returnTypeT, returnTypeI, generateFormData, toFormData, queryParamsSerializer } from './apis';
let defaultBasePath = '{{{basePath}}}';
// ===============================================
// This file is autogenerated - Please do not edit
// ===============================================
{{#operations}}
{{#description}}
/**
* {{&description}}
*/
{{/description}}
export enum {{classname}}ApiKeys {
{{#authMethods}}
{{#isApiKey}}
{{name}},
{{/isApiKey}}
{{/authMethods}}
}
export class {{classname}} {
protected _basePath = defaultBasePath;
protected _defaultHeaders : any = {};
protected _useQuerystring : boolean = false;
protected authentications = {
'default': <Authentication>new VoidAuth(),
{{#hasAuthMethods}}
{{#authMethods}}
{{#isBasicBasic}}
'{{name}}': new HttpBasicAuth(),
{{/isBasicBasic}}
{{#isBasicBearer}}
'{{name}}': new HttpBearerAuth(),
{{/isBasicBearer}}
{{#isApiKey}}
'{{name}}': new ApiKeyAuth({{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{#isKeyInQuery}}'query'{{/isKeyInQuery}}{{#isKeyInCookie}}'cookie'{{/isKeyInCookie}}, '{{keyParamName}}'),
{{/isApiKey}}
{{#isOAuth}}
'{{name}}': new OAuth(),
{{/isOAuth}}
{{/authMethods}}
{{/hasAuthMethods}}
}
protected interceptors: Interceptor[] = [];
constructor(basePath?: string) {
if (basePath) {
this.basePath = basePath;
}
}
set useQuerystring(value: boolean) {
this._useQuerystring = value;
}
set basePath(basePath: string) {
this._basePath = basePath;
}
set defaultHeaders(defaultHeaders: any) {
this._defaultHeaders = defaultHeaders;
}
get defaultHeaders() {
return this._defaultHeaders;
}
get basePath() {
return this._basePath;
}
public setDefaultAuthentication(auth: Authentication) {
this.authentications.default = auth;
}
public setApiKey(key: string) {
this.authentications.api_key.username = key;
}
{{#hasAuthMethods}}
{{#authMethods}}
{{#isBasicBasic}}
set username(username: string) {
this.authentications.{{name}}.username = username;
}
set password(password: string) {
this.authentications.{{name}}.password = password;
}
{{/isBasicBasic}}
{{#isBasicBearer}}
set accessToken(accessToken: string | (() => string)) {
this.authentications.{{name}}.accessToken = accessToken;
}
{{/isBasicBearer}}
{{#isOAuth}}
set accessToken(token: string) {
this.authentications.{{name}}.accessToken = token;
}
{{/isOAuth}}
{{/authMethods}}
{{/hasAuthMethods}}
public addInterceptor(interceptor: Interceptor) {
this.interceptors.push(interceptor);
}
{{#operation}}
/**
* {{¬es}}
{{#summary}}
* @summary {{&summary}}
{{/summary}}
{{#allParams}}
* @param {{paramName}} {{description}}
{{/allParams}}
* @param options
*/
public async {{nickname}} ({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}options: optionsI = {headers: {}}) : Promise<{{#returnType}}returnTypeT<{{{.}}}>{{/returnType}}{{^returnType}}returnTypeI{{/returnType}}> {
const localVarPath = this.basePath + '{{{path}}}'{{#pathParams}}
.replace('{' + '{{baseName}}' + '}', encodeURIComponent(String({{paramName}}))){{/pathParams}};
let localVarQueryParameters: any = {};
let localVarHeaderParams: any = (<any>Object).assign({}, this._defaultHeaders);
{{#hasProduces}}
const produces = [{{#produces}}'{{{mediaType}}}'{{^-last}}, {{/-last}}{{/produces}}];
// give precedence to 'application/json'
if (produces.indexOf('application/json') >= 0) {
localVarHeaderParams['content-type'] = 'application/json';
} else {
localVarHeaderParams['content-type'] = produces.join(",");
}
{{/hasProduces}}
let localVarFormParams: any = {};
let localVarBodyParams: any = undefined;
{{#allParams}}
{{#required}}
// verify required parameter '{{paramName}}' is not null or undefined
if ({{paramName}} === null || {{paramName}} === undefined) {
throw new Error('Required parameter {{paramName}} was null or undefined when calling {{nickname}}.');
}
{{/required}}
{{/allParams}}
{{#queryParams}}
if ({{paramName}} !== undefined) {
localVarQueryParameters['{{baseName}}'] = ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}");
}
{{/queryParams}}
{{#headerParams}}
localVarHeaderParams['{{baseName}}'] = ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}");
{{/headerParams}}
(<any>Object).assign(localVarHeaderParams, options.headers);
let localVarUseFormData = false;
{{#formParams}}
if ({{paramName}} !== undefined) {
{{#isFile}}
localVarFormParams['{{baseName}}'] = {{paramName}};
{{/isFile}}
{{^isFile}}
localVarFormParams['{{baseName}}'] = ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}");
{{/isFile}}
}
{{#isFile}}
localVarUseFormData = true;
{{/isFile}}
{{/formParams}}
{{#bodyParam}}
const result = generateFormData({{{paramName}}}, {{{dataType}}}.attributeTypeMap);
localVarUseFormData = result.localVarUseFormData;
let data = {};
if (localVarUseFormData) {
const formData = toFormData(result.data);
data = formData;
localVarHeaderParams = {
...localVarHeaderParams,
...formData.getHeaders(),
};
} else {
data = ObjectSerializer.serialize(
{{paramName}},
"{{{dataType}}}"
);
}
{{/bodyParam}}
let localVarRequestOptions: AxiosRequestConfig = {
method: '{{httpMethod}}',
params: localVarQueryParameters,
headers: localVarHeaderParams,
url: localVarPath,
paramsSerializer: this._useQuerystring ? queryParamsSerializer : undefined,
{{^isResponseFile}}
responseType: "json",
{{/isResponseFile}}
{{#isResponseFile}}
responseType: "arraybuffer",
{{/isResponseFile}}
{{#bodyParam}}
data,
{{/bodyParam}}
};
let authenticationPromise = Promise.resolve();
{{#authMethods}}
{{#isApiKey}}
if (this.authentications.{{name}}.apiKey) {
authenticationPromise = authenticationPromise.then(() => this.authentications.{{name}}.applyToRequest(localVarRequestOptions));
}
{{/isApiKey}}
{{#isBasicBasic}}
if (this.authentications.{{name}}.username) {
authenticationPromise = authenticationPromise.then(() => this.authentications.{{name}}.applyToRequest(localVarRequestOptions));
}
{{/isBasicBasic}}
{{#isBasicBearer}}
if (this.authentications.{{name}}.accessToken) {
authenticationPromise = authenticationPromise.then(() => this.authentications.{{name}}.applyToRequest(localVarRequestOptions));
}
{{/isBasicBearer}}
{{#isOAuth}}
if (this.authentications.{{name}}.accessToken) {
authenticationPromise = authenticationPromise.then(() => this.authentications.{{name}}.applyToRequest(localVarRequestOptions));
}
{{/isOAuth}}
{{/authMethods}}
authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions));
let interceptorPromise = authenticationPromise;
for (const interceptor of this.interceptors) {
interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions));
}
return interceptorPromise.then(() => {
return new Promise<{{#returnType}}returnTypeT<{{{.}}}>{{/returnType}}{{^returnType}}returnTypeI{{/returnType}}>((resolve, reject) => {
axios.request(localVarRequestOptions)
.then((response) => {
let body = response.data;
if (response.status && response.status >= 200 && response.status <= 299) {
{{#returnType}}
body = ObjectSerializer.deserialize(body, "{{{.}}}");
{{/returnType}}
resolve({ response: response, body: body });
} else {
reject(new HttpError(response, body, response.status));
}
}, (error: AxiosError) => {
if (error.response == null) {
reject(error);
return;
}
const response = error.response;
let body;
{{#returnType}}
{{#responses}}
{{#dataType}}
{{^isWildcard}}
{{^isRange}}
if (response.status === {{code}}) {
body = ObjectSerializer.deserialize(
response.data,
"{{{dataType}}}"
);
reject(new HttpError(response, body, response.status));
return;
}
{{/isRange}}
{{/isWildcard}}
{{/dataType}}
{{/responses}}
{{#responses}}
{{#dataType}}
{{#isRange}}
let rangeCodeLeft = Number("{{code}}"[0] + "00");
let rangeCodeRight = Number("{{code}}"[0] + "99");
if (response.status >= rangeCodeLeft && response.status <= rangeCodeRight) {
body = ObjectSerializer.deserialize(
response.data,
"{{{dataType}}}"
);
reject(new HttpError(response, body, response.status));
return;
}
{{/isRange}}
{{/dataType}}
{{/responses}}
{{#responses}}
{{#dataType}}
{{#isWildcard}}
body = ObjectSerializer.deserialize(
response.data.error,
"{{{dataType}}}"
);
reject(new HttpError(response, response.data, response.status));
return;
{{/isWildcard}}
{{/dataType}}
{{/responses}}
{{/returnType}}
});
});
});
}
{{/operation}}
}
{{/operations}}