Skip to content

docs(idempotency): review API docs & README #2917

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 12 commits into from
Aug 13, 2024
1 change: 0 additions & 1 deletion .markdownlintignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ LICENSE
# these will be removed from the ignore and linted in future PRs
packages/batch/README.md
packages/commons/README.md
packages/idempotency/README.md
packages/jmespath/README.md
packages/logger/README.md
packages/metrics/README.md
Expand Down
287 changes: 203 additions & 84 deletions docs/utilities/idempotency.md

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions examples/snippets/idempotency/samples/testingIdempotency.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"version": "2.0",
"routeKey": "ANY /createpayment",
"rawPath": "/createpayment",
"rawQueryString": "",
"headers": {
"Header1": "value1",
"X-Idempotency-Key": "abcdefg"
},
"requestContext": {
"accountId": "123456789012",
"apiId": "api-id",
"domainName": "id.execute-api.us-east-1.amazonaws.com",
"domainPrefix": "id",
"http": {
"method": "POST",
"path": "/createpayment",
"protocol": "HTTP/1.1",
"sourceIp": "ip",
"userAgent": "agent"
},
"requestId": "id",
"routeKey": "ANY /createpayment",
"stage": "$default",
"time": "10/Feb/2021:13:40:43 +0000",
"timeEpoch": 1612964443723
},
"body": "{\"user\":\"xyz\",\"productId\":\"123456789\"}",
"isBase64Encoded": false
}
38 changes: 38 additions & 0 deletions examples/snippets/idempotency/testingIdempotency.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { Context } from 'aws-lambda';
import { DocumentClient } from 'aws-sdk/clients/dynamodb';
import * as API_GATEWAY_EXAMPLE_EVENT from './samples/testingIdempotency.json';
import { idempotentHandler } from './testingIdempotency';

describe('Idempotent Handler', () => {
const ddb = new DocumentClient({});

it('should return the same response for the same request', async () => {
// given
const context = {} as Context;

const firstRequest = API_GATEWAY_EXAMPLE_EVENT;

// modify time field to simulate a different request
const secondRequest = {
...API_GATEWAY_EXAMPLE_EVENT,
requestContext: {
...API_GATEWAY_EXAMPLE_EVENT.requestContext,
time: 1612964493723,
},
};

// when
const firstResponse = await idempotentHandler(firstRequest, context);
const secondResponse = await idempotentHandler(secondRequest, context);
// then
expect(firstResponse).toEqual(secondResponse);
// check if we only have one item in the table
const idempotencyRecords = await ddb
.scan({ TableName: 'idempotency-store' })
.promise();

expect(idempotencyRecords.Items).toHaveLength(1);
expect(idempotencyRecords.Items?.[0].status).toEqual('COMPLETED');
expect(idempotencyRecords.Items?.[0].data).toEqual(firstResponse);
});
});
24 changes: 24 additions & 0 deletions examples/snippets/idempotency/testingIdempotency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
IdempotencyConfig,
makeIdempotent,
} from '@aws-lambda-powertools/idempotency';
import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';

const idempotencyConfig = new IdempotencyConfig({});
const persistenceStore = new DynamoDBPersistenceLayer({
tableName: 'idempotency-store',
});

const handler = async (event: unknown, context: unknown) => {
return {
statusCode: 200,
body: JSON.stringify({ message: 'Success', event: event }),
};
};

const idempotentHandler = makeIdempotent(handler, {
config: idempotencyConfig,
persistenceStore: persistenceStore,
});

export { idempotentHandler };
Loading