@dvsa/appdev-api-common 1.1.0 → 1.2.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.
Files changed (2) hide show
  1. package/http/index.js +20 -8
  2. package/package.json +1 -1
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
- body = await response.clone().json();
97
- }
98
- catch {
99
- // If JSON parsing fails, try text
100
- try {
101
- body = await response.clone().text();
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
- catch {
104
- body = null;
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dvsa/appdev-api-common",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "keywords": ["dvsa", "nodejs", "typescript"],
5
5
  "author": "DVSA",
6
6
  "repository": {