-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(idempotency): manipulate idempotent response via response hook (#…
…3071) Co-authored-by: Alexander Schueren <[email protected]> Co-authored-by: Andrea Amorosi <[email protected]>
- Loading branch information
1 parent
1a65746
commit f7c1769
Showing
9 changed files
with
263 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
8 changes: 8 additions & 0 deletions
8
examples/snippets/idempotency/samples/workingWithResponseHookIdempotentResponse.json
This file contains 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,8 @@ | ||
{ | ||
"message": "success", | ||
"paymentId": "31a964eb-7477-4fe1-99fe-7f8a6a351a7e", | ||
"statusCode": 200, | ||
"headers": { | ||
"x-idempotency-key": "function-name#mHfGv2vJ8h+ZvLIr/qGBbQ==" | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
examples/snippets/idempotency/samples/workingWithResponseHookSampleEvent.json
This file contains 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,4 @@ | ||
{ | ||
"user": "John Doe", | ||
"productId": "123456" | ||
} |
This file contains 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,58 @@ | ||
import { randomUUID } from 'node:crypto'; | ||
import type { JSONValue } from '@aws-lambda-powertools/commons/types'; | ||
import { | ||
IdempotencyConfig, | ||
makeIdempotent, | ||
} from '@aws-lambda-powertools/idempotency'; | ||
import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb'; | ||
import type { IdempotencyRecord } from '@aws-lambda-powertools/idempotency/persistence'; | ||
import type { Context } from 'aws-lambda'; | ||
import type { Request, Response, SubscriptionResult } from './types.js'; | ||
|
||
const persistenceStore = new DynamoDBPersistenceLayer({ | ||
tableName: 'idempotencyTableName', | ||
}); | ||
|
||
const responseHook = (response: JSONValue, record: IdempotencyRecord) => { | ||
// Return inserted Header data into the Idempotent Response | ||
(response as Response).headers = { | ||
'x-idempotency-key': record.idempotencyKey, | ||
}; | ||
|
||
// Must return the response here | ||
return response as JSONValue; | ||
}; | ||
|
||
const config = new IdempotencyConfig({ | ||
responseHook, | ||
}); | ||
|
||
const createSubscriptionPayment = async ( | ||
event: Request | ||
): Promise<SubscriptionResult> => { | ||
// ... create payment | ||
return { | ||
id: randomUUID(), | ||
productId: event.productId, | ||
}; | ||
}; | ||
|
||
export const handler = makeIdempotent( | ||
async (event: Request, _context: Context): Promise<Response> => { | ||
try { | ||
const payment = await createSubscriptionPayment(event); | ||
|
||
return { | ||
paymentId: payment.id, | ||
message: 'success', | ||
statusCode: 200, | ||
}; | ||
} catch (error) { | ||
throw new Error('Error creating payment'); | ||
} | ||
}, | ||
{ | ||
persistenceStore, | ||
config, | ||
} | ||
); |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.