@vida-global/core 2.0.3 → 2.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/http/client.js +19 -2
- package/lib/http/error.js +1 -1
- package/package.json +1 -1
- package/test/http/client.test.js +34 -0
- package/test/http/error.test.js +1 -1
- package/test/http/helpers/client.js +13 -2
- package/test/http/helpers/error.js +5 -2
package/lib/http/client.js
CHANGED
|
@@ -72,14 +72,31 @@ class HttpClient {
|
|
|
72
72
|
|
|
73
73
|
async #handleRequestResponse(res, payload) {
|
|
74
74
|
if (res.status && res.status < 300) {
|
|
75
|
-
const
|
|
76
|
-
return {data
|
|
75
|
+
const data = await this.#parseResponseData(res);
|
|
76
|
+
return {data, status: res.status};
|
|
77
77
|
} else {
|
|
78
78
|
throw new HttpError(res, payload);
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
|
|
83
|
+
async #parseResponseData(res) {
|
|
84
|
+
const body = await res.text();
|
|
85
|
+
if (!this.#isJsonResponse(res)) return body;
|
|
86
|
+
|
|
87
|
+
try {
|
|
88
|
+
return JSON.parse(body);
|
|
89
|
+
} catch(err) {
|
|
90
|
+
return body;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
#isJsonResponse(res) {
|
|
96
|
+
return res.headers?.get('content-type')?.includes('application/json') || false;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
|
|
83
100
|
#prepareRequestPayload(url, requestData, headers, method, timeout, signal) {
|
|
84
101
|
const payload = {};
|
|
85
102
|
|
package/lib/http/error.js
CHANGED
|
@@ -13,7 +13,7 @@ class HttpError extends Error {
|
|
|
13
13
|
get message() { return `HTTP Error: ${this.status}`};
|
|
14
14
|
get status() { return this.#response.status; }
|
|
15
15
|
get statusText() { return this.#response.statusText; }
|
|
16
|
-
get responseHeaders() { return
|
|
16
|
+
get responseHeaders() { return Object.fromEntries(this.#response.headers.entries()) }
|
|
17
17
|
get requestPayload() { return structuredClone(this.#requestPayload); }
|
|
18
18
|
get url() { return this.#response.url; }
|
|
19
19
|
|
package/package.json
CHANGED
package/test/http/client.test.js
CHANGED
|
@@ -92,6 +92,40 @@ describe('HttpClient', () => {
|
|
|
92
92
|
});
|
|
93
93
|
|
|
94
94
|
|
|
95
|
+
describe('response parsing', () => {
|
|
96
|
+
const client = new HttpClient();
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
it ('parses json responses', async () => {
|
|
100
|
+
Helpers.mockFetchResponse({ body: Helpers.responseBody, contentType: 'application/json' });
|
|
101
|
+
|
|
102
|
+
const result = await client.get(Helpers.apiUrl);
|
|
103
|
+
|
|
104
|
+
expect(result).toEqual({ data: Helpers.responsePayload, status: 200 });
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
it ('returns text responses as strings', async () => {
|
|
109
|
+
const body = 'ok';
|
|
110
|
+
Helpers.mockFetchResponse({ body, contentType: 'text/plain' });
|
|
111
|
+
|
|
112
|
+
const result = await client.get(Helpers.apiUrl);
|
|
113
|
+
|
|
114
|
+
expect(result).toEqual({ data: body, status: 200 });
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
it ('returns invalid json responses as strings', async () => {
|
|
119
|
+
const body = 'not json';
|
|
120
|
+
Helpers.mockFetchResponse({ body, contentType: 'application/json' });
|
|
121
|
+
|
|
122
|
+
const result = await client.get(Helpers.apiUrl);
|
|
123
|
+
|
|
124
|
+
expect(result).toEqual({ data: body, status: 200 });
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
|
|
95
129
|
describe('abort/timeout support', () => {
|
|
96
130
|
const client = new HttpClient();
|
|
97
131
|
|
package/test/http/error.test.js
CHANGED
|
@@ -26,7 +26,7 @@ describe('HttpError', () => {
|
|
|
26
26
|
});
|
|
27
27
|
|
|
28
28
|
it ('stores copies of headers and request payload', () => {
|
|
29
|
-
expect(error.responseHeaders).toEqual(response.headers);
|
|
29
|
+
expect(error.responseHeaders).toEqual(Object.fromEntries(response.headers.entries()));
|
|
30
30
|
expect(error.responseHeaders).not.toBe(response.headers);
|
|
31
31
|
expect(error.requestPayload).toEqual(requestPayload);
|
|
32
32
|
expect(error.requestPayload).not.toBe(requestPayload);
|
|
@@ -16,6 +16,7 @@ const baseUrl = `https://${TestHelpers.Faker.Text.randomString()}.com`.
|
|
|
16
16
|
const endpoint = `/${TestHelpers.Faker.Text.randomString()}`;
|
|
17
17
|
const apiUrl = `${baseUrl}${endpoint}`;
|
|
18
18
|
const responsePayload = randomPayload();
|
|
19
|
+
const responseBody = JSON.stringify(responsePayload);
|
|
19
20
|
|
|
20
21
|
|
|
21
22
|
class ClientWithUrlRoot extends HttpClient {
|
|
@@ -29,9 +30,17 @@ class ClientWithUrlRoot extends HttpClient {
|
|
|
29
30
|
* MOCKS
|
|
30
31
|
***************************************************************************************************/
|
|
31
32
|
const mockFetchSuccess = () => {
|
|
33
|
+
mockFetchResponse({ body: responseBody, contentType: 'application/json' });
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
const mockFetchResponse = ({ body, contentType, status = 200 }) => {
|
|
32
38
|
global.fetch = jest.fn().mockResolvedValue({
|
|
33
|
-
|
|
34
|
-
|
|
39
|
+
headers: {
|
|
40
|
+
get: jest.fn((headerName) => headerName == 'content-type' ? contentType : undefined),
|
|
41
|
+
},
|
|
42
|
+
status,
|
|
43
|
+
text: jest.fn().mockResolvedValue(body),
|
|
35
44
|
});
|
|
36
45
|
}
|
|
37
46
|
|
|
@@ -74,7 +83,9 @@ module.exports = {
|
|
|
74
83
|
ClientWithUrlRoot,
|
|
75
84
|
endpoint,
|
|
76
85
|
expectRequest,
|
|
86
|
+
mockFetchResponse,
|
|
77
87
|
mockFetchSuccess,
|
|
78
88
|
randomPayload,
|
|
89
|
+
responseBody,
|
|
79
90
|
responsePayload,
|
|
80
91
|
};
|
|
@@ -2,10 +2,13 @@ const TestHelpers = require('@vida-global/test-helpers');
|
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
function buildResponse(overrides = {}) {
|
|
5
|
+
const headers = {
|
|
6
|
+
requestId: TestHelpers.Faker.Text.randomString(),
|
|
7
|
+
traceId: TestHelpers.Faker.Text.randomString(),
|
|
8
|
+
}
|
|
5
9
|
return {
|
|
6
10
|
headers: {
|
|
7
|
-
|
|
8
|
-
traceId: TestHelpers.Faker.Text.randomString(),
|
|
11
|
+
entries: () => Object.entries(headers)
|
|
9
12
|
},
|
|
10
13
|
status: Math.floor(Math.random() * 500) + 100,
|
|
11
14
|
statusText: TestHelpers.Faker.Text.randomString(),
|