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

Req.url incorrectly evaluated for paths with nested proxy - ApiGateway #400

Open
saltonmassally opened this issue Apr 26, 2021 · 5 comments

Comments

@saltonmassally
Copy link

GIven the following resource /testing/{proxy+} a call to /testing/creategroup is evaluated to /creategroup rather than /testing/creatgroup.

Looking at getPathWithQueryStringParams, it appears that if proxy pathParameter is present, it is simply used, discarding the other parts. Would substituting the value for proxy for the token {proxxy+} into event.resource (/testing/{proxy+}) be a potential approach to fixing this?

@thetumper
Copy link

This appears related:
#362

@thyming
Copy link

thyming commented Apr 5, 2022

is there a workaround for this? my use case is wanting to create a callback url to a path, and with the same url structure as @saltonmassally has defined above.

@sanchesrm
Copy link

sanchesrm commented Apr 6, 2022

I've found a workaround for this, in my specific case I had to do this:

const handle = async (event, context) => {
  event.resource = '/users/{proxy+}'.replace(/^\/users/, '');
  event.requestContext.resourcePath = '/users/{proxy+}'.replace(/^\/users/, '');
  event.pathParameters.proxy = `users/${event.pathParameters.proxy}`;

  return serverlessExpressInstance(event, context)
}

@pejulian
Copy link

pejulian commented Sep 29, 2022

I've found a workaround for this, in my specific case I had to do this:

const handle = async (event, context) => {
  event.resource = '/users/{proxy+}'.replace(/^\/users/, '');
  event.requestContext.resourcePath = '/users/{proxy+}'.replace(/^\/users/, '');
  event.pathParameters.proxy = `users/${event.pathParameters.proxy}`;

  return serverlessExpressInstance(event, context)
}

Thank you for this. It worked perfectly for my scenario as well.
In my case, I had a non-changeable scenario where the nested greedy proxy had to be attached to a resource that also happens to be a pathParameter.

In my case, the fix was

export const handler: APIGatewayProxyHandler = async (event, context, callback) => {
    // Enable support for a "version" pathParameter that was defined in the AWS CDK project stack.
    // In the stack, the greedy proxy (.addProxy()) is attached to this resource.

    const nextEvent: APIGatewayProxyEvent = {
        ...event,
        resource: event.resource.replace(/^\/{version}/, ''),
        requestContext: {
            ...(event.requestContext ?? {}),
            resourcePath: event.requestContext.resourcePath.replace(/^\/{version}/, '')
        },
        pathParameters: {
            ...(event.pathParameters ?? {}),
            // Get the actual value of the "version" pathParameter and prefix it to the value of "proxy"
            proxy: `${event.pathParameters?.['version']}/${event.pathParameters?.['proxy']}`
        }
    };

    server = await boostrap();

    // Pass the modified event to the serverless express handler
    return server(nextEvent, context, callback);
};


@wladimirgrf
Copy link

wladimirgrf commented Jan 21, 2023

I got the same error.
It worked (without any changes in the event) only with the old version:

import serverlessExpress from 'aws-serverless-express';

async function bootstrap(): Promise<Server> {
  const app = await NestFactory.create(AppModule, {
    logger: ['error', 'warn'],
  });

  await app.init();

  const expressApp = app.getHttpAdapter().getInstance();
  return serverlessExpress.createServer(expressApp);
}

export const handler: Handler = async (
  event: APIGatewayProxyEvent,
  context: Context,
) => {
  server = server ?? (await bootstrap());

  return serverlessExpress.proxy(server, event, context, 'PROMISE').promise;
};

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

No branches or pull requests

6 participants