@dvsa/appdev-api-common 0.9.0 → 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 +16 -9
- package/http/index.js +37 -20
- package/package.json +6 -2
package/http/index.d.ts
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
|
-
type
|
|
2
|
-
|
|
3
|
-
|
|
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
|
+
};
|
|
4
10
|
export declare class HTTPError extends Error {
|
|
5
|
-
response:
|
|
6
|
-
constructor(message: string, response:
|
|
11
|
+
response: HTTPResponse;
|
|
12
|
+
constructor(message: string, response: HTTPResponse);
|
|
7
13
|
}
|
|
8
14
|
export declare class HTTP {
|
|
9
15
|
/**
|
|
@@ -12,7 +18,7 @@ export declare class HTTP {
|
|
|
12
18
|
* @param url
|
|
13
19
|
* @param options
|
|
14
20
|
*/
|
|
15
|
-
static get(url: string, options?: RequestInit): Promise<
|
|
21
|
+
static get(url: string, options?: RequestInit): Promise<HTTPResponse>;
|
|
16
22
|
/**
|
|
17
23
|
* Performs an HTTP POST request.
|
|
18
24
|
* Note: This method will throw an HTTPError if the response is not ok (status code 200-299) to emulate Axios behaviour.
|
|
@@ -20,7 +26,7 @@ export declare class HTTP {
|
|
|
20
26
|
* @param body
|
|
21
27
|
* @param options
|
|
22
28
|
*/
|
|
23
|
-
static post<T>(url: string, body: T, options?: RequestInit): Promise<
|
|
29
|
+
static post<T>(url: string, body: T, options?: RequestInit): Promise<HTTPResponse>;
|
|
24
30
|
/**
|
|
25
31
|
* Performs an HTTP PUT request.
|
|
26
32
|
* Note: This method will throw an HTTPError if the response is not ok (status code 200-299) to emulate Axios behaviour.
|
|
@@ -28,13 +34,14 @@ export declare class HTTP {
|
|
|
28
34
|
* @param body
|
|
29
35
|
* @param options
|
|
30
36
|
*/
|
|
31
|
-
static put<T>(url: string, body: T, options?: RequestInit): Promise<
|
|
37
|
+
static put<T>(url: string, body: T, options?: RequestInit): Promise<HTTPResponse>;
|
|
32
38
|
/**
|
|
33
39
|
* Performs an HTTP DELETE request.
|
|
34
40
|
* Note: This method will throw an HTTPError if the response is not ok (status code 200-299) to emulate Axios behaviour.
|
|
35
41
|
* @param url
|
|
36
42
|
* @param options
|
|
37
43
|
*/
|
|
38
|
-
static delete(url: string, options?: RequestInit): Promise<
|
|
44
|
+
static delete(url: string, options?: RequestInit): Promise<HTTPResponse>;
|
|
45
|
+
private static serialise;
|
|
39
46
|
}
|
|
40
47
|
export {};
|
package/http/index.js
CHANGED
|
@@ -21,13 +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}`,
|
|
26
|
-
...response,
|
|
27
|
-
body: await response.json(),
|
|
28
|
-
});
|
|
26
|
+
throw new HTTPError(`HTTP GET request failed with status ${response.status}`, serialisedResponse);
|
|
29
27
|
}
|
|
30
|
-
return
|
|
28
|
+
return serialisedResponse;
|
|
31
29
|
}
|
|
32
30
|
/**
|
|
33
31
|
* Performs an HTTP POST request.
|
|
@@ -43,13 +41,11 @@ class HTTP {
|
|
|
43
41
|
body: JSON.stringify(body),
|
|
44
42
|
...options,
|
|
45
43
|
});
|
|
44
|
+
const serialisedResponse = await HTTP.serialise(response);
|
|
46
45
|
if (!response.ok) {
|
|
47
|
-
throw new HTTPError(`HTTP POST request failed with status ${response.status}`,
|
|
48
|
-
...response,
|
|
49
|
-
body: await response.json(),
|
|
50
|
-
});
|
|
46
|
+
throw new HTTPError(`HTTP POST request failed with status ${response.status}`, serialisedResponse);
|
|
51
47
|
}
|
|
52
|
-
return
|
|
48
|
+
return serialisedResponse;
|
|
53
49
|
}
|
|
54
50
|
/**
|
|
55
51
|
* Performs an HTTP PUT request.
|
|
@@ -65,13 +61,11 @@ class HTTP {
|
|
|
65
61
|
body: JSON.stringify(body),
|
|
66
62
|
...options,
|
|
67
63
|
});
|
|
64
|
+
const serialisedResponse = await HTTP.serialise(response);
|
|
68
65
|
if (!response.ok) {
|
|
69
|
-
throw new HTTPError(`HTTP PUT request failed with status ${response.status}`,
|
|
70
|
-
...response,
|
|
71
|
-
body: await response.json(),
|
|
72
|
-
});
|
|
66
|
+
throw new HTTPError(`HTTP PUT request failed with status ${response.status}`, serialisedResponse);
|
|
73
67
|
}
|
|
74
|
-
return
|
|
68
|
+
return serialisedResponse;
|
|
75
69
|
}
|
|
76
70
|
/**
|
|
77
71
|
* Performs an HTTP DELETE request.
|
|
@@ -81,13 +75,36 @@ class HTTP {
|
|
|
81
75
|
*/
|
|
82
76
|
static async delete(url, options) {
|
|
83
77
|
const response = await fetch(url, { method: "DELETE", ...options });
|
|
78
|
+
const serialisedResponse = await HTTP.serialise(response);
|
|
84
79
|
if (!response.ok) {
|
|
85
|
-
throw new HTTPError(`HTTP DELETE request failed with status ${response.status}`,
|
|
86
|
-
...response,
|
|
87
|
-
body: await response.json(),
|
|
88
|
-
});
|
|
80
|
+
throw new HTTPError(`HTTP DELETE request failed with status ${response.status}`, serialisedResponse);
|
|
89
81
|
}
|
|
90
|
-
return
|
|
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
|
+
};
|
|
91
108
|
}
|
|
92
109
|
}
|
|
93
110
|
exports.HTTP = HTTP;
|
package/package.json
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dvsa/appdev-api-common",
|
|
3
|
-
"version": "0.9.0",
|
|
4
|
-
"keywords": [
|
|
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": {
|