Skip to content

Commit

Permalink
fix: Send post data without surrounding object (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
ffflorian committed Jun 12, 2019
1 parent 3a50709 commit 761d2f2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
4 changes: 1 addition & 3 deletions src/Swaxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import fs from 'fs-extra';
import initializeHelpers from 'handlebars-helpers';
import path from 'path';
import {Path, Spec} from 'swagger-schema-official';
import url from 'url';
import yaml from 'yamljs';

import {APIClientGenerator, IndexFileGenerator, ResourceGenerator} from './generators';
Expand Down Expand Up @@ -124,8 +123,7 @@ async function checkOutputDirectory(outputDirectory: string, forceDeletion?: boo

export async function writeClient(inputFile: string, outputDirectory: string, forceDeletion?: boolean): Promise<void> {
await checkOutputDirectory(outputDirectory, forceDeletion);
const parsedInput = url.parse(inputFile);
const isUrl = parsedInput.protocol && /^https?/.test(parsedInput.protocol);
const isUrl = /^(https?|ftps?):\/\//.test(inputFile);
const swaggerJson = isUrl ? await readInputURL(inputFile) : await readInputFile(inputFile);
await validateConfig(swaggerJson);
return generateClient(swaggerJson, outputDirectory);
Expand Down
6 changes: 4 additions & 2 deletions src/generators/MethodGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class MethodGenerator {
}

private buildBodyParameters(parameters?: (Parameter | Reference)[]): BodyParameter[] | undefined {
if (!parameters) {
if (!parameters || !parameters.length) {
return;
}

Expand All @@ -155,9 +155,11 @@ export class MethodGenerator {
return undefined;
}

const type = parameter.schema ? this.buildType(parameter.schema, parameter.name) : TypeScriptType.EMPTY_OBJECT;

return {
name: parameter.name,
type: parameter.schema ? this.buildType(parameter.schema, parameter.name) : TypeScriptType.EMPTY_OBJECT,
type,
};
})
.filter(Boolean) as BodyParameter[];
Expand Down
23 changes: 14 additions & 9 deletions src/templates/Resource.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,29 @@ export class {{{name}}} {
{{/each}}
): Promise<{{{this.returnType}}}> {
const resource = {{{this.formattedUrl}}};
const response = await this.apiClient.{{{this.method}}}
{{#isnt this.returnType "void"}}<{{{this.returnType}}}>{{/isnt}}
{{#eq this.returnType "void"}}
await this.apiClient.{{{this.method}}}
{{else}}
const {data} = await this.apiClient.{{{this.method}}}<{{{this.returnType}}}>
{{/eq}}
(
resource
{{#if bodyParameters}}
,
{
{{#if this.needsDataObj}}
{{#if this.needsDataObj}}
{
data: {
{{/if}}
{{#each bodyParameters}}{{this.name}},{{/each}}
{{#each bodyParameters}}{{this.name}},{{/each}}
{{#if this.needsDataObj}}
}
{{/if}}
}
}
}
{{/if}}
{{/if}}
);
return response.data;
{{#isnt this.returnType "void"}}
return data;
{{/isnt}}
}
{{/each}}
}
8 changes: 3 additions & 5 deletions src/test/snapshots/1-query-param-description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ export class ArchiveService {
body: {archive: {}; conversationId: string}
): Promise<{instanceId: string; name: string}> {
const resource = `/instance/${instanceId}/archive`;
const response = await this.apiClient.post<{
const {data} = await this.apiClient.post<{
instanceId: string;
name: string;
}>(resource, {
body,
});
return response.data;
}>(resource, body);
return data;
}
}

0 comments on commit 761d2f2

Please sign in to comment.