Skip to content

Commit

Permalink
feat: Add parameters (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
ffflorian committed May 8, 2019
1 parent 90040d3 commit 1d96823
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
8 changes: 1 addition & 7 deletions src/APIClientGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import fs from 'fs-extra';
import Handlebars from 'handlebars';
import path from 'path';
import {Path, Spec} from 'swagger-schema-official';

Expand All @@ -10,10 +9,6 @@ import {validateConfig} from './validator/SwaggerValidator';

require('handlebars-helpers')(['comparison']);

Handlebars.registerHelper('surroundWithCurlyBraces', text => {
return new Handlebars.SafeString(`{${text}}`);
});

export async function writeClient(inputFile: string, outputDirectory: string): Promise<void> {
const swaggerJson: Spec = await fs.readJson(inputFile);
await validateConfig(swaggerJson);
Expand All @@ -22,7 +17,7 @@ export async function writeClient(inputFile: string, outputDirectory: string): P

export async function exportServices(swaggerJson: Spec): Promise<ParsedResource[]> {
const resources: ParsedResource[] = [];
const recordedUrls: Record<string, {[pathName: string]: Path}> = {};
const recordedUrls: Record<string, Record<string, Path>> = {};

for (const url of Object.keys(swaggerJson.paths)) {
const normalizedUrl = StringUtil.normalizeUrl(url);
Expand All @@ -32,7 +27,6 @@ export async function exportServices(swaggerJson: Spec): Promise<ParsedResource[

recordedUrls[fullyQualifiedName] = {
...recordedUrls[fullyQualifiedName],

[url]: swaggerJson.paths[url],
};
}
Expand Down
13 changes: 13 additions & 0 deletions src/info/RequestMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import {inspect} from 'util';
import {StringUtil} from '../util/StringUtil';

class RequestMethod {
public parameterMethod: string;
public parameterName?: string;
public parameterData?: string;
public method: string;
public returnType: string;
public url: string;
Expand All @@ -16,6 +19,16 @@ class RequestMethod {
this.spec = spec;
this.responses = responses;

const parameterMatch = url.match(/\{([^}]+)\}$/);

if (parameterMatch) {
this.parameterName = parameterMatch[1];
this.parameterData = `{data: ${this.parameterName}}`;
}

const postFix = parameterMatch ? `By${StringUtil.camelCase(parameterMatch.splice(1), true)}` : 'All';
this.parameterMethod = `${method}${postFix}`;

if (method === 'delete' || method === 'head') {
this.returnType = 'void';
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/info/SwaxiosGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ export abstract class SwaxiosGenerator {
private async writeTemplate(): Promise<string> {
const renderedTemplate = await this.renderTemplate();
return prettier.format(renderedTemplate, {
bracketSpacing: false,
parser: 'typescript',
singleQuote: true,
trailingComma: 'es5',
});
}

Expand Down
21 changes: 12 additions & 9 deletions src/template/APIClass.hbs
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import {AxiosInstance} from 'axios';

class {{{name}}} {
export class {{{name}}} {
private readonly apiClient: AxiosInstance;

constructor(apiClient: AxiosInstance) {
this.apiClient = apiClient;
}
{{#each methods}}

async {{this.method}}(): Promise<{{{this.returnType}}}> {
async {{{this.parameterMethod}}}
(
{{#if this.parameterName}}{{{this.parameterName}}}: string{{/if}}
): Promise<{{{this.returnType}}}> {

const resource = '{{{this.normalizedUrl}}}';
{{#eq this.returnType "void"}}
const response = await this.apiClient.{{{this.method}}}(resource);
{{else}}
const response = await this.apiClient.{{{this.method}}}<{{{this.returnType}}}>(resource);
{{/eq}}
const response = await this.apiClient.{{{this.method}}}
{{#isnt this.returnType "void"}}<{{{this.returnType}}}>{{/isnt}}
(
resource
{{#if this.parameterName}}, {{{this.parameterData}}}{{/if}}
);
return response.data;
}
{{/each}}
}

export {{#surroundWithCurlyBraces name}}{{/surroundWithCurlyBraces}};

0 comments on commit 1d96823

Please sign in to comment.