Skip to content

Commit 5a38436

Browse files
committed
feat(openapi-fetch): Allow returning Response from onRequest callback
1 parent fad2a50 commit 5a38436

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

docs/openapi-fetch/middleware-auth.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ const cacheMiddleware: Middleware = {
8484
onResponse({ request, response }) {
8585
if (response.ok) {
8686
const key = getCacheKey(request);
87-
cache.set(key, response);
87+
cache.set(key, response.clone());
8888
}
8989
}
9090
};

packages/openapi-fetch/test/middleware/middleware.test.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,10 @@ test("type error occurs only when neither onRequest nor onResponse is specified"
446446

447447
test("can return response directly from onRequest", async () => {
448448
const customResponse = Response.json({});
449-
const client = createObservedClient<paths>();
449+
450+
const client = createObservedClient<paths>({}, () => {
451+
throw new Error("unexpected call to fetch");
452+
});
450453

451454
client.use({
452455
async onRequest() {
@@ -463,13 +466,12 @@ test("can return response directly from onRequest", async () => {
463466

464467
test("skips subsequent onRequest handlers when response is returned", async () => {
465468
let onRequestCalled = false;
466-
const customResponse = Response.json({});
467469
const client = createObservedClient<paths>();
468470

469471
client.use(
470472
{
471473
async onRequest() {
472-
return customResponse;
474+
return Response.json({});
473475
},
474476
},
475477
{
@@ -487,12 +489,11 @@ test("skips subsequent onRequest handlers when response is returned", async () =
487489

488490
test("skips onResponse handlers when response is returned from onRequest", async () => {
489491
let onResponseCalled = false;
490-
const customResponse = Response.json({});
491492
const client = createObservedClient<paths>();
492493

493494
client.use({
494495
async onRequest() {
495-
return customResponse;
496+
return Response.json({});
496497
},
497498
async onResponse() {
498499
onResponseCalled = true;

0 commit comments

Comments
 (0)