Skip to content

Commit

Permalink
feat: Parse required attributes for body and query parameters (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
bennycode committed Jul 25, 2019
1 parent b1987d6 commit 50c6893
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/Swaxios.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ describe('writeClient', () => {
);
expect(actual).toBe(expected);
});

it('supports parameters with required attributes', async () => {
const inputFile = path.resolve(__dirname, './test/snapshots/5-query-param-required.json');
await writeClient(inputFile, tempDir, true);

const actual = await fs.readFile(path.join(tempDir, 'rest/api/UserService.ts'), 'utf-8');
const expected = await fs.readFile(
path.resolve(__dirname, './test/snapshots/5-query-param-required.ts.fixture'),
'utf-8',
);

expect(actual).toBe(expected);
});
});

describe('exportServices', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/generators/MethodGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe('MethodGenerator', () => {
expect(methodDefinition.normalizedUrl).toBe('/identity-providers');
expect(methodDefinition.parameterMethod).toBe('postById');
expect(methodDefinition.pathParameters[0]).toEqual({name: 'id', type: 'any'});
expect(methodDefinition.bodyParameters![0]).toEqual({name: 'body', type: '{ user: string }'});
expect(methodDefinition.bodyParameters![0]).toEqual({name: 'body', required: false, type: '{ user: string }'});
});
});
});
3 changes: 3 additions & 0 deletions src/generators/MethodGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export enum TypeScriptType {

interface InternalParameter {
name: string;
required?: boolean;
type: string;
}

Expand Down Expand Up @@ -179,6 +180,7 @@ export class MethodGenerator {
: TypeScriptType.EMPTY_OBJECT;
this.bodyParameters.push({
name: parameter.name,
required: parameter.required,
type,
});
}
Expand All @@ -187,6 +189,7 @@ export class MethodGenerator {
const type = this.buildSimpleType(parameter.type);
this.queryParameters.push({
name: parameter.name,
required: parameter.required,
type,
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/templates/Resource.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ export class {{{name}}} {
{{{this.name}}}: {{{this.type}}},
{{/each}}
{{#each this.bodyParameters}}
{{{this.name}}}{{#isnt this.required}}?{{/isnt}}: {{{this.type}}},
{{{this.name}}}{{#isnt this.required true}}?{{/isnt}}: {{{this.type}}},
{{/each}}
{{#if this.queryParameters.length}}
params?: {
{{#each this.queryParameters}}
{{{this.name}}}{{#isnt this.required}}?{{/isnt}}: {{{this.type}}};
{{{this.name}}}{{#isnt this.required true}}?{{/isnt}}: {{{this.type}}};
{{/each}}
}
{{/if}}
Expand Down
48 changes: 48 additions & 0 deletions src/test/snapshots/5-query-param-required.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"info": {
"description": "User API",
"title": "",
"version": ""
},
"paths": {
"/api/user": {
"get": {
"consumes": [
"application/json"
],
"parameters": [
{
"in": "query",
"name": "firstname",
"required": true,
"type": "string"
},
{
"in": "query",
"name": "lastname",
"required": true,
"type": "string"
},
{
"in": "query",
"name": "age",
"required": false,
"type": "number"
}
],
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "",
"schema": {
"type": "string"
}
}
}
}
}
},
"swagger": "2.0"
}
29 changes: 29 additions & 0 deletions src/test/snapshots/5-query-param-required.ts.fixture
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* tslint:disable */

/**
* This file was automatically generated by "Swaxios".
* It should not be modified by hand.
*/

import {AxiosInstance} from 'axios';

export class UserService {
private readonly apiClient: AxiosInstance;

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

async getAll(params?: {
firstname: string;
lastname: string;
age?: number;
}): Promise<string> {
const resource = '/api/user';
const config = {
params,
};
const response = await this.apiClient.get<string>(resource, config);
return response.data;
}
}

0 comments on commit 50c6893

Please sign in to comment.