Skip to content

Commit a32b75b

Browse files
committed
handle 204 no-content, fixes #27
1 parent 329af28 commit a32b75b

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

src/fetcher.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ function getFetchParams(request: Request) {
136136

137137
async function getResponseData(response: Response) {
138138
const contentType = response.headers.get('content-type')
139+
if (response.status === 204 /* no content */) {
140+
return undefined
141+
}
139142
if (contentType && contentType.indexOf('application/json') !== -1) {
140143
return await response.json()
141144
}

test/fetch.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ describe('fetch', () => {
111111
expect(data.headers).not.toHaveProperty('content-type')
112112
})
113113

114+
it(`POST /nocontent`, async () => {
115+
const fun = fetcher.path('/nocontent').method('post').create()
116+
const { status, data } = await fun(undefined)
117+
expect(status).toBe(204)
118+
expect(data).toBeUndefined()
119+
})
120+
114121
it('GET /error', async () => {
115122
expect.assertions(3)
116123

test/mocks/handlers.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ const methods = {
6464
)
6565
: res(ctx.status(status))
6666
}),
67+
rest.post(`${HOST}/nocontent`, (req, res, ctx) => {
68+
return res(ctx.status(204))
69+
}),
6770
rest.get(`${HOST}/defaulterror`, (req, res, ctx) => {
6871
return res(ctx.status(500), ctx.body('internal server error'))
6972
}),

test/paths.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ export type paths = {
6161
patch: BodyAndQuery
6262
delete: BodyAndQuery
6363
}
64+
'/nocontent': {
65+
post: {
66+
parameters: {}
67+
responses: {
68+
204: unknown
69+
}
70+
}
71+
}
6472
'/error/{status}': {
6573
get: {
6674
parameters: {

0 commit comments

Comments
 (0)