From f8230cf890901bf0efde1bcd10b480f44c5599ab Mon Sep 17 00:00:00 2001 From: Egor Genning Date: Thu, 12 Jul 2018 08:50:28 +0700 Subject: [PATCH 1/2] add response type text --- .gitignore | 1 + lib/codegen.ts | 18 +++++++++++- .../typescript/angular/client.ts.handlebars | 28 ++++++++++++++++++- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 6ae5a0e..bc748c8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea/ out/ dist/ +/node_modules/ diff --git a/lib/codegen.ts b/lib/codegen.ts index 206a18f..a850e79 100644 --- a/lib/codegen.ts +++ b/lib/codegen.ts @@ -30,7 +30,23 @@ export class Codegen { return Case.pascal(value); }); - this.engine.registerHelper('concat', (...values: string[]) => { + this.engine.registerHelper('if_eq', function(a, b, opts) { + if (a == b) { + return opts.fn(this); + } else { + return opts.inverse(this); + } + }); + + this.engine.registerHelper('assign', function (varName, varValue, options) { + if (!options.data.root) { + options.data.root = {}; + } + options.data.root[varName] = varValue; + }); + + // tslint:disable-next-line + this.engine.registerHelper('concat', (...values) => { const options = values.pop(); return values.join(''); diff --git a/templates/typescript/angular/client.ts.handlebars b/templates/typescript/angular/client.ts.handlebars index deac1df..aab31f3 100644 --- a/templates/typescript/angular/client.ts.handlebars +++ b/templates/typescript/angular/client.ts.handlebars @@ -104,12 +104,38 @@ export class {{className info.title}} { } {{/first}}{{/each}} + {{#each responses}} + {{#if @first}} + {{#with (deref this)}}{{#first content}}{{#switch @key}} + {{#case "application/json"}} + {{assign 'responseType' 'json'}} + {{/case}} + {{#case "text/csv"}} + {{assign 'responseType' 'text'}} + {{/case}} + {{#default}} + {{assign 'responseType' 'json'}} + {{/default}} + {{/switch}}{{/first}}{{/with}} + {{/if}} + {{/each}} + + {{#if_eq @root.responseType 'json'}} return this.httpClient.request('{{method}}', `${this.basePath}${path}`, { {{#with (deref requestBody @root)}}body: this._encodeHttpBody(body, {{#first content}}'{{@key}}'{{/first}}),{{/with}} + responseType: 'json', params, headers, - {{! withCredentials: this.configuration.withCredentials, }} }); + {{/if_eq}} + {{#if_eq @root.responseType 'text'}} + return this.httpClient.request('{{method}}', `${this.basePath}${path}`, { + {{#with (deref requestBody @root)}}body: this._encodeHttpBody(body, {{#first content}}'{{@key}}'{{/first}}),{{/with}} + responseType: 'text', + params, + headers, + }); + {{/if_eq}} } {{/inline}} From 8046609627200c2fbe6e5f334a1dfd0db93e20c2 Mon Sep 17 00:00:00 2001 From: Egor Genning Date: Fri, 13 Jul 2018 09:26:14 +0700 Subject: [PATCH 2/2] fixes after review. remover redundant helpers --- lib/codegen.ts | 20 ++------ .../typescript/angular/client.ts.handlebars | 48 ++++++++----------- 2 files changed, 24 insertions(+), 44 deletions(-) diff --git a/lib/codegen.ts b/lib/codegen.ts index a850e79..4d30840 100644 --- a/lib/codegen.ts +++ b/lib/codegen.ts @@ -30,22 +30,6 @@ export class Codegen { return Case.pascal(value); }); - this.engine.registerHelper('if_eq', function(a, b, opts) { - if (a == b) { - return opts.fn(this); - } else { - return opts.inverse(this); - } - }); - - this.engine.registerHelper('assign', function (varName, varValue, options) { - if (!options.data.root) { - options.data.root = {}; - } - options.data.root[varName] = varValue; - }); - - // tslint:disable-next-line this.engine.registerHelper('concat', (...values) => { const options = values.pop(); @@ -81,6 +65,10 @@ export class Codegen { return Array.isArray(value1) && value1.indexOf(value2) >= 0; }); + this.engine.registerHelper('contains?', (value1: string, value2: string, _options) => { + return value1.indexOf(value2) >= 0; + }); + this.engine.registerHelper('successResponses', (value: OpenAPI3.Responses, _options): OpenAPI3.Responses => { const out: OpenAPI3.Responses = {}; Object.keys(value).filter(_ => !_.match(/^([45][\dX]{2}|default)$/)).forEach(key => { diff --git a/templates/typescript/angular/client.ts.handlebars b/templates/typescript/angular/client.ts.handlebars index aab31f3..077fd2a 100644 --- a/templates/typescript/angular/client.ts.handlebars +++ b/templates/typescript/angular/client.ts.handlebars @@ -106,36 +106,28 @@ export class {{className info.title}} { {{#each responses}} {{#if @first}} - {{#with (deref this)}}{{#first content}}{{#switch @key}} - {{#case "application/json"}} - {{assign 'responseType' 'json'}} - {{/case}} - {{#case "text/csv"}} - {{assign 'responseType' 'text'}} - {{/case}} - {{#default}} - {{assign 'responseType' 'json'}} - {{/default}} - {{/switch}}{{/first}}{{/with}} + {{#with (deref this)}} + {{#first content}} + {{#if (eq? @key "application/json")}} + return this.httpClient.request('{{../../method}}', `${this.basePath}${path}`, { + {{#with (deref ../../requestBody @root)}}body: this._encodeHttpBody(body, {{#first content}}'{{@key}}'{{/first}}),{{/with}} + responseType: 'json', + params, + headers, + }); + {{/if}} + {{#if (contains? @key "text")}} + return this.httpClient.request('{{../../method}}', `${this.basePath}${path}`, { + {{#with (deref ../../requestBody @root)}}body: this._encodeHttpBody(body, {{#first content}}'{{@key}}'{{/first}}),{{/with}} + responseType: 'text', + params, + headers, + }); + {{/if}} + {{/first}} + {{/with}} {{/if}} {{/each}} - - {{#if_eq @root.responseType 'json'}} - return this.httpClient.request('{{method}}', `${this.basePath}${path}`, { - {{#with (deref requestBody @root)}}body: this._encodeHttpBody(body, {{#first content}}'{{@key}}'{{/first}}),{{/with}} - responseType: 'json', - params, - headers, - }); - {{/if_eq}} - {{#if_eq @root.responseType 'text'}} - return this.httpClient.request('{{method}}', `${this.basePath}${path}`, { - {{#with (deref requestBody @root)}}body: this._encodeHttpBody(body, {{#first content}}'{{@key}}'{{/first}}),{{/with}} - responseType: 'text', - params, - headers, - }); - {{/if_eq}} } {{/inline}}