Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed problem with camel case method names (typescript fatal error) #8

Merged
merged 2 commits into from
Aug 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/views/swagger2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down
57 changes: 37 additions & 20 deletions templates/typescript-class.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'isomorphic-fetch';
/// <reference path="{{&.}}" />
{{/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}};
Expand All @@ -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}}
Expand All @@ -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('&');
}
Expand Down Expand Up @@ -67,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();
Expand Down
28 changes: 9 additions & 19 deletions templates/typescript-method.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
{
let path = '{{&path}}';
let headers: Headers = new Headers();
let form: any = {};

{{#isSecure}}
headers = this.appendAuthHeaders(headers);
Expand Down Expand Up @@ -73,26 +72,15 @@

{{#isHeaderParameter}}
{{#isSingleton}}
headers['{{&name}}'] = '{{&singleton}}';
headers.append('{{&name}}', '{{&singleton}}');
{{/isSingleton}}
{{^isSingleton}}
if(parameters['{{&camelCaseName}}'] !== undefined) {
headers['{{&name}}'] = parameters['{{&camelCaseName}}']!;
if(queryParameters['{{&camelCaseName}}'] !== undefined) {
headers.append('{{&name}}', queryParameters['{{&camelCaseName}}']!);
}
{{/isSingleton}}
{{/isHeaderParameter}}

{{#isFormParameter}}
{{#isSingleton}}
form['{{&name}}'] = '{{&singleton}}';
{{/isSingleton}}
{{^isSingleton}}
if(parameters['{{&camelCaseName}}'] !== undefined) {
form['{{&name}}'] = parameters['{{&camelCaseName}}'];
}
{{/isSingleton}}
{{/isFormParameter}}

{{#required}}
{{#isQueryParameter}}
if(queryParameters['{{&camelCaseName}}'] === undefined) {
Expand All @@ -103,19 +91,21 @@

{{/parameters}}

queryParameters = queryParametersToSnakeCase(queryParameters);


{{^isBodyParameter}}
{{#isPOST}}
form = queryParameters;
queryParameters = {};
// queryParameters = {};
{{/isPOST}}
{{/isBodyParameter}}


return this.request(
'{{method}}',
`${this.baseUrl}${path}`,
{{#hasBody}}body{{/hasBody}}{{^hasBody}}{}{{/hasBody}},
headers,
queryParameters,
form
queryParameters
);
}