Skip to content

Commit

Permalink
fix: clone request object on fetch based adapter (#37)
Browse files Browse the repository at this point in the history
* test: add test specs

* fix: clone request object on fetch based adapter

* chore: bump package
  • Loading branch information
orionmiz authored Feb 16, 2024
1 parent 03d215d commit f6ff0d9
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-students-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"plantae": patch
---

fix: clone request object on fetch based adapter
14 changes: 12 additions & 2 deletions packages/plantae/src/createMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ export function createResponseMiddleware<T, U>({
extendClientRequest,
convertToAdapterResponse,
extendClientResponse,
cloneClientRequest,
retry,
}: {
plugins: Plugin[];
convertToAdapterRequest: ConvertToAdapterRequest<T>;
extendClientRequest: ExtendClientRequest<T>;
convertToAdapterResponse: ConvertToAdapterResponse<U>;
extendClientResponse: ExtendClientResponse<U>;
cloneClientRequest?: (req: T) => T;
retry: Retryer<T, U>;
}) {
return async (clientResponse: U, clientRequest: T) => {
Expand All @@ -64,12 +66,17 @@ export function createResponseMiddleware<T, U>({
convertToAdapterRequest(clientRequest),
// eslint-disable-next-line @typescript-eslint/no-loop-func
async (adapterRequest) => {
clientRequest = await extendClientRequest(
const extendedClientRequest = await extendClientRequest(
clientRequest,
adapterRequest
);
const clonedClientRequest = cloneClientRequest
? cloneClientRequest(extendedClientRequest)
: extendedClientRequest;

clientResponse = await retry(clientRequest);
clientResponse = await retry(extendedClientRequest);

clientRequest = clonedClientRequest;

return convertToAdapterResponse(clientResponse);
}
Expand All @@ -86,6 +93,7 @@ export default function createMiddleware<Req, Res>({
extendClientRequest,
convertToAdapterResponse,
extendClientResponse,
cloneClientRequest,
plugins,
retry,
}: {
Expand All @@ -94,6 +102,7 @@ export default function createMiddleware<Req, Res>({
extendClientRequest: ExtendClientRequest<Req>;
convertToAdapterResponse: ConvertToAdapterResponse<Res>;
extendClientResponse: ExtendClientResponse<Res>;
cloneClientRequest?: (req: Req) => Req;
retry: Retryer<Req, Res>;
}) {
return {
Expand All @@ -108,6 +117,7 @@ export default function createMiddleware<Req, Res>({
extendClientRequest,
convertToAdapterResponse,
extendClientResponse,
cloneClientRequest,
retry,
}),
};
Expand Down
29 changes: 29 additions & 0 deletions packages/plantae/src/fetch/createFetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,4 +424,33 @@ describe("createFetch", () => {

expect(await res.text()).toBe("retried");
});

it("should pass unused request to afterResponse hook", async () => {
server.use(http.post(base("/"), () => new Response()));

const fetch = createFetch({
plugins: [
{
name: "plugin-clone-request",
hooks: {
beforeRequest: (req) => {
return new Request(req, {
method: "POST",
body: "modified",
});
},
afterResponse: (_, req, retry) => {
expect(req.bodyUsed).toBe(false);

return retry(req);
},
},
},
],
});

await fetch(base("/"));

expect.assertions(1);
});
});
4 changes: 3 additions & 1 deletion packages/plantae/src/fetch/createFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ const createFetch = ({
convertToAdapterResponse: (res) => res,
extendClientRequest: (_, req) => req as Request,
extendClientResponse: (_, res) => res as Response,
cloneClientRequest: (req) => req.clone(),
plugins,
retry: client,
});

const request = await requestMiddleware(initialRequest);
const clonedClientRequest = request.clone();

const initialResponse = await client(request);

return responseMiddleware(initialResponse, request);
return responseMiddleware(initialResponse, clonedClientRequest);
};
};

Expand Down
35 changes: 35 additions & 0 deletions packages/plantae/src/ky/createKyHooks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,4 +505,39 @@ describe("createKyHooks", () => {

expect(await res.text()).toBe("retried");
});

it("should pass unused request to afterResponse hook", async () => {
server.use(http.post(base("/"), () => new Response()));

const hooks = createKyHooks({
client: Ky,
plugins: [
{
name: "plugin-clone-request",
hooks: {
beforeRequest: (req) => {
return new Request(req, {
method: "POST",
body: "modified",
});
},
afterResponse: (_, req, retry) => {
expect(req.bodyUsed).toBe(false);

return retry(req);
},
},
},
],
});

const ky = Ky.create({
prefixUrl: baseURL,
hooks,
});

await ky.get("");

expect.assertions(1);
});
});
1 change: 1 addition & 0 deletions packages/plantae/src/ky/createKyHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const createKyHooks = ({
convertToAdapterResponse: (res) => res,
extendClientRequest: (_, req) => req as Request,
extendClientResponse: (_, res) => res as Response,
cloneClientRequest: (req) => req.clone(),
plugins,
retry: client,
});
Expand Down

0 comments on commit f6ff0d9

Please sign in to comment.