Skip to content

Commit

Permalink
feat: added a simple HttpResponse object to 'wrap' the response
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinEberhardt committed Mar 8, 2023
1 parent c7909fa commit 5d7501e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
10 changes: 5 additions & 5 deletions features/support/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,30 +138,30 @@ export class ModelSteps extends BaseModelStep {

@then(/the response should be of type (.*)/)
public checkResponseType(type: string) {
assert.equal(this.apiResponse.constructor.name, type);
assert.equal(this.apiResponse.data.constructor.name, type);
}

@then(/the response should be equal to "(.*)"/)
public checkResponseValue(value: string) {
assert.equal(this.apiResponse, value);
assert.equal(this.apiResponse.data, value);
}

@then(/the response should have a property ([a-zA-Z]*) with value (.*)/)
public checkResponseProperty(propName: string, propValue: string) {
const value = this.apiResponse[propName];
const value = this.apiResponse.data[propName];
const formattedValue =
value instanceof Date ? value.toISOString() : value.toString();
assert.equal(formattedValue, propValue);
}

@then(/the response should be an array/)
public checkArrayResponse() {
assert.isArray(this.apiResponse);
assert.isArray(this.apiResponse.data);
}

@when(/extracting the object at index ([0-9]*)/)
public extractAtIndex(index: string) {
this.apiResponse = this.apiResponse[parseInt(index)];
this.apiResponse.data = this.apiResponse.data[parseInt(index)];
}

@then(/the request method should be of type (.*)/)
Expand Down
7 changes: 5 additions & 2 deletions template/api.ts.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Configuration from "./configuration";
{{>modelIncludes models=components.schemas options=_options}}
{{>modelIncludes models=components.inlineObjects options=_options}}
import { request } from "./request";
import { HttpResponse } from "./response";
import { Parameter, ParameterBuilder } from "./parameterBuilder";
import { deserialize, serialize } from "./serializer";

Expand Down Expand Up @@ -43,7 +44,7 @@ export default class Api{{_tag.name}} {
{{typeConvert schema ../../../_options}}
{{#if schema.default}} = {{{quoteIfString schema.default}}}{{/if}},
{{/each}}
): Promise<{{#if _response.schema}}{{typeConvert _response.schema ../../_options}}{{else}}null{{/if}}> {
): Promise<{{#if _response.schema}}HttpResponse<{{typeConvert _response.schema ../../_options}}>{{else}}null{{/if}}> {

const builder = new ParameterBuilder();
{{#each _sortedParameters}}{{#unless _optional}}
Expand All @@ -58,7 +59,9 @@ export default class Api{{_tag.name}} {

const response = await request(this.config, "{{@root.path}}", "{{@key}}", builder.parameters);
{{#if _response.schema}}
return deserialize(response, "{{typeConvert _response.schema}}");
return {
data: deserialize(response, "{{typeConvert _response.schema}}")
};
{{else}}
return null;
{{/if}}
Expand Down
6 changes: 6 additions & 0 deletions template/response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* The response of a HTTP request, contains both the response data and associated metadata.
*/
export interface HttpResponse<T> {
data: T;
}

0 comments on commit 5d7501e

Please sign in to comment.