@dvsa/appdev-api-common 0.10.3-0 → 0.10.3

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
@@ -41,7 +41,8 @@ export declare class HTTP {
41
41
  * @param url
42
42
  * @param options
43
43
  */
44
- static delete(url: string, options?: Omit<RequestInit, "method">): 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,14 +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
40
  ...options,
40
41
  method: "POST",
41
42
  headers: {
42
- "Content-Type": "application/json",
43
+ ...(contentType && { "Content-Type": contentType }),
43
44
  ...options?.headers,
44
45
  },
45
- body: JSON.stringify(body),
46
+ body: processedBody,
46
47
  });
47
48
  const serialisedResponse = await HTTP.serialise(response);
48
49
  if (!response.ok) {
@@ -58,14 +59,15 @@ class HTTP {
58
59
  * @param options
59
60
  */
60
61
  static async put(url, body, options) {
62
+ const { contentType, processedBody } = HTTP.prepareBody(body);
61
63
  const response = await fetch(url, {
62
64
  ...options,
63
65
  method: "PUT",
64
66
  headers: {
65
- "Content-Type": "application/json",
67
+ ...(contentType && { "Content-Type": contentType }),
66
68
  ...options?.headers,
67
69
  },
68
- body: JSON.stringify(body),
70
+ body: processedBody,
69
71
  });
70
72
  const serialisedResponse = await HTTP.serialise(response);
71
73
  if (!response.ok) {
@@ -112,5 +114,26 @@ class HTTP {
112
114
  body,
113
115
  };
114
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
+ }
115
138
  }
116
139
  exports.HTTP = HTTP;
package/package.json CHANGED
@@ -1,11 +1,7 @@
1
1
  {
2
2
  "name": "@dvsa/appdev-api-common",
3
- "version": "0.10.3-0",
4
- "keywords": [
5
- "dvsa",
6
- "nodejs",
7
- "typescript"
8
- ],
3
+ "version": "0.10.3",
4
+ "keywords": ["dvsa", "nodejs", "typescript"],
9
5
  "author": "DVSA",
10
6
  "repository": {
11
7
  "type": "git",