Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ability to modify request handler #49

Open
Mr-Ples opened this issue Sep 5, 2024 · 1 comment · May be fixed by #50
Open

ability to modify request handler #49

Mr-Ples opened this issue Sep 5, 2024 · 1 comment · May be fixed by #50

Comments

@Mr-Ples
Copy link

Mr-Ples commented Sep 5, 2024

we are using remix-aws but i need to send analytics events for all requests (even non rendering routes) so i had to rip the code from this repo and uninstall remix-aws and just use the modified code.

would be nice if we could still use remix-aws instead tho. please add the ability to modify the request handler or inject a callback or something.

this is the modified version of the createRequestHandler that we are using. we basically just wrapped an analytics event around the request object before the response

export function createRequestHandler({
  build,
  getLoadContext,
  mode = process.env.NODE_ENV,
  awsProxy = AWSProxy.APIGatewayV2,
}: {
  build: ServerBuild;
  getLoadContext?: GetLoadContextFunction;
  mode?: string;
  awsProxy?: AWSProxy;
}): RequestHandler {
  const handleRequest = createRemixRequestHandler(build, mode);

  return async (
    event:
      | APIGatewayProxyEvent
      | APIGatewayProxyEventV2
      | ALBEvent /*, context*/,
  ) => {
    const awsAdapter = createRemixAdapter(awsProxy);
    const request = awsAdapter.createRemixRequest(
      event as APIGatewayProxyEvent & APIGatewayProxyEventV2 & ALBEvent,
    );
    const loadContext = getLoadContext?.(event);

    // analytics
    let newRequest = request;
    try {
      newRequest = await sendServerEvents(request);
    } catch (e) {
      // do nothing
      console.log("ANALYTICS SERVER ERROR");
      console.error(e);
    }

    const response = (await handleRequest(newRequest, loadContext)) as Response;

    return awsAdapter.sendRemixResponse(response);
  };
}
wingleung pushed a commit that referenced this issue Oct 5, 2024
@wingleung wingleung linked a pull request Oct 5, 2024 that will close this issue
@wingleung
Copy link
Owner

thanks for the idea, it's interesting there isn't any similar hook available in remix, it would be good to have this in remix as well.

PR created #50

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants