Skip to content

Commit

Permalink
fix: transform body into null when having null body status (#26)
Browse files Browse the repository at this point in the history
* fix: transform body into null when having null body status

* Bump
  • Loading branch information
orionmiz authored Oct 23, 2023
1 parent 6375ddc commit dbe98be
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/wet-schools-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"plantae": patch
---

fix: transform body into null when having null body status
19 changes: 13 additions & 6 deletions packages/plantae/src/axios/createAxiosInterceptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {

import createMiddleware from "../createMiddleware";
import type { AdapterRequest, AdapterResponse, Plugin } from "../types";
import { isArrayBuffer } from "../utils";
import { isArrayBuffer, isNullBodyStatus } from "../utils";

type InterceptorType = keyof AxiosInstance["interceptors"];

Expand Down Expand Up @@ -127,11 +127,18 @@ function convertToAdapterResponse(res: AxiosResponse): AdapterResponse {
(res.config.responseType === "json" ||
(!res.config.responseType && res.config.transitional?.forcedJSONParsing));

return new Response(isJSONBody ? JSON.stringify(res.data) : res.data, {
status: res.status,
statusText: res.statusText,
headers: new Headers(headers.toJSON(true) as HeadersInit),
});
return new Response(
isJSONBody
? JSON.stringify(res.data)
: isNullBodyStatus(res.status)
? null
: res.data,
{
status: res.status,
statusText: res.statusText,
headers: new Headers(headers.toJSON(true) as HeadersInit),
}
);
}

async function extendClientResponse(
Expand Down
4 changes: 4 additions & 0 deletions packages/plantae/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ const kindOfTest = (type: string) => {
};

export const isArrayBuffer = kindOfTest("ArrayBuffer");

// https://fetch.spec.whatwg.org/#statuses
export const isNullBodyStatus = (status: number) =>
[101, 103, 204, 205, 304].includes(status);

0 comments on commit dbe98be

Please sign in to comment.