Skip to content

Commit 134afc4

Browse files
committed
Update all the things. Run on Node 20.x. Fix upgrade issues.
1 parent ac0cf46 commit 134afc4

13 files changed

+5134
-4597
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ yarn-error.log
88
*.tfplan*
99
*.tfstate*
1010
.dynamodb/
11-
.vscode/
11+
.vscode/
12+
dynamodb.tar.gz

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM node:12-alpine
1+
FROM node:20-alpine
22
WORKDIR /app
33

44
COPY package.json yarn.lock serverless.yml ./

README.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ See [Apiary](http://docs.serverlessapi.apiary.io) for API structure - defined in
88

99
## Technologies Used
1010

11-
* [Serverless](https://github.com/serverless/serverless)
12-
* [TypeScript](https://github.com/microsoft/typescript)
13-
* [Node.js](https://github.com/nodejs/node)
14-
* [Webpack](https://github.com/webpack/webpack)
15-
* [DynamoDB](https://aws.amazon.com/dynamodb)
16-
* [Serverless-offline](https://github.com/dherault/serverless-offline)
17-
* [Serverless-webpack](https://github.com/elastic-coders/serverless-webpack)
18-
* [Serverless-dynamodb-local](https://github.com/99xt/serverless-dynamodb-local)
19-
* [Serverless-domain-manager](https://github.com/amplify-education/serverless-domain-manager)
20-
* [Jsonwebtoken](https://github.com/auth0/node-jsonwebtoken)
21-
* [Docker](https://www.docker.com)
11+
- [Serverless](https://github.com/serverless/serverless)
12+
- [TypeScript](https://github.com/microsoft/typescript)
13+
- [Node.js](https://github.com/nodejs/node)
14+
- [Webpack](https://github.com/webpack/webpack)
15+
- [DynamoDB](https://aws.amazon.com/dynamodb)
16+
- [Serverless-offline](https://github.com/dherault/serverless-offline)
17+
- [Serverless-webpack](https://github.com/elastic-coders/serverless-webpack)
18+
- [Serverless-dynamodb](https://github.com/raisenational/serverless-dynamodb)
19+
- [Serverless-domain-manager](https://github.com/amplify-education/serverless-domain-manager)
20+
- [Jsonwebtoken](https://github.com/auth0/node-jsonwebtoken)
21+
- [Docker](https://www.docker.com)
2222

2323
---
2424

package.json

+24-23
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,34 @@
1616
"author": "Jordan Hornblow <[email protected]>",
1717
"license": "MIT",
1818
"dependencies": {
19-
"aws-sdk": "^2.584.0",
20-
"jsonwebtoken": "^8.5.1",
21-
"moment": "^2.24.0",
22-
"uuid": "^3.3.3"
19+
"aws-sdk": "^2.1593.0",
20+
"jsonwebtoken": "^9.0.2",
21+
"moment": "^2.30.1",
22+
"uuid": "^9.0.1"
2323
},
2424
"devDependencies": {
25-
"@types/aws-lambda": "^8.10.36",
26-
"@types/jsonwebtoken": "^8.3.5",
27-
"@types/node": "^12.12.14",
28-
"@types/uuid": "^3.4.6",
29-
"@types/webpack": "^4.41.0",
30-
"cross-env": "^6.0.3",
31-
"fork-ts-checker-webpack-plugin": "^3.1.1",
32-
"serverless": "^1.58.0",
33-
"serverless-domain-manager": "^3.3.0",
25+
"@types/aws-lambda": "^8.10.136",
26+
"@types/jsonwebtoken": "^9.0.6",
27+
"@types/node": "^20.12.4",
28+
"@types/uuid": "^9.0.8",
29+
"@types/webpack": "^5.28.5",
30+
"@types/webpack-node-externals": "^3.0.4",
31+
"cross-env": "^7.0.3",
32+
"fork-ts-checker-webpack-plugin": "^9.0.2",
33+
"serverless": "^3.38.0",
34+
"serverless-domain-manager": "^7.3.8",
35+
"serverless-dynamodb": "^0.2.51",
3436
"serverless-dynamodb-autoscaling": "^0.6.2",
35-
"serverless-dynamodb-local": "^0.2.38",
36-
"serverless-offline": "^5.12.1",
37-
"serverless-webpack": "^5.3.1",
38-
"source-map-loader": "^0.2.4",
39-
"ts-loader": "^6.2.1",
37+
"serverless-offline": "^13.3.3",
38+
"serverless-webpack": "^5.13.0",
39+
"source-map-loader": "^5.0.0",
40+
"ts-loader": "^9.5.1",
4041
"tslint": "^5.20.1",
4142
"tslint-config-airbnb": "^5.11.2",
4243
"tslint-loader": "^3.5.4",
43-
"tsutils": "^3.17.1",
44-
"typescript": "^3.7.2",
45-
"webpack": "^4.41.2",
46-
"webpack-node-externals": "^1.7.2"
44+
"tsutils": "^3.21.0",
45+
"typescript": "^5.4.4",
46+
"webpack": "^5.91.0",
47+
"webpack-node-externals": "^3.0.0"
4748
}
48-
}
49+
}

serverless.yml

+6-12
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ service: serverless-node-dynamodb-api
22

33
plugins:
44
- serverless-webpack
5-
- serverless-dynamodb-local
5+
- serverless-dynamodb
66
- serverless-offline
77
- serverless-dynamodb-autoscaling
88
- serverless-domain-manager
99

1010
provider:
1111
name: aws
12-
runtime: nodejs12.x
12+
runtime: nodejs20.x
13+
memorySize: 1024
1314
stage: prod
1415
region: ap-southeast-2
1516
iamManagedPolicies:
@@ -38,14 +39,14 @@ custom:
3839
hostedZoneId: ${env:TF_VAR_route53_zone_id}
3940
webpack:
4041
webpackConfig: ./webpack.config.ts
41-
packager: 'yarn'
42+
packager: "yarn"
4243
includeModules:
4344
forceExclude:
4445
- aws-sdk
45-
dynamodb:
46+
serverless-dynamodb:
4647
start:
4748
port: ${env:DYNAMODB_PORT, 8000}
48-
host: ${env:DYNAMODB_HOST, localhost}
49+
host: ${env:DYNAMODB_HOST, "localhost"}
4950
migrate: true
5051
noStart: ${env:DYNAMODB_NO_START, false}
5152
stages:
@@ -66,14 +67,12 @@ custom:
6667
functions:
6768
authorizer:
6869
handler: src/index.authorizer
69-
memory: 1024
7070
timeout: 30
7171
environment:
7272
AUTH0_CLIENT_SECRET: ${env:AUTH0_CLIENT_SECRET}
7373

7474
pingo:
7575
handler: src/index.pingHandler
76-
memory: 1024
7776
timeout: 30
7877
events:
7978
- http:
@@ -84,7 +83,6 @@ functions:
8483

8584
getAllItems:
8685
handler: src/index.getAllItemsHandler
87-
memory: 1024
8886
timeout: 30
8987
events:
9088
- http:
@@ -99,7 +97,6 @@ functions:
9997

10098
getItem:
10199
handler: src/index.getItemHandler
102-
memory: 1024
103100
timeout: 30
104101
events:
105102
- http:
@@ -114,7 +111,6 @@ functions:
114111

115112
createItem:
116113
handler: src/index.createItemHandler
117-
memory: 1024
118114
timeout: 30
119115
events:
120116
- http:
@@ -129,7 +125,6 @@ functions:
129125

130126
updateItem:
131127
handler: src/index.updateItemHandler
132-
memory: 1024
133128
timeout: 30
134129
events:
135130
- http:
@@ -144,7 +139,6 @@ functions:
144139

145140
deleteItem:
146141
handler: src/index.deleteItemHandler
147-
memory: 1024
148142
timeout: 30
149143
events:
150144
- http:

src/Response.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export interface ResponseArgs {
22
statusCode?: number;
33
body?: any;
44
headers?: {
5-
[name: string]: string,
5+
[name: string]: string;
66
};
77
}
88

@@ -18,11 +18,12 @@ export default class Response {
1818
statusCode: number;
1919
body?: string;
2020
headers: {
21-
[name: string]: string,
21+
[name: string]: string;
2222
};
2323

2424
constructor(args: ResponseArgs = defaultResponseArgs) {
25-
this.statusCode = args.statusCode ?? defaultResponseArgs.statusCode as number;
25+
this.statusCode =
26+
args.statusCode ?? (defaultResponseArgs.statusCode as number);
2627
this.headers = {
2728
...defaultResponseArgs.headers,
2829
...(args.headers ?? {}),

src/ResponseError.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export default class ResponseError extends Error {
1414

1515
constructor(args: ResponseErrorArgs = defaultResponseErrorArgs) {
1616
super(args.message ?? defaultResponseErrorArgs.message);
17-
this.statusCode = args.statusCode ?? defaultResponseErrorArgs.statusCode as number;
18-
this.message = args.message ?? defaultResponseErrorArgs.message as string;
17+
this.statusCode =
18+
args.statusCode ?? (defaultResponseErrorArgs.statusCode as number);
19+
this.message = args.message ?? (defaultResponseErrorArgs.message as string);
1920
}
2021
}

src/database.ts

+42-20
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import { DynamoDB } from 'aws-sdk';
2-
import * as moment from 'moment';
3-
import * as uuidv4 from 'uuid/v4';
2+
import moment from 'moment';
3+
import { v4 } from 'uuid';
44
import Item from './Item';
55
import ResponseError from './ResponseError';
66

7-
const db = process.env.IS_OFFLINE ?
8-
new DynamoDB.DocumentClient({
9-
region: 'localhost',
10-
accessKeyId: 'MOCK_ACCESS_KEY_ID',
11-
secretAccessKey: 'MOCK_SECRET_ACCESS_KEY',
12-
endpoint: `http://${process.env.DYNAMODB_HOST || 'localhost'}:${process.env.DYNAMODB_PORT || 8000}`,
13-
}) :
14-
new DynamoDB.DocumentClient();
7+
const db = process.env.IS_OFFLINE
8+
? new DynamoDB.DocumentClient({
9+
region: 'localhost',
10+
accessKeyId: 'MOCK_ACCESS_KEY_ID',
11+
secretAccessKey: 'MOCK_SECRET_ACCESS_KEY',
12+
endpoint: `http://${process.env.DYNAMODB_HOST || 'localhost'}:${
13+
process.env.DYNAMODB_PORT || 8000
14+
}`,
15+
})
16+
: new DynamoDB.DocumentClient();
1517

1618
export async function getItems(userId: string): Promise<Item[]> {
1719
const params = {
@@ -28,7 +30,10 @@ export async function getItems(userId: string): Promise<Item[]> {
2830
return data.Items as Item[];
2931
}
3032

31-
export async function getItemById(userId: string, itemId: string): Promise<Item> {
33+
export async function getItemById(
34+
userId: string,
35+
itemId: string
36+
): Promise<Item> {
3237
const params = {
3338
TableName: 'items',
3439
Key: {
@@ -40,7 +45,10 @@ export async function getItemById(userId: string, itemId: string): Promise<Item>
4045
const data = await db.get(params).promise();
4146

4247
if (data.Item === undefined) {
43-
throw new ResponseError({ statusCode: 404, message: `An item could not be found with id: ${itemId}` });
48+
throw new ResponseError({
49+
statusCode: 404,
50+
message: `An item could not be found with id: ${itemId}`,
51+
});
4452
}
4553

4654
return data.Item as Item;
@@ -49,9 +57,10 @@ export async function getItemById(userId: string, itemId: string): Promise<Item>
4957
export async function createItem(userId: string, name: string): Promise<Item> {
5058
const params = {
5159
TableName: 'items',
52-
ConditionExpression: 'attribute_not_exists(id) AND attribute_not_exists(userId)',
60+
ConditionExpression:
61+
'attribute_not_exists(id) AND attribute_not_exists(userId)',
5362
Item: {
54-
id: uuidv4(),
63+
id: v4(),
5564
userId,
5665
name,
5766
createdUtc: moment().utc().toISOString(),
@@ -63,7 +72,11 @@ export async function createItem(userId: string, name: string): Promise<Item> {
6372
return params.Item;
6473
}
6574

66-
export async function updateItem(userId: string, itemId: string, name: string): Promise<void> {
75+
export async function updateItem(
76+
userId: string,
77+
itemId: string,
78+
name: string
79+
): Promise<void> {
6780
try {
6881
const params = {
6982
TableName: 'items',
@@ -83,16 +96,22 @@ export async function updateItem(userId: string, itemId: string, name: string):
8396
};
8497

8598
await db.update(params).promise();
86-
} catch (err) {
99+
} catch (err: any) {
87100
if (err.code === 'ConditionalCheckFailedException') {
88-
throw new ResponseError({ statusCode: 404, message: `An item could not be found with id: ${itemId}` });
101+
throw new ResponseError({
102+
statusCode: 404,
103+
message: `An item could not be found with id: ${itemId}`,
104+
});
89105
}
90106

91107
throw err;
92108
}
93109
}
94110

95-
export async function deleteItem(userId: string, itemId: string): Promise<void> {
111+
export async function deleteItem(
112+
userId: string,
113+
itemId: string
114+
): Promise<void> {
96115
try {
97116
const params = {
98117
TableName: 'items',
@@ -104,9 +123,12 @@ export async function deleteItem(userId: string, itemId: string): Promise<void>
104123
};
105124

106125
await db.delete(params).promise();
107-
} catch (err) {
126+
} catch (err: any) {
108127
if (err.code === 'ConditionalCheckFailedException') {
109-
throw new ResponseError({ statusCode: 404, message: `An item could not be found with id: ${itemId}` });
128+
throw new ResponseError({
129+
statusCode: 404,
130+
message: `An item could not be found with id: ${itemId}`,
131+
});
110132
}
111133

112134
throw err;

src/globals.d.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
declare module 'serverless-webpack';
2-
declare module 'webpack-node-externals';

0 commit comments

Comments
 (0)