adjust frontend to new backend
This commit is contained in:
261
templates/model/ObjectSerializer.mustache
Normal file
261
templates/model/ObjectSerializer.mustache
Normal file
@@ -0,0 +1,261 @@
|
||||
{{#models}}
|
||||
{{#model}}
|
||||
export * from '{{{ importPath }}}{{extensionForDeno}}';
|
||||
{{/model}}
|
||||
{{/models}}
|
||||
|
||||
{{#models}}
|
||||
{{#model}}
|
||||
import { {{classname}}{{#hasEnums}}{{#vars}}{{#isEnum}}, {{classname}}{{enumName}} {{/isEnum}} {{/vars}}{{/hasEnums}} } from '{{{ importPath }}}{{extensionForDeno}}';
|
||||
{{/model}}
|
||||
{{/models}}
|
||||
|
||||
/* tslint:disable:no-unused-variable */
|
||||
let primitives = [
|
||||
"string",
|
||||
"boolean",
|
||||
"double",
|
||||
"integer",
|
||||
"long",
|
||||
"float",
|
||||
"number",
|
||||
"any"
|
||||
];
|
||||
|
||||
const supportedMediaTypes: { [mediaType: string]: number } = {
|
||||
"application/json": Infinity,
|
||||
"application/octet-stream": 0,
|
||||
"application/x-www-form-urlencoded": 0
|
||||
}
|
||||
|
||||
|
||||
let enumsMap: Set<string> = new Set<string>([
|
||||
{{#models}}
|
||||
{{#model}}
|
||||
{{#isEnum}}
|
||||
"{{classname}}{{enumName}}",
|
||||
{{/isEnum}}
|
||||
{{#hasEnums}}
|
||||
{{#vars}}
|
||||
{{#isEnum}}
|
||||
"{{classname}}{{enumName}}",
|
||||
{{/isEnum}}
|
||||
{{/vars}}
|
||||
{{/hasEnums}}
|
||||
{{/model}}
|
||||
{{/models}}
|
||||
]);
|
||||
|
||||
let typeMap: {[index: string]: any} = {
|
||||
{{#models}}
|
||||
{{#model}}
|
||||
{{^isEnum}}
|
||||
"{{classname}}": {{classname}},
|
||||
{{/isEnum}}
|
||||
{{/model}}
|
||||
{{/models}}
|
||||
}
|
||||
|
||||
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.has(expectedType)) {
|
||||
return expectedType;
|
||||
}
|
||||
|
||||
if (!typeMap[expectedType]) {
|
||||
return expectedType; // w/e we don't know the type
|
||||
}
|
||||
|
||||
// Check the discriminator
|
||||
let discriminatorProperty = typeMap[expectedType].discriminator;
|
||||
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, format: string) {
|
||||
if (data == undefined) {
|
||||
return data;
|
||||
} else if (primitives.indexOf(type.toLowerCase()) !== -1) {
|
||||
return data;
|
||||
} else if (type.lastIndexOf("Array<", 0) === 0) { // string.startsWith pre es6
|
||||
let subType: string = type.replace("Array<", ""); // Array<Type> => Type>
|
||||
subType = subType.substring(0, subType.length - 1); // Type> => Type
|
||||
let transformedData: any[] = [];
|
||||
for (let index in data) {
|
||||
let date = data[index];
|
||||
transformedData.push(ObjectSerializer.serialize(date, subType, format));
|
||||
}
|
||||
return transformedData;
|
||||
} else if (type === "Date") {
|
||||
if (format == "date") {
|
||||
let month = data.getMonth()+1
|
||||
month = month < 10 ? "0" + month.toString() : month.toString()
|
||||
let day = data.getDate();
|
||||
day = day < 10 ? "0" + day.toString() : day.toString();
|
||||
|
||||
return data.getFullYear() + "-" + month + "-" + day;
|
||||
} else {
|
||||
return data.toISOString();
|
||||
}
|
||||
} else {
|
||||
if (enumsMap.has(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 in attributeTypes) {
|
||||
let attributeType = attributeTypes[index];
|
||||
instance[attributeType.baseName] = ObjectSerializer.serialize(data[attributeType.name], attributeType.type, attributeType.format);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
public static deserialize(data: any, type: string, format: string) {
|
||||
// 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 (type.lastIndexOf("Array<", 0) === 0) { // string.startsWith pre es6
|
||||
let subType: string = type.replace("Array<", ""); // Array<Type> => Type>
|
||||
subType = subType.substring(0, subType.length - 1); // Type> => Type
|
||||
let transformedData: any[] = [];
|
||||
for (let index in data) {
|
||||
let date = data[index];
|
||||
transformedData.push(ObjectSerializer.deserialize(date, subType, format));
|
||||
}
|
||||
return transformedData;
|
||||
} else if (type === "Date") {
|
||||
return new Date(data);
|
||||
} else {
|
||||
if (enumsMap.has(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 in attributeTypes) {
|
||||
let attributeType = attributeTypes[index];
|
||||
let value = ObjectSerializer.deserialize(data[attributeType.baseName], attributeType.type, attributeType.format);
|
||||
if (value !== undefined) {
|
||||
instance[attributeType.name] = value;
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Normalize media type
|
||||
*
|
||||
* We currently do not handle any media types attributes, i.e. anything
|
||||
* after a semicolon. All content is assumed to be UTF-8 compatible.
|
||||
*/
|
||||
public static normalizeMediaType(mediaType: string | undefined): string | undefined {
|
||||
if (mediaType === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
return mediaType.split(";")[0].trim().toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* From a list of possible media types, choose the one we can handle best.
|
||||
*
|
||||
* The order of the given media types does not have any impact on the choice
|
||||
* made.
|
||||
*/
|
||||
public static getPreferredMediaType(mediaTypes: Array<string>): string {
|
||||
/** According to OAS 3 we should default to json */
|
||||
if (!mediaTypes) {
|
||||
return "application/json";
|
||||
}
|
||||
|
||||
const normalMediaTypes = mediaTypes.map(this.normalizeMediaType);
|
||||
let selectedMediaType: string | undefined = undefined;
|
||||
let selectedRank: number = -Infinity;
|
||||
for (const mediaType of normalMediaTypes) {
|
||||
if (supportedMediaTypes[mediaType!] > selectedRank) {
|
||||
selectedMediaType = mediaType;
|
||||
selectedRank = supportedMediaTypes[mediaType!];
|
||||
}
|
||||
}
|
||||
|
||||
if (selectedMediaType === undefined) {
|
||||
throw new Error("None of the given media types are supported: " + mediaTypes.join(", "));
|
||||
}
|
||||
|
||||
return selectedMediaType!;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert data to a string according the given media type
|
||||
*/
|
||||
public static stringify(data: any, mediaType: string): string {
|
||||
if (mediaType === "text/plain") {
|
||||
return String(data);
|
||||
}
|
||||
|
||||
if (mediaType === "application/json") {
|
||||
return JSON.stringify(data);
|
||||
}
|
||||
|
||||
throw new Error("The mediaType " + mediaType + " is not supported by ObjectSerializer.stringify.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse data from a string according to the given media type
|
||||
*/
|
||||
public static parse(rawData: string, mediaType: string | undefined) {
|
||||
if (mediaType === undefined) {
|
||||
throw new Error("Cannot parse content. No Content-Type defined.");
|
||||
}
|
||||
|
||||
if (mediaType === "text/plain") {
|
||||
return rawData;
|
||||
}
|
||||
|
||||
if (mediaType === "application/json") {
|
||||
return JSON.parse(rawData);
|
||||
}
|
||||
|
||||
if (mediaType === "text/html") {
|
||||
return rawData;
|
||||
}
|
||||
|
||||
throw new Error("The mediaType " + mediaType + " is not supported by ObjectSerializer.parse.");
|
||||
}
|
||||
}
|
||||
126
templates/model/model.mustache
Normal file
126
templates/model/model.mustache
Normal file
@@ -0,0 +1,126 @@
|
||||
{{>licenseInfo}}
|
||||
{{#models}}
|
||||
{{#model}}
|
||||
{{#tsImports}}
|
||||
import { {{classname}} } from '{{filename}}{{extensionForDeno}}';
|
||||
{{/tsImports}}
|
||||
import { HttpFile } from '../http/http{{extensionForDeno}}';
|
||||
|
||||
{{#description}}
|
||||
/**
|
||||
* {{{.}}}
|
||||
*/
|
||||
{{/description}}
|
||||
{{^isEnum}}
|
||||
export interface I{{classname}} {{#parent}}extends I{{{.}}} {{/parent}}{
|
||||
{{#vars}}
|
||||
{{#description}}
|
||||
/**
|
||||
* {{{.}}}
|
||||
*/
|
||||
{{/description}}
|
||||
'{{name}}'?: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}};
|
||||
{{/vars}}
|
||||
}
|
||||
|
||||
export class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}implements I{{classname}} {
|
||||
{{#vars}}
|
||||
{{#description}}
|
||||
/**
|
||||
* {{{.}}}
|
||||
*/
|
||||
{{/description}}
|
||||
'{{name}}'{{#required}}!{{/required}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}};
|
||||
{{/vars}}
|
||||
|
||||
{{#discriminator}}
|
||||
static readonly discriminator: string | undefined = "{{discriminatorName}}";
|
||||
{{/discriminator}}
|
||||
{{^discriminator}}
|
||||
static readonly discriminator: string | undefined = undefined;
|
||||
{{/discriminator}}
|
||||
|
||||
{{^isArray}}
|
||||
static readonly attributeTypeMap: Array<{name: string, baseName: string, type: string, format: string, required?: boolean, minLength?: number, maxLength?: number, min?: number, max?: number, pattern?: RegExp}> = [
|
||||
{{#vars}}
|
||||
{
|
||||
"name": "{{name}}",
|
||||
"baseName": "{{baseName}}",
|
||||
"type": "{{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}",
|
||||
"format": "{{dataFormat}}"
|
||||
{{#required}},"required": {{required}}{{/required}}
|
||||
{{#minLength}},"minLength": {{minLength}}{{/minLength}}
|
||||
{{#maxLength}},"maxLength": {{maxLength}}{{/maxLength}}
|
||||
{{#min}},"min": {{min}}{{/min}}
|
||||
{{#max}},"max": {{max}}{{/max}}
|
||||
{{#pattern}},"pattern": {{pattern}}{{/pattern}}
|
||||
}{{^-last}},
|
||||
{{/-last}}
|
||||
{{/vars}}
|
||||
];
|
||||
|
||||
static getAttributeTypeMap() {
|
||||
{{#parent}}
|
||||
return super.getAttributeTypeMap().concat({{classname}}.attributeTypeMap);
|
||||
{{/parent}}
|
||||
{{^parent}}
|
||||
return {{classname}}.attributeTypeMap;
|
||||
{{/parent}}
|
||||
}
|
||||
{{/isArray}}
|
||||
|
||||
public constructor(json?: I{{classname}} | any) {
|
||||
{{#parent}}
|
||||
super();
|
||||
{{/parent}}
|
||||
{{#allVars}}
|
||||
{{#discriminatorValue}}
|
||||
this.{{name}} = "{{discriminatorValue}}";
|
||||
{{/discriminatorValue}}
|
||||
{{/allVars}}
|
||||
{{#discriminatorName}}
|
||||
this.{{discriminatorName}} = "{{classname}}";
|
||||
{{/discriminatorName}}
|
||||
this.init(json);
|
||||
}
|
||||
|
||||
{{^isArray}}
|
||||
static fromJson(json?: I{{classname}} | any): {{classname}} {
|
||||
return new {{classname}}().init(json);
|
||||
}
|
||||
|
||||
init(json?: I{{classname}} | any): this {
|
||||
{{#parent}}
|
||||
super.init(json);
|
||||
{{/parent}}
|
||||
{{#vars}}
|
||||
this['{{name}}'] = {{#isDateTime}}(json && (['number', 'string'].includes(typeof json['{{name}}']) || json['{{name}}'] instanceof Date)) ? new Date(json['{{name}}'] as any){{/isDateTime}}{{^isDateTime}}json ? {{#isModel}}{{dataType}}.fromJson(json['{{name}}']){{/isModel}}{{^isModel}}json['{{name}}']{{/isModel}}{{/isDateTime}} : {{defaultValue}} as any;
|
||||
{{/vars}}
|
||||
return this;
|
||||
}
|
||||
|
||||
toJson(): any {
|
||||
const ret: any = {};
|
||||
{{#vars}}
|
||||
ret['{{name}}'] = {{#isDateTime}}this.{{name}} instanceof Date ? this.{{name}}.toISOString() : this.{{name}}{{/isDateTime}}{{^isDateTime}}this['{{name}}']{{#isModel}}?.toJson(){{/isModel}}{{/isDateTime}};
|
||||
{{/vars}}
|
||||
return ret;
|
||||
}
|
||||
{{/isArray}}
|
||||
}
|
||||
|
||||
{{#hasEnums}}
|
||||
|
||||
{{#vars}}
|
||||
{{#isEnum}}
|
||||
export type {{classname}}{{enumName}} ={{#allowableValues}}{{#values}} "{{.}}" {{^-last}}|{{/-last}}{{/values}}{{/allowableValues}};
|
||||
{{/isEnum}}
|
||||
{{/vars}}
|
||||
|
||||
{{/hasEnums}}
|
||||
{{/isEnum}}
|
||||
{{#isEnum}}
|
||||
export type {{classname}} ={{#allowableValues}}{{#values}} "{{.}}" {{^-last}}|{{/-last}}{{/values}}{{/allowableValues}};
|
||||
{{/isEnum}}
|
||||
{{/model}}
|
||||
{{/models}}
|
||||
5
templates/model/models_all.mustache
Normal file
5
templates/model/models_all.mustache
Normal file
@@ -0,0 +1,5 @@
|
||||
{{#models}}
|
||||
{{#model}}
|
||||
export * from '{{{ importPath }}}{{extensionForDeno}}'
|
||||
{{/model}}
|
||||
{{/models}}
|
||||
Reference in New Issue
Block a user