Skip to content

Commit 0b9f249

Browse files
authored
fix(core): ts-rest overwriting a supplied content-type header (#534)
1 parent 59573f5 commit 0b9f249

3 files changed

Lines changed: 27 additions & 6 deletions

File tree

.changeset/lovely-snails-divide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ts-rest/core': patch
3+
---
4+
5+
Fix ts-rest overwriting a supplied content-type header

libs/ts-rest/core/src/lib/client.spec.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ const postsRouter = c.router({
112112
responses: {
113113
200: c.type<Post>(),
114114
},
115-
body: null,
115+
headers: z.object({
116+
'content-type': z.literal('application/merge-patch+json'),
117+
}),
118+
body: z.object({}).passthrough(),
116119
},
117120
deletePost: {
118121
method: 'DELETE',
@@ -516,20 +519,33 @@ describe('client', () => {
516519
describe('patch', () => {
517520
it('w/ body', async () => {
518521
const value = { key: 'value' };
522+
519523
fetchMock.patchOnce(
520524
{
521525
url: 'https://api.com/posts/1',
522526
},
523-
{ body: value, status: 200 },
527+
(_, req) => ({
528+
body: {
529+
contentType: (req.headers as any)['content-type'],
530+
reqBody: JSON.parse(req.body as string),
531+
},
532+
status: 200,
533+
}),
524534
);
525535

526536
const result = await client.posts.patchPost({
527537
params: { id: '1' },
538+
headers: {
539+
'content-type': 'application/merge-patch+json',
540+
},
541+
body: value,
528542
});
529543

530-
expect(result.body).toStrictEqual(value);
544+
expect(result.body).toEqual({
545+
contentType: 'application/merge-patch+json',
546+
reqBody: value,
547+
});
531548
expect(result.status).toBe(200);
532-
expect(result.headers.get('Content-Length')).toBe('15');
533549
expect(result.headers.get('Content-Type')).toBe('application/json');
534550
});
535551
});

libs/ts-rest/core/src/lib/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ export const fetchApi = ({
238238
...fetcherArgs,
239239
contentType: 'application/x-www-form-urlencoded',
240240
headers: {
241-
...fetcherArgs.headers,
242241
'content-type': 'application/x-www-form-urlencoded',
242+
...fetcherArgs.headers,
243243
},
244244
body:
245245
typeof body === 'string'
@@ -253,8 +253,8 @@ export const fetchApi = ({
253253
...fetcherArgs,
254254
contentType: 'application/json',
255255
headers: {
256-
...fetcherArgs.headers,
257256
'content-type': 'application/json',
257+
...fetcherArgs.headers,
258258
},
259259
body: JSON.stringify(body),
260260
};

0 commit comments

Comments
 (0)