Skip to content

Commit e684403

Browse files
authored
feat: return the response as-if for unknown content type (#145)
1 parent e537137 commit e684403

File tree

2 files changed

+29
-44
lines changed

2 files changed

+29
-44
lines changed

packages/clients/src/scw/fetch/__tests__/response-parser.ts

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@ const unmarshalJSON = (obj: unknown) => {
1919
return obj
2020
}
2121

22-
const makeValidFetchResponse = (
23-
contentType = 'application/json',
22+
const makeValidJSONResponse = (
2423
value: JSON | null = SIMPLE_REQ_BODY,
2524
status = 200,
2625
) =>
2726
new Response(value !== null ? convertObjToBuffer(value) : value, {
28-
headers: { 'Content-Type': contentType },
27+
headers: { 'Content-Type': 'application/json' },
28+
status,
29+
})
30+
31+
const makeValidTextResponse = (value: string | null, status = 200) =>
32+
new Response(value, {
33+
headers: { 'Content-Type': 'plain/text' },
2934
status,
3035
})
3136

@@ -82,7 +87,7 @@ describe(`responseParser`, () => {
8287
})
8388

8489
it(`triggers an error for unsuccessful unmarshalling`, async () => {
85-
const validResponse = makeValidFetchResponse()
90+
const validResponse = makeValidJSONResponse()
8691

8792
await expect(
8893
responseParser(() => {
@@ -91,7 +96,7 @@ describe(`responseParser`, () => {
9196
).rejects.toThrow(
9297
new ScalewayError(
9398
validResponse.status,
94-
`could not parse application/json response: couldn't unwrap response value`,
99+
`could not parse 'application/json' response: couldn't unwrap response value`,
95100
),
96101
)
97102

@@ -103,42 +108,25 @@ describe(`responseParser`, () => {
103108
).rejects.toThrow(
104109
new ScalewayError(
105110
validResponse.status,
106-
`could not parse application/json response`,
107-
),
108-
)
109-
})
110-
111-
it(`triggers an error for invalid content type`, async () => {
112-
const cType = 'plain/text'
113-
const invalidResponse = makeValidFetchResponse(cType)
114-
115-
return expect(parseJson(invalidResponse)).rejects.toThrow(
116-
new ScalewayError(
117-
invalidResponse.status,
118-
`invalid content type ${cType}`,
111+
`could not parse 'application/json' response`,
119112
),
120113
)
121114
})
122115

123-
it(`triggers an error for undefined content type`, async () => {
124-
const invalidResponse = new Response(convertObjToBuffer(SIMPLE_REQ_BODY), {
125-
headers: {},
126-
status: 200,
127-
})
116+
it(`returns the response as-if for unknown content type`, async () => {
117+
const textResponse = makeValidTextResponse('text-body')
128118

129-
return expect(parseJson(invalidResponse)).rejects.toThrow(
130-
new ScalewayError(invalidResponse.status, `invalid content type`),
131-
)
119+
return expect(parseAsIs(textResponse)).resolves.toBe('text-body')
132120
})
133121

134122
it(`returns a simple object for a valid 'Response' object`, async () =>
135-
expect(parseJson(makeValidFetchResponse())).resolves.toMatchObject(
123+
expect(parseJson(makeValidJSONResponse())).resolves.toMatchObject(
136124
SIMPLE_REQ_BODY,
137125
))
138126

139127
it(`returns undefined for a 204 status code, even if content-type is json`, async () =>
140128
expect(
141-
parseAsIs(makeValidFetchResponse('application/json', null, 204)),
129+
parseAsIs(makeValidJSONResponse(null, 204)),
142130
).resolves.toBeUndefined())
143131
})
144132

packages/clients/src/scw/fetch/response-parser.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,22 @@ export const responseParser =
5353
if (response.ok) {
5454
if (response.status === 204) return unmarshaller(undefined)
5555
const contentType = response.headers.get('Content-Type')
56-
switch (contentType) {
57-
case 'application/json':
58-
try {
56+
try {
57+
switch (contentType) {
58+
case 'application/json':
5959
return unmarshaller(
6060
fixLegacyTotalCount(await response.json(), response.headers),
6161
)
62-
} catch (err) {
63-
throw new ScalewayError(
64-
response.status,
65-
`could not parse ${contentType} response${
66-
err instanceof Error ? `: ${err.message}` : ''
67-
}`,
68-
)
69-
}
70-
default:
71-
throw new ScalewayError(
72-
response.status,
73-
`invalid content type ${contentType ?? ''}`.trim(),
74-
)
62+
default:
63+
return unmarshaller(await response.text())
64+
}
65+
} catch (err) {
66+
throw new ScalewayError(
67+
response.status,
68+
`could not parse '${contentType ?? ''}' response${
69+
err instanceof Error ? `: ${err.message}` : ''
70+
}`,
71+
)
7572
}
7673
}
7774

0 commit comments

Comments
 (0)