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..4d30840 100644 --- a/lib/codegen.ts +++ b/lib/codegen.ts @@ -30,7 +30,7 @@ export class Codegen { return Case.pascal(value); }); - this.engine.registerHelper('concat', (...values: string[]) => { + this.engine.registerHelper('concat', (...values) => { const options = values.pop(); return values.join(''); @@ -65,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 deac1df..077fd2a 100644 --- a/templates/typescript/angular/client.ts.handlebars +++ b/templates/typescript/angular/client.ts.handlebars @@ -104,12 +104,30 @@ export class {{className info.title}} { } {{/first}}{{/each}} - return this.httpClient.request('{{method}}', `${this.basePath}${path}`, { - {{#with (deref requestBody @root)}}body: this._encodeHttpBody(body, {{#first content}}'{{@key}}'{{/first}}),{{/with}} - params, - headers, - {{! withCredentials: this.configuration.withCredentials, }} - }); + {{#each responses}} + {{#if @first}} + {{#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}} } {{/inline}}