Skip to content

Commit

Permalink
add response hook
Browse files Browse the repository at this point in the history
fixes #49
  • Loading branch information
Wing Leung committed Oct 5, 2024
1 parent 6186f51 commit 00b5020
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ installGlobals()
export const handler = createRequestHandler({
build,
mode: process.env.NODE_ENV,
awsProxy: AWSProxy.APIGatewayV1
awsProxy: AWSProxy.APIGatewayV1,
responseHook: async (response: Response) => {

// do some transformations before sending the response

return response
}
})
```

Expand All @@ -54,6 +60,10 @@ By default the `awsProxy` is set to `AWSProxy.APIGatewayV2`.
- `AWSProxy.ALB`
- `AWSProxy.FunctionURL`

### `responseHook`

This hook allows you to do transformations on the response before sending it to the client.

## Notes

### split from @remix/architect
Expand Down
10 changes: 9 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ export function createRequestHandler({
build,
getLoadContext,
mode = process.env.NODE_ENV,
awsProxy = AWSProxy.APIGatewayV2
awsProxy = AWSProxy.APIGatewayV2,
responseHook,
}: {
build: ServerBuild;
getLoadContext?: GetLoadContextFunction;
mode?: string;
awsProxy?: AWSProxy;
responseHook?: (response: Response) => Promise<Response>
}): RequestHandler {
const handleRequest = createRemixRequestHandler(build, mode)

Expand All @@ -61,6 +63,12 @@ export function createRequestHandler({

const response = (await handleRequest(request, loadContext)) as Response

if (responseHook) {
const modifiedResponse = await responseHook(response)

return awsAdapter.sendRemixResponse(modifiedResponse)
}

return awsAdapter.sendRemixResponse(response)
}
}

0 comments on commit 00b5020

Please sign in to comment.