@dvsa/appdev-api-common 1.1.0 → 1.2.0-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.js +20 -8
- package/package.json +6 -2
package/http/index.js
CHANGED
|
@@ -93,17 +93,29 @@ class HTTP {
|
|
|
93
93
|
let body;
|
|
94
94
|
try {
|
|
95
95
|
// Clone the response so we don't consume the original body
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
//
|
|
100
|
-
|
|
101
|
-
body = await
|
|
96
|
+
const clonedResponse = response.clone();
|
|
97
|
+
// Extract the content-type header so we know how to serialise it
|
|
98
|
+
const contentType = response.headers.get("content-type") || "";
|
|
99
|
+
// Check if the JSON header is present
|
|
100
|
+
if (contentType.includes("application/json")) {
|
|
101
|
+
body = await clonedResponse.json();
|
|
102
|
+
}
|
|
103
|
+
// Check if the body is a buffer
|
|
104
|
+
else if (contentType.includes("application/pdf") ||
|
|
105
|
+
contentType.includes("image/") ||
|
|
106
|
+
contentType.includes("application/octet-stream")) {
|
|
107
|
+
const buffer = await clonedResponse.arrayBuffer();
|
|
108
|
+
body = Buffer.from(buffer).toString("base64");
|
|
102
109
|
}
|
|
103
|
-
|
|
104
|
-
|
|
110
|
+
// Otherwise attempt to serialise as text
|
|
111
|
+
else {
|
|
112
|
+
body = await clonedResponse.text();
|
|
105
113
|
}
|
|
106
114
|
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
console.error("Serialisation error:", error);
|
|
117
|
+
body = null;
|
|
118
|
+
}
|
|
107
119
|
return {
|
|
108
120
|
url: response.url,
|
|
109
121
|
status: response.status,
|
package/package.json
CHANGED