-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmodels.mustache
407 lines (366 loc) · 14.1 KB
/
models.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
{{^useCustomTemplateCode}}
{{#generateApis}}
import localVarRequest from 'request';
{{/generateApis}}
{{#models}}
{{#model}}
export * from '{{{ classFilename }}}';
{{/model}}
{{/models}}
import * as fs from 'fs';
export interface RequestDetailedFile {
value: Buffer;
options?: {
filename?: string;
contentType?: string;
}
}
export type RequestFile = string | Buffer | fs.ReadStream | RequestDetailedFile;
{{/useCustomTemplateCode}}
{{#useCustomTemplateCode}}
import { AxiosRequestConfig } from 'axios';
import * as fs from 'fs';
import { enumsMap, typeMap } from "./";
import { Headers } from "form-data";
export interface RequestDetailedFile {
value: Buffer;
options: {
filename: string;
contentType: string;
header?: string | Headers;
knownLength?: number;
filepath?: string;
};
}
interface AttributeType {
name: string;
baseName: string;
type: string;
}
export interface AttributeTypeMap extends Array<AttributeType>{}
export type RequestFile = fs.ReadStream | RequestDetailedFile;
{{/useCustomTemplateCode}}
{{! Object serialization only relevant if generating APIs, too }}
{{#generateApis}}
{{^useCustomTemplateCode}}
{{#models}}
{{#model}}
import { {{classname}} } from '{{{ classFilename }}}';
{{/model}}
{{/models}}
{{/useCustomTemplateCode}}
/* tslint:disable:no-unused-variable */
let primitives = [
"string",
"boolean",
"double",
"integer",
"long",
"float",
"number",
"any"
];
{{^useCustomTemplateCode}}
let enumsMap: {[index: string]: any} = {
{{#models}}
{{#model}}
{{#hasEnums}}
{{#vars}}
{{#isEnum}}
{{#isContainer}}"{{classname}}.{{enumName}}": {{classname}}.{{enumName}}{{/isContainer}}{{^isContainer}}"{{datatypeWithEnum}}": {{datatypeWithEnum}}{{/isContainer}},
{{/isEnum}}
{{/vars}}
{{/hasEnums}}
{{#isEnum}}
"{{classname}}": {{classname}},
{{/isEnum}}
{{/model}}
{{/models}}
}
let typeMap: {[index: string]: any} = {
{{#models}}
{{#model}}
{{^isEnum}}
"{{classname}}": {{classname}},
{{/isEnum}}
{{/model}}
{{/models}}
}
{{/useCustomTemplateCode}}
// Check if a string starts with another string without using es6 features
function startsWith(str: string, match: string): boolean {
return str.substring(0, match.length) === match;
}
// Check if a string ends with another string without using es6 features
function endsWith(str: string, match: string): boolean {
return str.length >= match.length && str.substring(str.length - match.length) === match;
}
const nullableSuffix = " | null";
const optionalSuffix = " | undefined";
const arrayPrefix = "Array<";
const arraySuffix = ">";
const mapPrefix = "{ [key: string]: ";
const mapSuffix = "; }";
export class ObjectSerializer {
public static findCorrectType(data: any, expectedType: string) {
if (data == undefined) {
return expectedType;
} else if (primitives.indexOf(expectedType.toLowerCase()) !== -1) {
return expectedType;
} else if (expectedType === "Date") {
return expectedType;
} else {
if (enumsMap[expectedType]) {
return expectedType;
}
if (!typeMap[expectedType]) {
return expectedType; // w/e we don't know the type
}
// Check the discriminator
let discriminatorProperty = typeMap[expectedType].discriminator;
{{#useCustomTemplateCode}}
let discriminatorValue = data[discriminatorProperty];
if (typeMap[expectedType].hasOwnProperty('discriminatorClassName')) {
let discriminatorClass = typeMap[expectedType].discriminatorClassName(
discriminatorValue
);
if (discriminatorClass) {
return discriminatorClass;
}
}
{{/useCustomTemplateCode}}
if (discriminatorProperty == null) {
return expectedType; // the type does not have a discriminator. use it.
} else {
if (data[discriminatorProperty]) {
var discriminatorType = data[discriminatorProperty];
if(typeMap[discriminatorType]){
return discriminatorType; // use the type given in the discriminator
} else {
return expectedType; // discriminator did not map to a type
}
} else {
return expectedType; // discriminator was not present (or an empty string)
}
}
}
}
public static serialize(data: any, type: string): any {
if (data == undefined) {
return data;
} else if (primitives.indexOf(type.toLowerCase()) !== -1) {
return data;
} else if (endsWith(type, nullableSuffix)) {
let subType: string = type.slice(0, -nullableSuffix.length); // Type | null => Type
return ObjectSerializer.serialize(data, subType);
} else if (endsWith(type, optionalSuffix)) {
let subType: string = type.slice(0, -optionalSuffix.length); // Type | undefined => Type
return ObjectSerializer.serialize(data, subType);
} else if (startsWith(type, arrayPrefix)) {
let subType: string = type.slice(arrayPrefix.length, -arraySuffix.length); // Array<Type> => Type
let transformedData: any[] = [];
for (let index = 0; index < data.length; index++) {
let datum = data[index];
transformedData.push(ObjectSerializer.serialize(datum, subType));
}
return transformedData;
} else if (startsWith(type, mapPrefix)) {
let subType: string = type.slice(mapPrefix.length, -mapSuffix.length); // { [key: string]: Type; } => Type
let transformedData: { [key: string]: any } = {};
for (let key in data) {
transformedData[key] = ObjectSerializer.serialize(
data[key],
subType,
);
}
return transformedData;
} else if (type === "Date") {
return data.toISOString();
} else {
if (enumsMap[type]) {
return data;
}
if (!typeMap[type]) { // in case we dont know the type
return data;
}
// Get the actual type of this object
type = this.findCorrectType(data, type);
// get the map for the correct type.
let attributeTypes = typeMap[type].getAttributeTypeMap();
let instance: {[index: string]: any} = {};
for (let index = 0; index < attributeTypes.length; index++) {
let attributeType = attributeTypes[index];
{{^useCustomTemplateCode}}
instance[attributeType.baseName] = ObjectSerializer.serialize(data[attributeType.name], attributeType.type);
{{/useCustomTemplateCode}}
{{#useCustomTemplateCode}}
let value = ObjectSerializer.serialize(data[attributeType.name], attributeType.type);
// Only add property if not null
if (value !== undefined) {
instance[attributeType.baseName] = value;
}
{{/useCustomTemplateCode}}
}
return instance;
}
}
public static deserialize(data: any, type: string): any {
// polymorphism may change the actual type.
type = ObjectSerializer.findCorrectType(data, type);
if (data == undefined) {
return data;
} else if (primitives.indexOf(type.toLowerCase()) !== -1) {
return data;
} else if (endsWith(type, nullableSuffix)) {
let subType: string = type.slice(0, -nullableSuffix.length); // Type | null => Type
return ObjectSerializer.deserialize(data, subType);
} else if (endsWith(type, optionalSuffix)) {
let subType: string = type.slice(0, -optionalSuffix.length); // Type | undefined => Type
return ObjectSerializer.deserialize(data, subType);
} else if (startsWith(type, arrayPrefix)) {
let subType: string = type.slice(arrayPrefix.length, -arraySuffix.length); // Array<Type> => Type
let transformedData: any[] = [];
for (let index = 0; index < data.length; index++) {
let datum = data[index];
transformedData.push(ObjectSerializer.deserialize(datum, subType));
}
return transformedData;
} else if (startsWith(type, mapPrefix)) {
let subType: string = type.slice(mapPrefix.length, -mapSuffix.length); // { [key: string]: Type; } => Type
let transformedData: { [key: string]: any } = {};
for (let key in data) {
transformedData[key] = ObjectSerializer.deserialize(
data[key],
subType,
);
}
return transformedData;
} else if (type === "Date") {
return new Date(data);
} else {
if (enumsMap[type]) {// is Enum
return data;
}
if (!typeMap[type]) { // dont know the type
return data;
}
let instance = new typeMap[type]();
let attributeTypes = typeMap[type].getAttributeTypeMap();
for (let index = 0; index < attributeTypes.length; index++) {
let attributeType = attributeTypes[index];
{{^useCustomTemplateCode}}
instance[attributeType.name] = ObjectSerializer.deserialize(data[attributeType.baseName], attributeType.type);
{{/useCustomTemplateCode}}
{{#useCustomTemplateCode}}
const propertyKey = data[attributeType.baseName] !== undefined
? attributeType.baseName
: attributeType.name;
instance[attributeType.name] = ObjectSerializer.deserialize(data[propertyKey], attributeType.type);
{{/useCustomTemplateCode}}
}
return instance;
}
}
}
export interface Authentication {
/**
* Apply authentication settings to header and query params.
*/
{{^useCustomTemplateCode}}
applyToRequest(requestOptions: localVarRequest.Options): Promise<void> | void;
{{/useCustomTemplateCode}}
{{#useCustomTemplateCode}}
applyToRequest(requestOptions: AxiosRequestConfig): Promise<void> | void;
{{/useCustomTemplateCode}}
}
export class HttpBasicAuth implements Authentication {
public username: string = '';
public password: string = '';
{{^useCustomTemplateCode}}
applyToRequest(requestOptions: localVarRequest.Options): void {
{{/useCustomTemplateCode}}
{{#useCustomTemplateCode}}
applyToRequest(requestOptions: AxiosRequestConfig): void {
{{/useCustomTemplateCode}}
requestOptions.auth = {
username: this.username, password: this.password
}
}
}
export class HttpBearerAuth implements Authentication {
public accessToken: string | (() => string) = '';
{{^useCustomTemplateCode}}
applyToRequest(requestOptions: localVarRequest.Options): void {
{{/useCustomTemplateCode}}
{{#useCustomTemplateCode}}
applyToRequest(requestOptions: AxiosRequestConfig): void {
{{/useCustomTemplateCode}}
if (requestOptions && requestOptions.headers) {
const accessToken = typeof this.accessToken === 'function'
? this.accessToken()
: this.accessToken;
requestOptions.headers["Authorization"] = "Bearer " + accessToken;
}
}
}
export class ApiKeyAuth implements Authentication {
public apiKey: string = '';
constructor(private location: string, private paramName: string) {
}
{{^useCustomTemplateCode}}
applyToRequest(requestOptions: localVarRequest.Options): void {
{{/useCustomTemplateCode}}
{{#useCustomTemplateCode}}
applyToRequest(requestOptions: AxiosRequestConfig): void {
{{/useCustomTemplateCode}}
if (this.location == "query") {
{{^useCustomTemplateCode}}
(<any>requestOptions.qs)[this.paramName] = this.apiKey;
{{/useCustomTemplateCode}}
{{#useCustomTemplateCode}}
(<any>requestOptions.params)[this.paramName] = this.apiKey;
{{/useCustomTemplateCode}}
} else if (this.location == "header" && requestOptions && requestOptions.headers) {
requestOptions.headers[this.paramName] = this.apiKey;
} else if (this.location == 'cookie' && requestOptions && requestOptions.headers) {
if (requestOptions.headers['Cookie']) {
requestOptions.headers['Cookie'] += '; ' + this.paramName + '=' + encodeURIComponent(this.apiKey);
}
else {
requestOptions.headers['Cookie'] = this.paramName + '=' + encodeURIComponent(this.apiKey);
}
}
}
}
export class OAuth implements Authentication {
public accessToken: string = '';
{{^useCustomTemplateCode}}
applyToRequest(requestOptions: localVarRequest.Options): void {
{{/useCustomTemplateCode}}
{{#useCustomTemplateCode}}
applyToRequest(requestOptions: AxiosRequestConfig): void {
{{/useCustomTemplateCode}}
if (requestOptions && requestOptions.headers) {
requestOptions.headers["Authorization"] = "Bearer " + this.accessToken;
}
}
}
export class VoidAuth implements Authentication {
public username: string = '';
public password: string = '';
{{^useCustomTemplateCode}}
applyToRequest(_: localVarRequest.Options): void {
{{/useCustomTemplateCode}}
{{#useCustomTemplateCode}}
applyToRequest(_: AxiosRequestConfig): void {
{{/useCustomTemplateCode}}
// Do nothing
}
}
{{^useCustomTemplateCode}}
export type Interceptor = (requestOptions: localVarRequest.Options) => (Promise<void> | void);
{{/useCustomTemplateCode}}
{{#useCustomTemplateCode}}
export type Interceptor = (requestOptions: AxiosRequestConfig) => (Promise<void> | void);
{{/useCustomTemplateCode}}
{{/generateApis}}