mirror of
https://github.com/valitydev/openapi-generator.git
synced 2024-11-07 19:08:52 +00:00
functional programming api for typescript-fetch
This commit is contained in:
parent
1939ce8e91
commit
885d3543df
@ -9,11 +9,18 @@ import * as assign from "core-js/library/fn/object/assign";
|
||||
interface Dictionary<T> { [index: string]: T; }
|
||||
export interface FetchAPI { (url: string, init?: any): Promise<any>; }
|
||||
|
||||
const BASE_PATH = "{{basePath}}";
|
||||
|
||||
export interface FetchArgs {
|
||||
url: string;
|
||||
options: any;
|
||||
}
|
||||
|
||||
export class BaseAPI {
|
||||
basePath: string;
|
||||
fetch: FetchAPI;
|
||||
|
||||
constructor(basePath: string = "{{basePath}}", fetch: FetchAPI = isomorphicFetch) {
|
||||
constructor(fetch: FetchAPI = isomorphicFetch, basePath: string = BASE_PATH) {
|
||||
this.basePath = basePath;
|
||||
this.fetch = fetch;
|
||||
}
|
||||
@ -51,19 +58,18 @@ export type {{classname}}{{datatypeWithEnum}} = {{#allowableValues}}{{#values}}"
|
||||
{{#apis}}
|
||||
{{#operations}}
|
||||
|
||||
{{#description}}
|
||||
/**
|
||||
* {{&description}}
|
||||
*/
|
||||
{{/description}}
|
||||
export class {{classname}} extends BaseAPI {
|
||||
* {{classname}} - fetch parameter creator{{#description}}
|
||||
* {{&description}}{{/description}}
|
||||
*/
|
||||
export const {{classname}}FetchParamCreactor = {
|
||||
{{#operation}}
|
||||
/** {{#summary}}
|
||||
* {{summary}}{{/summary}}{{#notes}}
|
||||
* {{notes}}{{/notes}}{{#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}}
|
||||
{{#required}}
|
||||
// verify required parameter "{{paramName}}" is set
|
||||
@ -72,7 +78,7 @@ export class {{classname}} extends BaseAPI {
|
||||
}
|
||||
{{/required}}
|
||||
{{/allParams}}
|
||||
const baseUrl = `${this.basePath}{{path}}`{{#pathParams}}
|
||||
const baseUrl = `{{path}}`{{#pathParams}}
|
||||
.replace(`{${"{{baseName}}"}}`, `${ params.{{paramName}} }`){{/pathParams}};
|
||||
let urlObj = url.parse(baseUrl, true);
|
||||
{{#hasQueryParams}}
|
||||
@ -105,13 +111,53 @@ export class {{classname}} extends BaseAPI {
|
||||
fetchOptions.headers = contentTypeHeader;
|
||||
}
|
||||
{{/hasHeaderParam}}
|
||||
return this.fetch(url.format(urlObj), fetchOptions).then((response) => {
|
||||
if (response.status >= 200 && response.status < 300) {
|
||||
return response{{#returnType}}.json(){{/returnType}};
|
||||
} else {
|
||||
throw response;
|
||||
}
|
||||
});
|
||||
return {
|
||||
url: url.format(urlObj),
|
||||
options: fetchOptions,
|
||||
};
|
||||
},
|
||||
{{/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}}
|
||||
}
|
||||
|
@ -4,7 +4,8 @@
|
||||
"target": "{{#supportsES6}}es6{{/supportsES6}}{{^supportsES6}}es5{{/supportsES6}}",
|
||||
"module": "commonjs",
|
||||
"noImplicitAny": true,
|
||||
"outDir": "dist"
|
||||
"outDir": "dist",
|
||||
"rootDir": "."
|
||||
},
|
||||
"exclude": [
|
||||
"dist",
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -4,7 +4,8 @@
|
||||
"target": "es5",
|
||||
"module": "commonjs",
|
||||
"noImplicitAny": true,
|
||||
"outDir": "dist"
|
||||
"outDir": "dist",
|
||||
"rootDir": "."
|
||||
},
|
||||
"exclude": [
|
||||
"dist",
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -4,7 +4,8 @@
|
||||
"target": "es6",
|
||||
"module": "commonjs",
|
||||
"noImplicitAny": true,
|
||||
"outDir": "dist"
|
||||
"outDir": "dist",
|
||||
"rootDir": "."
|
||||
},
|
||||
"exclude": [
|
||||
"dist",
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -4,7 +4,8 @@
|
||||
"target": "es5",
|
||||
"module": "commonjs",
|
||||
"noImplicitAny": true,
|
||||
"outDir": "dist"
|
||||
"outDir": "dist",
|
||||
"rootDir": "."
|
||||
},
|
||||
"exclude": [
|
||||
"dist",
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,13 +1,12 @@
|
||||
import {expect} from 'chai';
|
||||
import * as Swagger from 'typescript-fetch-api';
|
||||
import {PetApi, Pet, Category} from 'typescript-fetch-api';
|
||||
|
||||
describe('PetApi', () => {
|
||||
let api: Swagger.PetApi;
|
||||
|
||||
let fixture: Swagger.Pet = createTestFixture();
|
||||
let api: PetApi;
|
||||
let fixture: Pet = createTestFixture();
|
||||
|
||||
beforeEach(() => {
|
||||
api = new Swagger.PetApi();
|
||||
api = new PetApi();
|
||||
});
|
||||
|
||||
it('should add and delete Pet', () => {
|
||||
@ -40,19 +39,18 @@ describe('PetApi', () => {
|
||||
return api.getPetById({ petId: fixture.id }).then((result) => {
|
||||
return expect(result).to.not.exist;
|
||||
}, (err) => {
|
||||
console.log(err)
|
||||
return expect(err).to.exist;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function createTestFixture(ts = Date.now()) {
|
||||
const category: Swagger.Category = {
|
||||
const category: Category = {
|
||||
'id': ts,
|
||||
'name': `category${ts}`,
|
||||
};
|
||||
|
||||
const pet: Swagger.Pet = {
|
||||
const pet: Pet = {
|
||||
'id': ts,
|
||||
'name': `pet${ts}`,
|
||||
'category': category,
|
||||
|
@ -1,11 +1,11 @@
|
||||
import {expect} from 'chai';
|
||||
import * as Swagger from 'typescript-fetch-api';
|
||||
import {StoreApi} from 'typescript-fetch-api';
|
||||
|
||||
describe('StoreApi', function() {
|
||||
let api: Swagger.StoreApi;
|
||||
let api: StoreApi;
|
||||
|
||||
beforeEach(function() {
|
||||
api = new Swagger.StoreApi();
|
||||
api = new StoreApi();
|
||||
});
|
||||
|
||||
it('should get inventory', function() {
|
||||
|
Loading…
Reference in New Issue
Block a user