-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathmakeIdempotentJmes.ts
51 lines (46 loc) · 1.28 KB
/
makeIdempotentJmes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { randomUUID } from 'node:crypto';
import {
makeIdempotent,
IdempotencyConfig,
} from '@aws-lambda-powertools/idempotency';
import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';
import type { Context } from 'aws-lambda';
import type { Request, Response, SubscriptionResult } from './types';
const persistenceStore = new DynamoDBPersistenceLayer({
tableName: 'idempotencyTableName',
});
const createSubscriptionPayment = async (
_user: string,
productId: string
): Promise<SubscriptionResult> => {
// ... create payment
return {
id: randomUUID(),
productId: productId,
};
};
// Deserialize JSON string under the "body" key, then extract the "user" and "productId" keys
const config = new IdempotencyConfig({
eventKeyJmesPath: 'powertools_json(body).["user", "productId"]',
});
export const handler = makeIdempotent(
async (event: Request, _context: Context): Promise<Response> => {
try {
const payment = await createSubscriptionPayment(
event.user,
event.productId
);
return {
paymentId: payment.id,
message: 'success',
statusCode: 200,
};
} catch (error) {
throw new Error('Error creating payment');
}
},
{
persistenceStore,
config,
}
);