Skip to content

Commit

Permalink
Replaced nest in favour of aws api gateway + lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
Facundo De Lorenzo committed May 16, 2024
1 parent e748c6b commit 7af5fc3
Show file tree
Hide file tree
Showing 15 changed files with 177 additions and 2,947 deletions.
63 changes: 5 additions & 58 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,12 @@
"private": true,
"license": "UNLICENSED",
"scripts": {
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/platform-fastify": "^10.3.8",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1"
"build": "rm -rf dist && tsc -b",
"postbuild": "cp package.json dist/package.json"
},
"devDependencies": {
"@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/node": "^20.3.1",
"@types/supertest": "^6.0.0",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"eslint": "^8.42.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"jest": "^29.5.0",
"prettier": "^3.0.0",
"source-map-support": "^0.5.21",
"supertest": "^6.3.3",
"ts-jest": "^29.1.0",
"ts-loader": "^9.4.3",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.3"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
"@types/aws-lambda": "^8.10.137",
"@types/node": "^20.12.11",
"typescript": "^5.4.5"
}
}
9 changes: 9 additions & 0 deletions packages/api/samconfig.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version = 0.1
[default]
[default.deploy]
[default.deploy.parameters]
s3_bucket = "faq-lambda-artifacts"
region = "us-east-1"
confirm_changeset = true
capabilities = "CAPABILITY_IAM"

52 changes: 0 additions & 52 deletions packages/api/src/app.controller.spec.ts

This file was deleted.

17 changes: 0 additions & 17 deletions packages/api/src/app.controller.ts

This file was deleted.

10 changes: 0 additions & 10 deletions packages/api/src/app.module.ts

This file was deleted.

33 changes: 0 additions & 33 deletions packages/api/src/app.service.ts

This file was deleted.

12 changes: 0 additions & 12 deletions packages/api/src/main.ts

This file was deleted.

74 changes: 74 additions & 0 deletions packages/api/src/products.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { APIGatewayEvent, Callback, Context } from 'aws-lambda';
import { CreateOrderRequest, Order, Product } from './types';

const responseHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Methods': 'OPTIONS,GET, POST',
};

exports.get = async (
event: APIGatewayEvent,
context: Context,
callback: Callback,
) => {
try {
const products: Product[] = [
{ name: 'Kuva Roast Rib Eye', price: 418 },
{ name: 'Guadalupe Half Rack', price: 298 },
{ name: 'Tohono Chicken', price: 308 },
];

return {
isBase64Encoded: false,
statusCode: 200,
headers: responseHeaders,
body: JSON.stringify(products),
};
} catch (error) {
return {
isBase64Encoded: false,
statusCode: 500,
headers: responseHeaders,
body: error.message,
};
}
};

exports.createOrder = async (
event: APIGatewayEvent,
context: Context,
callback: Callback,
) => {
try {
const req: CreateOrderRequest = JSON.parse(event.body);

if (!req?.products)
return {
isBase64Encoded: false,
statusCode: 400,
headers: responseHeaders,
body: "Products must be provided",
}
const order: Order = {
products: req.products,
totalPrice: req.products.reduce((acc, val) => acc + val.price, 0),
};

return {
isBase64Encoded: false,
statusCode: 200,
headers: responseHeaders,
body: JSON.stringify(order),
};
} catch (error) {
return {
isBase64Encoded: false,
statusCode: 500,
headers: responseHeaders,
body: error.message,
};
}
};


13 changes: 13 additions & 0 deletions packages/api/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export interface CreateOrderRequest {
products: Product[];
}

export interface Product {
name: string;
price: number;
}

export interface Order {
products: Product[];
totalPrice: number;
}
34 changes: 34 additions & 0 deletions packages/api/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: Api

Resources:
GetProductsFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./dist
Handler: products.get
Runtime: nodejs20.x
MemorySize: 128
Timeout: 10
Events:
Event:
Type: Api
Properties:
Method: GET
Path: /products

createOrderFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./dist
Handler: products.createOrder
Runtime: nodejs20.x
MemorySize: 128
Timeout: 10
Events:
Event:
Type: Api
Properties:
Method: POST
Path: /createOrder
24 changes: 0 additions & 24 deletions packages/api/test/app.e2e-spec.ts

This file was deleted.

9 changes: 0 additions & 9 deletions packages/api/test/jest-e2e.json

This file was deleted.

5 changes: 3 additions & 2 deletions packages/api/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"compilerOptions": {
"module": "commonjs",
"module": "node16",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "ES2021",
"target": "es2022",
"lib": ["es2022"],
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
Expand Down
3 changes: 2 additions & 1 deletion packages/front/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const App = () => {
}}
>
<Title>This is a title</Title>
<div>{products.length > 0 && "hay productitos jeje"}</div>
<div>{products.length > 0 && "hay productos"}</div>
<div>{products.length === 0 && "No hay productos :("}</div>
<img height={600} />
</div>
);
Expand Down
Loading

0 comments on commit 7af5fc3

Please sign in to comment.