Skip to content

Commit

Permalink
Add missed header Accept in createJsonQuery/createJsonMutation
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkamyshev committed Nov 22, 2023
1 parent 7432c75 commit a3b87b6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/tall-eggs-remain.md
@@ -0,0 +1,5 @@
---
'@farfetched/core': patch
---

Add missed header Accept in `createJsonQuery`/`createJsonMutation`
Expand Up @@ -27,6 +27,7 @@ describe('fetch/json.request.headers', () => {

expect(fetchMock.mock.calls[0][0].headers).toEqual(
new Headers({
Accept: 'application/json',
'content-type': 'application/json',
})
);
Expand All @@ -46,6 +47,8 @@ describe('fetch/json.request.headers', () => {
params: {},
});

expect(fetchMock.mock.calls[0][0].headers).toEqual(new Headers({}));
expect(fetchMock.mock.calls[0][0].headers).toEqual(
new Headers({ Accept: 'application/json' })
);
});
});
15 changes: 10 additions & 5 deletions packages/core/src/fetch/json.ts
Expand Up @@ -40,11 +40,16 @@ export function createJsonApiRequest<R extends CreationRequestConfig>(
headers: normalizeStaticOrReactive(config.request.headers),
},
({ method, headers }) =>
['GET', 'HEAD'].includes(method!) // TODO: fix type inferences
? headers
: mergeRecords(headers, {
'Content-Type': 'application/json',
})
// reversed merge order to allow any modifications in the user code
mergeRecords(
{
Accept: 'application/json',
'Content-Type': ['GET', 'HEAD'].includes(method)
? undefined
: 'application/json',
},
headers
)
);

const jsonApiCallFx = createApiRequest<
Expand Down

0 comments on commit a3b87b6

Please sign in to comment.