Skip to content

Commit 2e0d66e

Browse files
authored
Merge pull request #29 from ajaishankar/develop
version 1.1.3
2 parents 8aecdf6 + dc1bdc5 commit 2e0d66e

File tree

6 files changed

+46
-11
lines changed

6 files changed

+46
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ type Err = FetchErrorType<typeof findPetsByStatus>
181181
182182
### Utility Methods
183183
184-
- `arrayRequestBody` - Helper to merge params when request body is an array
184+
- `arrayRequestBody` - Helper to merge params when request body is an array [see issue](https://github.com/ajaishankar/openapi-typescript-fetch/issues/3#issuecomment-952963986)
185185
186186
```ts
187187

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": "1.1.2",
4+
"version": "1.1.3",
55
"engines": {
66
"node": ">= 12.0.0",
77
"npm": ">= 7.0.0"

src/fetcher.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ function getQuery(
7272
return queryString(queryObj)
7373
}
7474

75-
function getHeaders(init?: HeadersInit) {
75+
function getHeaders(body?: string, init?: HeadersInit) {
7676
const headers = new Headers(init)
7777

78-
if (!headers.has('Content-Type')) {
78+
if (body !== undefined && !headers.has('Content-Type')) {
7979
headers.append('Content-Type', 'application/json')
8080
}
8181

@@ -120,21 +120,25 @@ function getFetchParams(request: Request) {
120120

121121
const path = getPath(request.path, payload)
122122
const query = getQuery(request.method, payload, request.queryParams)
123-
const headers = getHeaders(request.init?.headers)
123+
const body = getBody(request.method, payload)
124+
const headers = getHeaders(body, request.init?.headers)
124125
const url = request.baseUrl + path + query
125126

126127
const init = {
127128
...request.init,
128129
method: request.method.toUpperCase(),
129130
headers,
130-
body: getBody(request.method, payload),
131+
body,
131132
}
132133

133134
return { url, init }
134135
}
135136

136137
async function getResponseData(response: Response) {
137138
const contentType = response.headers.get('content-type')
139+
if (response.status === 204 /* no content */) {
140+
return undefined
141+
}
138142
if (contentType && contentType.indexOf('application/json') !== -1) {
139143
return await response.json()
140144
}
@@ -179,7 +183,6 @@ function wrapMiddlewares(middlewares: Middleware[], fetch: Fetch): Fetch {
179183
return fetch(url, init)
180184
}
181185
const current = middlewares[index]
182-
init = init || { headers: getHeaders() }
183186
return await current(url, init, (nextUrl, nextInit) =>
184187
handler(index + 1, nextUrl, nextInit),
185188
)

test/fetch.test.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ describe('fetch', () => {
2424

2525
const expectedHeaders = {
2626
authorization: 'Bearer token',
27-
'content-type': 'application/json',
2827
accept: 'application/json',
2928
}
3029

30+
const headersWithContentType = {
31+
...expectedHeaders,
32+
'content-type': 'application/json',
33+
}
34+
3135
it('GET /query/{a}/{b}', async () => {
3236
const fun = fetcher.path('/query/{a}/{b}').method('get').create()
3337

@@ -60,7 +64,7 @@ describe('fetch', () => {
6064
expect(data.params).toEqual({ id: '1' })
6165
expect(data.body).toEqual({ list: ['b', 'c'] })
6266
expect(data.query).toEqual({})
63-
expect(data.headers).toEqual(expectedHeaders)
67+
expect(data.headers).toEqual(headersWithContentType)
6468
})
6569
})
6670

@@ -73,7 +77,7 @@ describe('fetch', () => {
7377
expect(data.params).toEqual({ id: '1' })
7478
expect(data.body).toEqual(['b', 'c'])
7579
expect(data.query).toEqual({})
76-
expect(data.headers).toEqual(expectedHeaders)
80+
expect(data.headers).toEqual(headersWithContentType)
7781
})
7882
})
7983

@@ -93,10 +97,27 @@ describe('fetch', () => {
9397
expect(data.params).toEqual({ id: '1' })
9498
expect(data.body).toEqual({ list: ['b', 'c'] })
9599
expect(data.query).toEqual({ scalar: 'a' })
96-
expect(data.headers).toEqual(expectedHeaders)
100+
expect(data.headers).toEqual(headersWithContentType)
97101
})
98102
})
99103

104+
it(`DELETE /body/{id} (empty body)`, async () => {
105+
const fun = fetcher.path('/body/{id}').method('delete').create()
106+
107+
const { data } = await fun({ id: 1 } as any)
108+
109+
expect(data.params).toEqual({ id: '1' })
110+
expect(data.headers).toHaveProperty('accept')
111+
expect(data.headers).not.toHaveProperty('content-type')
112+
})
113+
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+
100121
it('GET /error', async () => {
101122
expect.assertions(3)
102123

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)