From 44078ef60544783625c5eadc26e8e1fbf201acb5 Mon Sep 17 00:00:00 2001 From: Jan Michael VIncent Trinidad Date: Sat, 26 Sep 2020 18:12:18 +0400 Subject: [PATCH] feat: use fetch from datasource and fetchParamCreator to create request --- lib/typescript-fetch/api.mustache | 49 ++++--------------------------- 1 file changed, 6 insertions(+), 43 deletions(-) diff --git a/lib/typescript-fetch/api.mustache b/lib/typescript-fetch/api.mustache index 14758af..de9314b 100644 --- a/lib/typescript-fetch/api.mustache +++ b/lib/typescript-fetch/api.mustache @@ -99,51 +99,14 @@ export class {{classname}}Api extends RESTDataSource { super(); this.baseURL = baseUrl; } + paramCreator = {{classname}}FetchParamCreator() {{#operation}} - {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}): Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Response{{/returnType}}> { + {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}): Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Response{{/returnType}}> { + const args = this.paramCreator.{{nickname}}({{#allParams}}{{paramName}}, {{/allParams}}) - const localVarPath = `{{{path}}}`{{#pathParams}} - .replace(`{${"{{baseName}}"}}`, encodeURIComponent(String({{paramName}}))){{/pathParams}}; - - const localVarUrlObj = url.parse(localVarPath, true); - - const localVarQueryParameter = {} - {{#queryParams}} - {{#isListContainer}} - if ({{paramName}}) { - {{#isCollectionFormatMulti}} - localVarQueryParameter['{{baseName}}'] = {{paramName}}; - {{/isCollectionFormatMulti}} - {{^isCollectionFormatMulti}} - localVarQueryParameter['{{baseName}}'] = {{paramName}}.join(COLLECTION_FORMATS["{{collectionFormat}}"]); - {{/isCollectionFormatMulti}} - } - {{/isListContainer}} - {{^isListContainer}} - if ({{paramName}} !== undefined) { - {{#isDateTime}} - localVarQueryParameter['{{baseName}}'] = ({{paramName}} as any).toISOString(); - {{/isDateTime}} - {{^isDateTime}} - {{#isDate}} - localVarQueryParameter['{{baseName}}'] = ({{paramName}} as any).toISOString(); - {{/isDate}} - {{^isDate}} - localVarQueryParameter['{{baseName}}'] = {{paramName}}; - {{/isDate}} - {{/isDateTime}} - } - {{/isListContainer}} - - {{/queryParams}} - - localVarUrlObj.query = Object.assign({}, localVarUrlObj.query, localVarQueryParameter); - - delete localVarUrlObj.search; - - - return this.{{httpMethod}}(url.format(localVarUrlObj),{{#bodyParam}}{{paramName}}{{/bodyParam}}) - } + // @ts-ignore + return this.fetch(Object.assign(args.options, { path: args.url })) + } {{/operation}} }