-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathpowertoolsJsonIdempotencyJmespath.ts
51 lines (44 loc) · 1.28 KB
/
powertoolsJsonIdempotencyJmespath.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 {
IdempotencyConfig,
makeIdempotent,
} from '@aws-lambda-powertools/idempotency';
import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';
import type { APIGatewayEvent } from 'aws-lambda';
import { randomUUID } from 'node:crypto';
const persistenceStore = new DynamoDBPersistenceLayer({
tableName: 'IdempotencyTable',
});
export const handler = makeIdempotent(
async (event: APIGatewayEvent) => {
const body = JSON.parse(event.body || '{}');
const { user, productId } = body;
const result = await createSubscriptionPayment(user, productId);
return {
statusCode: 200,
body: JSON.stringify({
paymentId: result.id,
message: 'success',
}),
};
},
{
persistenceStore,
config: new IdempotencyConfig({
eventKeyJmesPath: 'powertools_json(body)',
}),
}
);
const createSubscriptionPayment = async (
user: string,
productId: string
): Promise<{ id: string; message: string }> => {
const payload = { user, productId };
const response = await fetch('https://httpbin.org/anything', {
method: 'POST',
body: JSON.stringify(payload),
});
if (!response.ok) {
throw new Error('Failed to create subscription payment');
}
return { id: randomUUID(), message: 'paid' };
};