@dvsa/appdev-api-common 0.8.1 → 0.9.1-canary.0

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/http/index.d.ts CHANGED
@@ -1,6 +1,15 @@
1
+ type HTTPResponse = {
2
+ url: string;
3
+ status: number;
4
+ statusText: string;
5
+ headers: Record<string, string>;
6
+ redirected: boolean;
7
+ type: 'basic' | 'cors' | 'default' | 'error' | 'opaque' | 'opaqueredirect';
8
+ body?: unknown;
9
+ };
1
10
  export declare class HTTPError extends Error {
2
- response: Response;
3
- constructor(message: string, response: Response);
11
+ response: HTTPResponse;
12
+ constructor(message: string, response: HTTPResponse);
4
13
  }
5
14
  export declare class HTTP {
6
15
  /**
@@ -9,7 +18,7 @@ export declare class HTTP {
9
18
  * @param url
10
19
  * @param options
11
20
  */
12
- static get(url: string, options?: RequestInit): Promise<Response>;
21
+ static get(url: string, options?: RequestInit): Promise<HTTPResponse>;
13
22
  /**
14
23
  * Performs an HTTP POST request.
15
24
  * Note: This method will throw an HTTPError if the response is not ok (status code 200-299) to emulate Axios behaviour.
@@ -17,7 +26,7 @@ export declare class HTTP {
17
26
  * @param body
18
27
  * @param options
19
28
  */
20
- static post<T>(url: string, body: T, options?: RequestInit): Promise<Response>;
29
+ static post<T>(url: string, body: T, options?: RequestInit): Promise<HTTPResponse>;
21
30
  /**
22
31
  * Performs an HTTP PUT request.
23
32
  * Note: This method will throw an HTTPError if the response is not ok (status code 200-299) to emulate Axios behaviour.
@@ -25,12 +34,14 @@ export declare class HTTP {
25
34
  * @param body
26
35
  * @param options
27
36
  */
28
- static put<T>(url: string, body: T, options?: RequestInit): Promise<Response>;
37
+ static put<T>(url: string, body: T, options?: RequestInit): Promise<HTTPResponse>;
29
38
  /**
30
39
  * Performs an HTTP DELETE request.
31
40
  * Note: This method will throw an HTTPError if the response is not ok (status code 200-299) to emulate Axios behaviour.
32
41
  * @param url
33
42
  * @param options
34
43
  */
35
- static delete(url: string, options?: RequestInit): Promise<Response>;
44
+ static delete(url: string, options?: RequestInit): Promise<HTTPResponse>;
45
+ private static serialise;
36
46
  }
47
+ export {};
package/http/index.js CHANGED
@@ -21,10 +21,11 @@ class HTTP {
21
21
  */
22
22
  static async get(url, options) {
23
23
  const response = await fetch(url, { method: "GET", ...options });
24
+ const serialisedResponse = await HTTP.serialise(response);
24
25
  if (!response.ok) {
25
- throw new HTTPError(`HTTP GET request failed with status ${response.status}`, response);
26
+ throw new HTTPError(`HTTP GET request failed with status ${response.status}`, serialisedResponse);
26
27
  }
27
- return response;
28
+ return serialisedResponse;
28
29
  }
29
30
  /**
30
31
  * Performs an HTTP POST request.
@@ -40,10 +41,11 @@ class HTTP {
40
41
  body: JSON.stringify(body),
41
42
  ...options,
42
43
  });
44
+ const serialisedResponse = await HTTP.serialise(response);
43
45
  if (!response.ok) {
44
- throw new HTTPError(`HTTP POST request failed with status ${response.status}`, response);
46
+ throw new HTTPError(`HTTP POST request failed with status ${response.status}`, serialisedResponse);
45
47
  }
46
- return response;
48
+ return serialisedResponse;
47
49
  }
48
50
  /**
49
51
  * Performs an HTTP PUT request.
@@ -59,10 +61,11 @@ class HTTP {
59
61
  body: JSON.stringify(body),
60
62
  ...options,
61
63
  });
64
+ const serialisedResponse = await HTTP.serialise(response);
62
65
  if (!response.ok) {
63
- throw new HTTPError(`HTTP PUT request failed with status ${response.status}`, response);
66
+ throw new HTTPError(`HTTP PUT request failed with status ${response.status}`, serialisedResponse);
64
67
  }
65
- return response;
68
+ return serialisedResponse;
66
69
  }
67
70
  /**
68
71
  * Performs an HTTP DELETE request.
@@ -72,10 +75,36 @@ class HTTP {
72
75
  */
73
76
  static async delete(url, options) {
74
77
  const response = await fetch(url, { method: "DELETE", ...options });
78
+ const serialisedResponse = await HTTP.serialise(response);
75
79
  if (!response.ok) {
76
- throw new HTTPError(`HTTP DELETE request failed with status ${response.status}`, response);
80
+ throw new HTTPError(`HTTP DELETE request failed with status ${response.status}`, serialisedResponse);
77
81
  }
78
- return response;
82
+ return serialisedResponse;
83
+ }
84
+ static async serialise(response) {
85
+ let body;
86
+ try {
87
+ // Clone the response so we don't consume the original body
88
+ body = await response.clone().json();
89
+ }
90
+ catch {
91
+ // If JSON parsing fails, try text
92
+ try {
93
+ body = await response.clone().text();
94
+ }
95
+ catch {
96
+ body = null;
97
+ }
98
+ }
99
+ return {
100
+ url: response.url,
101
+ status: response.status,
102
+ statusText: response.statusText,
103
+ headers: Object.fromEntries(response.headers.entries()),
104
+ redirected: response.redirected,
105
+ type: response.type,
106
+ body,
107
+ };
79
108
  }
80
109
  }
81
110
  exports.HTTP = HTTP;
package/package.json CHANGED
@@ -1,7 +1,11 @@
1
1
  {
2
2
  "name": "@dvsa/appdev-api-common",
3
- "version": "0.8.1",
4
- "keywords": ["dvsa", "nodejs", "typescript"],
3
+ "version": "0.9.1-canary.0",
4
+ "keywords": [
5
+ "dvsa",
6
+ "nodejs",
7
+ "typescript"
8
+ ],
5
9
  "author": "DVSA",
6
10
  "description": "Utils library for common API functionality",
7
11
  "publishConfig": {