functional programming api for typescript-fetch

This commit is contained in:
Leon Yu 2016-05-13 17:50:56 -04:00
parent 1939ce8e91
commit 885d3543df
11 changed files with 2301 additions and 11737 deletions

View File

@ -9,11 +9,18 @@ import * as assign from "core-js/library/fn/object/assign";
interface Dictionary<T> { [index: string]: T; } interface Dictionary<T> { [index: string]: T; }
export interface FetchAPI { (url: string, init?: any): Promise<any>; } export interface FetchAPI { (url: string, init?: any): Promise<any>; }
const BASE_PATH = "{{basePath}}";
export interface FetchArgs {
url: string;
options: any;
}
export class BaseAPI { export class BaseAPI {
basePath: string; basePath: string;
fetch: FetchAPI; fetch: FetchAPI;
constructor(basePath: string = "{{basePath}}", fetch: FetchAPI = isomorphicFetch) { constructor(fetch: FetchAPI = isomorphicFetch, basePath: string = BASE_PATH) {
this.basePath = basePath; this.basePath = basePath;
this.fetch = fetch; this.fetch = fetch;
} }
@ -51,19 +58,18 @@ export type {{classname}}{{datatypeWithEnum}} = {{#allowableValues}}{{#values}}"
{{#apis}} {{#apis}}
{{#operations}} {{#operations}}
{{#description}}
/** /**
* {{&description}} * {{classname}} - fetch parameter creator{{#description}}
*/ * {{&description}}{{/description}}
{{/description}} */
export class {{classname}} extends BaseAPI { export const {{classname}}FetchParamCreactor = {
{{#operation}} {{#operation}}
/** {{#summary}} /** {{#summary}}
* {{summary}}{{/summary}}{{#notes}} * {{summary}}{{/summary}}{{#notes}}
* {{notes}}{{/notes}}{{#allParams}} * {{notes}}{{/notes}}{{#allParams}}
* @param {{paramName}} {{description}}{{/allParams}} * @param {{paramName}} {{description}}{{/allParams}}
*/ */
{{nickname}}({{#hasParams}}params: { {{#allParams}} {{paramName}}{{^required}}?{{/required}}: {{{dataType}}};{{/allParams}} }{{/hasParams}}): Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}any{{/returnType}}> { {{nickname}}({{#hasParams}}params: { {{#allParams}} {{paramName}}{{^required}}?{{/required}}: {{{dataType}}};{{/allParams}} }{{/hasParams}}): FetchArgs {
{{#allParams}} {{#allParams}}
{{#required}} {{#required}}
// verify required parameter "{{paramName}}" is set // verify required parameter "{{paramName}}" is set
@ -72,7 +78,7 @@ export class {{classname}} extends BaseAPI {
} }
{{/required}} {{/required}}
{{/allParams}} {{/allParams}}
const baseUrl = `${this.basePath}{{path}}`{{#pathParams}} const baseUrl = `{{path}}`{{#pathParams}}
.replace(`{${"{{baseName}}"}}`, `${ params.{{paramName}} }`){{/pathParams}}; .replace(`{${"{{baseName}}"}}`, `${ params.{{paramName}} }`){{/pathParams}};
let urlObj = url.parse(baseUrl, true); let urlObj = url.parse(baseUrl, true);
{{#hasQueryParams}} {{#hasQueryParams}}
@ -105,13 +111,53 @@ export class {{classname}} extends BaseAPI {
fetchOptions.headers = contentTypeHeader; fetchOptions.headers = contentTypeHeader;
} }
{{/hasHeaderParam}} {{/hasHeaderParam}}
return this.fetch(url.format(urlObj), fetchOptions).then((response) => { return {
if (response.status >= 200 && response.status < 300) { url: url.format(urlObj),
return response{{#returnType}}.json(){{/returnType}}; options: fetchOptions,
} else { };
throw response; },
} {{/operation}}
}); }
/**
* {{classname}} - functional programming interface{{#description}}
* {{&description}}{{/description}}
*/
export const {{classname}}Fp = {
{{#operation}}
/** {{#summary}}
* {{summary}}{{/summary}}{{#notes}}
* {{notes}}{{/notes}}{{#allParams}}
* @param {{paramName}} {{description}}{{/allParams}}
*/
{{nickname}}({{#hasParams}}params: { {{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}; {{/allParams}} }{{/hasParams}}): (fetch: FetchAPI, basePath?: string) => Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}any{{/returnType}}> {
const fetchArgs = {{classname}}FetchParamCreactor.{{nickname}}({{#hasParams}}params{{/hasParams}});
return (fetch: FetchAPI = isomorphicFetch, basePath: string = BASE_PATH) => {
return fetch(basePath + fetchArgs.url, fetchArgs.options).then((response) => {
if (response.status >= 200 && response.status < 300) {
return response{{#returnType}}.json(){{/returnType}};
} else {
throw response;
}
});
};
},
{{/operation}}
};
/**
* {{classname}} - object-oriented interface{{#description}}
* {{&description}}{{/description}}
*/
export class {{classname}} extends BaseAPI {
{{#operation}}
/** {{#summary}}
* {{summary}}{{/summary}}{{#notes}}
* {{notes}}{{/notes}}{{#allParams}}
* @param {{paramName}} {{description}}{{/allParams}}
*/
{{nickname}}({{#hasParams}}params: { {{#allParams}} {{paramName}}{{^required}}?{{/required}}: {{{dataType}}};{{/allParams}} }{{/hasParams}}) {
return {{classname}}Fp.{{nickname}}({{#hasParams}}params{{/hasParams}})(this.fetch, this.basePath);
} }
{{/operation}} {{/operation}}
} }

View File

@ -4,7 +4,8 @@
"target": "{{#supportsES6}}es6{{/supportsES6}}{{^supportsES6}}es5{{/supportsES6}}", "target": "{{#supportsES6}}es6{{/supportsES6}}{{^supportsES6}}es5{{/supportsES6}}",
"module": "commonjs", "module": "commonjs",
"noImplicitAny": true, "noImplicitAny": true,
"outDir": "dist" "outDir": "dist",
"rootDir": "."
}, },
"exclude": [ "exclude": [
"dist", "dist",

View File

@ -4,7 +4,8 @@
"target": "es5", "target": "es5",
"module": "commonjs", "module": "commonjs",
"noImplicitAny": true, "noImplicitAny": true,
"outDir": "dist" "outDir": "dist",
"rootDir": "."
}, },
"exclude": [ "exclude": [
"dist", "dist",

View File

@ -4,7 +4,8 @@
"target": "es6", "target": "es6",
"module": "commonjs", "module": "commonjs",
"noImplicitAny": true, "noImplicitAny": true,
"outDir": "dist" "outDir": "dist",
"rootDir": "."
}, },
"exclude": [ "exclude": [
"dist", "dist",

View File

@ -4,7 +4,8 @@
"target": "es5", "target": "es5",
"module": "commonjs", "module": "commonjs",
"noImplicitAny": true, "noImplicitAny": true,
"outDir": "dist" "outDir": "dist",
"rootDir": "."
}, },
"exclude": [ "exclude": [
"dist", "dist",

File diff suppressed because it is too large Load Diff

View File

@ -1,13 +1,12 @@
import {expect} from 'chai'; import {expect} from 'chai';
import * as Swagger from 'typescript-fetch-api'; import {PetApi, Pet, Category} from 'typescript-fetch-api';
describe('PetApi', () => { describe('PetApi', () => {
let api: Swagger.PetApi; let api: PetApi;
let fixture: Pet = createTestFixture();
let fixture: Swagger.Pet = createTestFixture();
beforeEach(() => { beforeEach(() => {
api = new Swagger.PetApi(); api = new PetApi();
}); });
it('should add and delete Pet', () => { it('should add and delete Pet', () => {
@ -40,19 +39,18 @@ describe('PetApi', () => {
return api.getPetById({ petId: fixture.id }).then((result) => { return api.getPetById({ petId: fixture.id }).then((result) => {
return expect(result).to.not.exist; return expect(result).to.not.exist;
}, (err) => { }, (err) => {
console.log(err)
return expect(err).to.exist; return expect(err).to.exist;
}); });
}); });
}); });
function createTestFixture(ts = Date.now()) { function createTestFixture(ts = Date.now()) {
const category: Swagger.Category = { const category: Category = {
'id': ts, 'id': ts,
'name': `category${ts}`, 'name': `category${ts}`,
}; };
const pet: Swagger.Pet = { const pet: Pet = {
'id': ts, 'id': ts,
'name': `pet${ts}`, 'name': `pet${ts}`,
'category': category, 'category': category,

View File

@ -1,11 +1,11 @@
import {expect} from 'chai'; import {expect} from 'chai';
import * as Swagger from 'typescript-fetch-api'; import {StoreApi} from 'typescript-fetch-api';
describe('StoreApi', function() { describe('StoreApi', function() {
let api: Swagger.StoreApi; let api: StoreApi;
beforeEach(function() { beforeEach(function() {
api = new Swagger.StoreApi(); api = new StoreApi();
}); });
it('should get inventory', function() { it('should get inventory', function() {