1
1
const AWS = require ( "aws-sdk" ) ;
2
2
const express = require ( "express" ) ;
3
3
const serverless = require ( "serverless-http" ) ;
4
-
5
4
const app = express ( ) ;
6
5
7
6
const USERS_TABLE = process . env . USERS_TABLE ;
8
7
const dynamoDbClient = new AWS . DynamoDB . DocumentClient ( ) ;
9
8
9
+
10
10
app . use ( express . json ( ) ) ;
11
11
12
12
app . get ( "/users/:userId" , async function ( req , res ) {
@@ -34,18 +34,15 @@ app.get("/users/:userId", async function (req, res) {
34
34
} ) ;
35
35
36
36
app . post ( "/users" , async function ( req , res ) {
37
- const { userId, name, token } = req . body ;
37
+ const { userId, name } = req . body ;
38
38
if ( typeof userId !== "string" ) {
39
39
res . status ( 400 ) . json ( { error : '"userId" must be a string' } ) ;
40
40
} else if ( typeof name !== "string" ) {
41
41
res . status ( 400 ) . json ( { error : '"name" must be a string' } ) ;
42
- } else if ( typeof token !== "string" ) {
43
- res . status ( 400 ) . json ( { error : '"token" must be a string' } ) ;
44
42
}
45
43
46
44
const params = {
47
- // idempotence check
48
- ClientRequestToken : token ,
45
+ ClientRequestToken : req . context . awsRequestId ,
49
46
TransactItems : [
50
47
{
51
48
Update : {
@@ -61,7 +58,7 @@ app.post("/users", async function (req, res) {
61
58
]
62
59
} ;
63
60
64
- try {
61
+ try {
65
62
await dynamoDbClient . transactWrite ( params ) . promise ( ) ;
66
63
res . json ( { userId, name } ) ;
67
64
} catch ( error ) {
@@ -78,4 +75,8 @@ app.use((req, res, next) => {
78
75
} ) ;
79
76
80
77
81
- module . exports . handler = serverless ( app ) ;
78
+ module . exports . handler = serverless ( app , {
79
+ request :( req , event , context ) => {
80
+ req . context = context
81
+ }
82
+ } ) ;
0 commit comments