Skip to content

Commit ad35ee4

Browse files
committed
feat: enhance response parsing to handle string data and JSON conversion
1 parent b214107 commit ad35ee4

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/response-parser.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
CONTENT_TYPE,
77
FUNCTION,
88
OBJECT,
9+
STRING,
910
} from './constants';
1011
import {
1112
DefaultResponse,
@@ -68,18 +69,21 @@ export async function parseResponseData<
6869
typeof response.blob === FUNCTION
6970
) {
7071
data = await response.blob(); // Parse as blob
71-
} else if (mimeType.startsWith('text/')) {
72-
data = await response.text(); // Parse as text
7372
} else {
74-
try {
75-
const responseClone = response.clone();
76-
77-
// Handle edge case of no content type being provided... We assume JSON here.
78-
data = await responseClone.json();
79-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
80-
} catch (_e) {
81-
// Handle streams
82-
data = await response.text();
73+
data = await response.text();
74+
75+
if (typeof data === STRING) {
76+
const trimmed = data.trim();
77+
if (
78+
(trimmed.startsWith('{') && trimmed.endsWith('}')) ||
79+
(trimmed.startsWith('[') && trimmed.endsWith(']'))
80+
) {
81+
try {
82+
data = JSON.parse(trimmed);
83+
} catch {
84+
// leave as text if parsing fails
85+
}
86+
}
8387
}
8488
}
8589
// eslint-disable-next-line @typescript-eslint/no-unused-vars

0 commit comments

Comments
 (0)