@vida-global/core 2.0.4 → 2.0.6

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.
@@ -72,14 +72,31 @@ class HttpClient {
72
72
 
73
73
  async #handleRequestResponse(res, payload) {
74
74
  if (res.status && res.status < 300) {
75
- const json = await res.json();
76
- return {data: json, status: res.status};
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
 
@@ -36,7 +36,7 @@ class VidaServerController {
36
36
  ***********************************************************************************************/
37
37
  async performRequest(action) {
38
38
  try {
39
- APM.setControllerName(this.constructor.name, action);
39
+ this.setControllerNameForAPM(action)
40
40
  await this.#performRequest(action)
41
41
  } catch(err) {
42
42
  await this.#handleError(err);
@@ -44,6 +44,11 @@ class VidaServerController {
44
44
  }
45
45
 
46
46
 
47
+ setControllerNameForAPM(action) {
48
+ APM.setControllerName(this.constructor.name, action);
49
+ }
50
+
51
+
47
52
  async #performRequest(action) {
48
53
  await this.#setupRequestState()
49
54
  if (this.rendered) return this.#response.end();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vida-global/core",
3
- "version": "2.0.4",
3
+ "version": "2.0.6",
4
4
  "description": "Core libraries for supporting Vida development",
5
5
  "author": "",
6
6
  "license": "ISC",
@@ -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
 
@@ -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
- json: jest.fn().mockResolvedValue(responsePayload),
34
- status: 200,
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
  };