|
| 1 | +import type { Context } from 'aws-lambda'; |
| 2 | +import { LambdaInterface } from '@aws-lambda-powertools/commons'; |
| 3 | +import { idempotent } from '../../src'; |
| 4 | +import { Logger } from '../../../logger'; |
| 5 | +import { DynamoDBPersistenceLayer } from '../../src/persistence/DynamoDBPersistenceLayer'; |
| 6 | +import { IdempotencyConfig } from '../../src/'; |
| 7 | + |
| 8 | +const IDEMPOTENCY_TABLE_NAME = |
| 9 | + process.env.IDEMPOTENCY_TABLE_NAME || 'table_name'; |
| 10 | +const dynamoDBPersistenceLayer = new DynamoDBPersistenceLayer({ |
| 11 | + tableName: IDEMPOTENCY_TABLE_NAME, |
| 12 | +}); |
| 13 | + |
| 14 | +const dynamoDBPersistenceLayerCustomized = new DynamoDBPersistenceLayer({ |
| 15 | + tableName: IDEMPOTENCY_TABLE_NAME, |
| 16 | + dataAttr: 'dataAttr', |
| 17 | + keyAttr: 'customId', |
| 18 | + expiryAttr: 'expiryAttr', |
| 19 | + statusAttr: 'statusAttr', |
| 20 | + inProgressExpiryAttr: 'inProgressExpiryAttr', |
| 21 | + staticPkValue: 'staticPkValue', |
| 22 | + validationKeyAttr: 'validationKeyAttr', |
| 23 | +}); |
| 24 | + |
| 25 | +const config = new IdempotencyConfig({}); |
| 26 | + |
| 27 | +class DefaultLambda implements LambdaInterface { |
| 28 | + @idempotent({ persistenceStore: dynamoDBPersistenceLayer }) |
| 29 | + public async handler( |
| 30 | + _event: Record<string, unknown>, |
| 31 | + _context: Context |
| 32 | + ): Promise<string> { |
| 33 | + logger.info(`Got test event: ${JSON.stringify(_event)}`); |
| 34 | + // sleep to enforce error with parallel execution |
| 35 | + await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 36 | + |
| 37 | + return 'Hello World'; |
| 38 | + } |
| 39 | + |
| 40 | + @idempotent({ |
| 41 | + persistenceStore: dynamoDBPersistenceLayerCustomized, |
| 42 | + config: config, |
| 43 | + }) |
| 44 | + public async handlerCustomized( |
| 45 | + event: { foo: string }, |
| 46 | + context: Context |
| 47 | + ): Promise<string> { |
| 48 | + config.registerLambdaContext(context); |
| 49 | + logger.info('Processed event', { details: event.foo }); |
| 50 | + |
| 51 | + return event.foo; |
| 52 | + } |
| 53 | + |
| 54 | + @idempotent({ |
| 55 | + persistenceStore: dynamoDBPersistenceLayer, |
| 56 | + config: new IdempotencyConfig({ |
| 57 | + useLocalCache: false, |
| 58 | + expiresAfterSeconds: 1, |
| 59 | + eventKeyJmesPath: 'foo', |
| 60 | + }), |
| 61 | + }) |
| 62 | + public async handlerExpired( |
| 63 | + event: { foo: string; invocation: number }, |
| 64 | + context: Context |
| 65 | + ): Promise<{ foo: string; invocation: number }> { |
| 66 | + logger.addContext(context); |
| 67 | + |
| 68 | + logger.info('Processed event', { details: event.foo }); |
| 69 | + |
| 70 | + return { |
| 71 | + foo: event.foo, |
| 72 | + invocation: event.invocation, |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + @idempotent({ persistenceStore: dynamoDBPersistenceLayer }) |
| 77 | + public async handlerParallel( |
| 78 | + event: { foo: string }, |
| 79 | + context: Context |
| 80 | + ): Promise<string> { |
| 81 | + logger.addContext(context); |
| 82 | + |
| 83 | + await new Promise((resolve) => setTimeout(resolve, 1500)); |
| 84 | + |
| 85 | + logger.info('Processed event', { details: event.foo }); |
| 86 | + |
| 87 | + return event.foo; |
| 88 | + } |
| 89 | + |
| 90 | + @idempotent({ |
| 91 | + persistenceStore: dynamoDBPersistenceLayer, |
| 92 | + config: new IdempotencyConfig({ |
| 93 | + eventKeyJmesPath: 'foo', |
| 94 | + }), |
| 95 | + }) |
| 96 | + public async handlerTimeout( |
| 97 | + event: { foo: string; invocation: number }, |
| 98 | + context: Context |
| 99 | + ): Promise<{ foo: string; invocation: number }> { |
| 100 | + logger.addContext(context); |
| 101 | + |
| 102 | + if (event.invocation === 0) { |
| 103 | + await new Promise((resolve) => setTimeout(resolve, 4000)); |
| 104 | + } |
| 105 | + |
| 106 | + logger.info('Processed event', { |
| 107 | + details: event.foo, |
| 108 | + }); |
| 109 | + |
| 110 | + return { |
| 111 | + foo: event.foo, |
| 112 | + invocation: event.invocation, |
| 113 | + }; |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +const defaultLambda = new DefaultLambda(); |
| 118 | +const handler = defaultLambda.handler.bind(defaultLambda); |
| 119 | +const handlerParallel = defaultLambda.handlerParallel.bind(defaultLambda); |
| 120 | + |
| 121 | +const handlerCustomized = defaultLambda.handlerCustomized.bind(defaultLambda); |
| 122 | + |
| 123 | +const handlerTimeout = defaultLambda.handlerTimeout.bind(defaultLambda); |
| 124 | + |
| 125 | +const handlerExpired = defaultLambda.handlerExpired.bind(defaultLambda); |
| 126 | + |
| 127 | +const logger = new Logger(); |
| 128 | + |
| 129 | +class LambdaWithKeywordArgument implements LambdaInterface { |
| 130 | + public async handler( |
| 131 | + event: { id: string }, |
| 132 | + _context: Context |
| 133 | + ): Promise<string> { |
| 134 | + config.registerLambdaContext(_context); |
| 135 | + await this.process(event.id, 'bar'); |
| 136 | + |
| 137 | + return 'Hello World Keyword Argument'; |
| 138 | + } |
| 139 | + |
| 140 | + @idempotent({ |
| 141 | + persistenceStore: dynamoDBPersistenceLayer, |
| 142 | + config: config, |
| 143 | + dataIndexArgument: 1, |
| 144 | + }) |
| 145 | + public async process(id: string, foo: string): Promise<string> { |
| 146 | + logger.info('Got test event', { id, foo }); |
| 147 | + |
| 148 | + return 'idempotent result: ' + foo; |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +const handlerDataIndexArgument = new LambdaWithKeywordArgument(); |
| 153 | +const handlerWithKeywordArgument = handlerDataIndexArgument.handler.bind( |
| 154 | + handlerDataIndexArgument |
| 155 | +); |
| 156 | + |
| 157 | +export { |
| 158 | + handler, |
| 159 | + handlerCustomized, |
| 160 | + handlerExpired, |
| 161 | + handlerWithKeywordArgument, |
| 162 | + handlerTimeout, |
| 163 | + handlerParallel, |
| 164 | +}; |
0 commit comments