forked from loafle/openapi-generator-original
Feature/typescript fetch/map templating (#2913)
* Add openapi dictionary/map support to typescript-fetch client generator Change isContainer -> isListContainer for existing array support. Add isMapContainer control flow, adding map support. Add utility function to help map openapi map/dictionaries to ts maps. Close #1878 * Run typescript-fetch generator scripts and update output files
This commit is contained in:
committed by
William Cheng
parent
d5e24f0111
commit
87c9de210f
@@ -1,4 +1,4 @@
|
||||
import { exists } from '../runtime';
|
||||
import { exists, mapValues } from '../runtime';
|
||||
{{#hasImports}}
|
||||
import {
|
||||
{{#imports}}
|
||||
@@ -46,17 +46,22 @@ export function {{classname}}FromJSON(json: any): {{classname}} {
|
||||
{{/isDate}}
|
||||
{{/isPrimitiveType}}
|
||||
{{^isPrimitiveType}}
|
||||
{{#isContainer}}
|
||||
{{#isListContainer}}
|
||||
'{{name}}': {{^required}}!exists(json, '{{baseName}}') ? undefined : {{/required}}(json['{{baseName}}'] as Array<any>).map({{#items}}{{datatype}}{{/items}}FromJSON),
|
||||
{{/isContainer}}
|
||||
{{^isContainer}}
|
||||
{{/isListContainer}}
|
||||
{{#isMapContainer}}
|
||||
'{{name}}': {{^required}}!exists(json, '{{baseName}}') ? undefined : {{/required}}mapValues(json['{{baseName}}'], {{#items}}{{datatype}}{{/items}}FromJSON),
|
||||
{{/isMapContainer}}
|
||||
{{^isListContainer}}
|
||||
{{^isMapContainer}}
|
||||
{{^isFreeFormObject}}
|
||||
'{{name}}': {{^required}}!exists(json, '{{baseName}}') ? undefined : {{/required}}{{datatype}}FromJSON(json['{{baseName}}']),
|
||||
{{/isFreeFormObject}}
|
||||
{{#isFreeFormObject}}
|
||||
'{{name}}': {{^required}}!exists(json, '{{baseName}}') ? undefined : {{/required}}json['{{baseName}}'],
|
||||
{{/isFreeFormObject}}
|
||||
{{/isContainer}}
|
||||
{{/isMapContainer}}
|
||||
{{/isListContainer}}
|
||||
{{/isPrimitiveType}}
|
||||
{{/allVars}}
|
||||
};
|
||||
@@ -78,17 +83,22 @@ export function {{classname}}ToJSON(value?: {{classname}}): any {
|
||||
'{{baseName}}': {{#isDate}}{{^required}}value.{{name}} === undefined ? undefined : {{/required}}value.{{name}}.toISOString().substr(0,10){{/isDate}}{{#isDateTime}}{{^required}}value.{{name}} === undefined ? undefined : {{/required}}value.{{name}}.toISOString(){{/isDateTime}}{{^isDate}}{{^isDateTime}}value.{{name}}{{/isDateTime}}{{/isDate}},
|
||||
{{/isPrimitiveType}}
|
||||
{{^isPrimitiveType}}
|
||||
{{#isContainer}}
|
||||
{{#isListContainer}}
|
||||
'{{baseName}}': {{^required}}value.{{name}} === undefined ? undefined : {{/required}}(value.{{name}} as Array<any>).map({{#items}}{{datatype}}{{/items}}ToJSON),
|
||||
{{/isContainer}}
|
||||
{{^isContainer}}
|
||||
{{/isListContainer}}
|
||||
{{#isMapContainer}}
|
||||
'{{baseName}}': {{^required}}value.{{name}} === undefined ? undefined : {{/required}}mapValues(value.{{name}}, {{#items}}{{datatype}}{{/items}}ToJSON),
|
||||
{{/isMapContainer}}
|
||||
{{^isListContainer}}
|
||||
{{^isMapContainer}}
|
||||
{{^isFreeFormObject}}
|
||||
'{{baseName}}': {{datatype}}ToJSON(value.{{name}}),
|
||||
{{/isFreeFormObject}}
|
||||
{{#isFreeFormObject}}
|
||||
'{{baseName}}': value.{{name}},
|
||||
{{/isFreeFormObject}}
|
||||
{{/isContainer}}
|
||||
{{/isMapContainer}}
|
||||
{{/isListContainer}}
|
||||
{{/isPrimitiveType}}
|
||||
{{/isReadOnly}}
|
||||
{{/allVars}}
|
||||
|
||||
@@ -202,6 +202,13 @@ export function querystring(params: HTTPQuery, prefix: string = ''): string {
|
||||
.join('&');
|
||||
}
|
||||
|
||||
export function mapValues(data: any, fn: (item: any) => any) {
|
||||
return Object.keys(data).reduce(
|
||||
(acc, key) => ({ ...acc, [key]: fn(data[key]) }),
|
||||
{}
|
||||
);
|
||||
}
|
||||
|
||||
export interface RequestContext {
|
||||
fetch: FetchAPI;
|
||||
url: string;
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
import { exists } from '../runtime';
|
||||
import { exists, mapValues } from '../runtime';
|
||||
/**
|
||||
* A category for a pet
|
||||
* @export
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
import { exists } from '../runtime';
|
||||
import { exists, mapValues } from '../runtime';
|
||||
/**
|
||||
* Describes the result of uploading an image resource
|
||||
* @export
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
import { exists } from '../runtime';
|
||||
import { exists, mapValues } from '../runtime';
|
||||
/**
|
||||
* An order for a pets from the pet store
|
||||
* @export
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
import { exists } from '../runtime';
|
||||
import { exists, mapValues } from '../runtime';
|
||||
import {
|
||||
Category,
|
||||
CategoryFromJSON,
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
import { exists } from '../runtime';
|
||||
import { exists, mapValues } from '../runtime';
|
||||
/**
|
||||
* A tag for a pet
|
||||
* @export
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
import { exists } from '../runtime';
|
||||
import { exists, mapValues } from '../runtime';
|
||||
/**
|
||||
* A User who is purchasing from the pet store
|
||||
* @export
|
||||
|
||||
@@ -213,6 +213,13 @@ export function querystring(params: HTTPQuery, prefix: string = ''): string {
|
||||
.join('&');
|
||||
}
|
||||
|
||||
export function mapValues(data: any, fn: (item: any) => any) {
|
||||
return Object.keys(data).reduce(
|
||||
(acc, key) => ({ ...acc, [key]: fn(data[key]) }),
|
||||
{}
|
||||
);
|
||||
}
|
||||
|
||||
export interface RequestContext {
|
||||
fetch: FetchAPI;
|
||||
url: string;
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
import { exists } from '../runtime';
|
||||
import { exists, mapValues } from '../runtime';
|
||||
/**
|
||||
* A category for a pet
|
||||
* @export
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
import { exists } from '../runtime';
|
||||
import { exists, mapValues } from '../runtime';
|
||||
/**
|
||||
* Describes the result of uploading an image resource
|
||||
* @export
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
import { exists } from '../runtime';
|
||||
import { exists, mapValues } from '../runtime';
|
||||
/**
|
||||
* An order for a pets from the pet store
|
||||
* @export
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
import { exists } from '../runtime';
|
||||
import { exists, mapValues } from '../runtime';
|
||||
import {
|
||||
Category,
|
||||
CategoryFromJSON,
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
import { exists } from '../runtime';
|
||||
import { exists, mapValues } from '../runtime';
|
||||
/**
|
||||
* A tag for a pet
|
||||
* @export
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
import { exists } from '../runtime';
|
||||
import { exists, mapValues } from '../runtime';
|
||||
/**
|
||||
* A User who is purchasing from the pet store
|
||||
* @export
|
||||
|
||||
@@ -213,6 +213,13 @@ export function querystring(params: HTTPQuery, prefix: string = ''): string {
|
||||
.join('&');
|
||||
}
|
||||
|
||||
export function mapValues(data: any, fn: (item: any) => any) {
|
||||
return Object.keys(data).reduce(
|
||||
(acc, key) => ({ ...acc, [key]: fn(data[key]) }),
|
||||
{}
|
||||
);
|
||||
}
|
||||
|
||||
export interface RequestContext {
|
||||
fetch: FetchAPI;
|
||||
url: string;
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
import { exists } from '../runtime';
|
||||
import { exists, mapValues } from '../runtime';
|
||||
/**
|
||||
* A category for a pet
|
||||
* @export
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
import { exists } from '../runtime';
|
||||
import { exists, mapValues } from '../runtime';
|
||||
/**
|
||||
* Describes the result of uploading an image resource
|
||||
* @export
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
import { exists } from '../runtime';
|
||||
import { exists, mapValues } from '../runtime';
|
||||
/**
|
||||
* An order for a pets from the pet store
|
||||
* @export
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
import { exists } from '../runtime';
|
||||
import { exists, mapValues } from '../runtime';
|
||||
import {
|
||||
Category,
|
||||
CategoryFromJSON,
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
import { exists } from '../runtime';
|
||||
import { exists, mapValues } from '../runtime';
|
||||
/**
|
||||
* A tag for a pet
|
||||
* @export
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
import { exists } from '../runtime';
|
||||
import { exists, mapValues } from '../runtime';
|
||||
/**
|
||||
* A User who is purchasing from the pet store
|
||||
* @export
|
||||
|
||||
@@ -213,6 +213,13 @@ export function querystring(params: HTTPQuery, prefix: string = ''): string {
|
||||
.join('&');
|
||||
}
|
||||
|
||||
export function mapValues(data: any, fn: (item: any) => any) {
|
||||
return Object.keys(data).reduce(
|
||||
(acc, key) => ({ ...acc, [key]: fn(data[key]) }),
|
||||
{}
|
||||
);
|
||||
}
|
||||
|
||||
export interface RequestContext {
|
||||
fetch: FetchAPI;
|
||||
url: string;
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
import { exists } from '../runtime';
|
||||
import { exists, mapValues } from '../runtime';
|
||||
/**
|
||||
* A category for a pet
|
||||
* @export
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
import { exists } from '../runtime';
|
||||
import { exists, mapValues } from '../runtime';
|
||||
/**
|
||||
* Describes the result of uploading an image resource
|
||||
* @export
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
import { exists } from '../runtime';
|
||||
import { exists, mapValues } from '../runtime';
|
||||
/**
|
||||
* An order for a pets from the pet store
|
||||
* @export
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
import { exists } from '../runtime';
|
||||
import { exists, mapValues } from '../runtime';
|
||||
import {
|
||||
Category,
|
||||
CategoryFromJSON,
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
import { exists } from '../runtime';
|
||||
import { exists, mapValues } from '../runtime';
|
||||
/**
|
||||
* A tag for a pet
|
||||
* @export
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
import { exists } from '../runtime';
|
||||
import { exists, mapValues } from '../runtime';
|
||||
/**
|
||||
* A User who is purchasing from the pet store
|
||||
* @export
|
||||
|
||||
@@ -213,6 +213,13 @@ export function querystring(params: HTTPQuery, prefix: string = ''): string {
|
||||
.join('&');
|
||||
}
|
||||
|
||||
export function mapValues(data: any, fn: (item: any) => any) {
|
||||
return Object.keys(data).reduce(
|
||||
(acc, key) => ({ ...acc, [key]: fn(data[key]) }),
|
||||
{}
|
||||
);
|
||||
}
|
||||
|
||||
export interface RequestContext {
|
||||
fetch: FetchAPI;
|
||||
url: string;
|
||||
|
||||
Reference in New Issue
Block a user