Commit 9f7e0784 authored by tom's avatar tom

add header for POST/PUT requests

parent 31138144
...@@ -19,15 +19,13 @@ export default function useFetch() { ...@@ -19,15 +19,13 @@ export default function useFetch() {
const { token } = queryClient.getQueryData<CsrfData>(getResourceKey('csrf')) || {}; const { token } = queryClient.getQueryData<CsrfData>(getResourceKey('csrf')) || {};
return React.useCallback(<Success, Error>(path: string, params?: Params): Promise<Success | ResourceError<Error>> => { return React.useCallback(<Success, Error>(path: string, params?: Params): Promise<Success | ResourceError<Error>> => {
const hasBody = params?.method && params?.body && ![ 'GET', 'HEAD' ].includes(params.method);
const reqParams = { const reqParams = {
...params, ...params,
body: params?.method && params?.body && ![ 'GET', 'HEAD' ].includes(params.method) ? body: hasBody ? JSON.stringify({ ...params.body, _csrf_token: token }) : undefined,
JSON.stringify({
...params.body,
_csrf_token: token,
}) :
undefined,
headers: { headers: {
...(hasBody ? { 'Content-type': 'application/json' } : undefined),
...params?.headers, ...params?.headers,
// ...(token ? { 'x-csrf-token': token } : {}), // ...(token ? { 'x-csrf-token': token } : {}),
}, },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment