Skip to content

Commit 6db0bbd

Browse files
committed
send content type only if body is defined, fixes #19
1 parent 8aecdf6 commit 6db0bbd

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

src/fetcher.ts

Lines changed: 5 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,14 +120,15 @@ 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 }
@@ -179,7 +180,6 @@ function wrapMiddlewares(middlewares: Middleware[], fetch: Fetch): Fetch {
179180
return fetch(url, init)
180181
}
181182
const current = middlewares[index]
182-
init = init || { headers: getHeaders() }
183183
return await current(url, init, (nextUrl, nextInit) =>
184184
handler(index + 1, nextUrl, nextInit),
185185
)

test/fetch.test.ts

Lines changed: 18 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,20 @@ 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+
100114
it('GET /error', async () => {
101115
expect.assertions(3)
102116

0 commit comments

Comments
 (0)