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:
Erik Hvattum
2019-05-20 10:54:43 +02:00
committed by William Cheng
parent d5e24f0111
commit 87c9de210f
30 changed files with 78 additions and 33 deletions

View File

@@ -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}}

View File

@@ -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;

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -11,7 +11,7 @@
* Do not edit the class manually.
*/
import { exists } from '../runtime';
import { exists, mapValues } from '../runtime';
import {
Category,
CategoryFromJSON,

View File

@@ -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

View File

@@ -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

View File

@@ -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;

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -11,7 +11,7 @@
* Do not edit the class manually.
*/
import { exists } from '../runtime';
import { exists, mapValues } from '../runtime';
import {
Category,
CategoryFromJSON,

View File

@@ -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

View File

@@ -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

View File

@@ -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;

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -11,7 +11,7 @@
* Do not edit the class manually.
*/
import { exists } from '../runtime';
import { exists, mapValues } from '../runtime';
import {
Category,
CategoryFromJSON,

View File

@@ -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

View File

@@ -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

View File

@@ -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;

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -11,7 +11,7 @@
* Do not edit the class manually.
*/
import { exists } from '../runtime';
import { exists, mapValues } from '../runtime';
import {
Category,
CategoryFromJSON,

View File

@@ -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

View File

@@ -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

View File

@@ -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;