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

Register metadata on classes #1

Merged
merged 13 commits into from Apr 18, 2021
Merged

Register metadata on classes #1

merged 13 commits into from Apr 18, 2021

Conversation

revmischa
Copy link
Member

@revmischa revmischa commented Apr 10, 2021

This uses reflect-metadata to stash metadata on classes and methods for generating CDK resources. Seems to work pretty well.

See the test to get an idea of how it should work

The idea is this should generate two lambda functions and three APIGW routes:

  1. AlbumCrudApi handler
  2. /album/test custom route
  3. /blargle function-based route (no class) - can't use a decorator on a function (I think) but we can simply wrap it in a function
@Entity()
export class Album extends BaseModel {
  @Column({ nullable: true })
  title: string;
}

@CrudApi({ model: Album, route: "/album", memorySize: 512 })
export class AlbumCrudApi extends CrudApiBase {
  @SubRoute("/test")
  async test() {
    return "Testerino";
  }

  post: APIGatewayProxyHandlerV2 = async () => "Posterino";
}

Route({ route: "/blargle" })(async function (event) {
  return JSON.stringify({
    message: "function route",
    rawQueryString: event.rawQueryString,
  });
});

Tests pass but CI is broken because of this dumb CDK issue

@revmischa revmischa self-assigned this Apr 17, 2021
Copy link

@Dizzzmas Dizzzmas left a comment

Choose a reason for hiding this comment

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

Looks lit.

Wonder what other potential uses of reflect-metadata there are.

@@ -14,10 +13,22 @@ export class Topic extends BaseModel {
name: string;
}

@RegisterCrudApi(app, { model: Topic, route: "/topic", memorySize: 512 })
@CrudApi({ model: Topic, route: "/topic", memorySize: 512 })

Choose a reason for hiding this comment

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

How do we stick schema validation on top of it?

Use that library to put decorators on top of fields in Topic?
https://github.com/typestack/class-validator

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah that's one option and would fit nicely. Would like to look into io-ts again

Choose a reason for hiding this comment

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

There's a library mainly for form validation, that allows to create a schema and then validate user input against it.

If we can stick it on top of the API could kill 2 birds with 1 stone.

https://github.com/jquense/yup

Comment on lines +4 to 5
// a list of resources we would like infrastructure generated for
export const stackResources = [TopicCrudApi];

Choose a reason for hiding this comment

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

We need to import TopicCrudApi, but don't need to do so with the function wrapped in Route({ route: "/blargle" })?

Copy link
Member Author

Choose a reason for hiding this comment

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

No you're right we do, just haven't gotten that far yet

@revmischa revmischa merged commit 7616929 into master Apr 18, 2021
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.

None yet

2 participants