Skip to content

Commit

Permalink
Support form data in Aurelia swagger-api#5987
Browse files Browse the repository at this point in the history
  • Loading branch information
ksm2 committed Jul 6, 2017
1 parent 253e6be commit 9b98708
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,48 @@ export class Api {
this.authStorage = authStorage;
}

/**
* Encodes a query string.
*
* @param params The params to encode.
* @return An encoded query string.
*/
protected queryString(params: { [key: string]: any }): string {
const queries = [];
for (let key in params) {
const value = this.toString(params[key]);
if (value != null) {
queries.push(`${key}=${encodeURIComponent(value)}`);
}
}

return queries.join('&');
}

/**
* Converts a value to string.
*
* @param value The value to convert.
*/
protected toString(value: any): string | null {
if (value === null) {
return null;
}
switch (typeof value) {
case 'undefined': return null;
case 'boolean': return value ? 'true' : 'false';
case 'string': return value;
default: return '' + value;
}
}

/**
* Ensures that a given parameter is set.
*
* @param context A name for the callee's context.
* @param params The parameters being set.
* @param paramName The required parameter to check.
*/
protected ensureParamIsSet<T>(context: string, params: T, paramName: keyof T): void {
if (null === params[paramName]) {
throw new Error(`Missing required parameter ${paramName} when calling ${context}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ export class AuthStorage {
return this;
}

/**
* Removes the {{name}} auth method value.
*/
remove{{name}}(): this {
this.storage.delete('{{name}}');
return this;
}

/**
* Gets the {{name}} auth method value.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,43 @@ export class {{classname}} extends Api {
const url = `${this.basePath}{{{path}}}`{{#pathParams}}
.replace(`{${'{{baseName}}'}}`, `${params['{{paramName}}']}`){{/pathParams}};


const response = await this.httpClient.createRequest(url)
.as{{httpMethod}}(){{#hasQueryParams}}
// Set HTTP method
.as{{httpMethod}}()
{{#hasQueryParams}}
// Set query parameters
.withParams({ {{#queryParams}}
'{{baseName}}': params['{{paramName}}'],{{/queryParams}}
}){{/hasQueryParams}}{{#bodyParam}}
})
{{/hasQueryParams}}
{{#hasFormParams}}
// Encode form parameters
.withHeader('content-type', 'application/x-www-form-urlencoded')
.withContent(this.queryString({ {{#formParams}}
'{{baseName}}': params['{{paramName}}'],{{/formParams}}
}))
{{/hasFormParams}}
{{#hasBodyParam}}
{{#bodyParam}}
// Encode body parameter
.withHeader('content-type', 'application/json')
.withContent(JSON.stringify(params['{{paramName}}'] || {}))
.withHeader('content-type', 'application/json'){{/bodyParam}}{{#headerParams}}
{{/bodyParam}}
{{/hasBodyParam}}
{{#headerParams}}
.withHeader('{{baseName}}', params['{{paramName}}']){{/headerParams}}
{{#authMethods}}
// Authentication '{{name}}' required
{{#isApiKey}}
{{#isKeyInHeader}}
{{#isApiKey}}
{{#isKeyInHeader}}
.withHeader('{{keyParamName}}', this.authStorage.get{{name}}())
{{/isKeyInHeader}}
{{#isKeyInQuery}}
{{/isKeyInHeader}}
{{#isKeyInQuery}}
.withParams({ {{keyParamName}}: this.authStorage.get{{name}}() })
{{/isKeyInQuery}}
{{/isApiKey}}
{{/isKeyInQuery}}
{{/isApiKey}}
{{/authMethods}}
// Send the request
.send();

if (response.statusCode < 200 || response.statusCode >= 300) {
Expand Down

0 comments on commit 9b98708

Please sign in to comment.