Skip to content

Commit 1782412

Browse files
committed
fix: 304 don't read body
1 parent 3f8aa2f commit 1782412

File tree

6 files changed

+35
-4
lines changed

6 files changed

+35
-4
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "openapi-typescript-fetch",
33
"description": "A typed fetch client for openapi-typescript",
4-
"version": "2.1.0",
4+
"version": "2.2.0",
55
"engines": {
66
"node": ">= 12.0.0",
77
"npm": ">= 7.0.0"

src/fetcher.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ 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 */) {
139+
// no content or not modified
140+
if (response.status === 204 || response.status === 304) {
140141
return undefined
141142
}
142143
if (contentType && contentType.indexOf('application/json') !== -1) {

test/fetch.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,20 @@ describe('fetch', () => {
118118
expect(data).toBeUndefined()
119119
})
120120

121+
it(`GET /notmodified (should not read body)`, async () => {
122+
const fun = fetcher.path('/notmodified').method('get').create()
123+
try {
124+
await fun(undefined)
125+
throw new Error('should throw api exception')
126+
} catch (err) {
127+
expect(err instanceof ApiError).toBe(true)
128+
if (err instanceof ApiError) {
129+
expect(err.headers.get('content-type')).toBe('application/json')
130+
expect(err.status).toBe(304)
131+
}
132+
}
133+
})
134+
121135
it('GET /error', async () => {
122136
expect.assertions(3)
123137

test/mocks/handlers.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ const methods = {
8787
rest.post(`${HOST}/nocontent`, (req, res, ctx) => {
8888
return res(ctx.status(204))
8989
}),
90+
rest.get(`${HOST}/notmodified`, (req, res) => {
91+
// force bad json response
92+
return res((res) => {
93+
res.status = 304
94+
res.headers.set('Content-Type', 'application/json')
95+
return res
96+
})
97+
}),
9098
rest.get(`${HOST}/defaulterror`, (req, res, ctx) => {
9199
return res(ctx.status(500), ctx.body('internal server error'))
92100
}),

test/paths.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ export type paths = {
7070
}
7171
}
7272
}
73+
'/notmodified': {
74+
get: {
75+
parameters: {}
76+
responses: {
77+
304: unknown
78+
}
79+
}
80+
}
7381
'/error/{status}': {
7482
get: {
7583
parameters: {

0 commit comments

Comments
 (0)