lambda-local icon indicating copy to clipboard operation
lambda-local copied to clipboard

add context overwrite option

Open nazoking opened this issue 1 year ago • 1 comments

My use case was that I wanted the "awsRequestId" to be something I specified. In this issue #225, the proposer seems to want to change the function name to any name. In either case, you can configure it using this additional option.

The added test case becomes a sample.

            lambdalocal.execute({
                event: require(path.join(__dirname, "./events/test-event.js")),
                lambdaPath: path.join(__dirname, "./functs/test-func.js"),
                lambdaHandler: functionName,
                contextOverwrite: function (ctx){
                    ctx.awsRequestId = "test id";
                    ctx.functionName = "test function";
                },
                callback: function (err, done) {
                    assert.equal(done.result, "testvar");
                    assert.equal(done.context.awsRequestId, "test id");
                    assert.equal(done.context.functionName, "test function");
                    cb();
                }
            });

Specify a function in the optional contextOverwrite passed to the execute method. That function receives the created context. You can modify the context inside the function. If you modify context.done etc., it is expected that it will not work as expected, but please forgive this as it is the developer's own responsibility.

nazoking avatar Sep 27 '23 03:09 nazoking

This should be please added to the README file and/or documentation - I only came across this by accident. Thanks!

tfrancois avatar Jan 22 '24 07:01 tfrancois

Thanks for the PR & sorry for the delay.

gpotter2 avatar Apr 13 '24 21:04 gpotter2