Skip to content

Commit 7d00685

Browse files
feat: added a new response object, which carries additional metadata
1 parent e107cfd commit 7d00685

4 files changed

Lines changed: 30 additions & 13 deletions

File tree

features/support/support.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ When(
7878
);
7979

8080
When(/extracting the object at index ([0-9]*)/, (index) => {
81-
apiResponse = apiResponse[parseInt(index)];
81+
apiResponse.data = apiResponse.data[parseInt(index)];
8282
});
8383

8484
When(
@@ -115,21 +115,21 @@ Then(
115115
);
116116

117117
Then(/the response should be of type (.*)/, (type) => {
118-
assert.equal(apiResponse.constructor.name, type);
118+
assert.equal(apiResponse.data.constructor.name, type);
119119
});
120120

121121
Then(
122122
/the response should have a property ([a-zA-Z]*) with value (.*)/,
123123
(propName, propValue) => {
124-
const value = apiResponse[propName];
124+
const value = apiResponse.data[propName];
125125
const formattedValue =
126126
value instanceof Date ? value.toISOString() : value.toString();
127127
assert.equal(formattedValue, propValue);
128128
}
129129
);
130130

131131
Then(/the response should be an array/, () => {
132-
assert.isArray(apiResponse);
132+
assert.isArray(apiResponse.data);
133133
});
134134

135135
Then(/the requested URL should be (.*)/, (url) => {
@@ -141,11 +141,11 @@ Then(/the request should have a body with value (.*)/, (body) => {
141141
});
142142

143143
Then(/the response should be equal to "(.*)"/, (value) => {
144-
assert.equal(apiResponse, value);
144+
assert.equal(apiResponse.data, value);
145145
});
146146

147147
Then(/the response should be null/, () => {
148-
assert.isNull(apiResponse);
148+
assert.isNull(apiResponse.data);
149149
});
150150

151151
Then(/it should generate a model object named (\w*)/, (modelName) => {

features/support/util.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ function createApi(serverIndex = 0) {
2828
const Configuration = require("../api/configuration.js");
2929
const mockTransport = async (params) => {
3030
mock.requestParams = params;
31-
return mock.serverResponseObject;
31+
return {
32+
data: mock.serverResponseObject
33+
};
3234
};
3335

3436
const config = new Configuration(mockTransport);

template/api.js.handlebars

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,14 @@ class Api{{_tag.name}} {
5757

5858
const response = await request(this.config, "{{@root.path}}", "{{@key}}", builder.parameters);
5959
{{#if _response.schema}}
60-
return deserialize(response, "{{typeConvert _response.schema}}");
60+
return {
61+
...response,
62+
data: deserialize(response.data, "{{typeConvert _response.schema}}")
63+
}
6164
{{else}}
62-
return null;
65+
return {
66+
...response
67+
};
6368
{{/if}}
6469
};
6570

template/fetch.js.handlebars

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
async function transport(params) {
2-
const response = await fetch(params.url, params);
3-
if (response.status !== 200) {
4-
throw new Error(`${response.status} ${response.statusText}`);
2+
const fetchResponse = await fetch(params.url, params);
3+
4+
if (fetchResponse.ok) {
5+
const json = await fetchResponse.json();
6+
return {
7+
data: json,
8+
statusCode: fetchResponse.status,
9+
headers: fetchResponse.headers,
10+
type: fetchResponse.type,
11+
};
12+
} else {
13+
throw new Error(
14+
`Error ${fetchResponse.status}: ${fetchResponse.statusText}`
15+
);
516
}
6-
return await response.json();
717
}
818

919
{{{export}}} transport;

0 commit comments

Comments
 (0)