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

Maintenance: Lambda Typescript Foundations #3429

Draft
wants to merge 1 commit into
base: maintenance/lambda-refactor
Choose a base branch
from

Conversation

rumzledz
Copy link
Contributor

@rumzledz rumzledz commented Oct 22, 2024

Description

Note

It doesn't make sense to get this reviewed until we can first verify if it's still going to work properly once we deploy it to our QA environment.
I believe there's also a chance that this might be rendered obsolete due to some upcoming ops work. @bogdan-1337 or @rdig can tell you more about it.
And so this will be in "Draft" mode until we come up with a final decision.

This PR introduces Typescript to the bridgeXYZMutation lambda. The goal is to consider this as our standard when converting other existing lambdas into Typescript as well as for new ones.

With this setup, you should just be able to focus on building/maintaining the typescript files in a lambda, taking advantage of type safety and other goodies that come with TypeScript while esbulid transpiles your changes in realtime.

Key Things To Note

Why I'm using esbuild

  • It is used to transpile TypeScript code into JavaScript
  • It helps us generate a single, inlined JavaScript file (index.js) that consolidates all the TypeScript files. This inlining avoids issues with dependencies and paths, making the deployment artifacts easier to manage.

The esbuild script can be found in scripts/transpile-lambdas.js.

There are two package.json scripts you can use to conveniently run this:

# Uses esbuild's watch mode to transpile the TypeScript files and watch for TypeScript file changes
# This is run during local development as part of `npm run dev`
npm run lambdas:watch

# Simply transpiles the lambda TypeScript files
# This is run as part of the amplify-deploy.sh script to make sure that the TypeScript lambda files are transpiled into a single index.js file before they are deployed
npm run lambdas:transpile 

Why I added the source-map-support package

  • It links error messages back to the original TypeScript files. So when something breaks, you’ll see the exact file and line number in your TypeScript code, not the compiled JavaScript
  • All you have to do is add import 'source-map-support/register'; at the top of the TypeScript entry file (index.ts) and it will just work 😄
// index.ts
import 'source-map-support/register';

import { Something } from 'somewhere';

// Rest of your code...
Before After
Screenshot 2024-10-22 at 20 56 20 Screenshot 2024-10-22 at 21 12 58

Sections marked with // @TODO: Lambda utils refactor candidate

My next task is to create an npm package that hosts all of our Lambda shared utils. So sections marked with this will be scrapped and moved to that npm package as soon as that's available. There's possibly more of them hidden somewhere but I'll cross that bridge when I get there.

Testing

We will have to test all features of the app that rely on the bridgeXYZMutation lambda. Some of the key things to test would be these places:

Crypto to fiat page

URL: http://localhost:9091/account/crypto-to-fiat

Screenshot 2024-10-23 at 23 03 50

Crypto to fiat tab in the UserHub component

image

TODO

  • Get assistance from Bogdan when deploying this lambda to our QA environment BUT this will have to wait until Dave's Notifications OKR has been QA'd

Resolves #3187

@rumzledz rumzledz self-assigned this Oct 22, 2024
import { createLiquidationAddress, getUser } from '../graphql';
import { AppSyncResolverEvent, HandlerContext } from '../types';
import { Maybe } from 'graphql/jsutils/Maybe';
import { KycStatus, User } from '../../../../../../src/graphql/generated';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A MEGA relative import. Yuck. I know. But I'll probably deal with this in another PR if I really have to.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah would be great if it's possible to add an @amplify alias or something!

Copy link
Contributor

Choose a reason for hiding this comment

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

Is some of this left over from testing? Not sure why there are changes to the number of default colonies, removing Amy and Fry, etc

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Definitely left over! 👀 I wanted to make the create-data script faster 🏃 I'll make sure to update this before I officially open this for a proper review!

const watchDir = './amplify';

const watcher = chokidar.watch(watchDir, {
ignored: /^(?!.*\.env$).*\/\.[^/]+/, // Ignore dotfiles except .env
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we ignore files inside of the node_modules folder here too if it is only a small addition? 👀

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yes thanks @iamsamgibbs I forgot about this suggestion of yours!

package.json Outdated
@@ -40,7 +40,9 @@
"forward-time": "node scripts/forward-time",
"playwright:install": "npx playwright install --with-deps",
"playwright:run": "playwright test",
"playwright:watch": "playwright test --ui"
"playwright:watch": "playwright test --ui",
"watch:lambdas": "node scripts/transpile-lambdas.js",
Copy link
Contributor

Choose a reason for hiding this comment

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

This file scripts/transpile-lambdas.js doesn't exist?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you! Left over from previous implementation 🙃

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'll be leaving it in after all after reverting back to a previous implementation 😄

@rumzledz rumzledz force-pushed the maintenance/3187-lambda-ts-foundations branch 11 times, most recently from 0ac103a to d3b4107 Compare October 23, 2024 13:49
@rumzledz rumzledz changed the base branch from maintenance/lambda-refactor to master October 23, 2024 13:55
@rumzledz rumzledz changed the base branch from master to maintenance/lambda-refactor October 23, 2024 13:56
@rumzledz rumzledz force-pushed the maintenance/3187-lambda-ts-foundations branch 4 times, most recently from 638f510 to 9f96d81 Compare October 23, 2024 15:37
@rumzledz rumzledz marked this pull request as ready for review October 23, 2024 15:37
@rumzledz rumzledz requested review from a team as code owners October 23, 2024 15:37
@rumzledz rumzledz force-pushed the maintenance/3187-lambda-ts-foundations branch 3 times, most recently from 4914b32 to cfb6bfe Compare October 23, 2024 16:02
@rumzledz rumzledz force-pushed the maintenance/3187-lambda-ts-foundations branch from cfb6bfe to ab52415 Compare October 23, 2024 16:25
@rumzledz rumzledz marked this pull request as draft October 25, 2024 14:07
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