Skip to content

Commit

Permalink
Feature/typescript fetch/map templating (#2913)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ehvattum authored and wing328 committed May 20, 2019
1 parent d5e24f0 commit 87c9de2
Show file tree
Hide file tree
Showing 30 changed files with 78 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { exists } from '../runtime';
import { exists, mapValues } from '../runtime';
{{#hasImports}}
import {
{{#imports}}
Expand Down Expand Up @@ -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}}
};
Expand All @@ -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}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Do not edit the class manually.
*/

import { exists } from '../runtime';
import { exists, mapValues } from '../runtime';
import {
Category,
CategoryFromJSON,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Do not edit the class manually.
*/

import { exists } from '../runtime';
import { exists, mapValues } from '../runtime';
import {
Category,
CategoryFromJSON,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Do not edit the class manually.
*/

import { exists } from '../runtime';
import { exists, mapValues } from '../runtime';
import {
Category,
CategoryFromJSON,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Do not edit the class manually.
*/

import { exists } from '../runtime';
import { exists, mapValues } from '../runtime';
import {
Category,
CategoryFromJSON,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 87c9de2

Please sign in to comment.