Skip to content

Commit

Permalink
fix: Support response body for DELETE requests (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
bennycode committed Jul 18, 2019
1 parent 3c3210b commit d3a9013
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/Swaxios.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ describe('writeClient', () => {
);
expect(actual).toBe(expected);
});

it('supports response types on DELETE requests', async () => {
const inputFile = path.resolve(__dirname, './test/snapshots/4-delete-by-id-number-with-response.json');
await writeClient(inputFile, tempDir, true);
const actual = await fs.readFile(path.join(tempDir, 'rest/api/v1/ExchangeService.ts'), 'utf-8');
const expected = await fs.readFile(
path.resolve(__dirname, './test/snapshots/4-delete-by-id-number-with-response.ts.fixture'),
'utf-8',
);
expect(actual).toBe(expected);
});
});

describe('exportServices', () => {
Expand Down
16 changes: 13 additions & 3 deletions src/generators/MethodGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ export class MethodGenerator {

this.method = method;

if (this.method === 'delete' || this.method === 'head') {
this.returnType = 'void';
} else {
if (this.includesSuccessResponse(this.responses)) {
this.returnType = this.buildResponseSchema();
} else {
this.returnType = 'void';
}

this.needsDataObj = !(
Expand All @@ -99,6 +99,15 @@ export class MethodGenerator {
);
}

private includesSuccessResponse(responses: Record<string, Response | Reference>): boolean {
for (const [successCode, response] of Object.entries(responses)) {
if (successCode.startsWith('2') && response.hasOwnProperty('schema')) {
return true;
}
}
return false;
}

private buildDescriptions(): Description[] | undefined {
if (this.operation.parameters) {
const parameters = this.operation.parameters.filter(
Expand Down Expand Up @@ -266,6 +275,7 @@ export class MethodGenerator {
}

private buildResponseSchema(): string {
// TODO: This does not cover other "success" codes such as "206", etc.
const response200 = this.responses['200'] as Response;
const response201 = this.responses['201'] as Response;

Expand Down
41 changes: 41 additions & 0 deletions src/test/snapshots/4-delete-by-id-number-with-response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"basePath": "/",
"info": {
"description": "",
"title": "",
"version": ""
},
"paths": {
"/api/v1/exchange/{id}": {
"delete": {
"consumes": [
"application/json"
],
"parameters": [
{
"in": "path",
"name": "id",
"required": true,
"type": "number"
}
],
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "",
"schema": {
"type": "number"
}
}
}
}
}
},
"schemes": [
"http"
],
"swagger": "2.0",
"tags": []
}
22 changes: 22 additions & 0 deletions src/test/snapshots/4-delete-by-id-number-with-response.ts.fixture
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* tslint:disable */

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

import {AxiosInstance} from 'axios';

export class ExchangeService {
private readonly apiClient: AxiosInstance;

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

async deleteById(id: number): Promise<number> {
const resource = `/api/v1/exchange/${id}`;
const response = await this.apiClient.delete<number>(resource);
return response.data;
}
}

0 comments on commit d3a9013

Please sign in to comment.