From 5cf86e9bca1248be92259f134c21d6fd52313d60 Mon Sep 17 00:00:00 2001 From: Matej Baco Date: Mon, 6 Aug 2018 13:13:07 +0200 Subject: [PATCH 1/2] Fixed problem with camel case method names (typescript fatal error) --- lib/views/swagger2.js | 6 +++++ templates/typescript-class.mustache | 40 +++++++++++++++++++++++++--- templates/typescript-method.mustache | 11 ++++---- 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/lib/views/swagger2.js b/lib/views/swagger2.js index 9f3cd976..c9050f1b 100644 --- a/lib/views/swagger2.js +++ b/lib/views/swagger2.js @@ -192,6 +192,12 @@ var getViewForSwagger2 = function(opts, type) { if (parameter.required && parameter.in !== 'path') { hasAnyRequired = true; } + + parameter.isDefaultQuoted = false; + if (parameter.default && typeof parameter.default === 'string') { + parameter.isDefaultQuoted = true; + } + method.parameters.push(parameter); }); diff --git a/templates/typescript-class.mustache b/templates/typescript-class.mustache index ebc5c3f7..c5cb2cb6 100644 --- a/templates/typescript-class.mustache +++ b/templates/typescript-class.mustache @@ -5,7 +5,7 @@ import 'isomorphic-fetch'; /// {{/imports}} -type QueryParameters = { [key: string]: string | number | string[] | number[] }; +type QueryParameters = { [key: string]: string | number | string[] | number[] | undefined }; {{#definitions}} export type {{&name}} = {{#tsType}}{{> type}}{{/tsType}}; @@ -20,6 +20,35 @@ class ApiError { } } +const queryParametersToSnakeCase = (parameters: QueryParameters) => { + const snakeCaseParams: any = {}; + + Object.keys(parameters).map(key => { + const value = parameters[key]; + + snakeCaseParams[toSnakeCase(key)] = value; + }); + + return snakeCaseParams; +} + +const toSnakeCase = (msg: string) => { + const upperChars = msg.match(/([A-Z])/g); + if (! upperChars) { + return msg; + } + + let str = msg.toString(); + for (var i = 0, n = upperChars.length; i < n; i++) { + str = str.replace(new RegExp(upperChars[i]), '_' + upperChars[i].toLowerCase()); + } + + if (str.slice(0, 1) === '_') { + str = str.slice(1); + } + return str; +} + /** * {{&description}} * @class {{&className}} @@ -37,9 +66,12 @@ class {{&className}} { serializeQueryParams(parameters: QueryParameters) { const str: string[] = []; for (let p in parameters) { - if (parameters.hasOwnProperty(p)) { - str.push(`${encodeURIComponent(p)}=${encodeURIComponent(parameters[p].toString())}`); - } + if (parameters.hasOwnProperty(p)) { + if(parameters[p] !== undefined) { + const parameter = parameters[p] || ""; + str.push(`${encodeURIComponent(p)}=${encodeURIComponent(parameter.toString())}`); + } + } } return str.join('&'); } diff --git a/templates/typescript-method.mustache b/templates/typescript-method.mustache index 49f1168d..6a89ca88 100644 --- a/templates/typescript-method.mustache +++ b/templates/typescript-method.mustache @@ -76,8 +76,8 @@ headers['{{&name}}'] = '{{&singleton}}'; {{/isSingleton}} {{^isSingleton}} - if(parameters['{{&camelCaseName}}'] !== undefined) { - headers['{{&name}}'] = parameters['{{&camelCaseName}}']!; + if(queryParameters['{{&camelCaseName}}'] !== undefined) { + headers['{{&name}}'] = queryParameters['{{&camelCaseName}}']!; } {{/isSingleton}} {{/isHeaderParameter}} @@ -87,8 +87,8 @@ form['{{&name}}'] = '{{&singleton}}'; {{/isSingleton}} {{^isSingleton}} - if(parameters['{{&camelCaseName}}'] !== undefined) { - form['{{&name}}'] = parameters['{{&camelCaseName}}']; + if(queryParameters['{{&camelCaseName}}'] !== undefined) { + form['{{&name}}'] = queryParameters['{{&camelCaseName}}']; } {{/isSingleton}} {{/isFormParameter}} @@ -103,10 +103,11 @@ {{/parameters}} + queryParameters = queryParametersToSnakeCase(queryParameters); + {{^isBodyParameter}} {{#isPOST}} form = queryParameters; - queryParameters = {}; {{/isPOST}} {{/isBodyParameter}} From 591c753bd5a642992940eb1cb84268c0e1f6e3f6 Mon Sep 17 00:00:00 2001 From: Jozef Cipa Date: Tue, 7 Aug 2018 16:29:05 +0200 Subject: [PATCH 2/2] removed form data parameters --- templates/typescript-class.mustache | 17 +---------------- templates/typescript-method.mustache | 23 ++++++----------------- 2 files changed, 7 insertions(+), 33 deletions(-) diff --git a/templates/typescript-class.mustache b/templates/typescript-class.mustache index c5cb2cb6..729403be 100644 --- a/templates/typescript-class.mustache +++ b/templates/typescript-class.mustache @@ -99,31 +99,16 @@ class {{&className}} { } {{/isSecure}} - private async request(method: string, url: string, body: any, headers: any, queryParameters: QueryParameters, form: any) { + private async request(method: string, url: string, body: any, headers: any, queryParameters: QueryParameters) { const queryParams = queryParameters && Object.keys(queryParameters).length ? this.serializeQueryParams(queryParameters) : null ; const urlWithParams = url + (queryParams ? '?' + queryParams : ''); - // ugly hack, we need to delete Content-Type header with multipart/form-data - // that way, browser will calculate form specific headers on it's own - // contentTypeHeader[0] because nearly every header's value is set using array - const contentTypeHeader = headers['Content-Type']; - if(contentTypeHeader && contentTypeHeader[0] === 'multipart/form-data') { - delete headers['Content-Type']; - } - if (body && !Object.keys(body).length) { body = undefined; } else { body = JSON.stringify(body); } - if (form && Object.keys(form).length) { - body = new FormData(); - for (let k in form) { - body.append(k, form[k]); - } - } - const response = await fetch(urlWithParams, { method, headers, body }); if(response.ok) { return response.json(); diff --git a/templates/typescript-method.mustache b/templates/typescript-method.mustache index 6a89ca88..c4a4520d 100644 --- a/templates/typescript-method.mustache +++ b/templates/typescript-method.mustache @@ -33,7 +33,6 @@ { let path = '{{&path}}'; let headers: Headers = new Headers(); - let form: any = {}; {{#isSecure}} headers = this.appendAuthHeaders(headers); @@ -73,26 +72,15 @@ {{#isHeaderParameter}} {{#isSingleton}} - headers['{{&name}}'] = '{{&singleton}}'; + headers.append('{{&name}}', '{{&singleton}}'); {{/isSingleton}} {{^isSingleton}} if(queryParameters['{{&camelCaseName}}'] !== undefined) { - headers['{{&name}}'] = queryParameters['{{&camelCaseName}}']!; + headers.append('{{&name}}', queryParameters['{{&camelCaseName}}']!); } {{/isSingleton}} {{/isHeaderParameter}} - {{#isFormParameter}} - {{#isSingleton}} - form['{{&name}}'] = '{{&singleton}}'; - {{/isSingleton}} - {{^isSingleton}} - if(queryParameters['{{&camelCaseName}}'] !== undefined) { - form['{{&name}}'] = queryParameters['{{&camelCaseName}}']; - } - {{/isSingleton}} - {{/isFormParameter}} - {{#required}} {{#isQueryParameter}} if(queryParameters['{{&camelCaseName}}'] === undefined) { @@ -105,18 +93,19 @@ queryParameters = queryParametersToSnakeCase(queryParameters); + {{^isBodyParameter}} {{#isPOST}} - form = queryParameters; + // queryParameters = {}; {{/isPOST}} {{/isBodyParameter}} + return this.request( '{{method}}', `${this.baseUrl}${path}`, {{#hasBody}}body{{/hasBody}}{{^hasBody}}{}{{/hasBody}}, headers, - queryParameters, - form + queryParameters ); }