Skip to content
This repository has been archived by the owner on Nov 11, 2023. It is now read-only.

Commit

Permalink
- support form-data on mutate function (already fixed for useMutate)
Browse files Browse the repository at this point in the history
- fix issue 264
  • Loading branch information
dcruzin authored and fabien0102 committed Apr 16, 2021
1 parent 7f1f69b commit e6a1d0f
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Mutate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,19 +193,24 @@ class ContextlessMutate<TData, TError, TQueryParams, TRequestBody, TPathParams>

const request = new Request(makeRequestPath(), {
method: verb,
body: typeof body === "object" ? JSON.stringify(body) : body,
body: body instanceof FormData ? body : typeof body === "object" ? JSON.stringify(body) : body,
...(typeof providerRequestOptions === "function"
? await providerRequestOptions<TRequestBody>(makeRequestPath(), verb, body)
: providerRequestOptions),
...mutateRequestOptions,
headers: {
"content-type": typeof body === "object" ? "application/json" : "text/plain",
...(typeof providerRequestOptions === "function"
? (await providerRequestOptions<TRequestBody>(makeRequestPath(), verb, body)).headers
: (providerRequestOptions || {}).headers),
...(mutateRequestOptions ? mutateRequestOptions.headers : {}),
},
} as RequestInit); // Type assertion for version of TypeScript that can't yet discriminate.

// only set default content-type if body is not of type FormData and there is no content-type already defined on mutateRequestOptions.headers
if (!(body instanceof FormData) && !request.headers.has("content-type")) {
request.headers.set("content-type", typeof body === "object" ? "application/json" : "text/plain");
}

if (onRequest) onRequest(request);

let response: Response;
Expand Down

0 comments on commit e6a1d0f

Please sign in to comment.