Skip to content

Commit 6dd18a0

Browse files
authored
docs(idempotency): simplify snippets (#4150)
1 parent a147c3b commit 6dd18a0

21 files changed

+135
-202
lines changed

docs/features/idempotency.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ You can quickly start by initializing the `DynamoDBPersistenceLayer` class and u
144144

145145
=== "index.ts"
146146

147-
```typescript hl_lines="2-3 21 35-38"
147+
```typescript hl_lines="2-3 21 31-33"
148148
--8<-- "examples/snippets/idempotency/makeIdempotentBase.ts"
149149
```
150150

@@ -206,7 +206,7 @@ Similar to the `makeIdempotent` function wrapper, you can quickly make your Lamb
206206

207207
=== "index.ts"
208208

209-
```typescript hl_lines="22 36-40"
209+
```typescript hl_lines="22 33-35"
210210
--8<-- "examples/snippets/idempotency/makeHandlerIdempotent.ts"
211211
```
212212

@@ -237,7 +237,7 @@ Imagine the function executes successfully, but the client never receives the re
237237

238238
=== "index.ts"
239239

240-
```typescript hl_lines="4 27 49"
240+
```typescript hl_lines="4 27"
241241
--8<-- "examples/snippets/idempotency/makeIdempotentJmes.ts"
242242
```
243243

@@ -304,7 +304,7 @@ If an error is thrown _outside_ the scope of the decorated function and after yo
304304

305305
=== "Handling exceptions"
306306

307-
```typescript hl_lines="39-40 47-48"
307+
```typescript
308308
--8<-- "examples/snippets/idempotency/workingWithExceptions.ts"
309309
```
310310

@@ -774,7 +774,7 @@ You can use the `keyPrefix` parameter in any of the idempotency configurations t
774774

775775
=== "Using a custom prefix with function wrapper"
776776

777-
```typescript hl_lines="25"
777+
```typescript hl_lines="21"
778778
--8<-- "examples/snippets/idempotency/customKeyPrefixFnWrapper.ts"
779779
```
780780

@@ -864,7 +864,7 @@ Below an example implementation of a custom persistence layer backed by a generi
864864

865865
=== "CustomPersistenceLayer"
866866

867-
```typescript hl_lines="9 19 28 35 52 95"
867+
```typescript hl_lines="9 19 28 35 54 97"
868868
--8<-- "examples/snippets/idempotency/advancedBringYourOwnPersistenceLayer.ts"
869869
```
870870

@@ -891,7 +891,7 @@ You can set up a `responseHook` in the `IdempotentConfig` class to manipulate th
891891

892892
=== "Using an Idempotent Response Hook"
893893

894-
```typescript hl_lines="16 19 27 56"
894+
```typescript hl_lines="16 19 27"
895895
--8<-- "examples/snippets/idempotency/workingWithResponseHook.ts"
896896
```
897897

examples/snippets/idempotency/advancedBringYourOwnPersistenceLayer.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ class CustomPersistenceLayer extends BasePersistenceLayer {
4444
return new IdempotencyRecord({
4545
...(item as unknown as IdempotencyRecordOptions),
4646
});
47-
} catch (_error) {
48-
throw new IdempotencyItemNotFoundError();
47+
} catch (error) {
48+
throw new IdempotencyItemNotFoundError('Item not found in store', {
49+
cause: error,
50+
});
4951
}
5052
}
5153

examples/snippets/idempotency/advancedBringYourOwnPersistenceLayerUsage.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,12 @@ export const handler = async (
3838
): Promise<Response> => {
3939
config.registerLambdaContext(context);
4040

41-
try {
42-
const transactionId = randomUUID();
43-
const payment = await createSubscriptionPayment(transactionId, event);
41+
const transactionId = randomUUID();
42+
const payment = await createSubscriptionPayment(transactionId, event);
4443

45-
return {
46-
paymentId: payment.id,
47-
message: 'success',
48-
statusCode: 200,
49-
};
50-
} catch (_error) {
51-
throw new Error('Error creating payment');
52-
}
44+
return {
45+
paymentId: payment.id,
46+
message: 'success',
47+
statusCode: 200,
48+
};
5349
};

examples/snippets/idempotency/customKeyPrefixDecorator.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,13 @@ class Lambda {
1313
keyPrefix: 'createSubscriptionPayment',
1414
})
1515
async handler(_event: unknown, _context: Context) {
16-
try {
17-
// ... create payment
16+
// ... create payment
1817

19-
return {
20-
paymentId: randomUUID(),
21-
message: 'success',
22-
statusCode: 200,
23-
};
24-
} catch (_error) {
25-
throw new Error('Error creating payment');
26-
}
18+
return {
19+
paymentId: randomUUID(),
20+
message: 'success',
21+
statusCode: 200,
22+
};
2723
}
2824
}
2925

examples/snippets/idempotency/customKeyPrefixFnWrapper.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,13 @@ const persistenceStore = new DynamoDBPersistenceLayer({
88

99
export const handler = makeIdempotent(
1010
async () => {
11-
try {
12-
// ... create payment
11+
// ... create payment
1312

14-
return {
15-
paymentId: randomUUID(),
16-
message: 'success',
17-
statusCode: 200,
18-
};
19-
} catch (_error) {
20-
throw new Error('Error creating payment');
21-
}
13+
return {
14+
paymentId: randomUUID(),
15+
message: 'success',
16+
statusCode: 200,
17+
};
2218
},
2319
{
2420
persistenceStore,

examples/snippets/idempotency/customKeyPrefixMiddleware.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,11 @@ export const handler = middy()
1515
})
1616
)
1717
.handler(async () => {
18-
try {
19-
// ... create payment
18+
// ... create payment
2019

21-
return {
22-
paymentId: randomUUID(),
23-
message: 'success',
24-
statusCode: 200,
25-
};
26-
} catch (_error) {
27-
throw new Error('Error creating payment');
28-
}
20+
return {
21+
paymentId: randomUUID(),
22+
message: 'success',
23+
statusCode: 200,
24+
};
2925
});

examples/snippets/idempotency/customizePersistenceLayer.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,13 @@ const persistenceStore = new DynamoDBPersistenceLayer({
1616

1717
export const handler = middy(
1818
async (_event: Request, _context: Context): Promise<Response> => {
19-
try {
20-
// ... create payment
19+
// ... create payment
2120

22-
return {
23-
paymentId: '1234567890',
24-
message: 'success',
25-
statusCode: 200,
26-
};
27-
} catch (_error) {
28-
throw new Error('Error creating payment');
29-
}
21+
return {
22+
paymentId: '1234567890',
23+
message: 'success',
24+
statusCode: 200,
25+
};
3026
}
3127
).use(
3228
makeHandlerIdempotent({

examples/snippets/idempotency/makeHandlerIdempotent.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,13 @@ const createSubscriptionPayment = async (
2121

2222
export const handler = middy(
2323
async (event: Request, _context: Context): Promise<Response> => {
24-
try {
25-
const payment = await createSubscriptionPayment(event);
24+
const payment = await createSubscriptionPayment(event);
2625

27-
return {
28-
paymentId: payment.id,
29-
message: 'success',
30-
statusCode: 200,
31-
};
32-
} catch (_error) {
33-
throw new Error('Error creating payment');
34-
}
26+
return {
27+
paymentId: payment.id,
28+
message: 'success',
29+
statusCode: 200,
30+
};
3531
}
3632
).use(
3733
makeHandlerIdempotent({

examples/snippets/idempotency/makeIdempotentAnyFunction.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,15 @@ export const handler = async (
4242
context: Context
4343
): Promise<Response> => {
4444
config.registerLambdaContext(context);
45-
try {
46-
const transactionId = randomUUID();
47-
const payment = await createSubscriptionPayment(transactionId, event);
4845

49-
await reportSubscriptionMetrics(transactionId, event.user);
46+
const transactionId = randomUUID();
47+
const payment = await createSubscriptionPayment(transactionId, event);
5048

51-
return {
52-
paymentId: payment.id,
53-
message: 'success',
54-
statusCode: 200,
55-
};
56-
} catch (_error) {
57-
throw new Error('Error creating payment');
58-
}
49+
await reportSubscriptionMetrics(transactionId, event.user);
50+
51+
return {
52+
paymentId: payment.id,
53+
message: 'success',
54+
statusCode: 200,
55+
};
5956
};

examples/snippets/idempotency/makeIdempotentBase.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,13 @@ const createSubscriptionPayment = async (
2020

2121
export const handler = makeIdempotent(
2222
async (event: Request, _context: Context): Promise<Response> => {
23-
try {
24-
const payment = await createSubscriptionPayment(event);
23+
const payment = await createSubscriptionPayment(event);
2524

26-
return {
27-
paymentId: payment.id,
28-
message: 'success',
29-
statusCode: 200,
30-
};
31-
} catch (_error) {
32-
throw new Error('Error creating payment');
33-
}
25+
return {
26+
paymentId: payment.id,
27+
message: 'success',
28+
statusCode: 200,
29+
};
3430
},
3531
{
3632
persistenceStore,

0 commit comments

Comments
 (0)