-
Notifications
You must be signed in to change notification settings - Fork 156
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
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
51e9f40
add param
am29d 9995be6
change readme and api docs
am29d 3503511
Merge branch 'main' into 2381/idempotency-readme-api-docs
am29d 707d1c1
add test example
am29d e89e165
add link to e2e tests
am29d 130d7d8
remove ignores and toc
am29d e28fc08
add testing section placeholder
am29d 74c4edf
add testing section
am29d ea2fc80
fix indent in readme examples
am29d 3dcd3ef
pass clientconfig instead of the client
am29d aafdbb5
fix warnings in typedoc
am29d 541edb5
Merge branch 'main' into 2381/idempotency-readme-api-docs
dreamorosi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
30 changes: 30 additions & 0 deletions
30
examples/snippets/idempotency/samples/testingIdempotency.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.