Skip to content

docs(idempotency): simplify snippets #4150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions docs/features/idempotency.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ You can quickly start by initializing the `DynamoDBPersistenceLayer` class and u

=== "index.ts"

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

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

=== "index.ts"

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

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

=== "index.ts"

```typescript hl_lines="4 27 49"
```typescript hl_lines="4 27"
--8<-- "examples/snippets/idempotency/makeIdempotentJmes.ts"
```

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

=== "Handling exceptions"

```typescript hl_lines="39-40 47-48"
```typescript
--8<-- "examples/snippets/idempotency/workingWithExceptions.ts"
```

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

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

```typescript hl_lines="25"
```typescript hl_lines="21"
--8<-- "examples/snippets/idempotency/customKeyPrefixFnWrapper.ts"
```

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

=== "CustomPersistenceLayer"

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

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

=== "Using an Idempotent Response Hook"

```typescript hl_lines="16 19 27 56"
```typescript hl_lines="16 19 27"
--8<-- "examples/snippets/idempotency/workingWithResponseHook.ts"
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ class CustomPersistenceLayer extends BasePersistenceLayer {
return new IdempotencyRecord({
...(item as unknown as IdempotencyRecordOptions),
});
} catch (_error) {
throw new IdempotencyItemNotFoundError();
} catch (error) {
throw new IdempotencyItemNotFoundError('Item not found in store', {
cause: error,
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,12 @@ export const handler = async (
): Promise<Response> => {
config.registerLambdaContext(context);

try {
const transactionId = randomUUID();
const payment = await createSubscriptionPayment(transactionId, event);
const transactionId = randomUUID();
const payment = await createSubscriptionPayment(transactionId, event);

return {
paymentId: payment.id,
message: 'success',
statusCode: 200,
};
} catch (_error) {
throw new Error('Error creating payment');
}
return {
paymentId: payment.id,
message: 'success',
statusCode: 200,
};
};
16 changes: 6 additions & 10 deletions examples/snippets/idempotency/customKeyPrefixDecorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,13 @@ class Lambda {
keyPrefix: 'createSubscriptionPayment',
})
async handler(_event: unknown, _context: Context) {
try {
// ... create payment
// ... create payment

return {
paymentId: randomUUID(),
message: 'success',
statusCode: 200,
};
} catch (_error) {
throw new Error('Error creating payment');
}
return {
paymentId: randomUUID(),
message: 'success',
statusCode: 200,
};
}
}

Expand Down
16 changes: 6 additions & 10 deletions examples/snippets/idempotency/customKeyPrefixFnWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@ const persistenceStore = new DynamoDBPersistenceLayer({

export const handler = makeIdempotent(
async () => {
try {
// ... create payment
// ... create payment

return {
paymentId: randomUUID(),
message: 'success',
statusCode: 200,
};
} catch (_error) {
throw new Error('Error creating payment');
}
return {
paymentId: randomUUID(),
message: 'success',
statusCode: 200,
};
},
{
persistenceStore,
Expand Down
16 changes: 6 additions & 10 deletions examples/snippets/idempotency/customKeyPrefixMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@ export const handler = middy()
})
)
.handler(async () => {
try {
// ... create payment
// ... create payment

return {
paymentId: randomUUID(),
message: 'success',
statusCode: 200,
};
} catch (_error) {
throw new Error('Error creating payment');
}
return {
paymentId: randomUUID(),
message: 'success',
statusCode: 200,
};
});
16 changes: 6 additions & 10 deletions examples/snippets/idempotency/customizePersistenceLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@ const persistenceStore = new DynamoDBPersistenceLayer({

export const handler = middy(
async (_event: Request, _context: Context): Promise<Response> => {
try {
// ... create payment
// ... create payment

return {
paymentId: '1234567890',
message: 'success',
statusCode: 200,
};
} catch (_error) {
throw new Error('Error creating payment');
}
return {
paymentId: '1234567890',
message: 'success',
statusCode: 200,
};
}
).use(
makeHandlerIdempotent({
Expand Down
16 changes: 6 additions & 10 deletions examples/snippets/idempotency/makeHandlerIdempotent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,13 @@ const createSubscriptionPayment = async (

export const handler = middy(
async (event: Request, _context: Context): Promise<Response> => {
try {
const payment = await createSubscriptionPayment(event);
const payment = await createSubscriptionPayment(event);

return {
paymentId: payment.id,
message: 'success',
statusCode: 200,
};
} catch (_error) {
throw new Error('Error creating payment');
}
return {
paymentId: payment.id,
message: 'success',
statusCode: 200,
};
}
).use(
makeHandlerIdempotent({
Expand Down
21 changes: 9 additions & 12 deletions examples/snippets/idempotency/makeIdempotentAnyFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,15 @@ export const handler = async (
context: Context
): Promise<Response> => {
config.registerLambdaContext(context);
try {
const transactionId = randomUUID();
const payment = await createSubscriptionPayment(transactionId, event);

await reportSubscriptionMetrics(transactionId, event.user);
const transactionId = randomUUID();
const payment = await createSubscriptionPayment(transactionId, event);

return {
paymentId: payment.id,
message: 'success',
statusCode: 200,
};
} catch (_error) {
throw new Error('Error creating payment');
}
await reportSubscriptionMetrics(transactionId, event.user);

return {
paymentId: payment.id,
message: 'success',
statusCode: 200,
};
};
16 changes: 6 additions & 10 deletions examples/snippets/idempotency/makeIdempotentBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,13 @@ const createSubscriptionPayment = async (

export const handler = makeIdempotent(
async (event: Request, _context: Context): Promise<Response> => {
try {
const payment = await createSubscriptionPayment(event);
const payment = await createSubscriptionPayment(event);

return {
paymentId: payment.id,
message: 'success',
statusCode: 200,
};
} catch (_error) {
throw new Error('Error creating payment');
}
return {
paymentId: payment.id,
message: 'success',
statusCode: 200,
};
},
{
persistenceStore,
Expand Down
22 changes: 9 additions & 13 deletions examples/snippets/idempotency/makeIdempotentJmes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,16 @@ const config = new IdempotencyConfig({

export const handler = makeIdempotent(
async (event: Request, _context: Context): Promise<Response> => {
try {
const payment = await createSubscriptionPayment(
event.user,
event.productId
);
const payment = await createSubscriptionPayment(
event.user,
event.productId
);

return {
paymentId: payment.id,
message: 'success',
statusCode: 200,
};
} catch (_error) {
throw new Error('Error creating payment');
}
return {
paymentId: payment.id,
message: 'success',
statusCode: 200,
};
},
{
persistenceStore,
Expand Down
19 changes: 8 additions & 11 deletions examples/snippets/idempotency/makeIdempotentLambdaContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,13 @@ export const handler = async (
): Promise<Response> => {
// Register the Lambda context to the IdempotencyConfig instance
config.registerLambdaContext(context);
try {
const transactionId = randomUUID();
const payment = await createSubscriptionPayment(transactionId, event);

return {
paymentId: payment.id,
message: 'success',
statusCode: 200,
};
} catch (_error) {
throw new Error('Error creating payment');
}
const transactionId = randomUUID();
const payment = await createSubscriptionPayment(transactionId, event);

return {
paymentId: payment.id,
message: 'success',
statusCode: 200,
};
};
16 changes: 6 additions & 10 deletions examples/snippets/idempotency/workingWithCompositeKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@ const persistenceStore = new DynamoDBPersistenceLayer({

export const handler = middy(
async (_event: Request, _context: Context): Promise<Response> => {
try {
// ... create payment
// ... create payment

return {
paymentId: '12345',
message: 'success',
statusCode: 200,
};
} catch (_error) {
throw new Error('Error creating payment');
}
return {
paymentId: '12345',
message: 'success',
statusCode: 200,
};
}
).use(
makeHandlerIdempotent({
Expand Down
16 changes: 6 additions & 10 deletions examples/snippets/idempotency/workingWithCustomClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,13 @@ const persistenceStore = new DynamoDBPersistenceLayer({

export const handler = makeIdempotent(
async (_event: Request, _context: Context): Promise<Response> => {
try {
// ... create payment
// ... create payment

return {
paymentId: '12345',
message: 'success',
statusCode: 200,
};
} catch (_error) {
throw new Error('Error creating payment');
}
return {
paymentId: '12345',
message: 'success',
statusCode: 200,
};
},
{
persistenceStore,
Expand Down
16 changes: 6 additions & 10 deletions examples/snippets/idempotency/workingWithCustomConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@ const persistenceStore = new DynamoDBPersistenceLayer({

export const handler = makeIdempotent(
async (_event: Request, _context: Context): Promise<Response> => {
try {
// ... create payment
// ... create payment

return {
paymentId: '12345',
message: 'success',
statusCode: 200,
};
} catch (_error) {
throw new Error('Error creating payment');
}
return {
paymentId: '12345',
message: 'success',
statusCode: 200,
};
},
{
persistenceStore,
Expand Down
Loading