Skip to content

Commit

Permalink
fix: settle middleware response for onRejected (#28)
Browse files Browse the repository at this point in the history
* fix: settle middleware response for `onRejected`

* Bump
  • Loading branch information
orionmiz authored Oct 23, 2023
1 parent a160f40 commit a96136e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/healthy-ads-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"plantae": patch
---

fix: settle middleware response for onRejected
10 changes: 9 additions & 1 deletion packages/plantae/src/axios/createAxiosInterceptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
import createMiddleware from "../createMiddleware";
import type { AdapterRequest, AdapterResponse, Plugin } from "../types";
import { isArrayBuffer, isNullBodyStatus } from "../utils";
import settle from "./utils";

type InterceptorType = keyof AxiosInstance["interceptors"];

Expand Down Expand Up @@ -225,7 +226,14 @@ const createAxiosInterceptors = ({
const { response } = err;

if (response && response.config) {
return responseMiddleware(response, response.config);
const middlewareResponse = await responseMiddleware(
response,
response.config
);

return new Promise((resolve, reject) => {
settle(resolve, reject, middlewareResponse);
});
}

return Promise.reject(err);
Expand Down
26 changes: 26 additions & 0 deletions packages/plantae/src/axios/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { AxiosResponse } from "axios";
import { AxiosError } from "axios";

// https://github.com/axios/axios/blob/7d45ab2e2ad6e59f5475e39afd4b286b1f393fc0/lib/core/settle.js#L15
export default function settle(
resolve: Function,
reject: Function,
response: AxiosResponse
) {
const validateStatus = response.config.validateStatus;
if (!response.status || !validateStatus || validateStatus(response.status)) {
resolve(response);
} else {
reject(
new AxiosError(
"Request failed with status code " + response.status,
[AxiosError.ERR_BAD_REQUEST, AxiosError.ERR_BAD_RESPONSE][
Math.floor(response.status / 100) - 4
],
response.config,
response.request,
response
)
);
}
}

0 comments on commit a96136e

Please sign in to comment.