Skip to content

Commit

Permalink
fix(server): subsequent onResponse hooks get the latest response (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan authored Feb 14, 2025
1 parent 3482961 commit 7d28669
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 18 deletions.
24 changes: 24 additions & 0 deletions .changeset/strange-peas-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
'@whatwg-node/server': patch
---

When two plugins use the `onResponse` hook and the first one modifies the response, the second one should get the modified one;

```ts
[
{
onResponse({ setResponse, fetchAPI }) {
setResponse(
fetchAPI.Response.json({
foo: 'bar'
}, { status: 418 })
)
}
},
{
onResponse({ response }) {
console.log(response.status) // 418
}
}
]
```
28 changes: 10 additions & 18 deletions packages/server/src/createServerAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { AsyncDisposableStack, DisposableSymbols } from '@whatwg-node/disposablestack';
import * as DefaultFetchAPI from '@whatwg-node/fetch';
import {
OnRequestHook,
OnResponseEventPayload,
OnResponseHook,
ServerAdapterPlugin,
} from './plugins/types.js';
import { OnRequestHook, OnResponseHook, ServerAdapterPlugin } from './plugins/types.js';
import {
FetchAPI,
FetchEvent,
Expand Down Expand Up @@ -212,19 +207,16 @@ function createServerAdapter<
if (onResponseHooks.length === 0) {
return response;
}
const onResponseHookPayload: OnResponseEventPayload<
TServerContext & ServerAdapterInitialContext
> = {
request,
response,
serverContext,
setResponse(newResponse) {
response = newResponse;
},
fetchAPI,
};
const onResponseHooksIteration$ = iterateAsyncVoid(onResponseHooks, onResponseHook =>
onResponseHook(onResponseHookPayload),
onResponseHook({
request,
response,
serverContext,
setResponse(newResponse) {
response = newResponse;
},
fetchAPI,
}),
);
if (isPromise(onResponseHooksIteration$)) {
return onResponseHooksIteration$.then(() => response);
Expand Down
31 changes: 31 additions & 0 deletions packages/server/test/plugins.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { describe, expect, it } from '@jest/globals';
import { Request, Response } from '@whatwg-node/fetch';
import { createServerAdapter } from '@whatwg-node/server';

describe('Plugins', () => {
it('should reflect updated response in subsequent plugins', async () => {
let firstRes: Response | undefined;
let secondRes: Response | undefined;
const adapter = createServerAdapter<{}>(() => Response.json({ message: 'Hello, World!' }), {
plugins: [
{
onResponse({ response, setResponse }) {
firstRes = response;
setResponse(Response.json({ message: 'Good bye!' }, { status: 418 }));
},
},
{
onResponse({ response }) {
secondRes = response;
},
},
],
});
const request = new Request('http://localhost');
const response = await adapter.fetch(request);
expect(response.status).toBe(418);
expect(await response.json()).toEqual({ message: 'Good bye!' });
expect(firstRes?.status).toBe(200);
expect(secondRes?.status).toBe(418);
});
});

0 comments on commit 7d28669

Please sign in to comment.