Skip to content

fix(#18): expose aws context #19

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

moeriki
Copy link
Contributor

@moeriki moeriki commented Mar 21, 2020

No description provided.

src/types.ts Outdated
Comment on lines 11 to 14
export interface AwsContext extends Context {
aws: { context: any, event: AwsRequest }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we have to literally extend it Context. I would rather use interface declaration merging.

The syntax for this would be something like this (untested, based on memory):

declare module "@curveball/core" {
  interface Context {
    aws?: { 
      context: any,
      event: any
    }
}

The issue with simply extending the interface, is that everything else needs to be extended (Application, middlewares, etc), or add typeguards in a bunch of different places. Neither are ideal I think.

by using declaration merging, all you have to do is install the package, and ctx.aws will be immediately available everywhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just read up on declaration merging. Interesting stuff to have in your toolbox :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea =) makes total sense too for a language where monkey patching is common

src/index.ts Outdated
@@ -14,7 +14,8 @@ export default function lambdaHandler(app: Application): AwsLambdaHandler {
awsRequest.isBase64Encoded ? Buffer.from(awsRequest.body, 'base64') : awsRequest.body,
);
const response = new MemoryResponse();
const context = new Context(request, response);
const context = new Context(request, response) as AwsContext;
context.aws = { context, event: awsRequest };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's needed to nest the context, I would just want to add the lambda specific objects to context.aws

@moeriki moeriki force-pushed the issue-18-aws-context branch from a2a0ef8 to a65a2db Compare March 23, 2020 10:21
@moeriki moeriki changed the title [WIP] fix(#18): expose aws context fix(#18): expose aws context Mar 23, 2020
@moeriki
Copy link
Contributor Author

moeriki commented Mar 23, 2020

I rebased to master and am testing with my project.

I currently set ctx.aws?: {} though I think it's not necessary to have it optional.

Right now I have to wrap ctx.aws in a type-checked if. I don't think it can ever be undefined?

@evert
Copy link
Member

evert commented Mar 23, 2020

Everything looks great so far. There are some situations where ctx.aws could not be set, which made me feel it's a bit safer to make it optional.

The 2 cases I can think of are:

  1. A server application is written for both Lambda, Azure and regular http service.
  2. Your application emits an internal sub-request, via app.handle() or app.subRequest().

In each of those cases the handler will not have triggered.

I feel a bit safer making it optional for that reason. If you are very certain for your specific case that it's always going to be set, you can always access the property with ctx.aws!.context

But taking that 'risk' of ignoring the undefined case feels a bit better if that's done by the user of the library, vs. the library itself. The user has a better idea of if this case happens to them.

@evert
Copy link
Member

evert commented Mar 23, 2020

P.s. the linter can fix the build error with make fix

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 this pull request may close these issues.

2 participants