@dvsa/appdev-api-common 0.10.2 → 0.10.3-1
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 +5 -4
- package/http/index.js +35 -6
- package/package.json +1 -1
package/http/index.d.ts
CHANGED
|
@@ -18,7 +18,7 @@ export declare class HTTP {
|
|
|
18
18
|
* @param url
|
|
19
19
|
* @param options
|
|
20
20
|
*/
|
|
21
|
-
static get(url: string, options?: RequestInit): Promise<HTTPResponse>;
|
|
21
|
+
static get(url: string, options?: Omit<RequestInit, "method" | "body">): Promise<HTTPResponse>;
|
|
22
22
|
/**
|
|
23
23
|
* Performs an HTTP POST request.
|
|
24
24
|
* Note: This method will throw an HTTPError if the response is not ok (status code 200-299) to emulate Axios behaviour.
|
|
@@ -26,7 +26,7 @@ export declare class HTTP {
|
|
|
26
26
|
* @param body
|
|
27
27
|
* @param options
|
|
28
28
|
*/
|
|
29
|
-
static post<T>(url: string, body: T, options?: RequestInit): Promise<HTTPResponse>;
|
|
29
|
+
static post<T>(url: string, body: T, options?: Omit<RequestInit, "method" | "body">): Promise<HTTPResponse>;
|
|
30
30
|
/**
|
|
31
31
|
* Performs an HTTP PUT request.
|
|
32
32
|
* Note: This method will throw an HTTPError if the response is not ok (status code 200-299) to emulate Axios behaviour.
|
|
@@ -34,14 +34,15 @@ export declare class HTTP {
|
|
|
34
34
|
* @param body
|
|
35
35
|
* @param options
|
|
36
36
|
*/
|
|
37
|
-
static put<T>(url: string, body: T, options?: RequestInit): Promise<HTTPResponse>;
|
|
37
|
+
static put<T>(url: string, body: T, options?: Omit<RequestInit, "method" | "body">): Promise<HTTPResponse>;
|
|
38
38
|
/**
|
|
39
39
|
* Performs an HTTP DELETE request.
|
|
40
40
|
* Note: This method will throw an HTTPError if the response is not ok (status code 200-299) to emulate Axios behaviour.
|
|
41
41
|
* @param url
|
|
42
42
|
* @param options
|
|
43
43
|
*/
|
|
44
|
-
static delete(url: string, options?: RequestInit): Promise<HTTPResponse>;
|
|
44
|
+
static delete(url: string, options?: Omit<RequestInit, "method" | "body">): Promise<HTTPResponse>;
|
|
45
45
|
private static serialise;
|
|
46
|
+
private static prepareBody;
|
|
46
47
|
}
|
|
47
48
|
export {};
|
package/http/index.js
CHANGED
|
@@ -35,11 +35,15 @@ class HTTP {
|
|
|
35
35
|
* @param options
|
|
36
36
|
*/
|
|
37
37
|
static async post(url, body, options) {
|
|
38
|
+
const { contentType, processedBody } = HTTP.prepareBody(body);
|
|
38
39
|
const response = await fetch(url, {
|
|
39
|
-
method: "POST",
|
|
40
|
-
headers: { "Content-Type": "application/json", ...options?.headers },
|
|
41
|
-
body: JSON.stringify(body),
|
|
42
40
|
...options,
|
|
41
|
+
method: "POST",
|
|
42
|
+
headers: {
|
|
43
|
+
...(contentType && { "Content-Type": contentType }),
|
|
44
|
+
...options?.headers,
|
|
45
|
+
},
|
|
46
|
+
body: processedBody,
|
|
43
47
|
});
|
|
44
48
|
const serialisedResponse = await HTTP.serialise(response);
|
|
45
49
|
if (!response.ok) {
|
|
@@ -55,11 +59,15 @@ class HTTP {
|
|
|
55
59
|
* @param options
|
|
56
60
|
*/
|
|
57
61
|
static async put(url, body, options) {
|
|
62
|
+
const { contentType, processedBody } = HTTP.prepareBody(body);
|
|
58
63
|
const response = await fetch(url, {
|
|
59
|
-
method: "PUT",
|
|
60
|
-
headers: { "Content-Type": "application/json", ...options?.headers },
|
|
61
|
-
body: JSON.stringify(body),
|
|
62
64
|
...options,
|
|
65
|
+
method: "PUT",
|
|
66
|
+
headers: {
|
|
67
|
+
...(contentType && { "Content-Type": contentType }),
|
|
68
|
+
...options?.headers,
|
|
69
|
+
},
|
|
70
|
+
body: processedBody,
|
|
63
71
|
});
|
|
64
72
|
const serialisedResponse = await HTTP.serialise(response);
|
|
65
73
|
if (!response.ok) {
|
|
@@ -106,5 +114,26 @@ class HTTP {
|
|
|
106
114
|
body,
|
|
107
115
|
};
|
|
108
116
|
}
|
|
117
|
+
static prepareBody(body) {
|
|
118
|
+
if (body instanceof FormData) {
|
|
119
|
+
return { contentType: undefined, processedBody: body };
|
|
120
|
+
}
|
|
121
|
+
if (body instanceof URLSearchParams) {
|
|
122
|
+
return {
|
|
123
|
+
contentType: "application/x-www-form-urlencoded",
|
|
124
|
+
processedBody: body,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
if (typeof body === "string") {
|
|
128
|
+
return {
|
|
129
|
+
contentType: "text/plain",
|
|
130
|
+
processedBody: body,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
contentType: "application/json",
|
|
135
|
+
processedBody: JSON.stringify(body),
|
|
136
|
+
};
|
|
137
|
+
}
|
|
109
138
|
}
|
|
110
139
|
exports.HTTP = HTTP;
|