|
| 1 | +import { |
| 2 | + IdempotencyItemAlreadyExistsError, |
| 3 | + IdempotencyItemNotFoundError, |
| 4 | + IdempotencyRecordStatus, |
| 5 | +} from '@aws-lambda-powertools/idempotency'; |
| 6 | +import { IdempotencyRecordOptions } from '@aws-lambda-powertools/idempotency/types'; |
| 7 | +import { |
| 8 | + IdempotencyRecord, |
| 9 | + BasePersistenceLayer, |
| 10 | +} from '@aws-lambda-powertools/idempotency/persistence'; |
| 11 | +import { getSecret } from '@aws-lambda-powertools/parameters/secrets'; |
| 12 | +import { Transform } from '@aws-lambda-powertools/parameters'; |
| 13 | +import { |
| 14 | + ProviderClient, |
| 15 | + ProviderItemAlreadyExists, |
| 16 | +} from './advancedBringYourOwnPersistenceLayerProvider'; |
| 17 | +import type { ApiSecret, ProviderItem } from './types'; |
| 18 | + |
| 19 | +class CustomPersistenceLayer extends BasePersistenceLayer { |
| 20 | + #collectionName: string; |
| 21 | + #client?: ProviderClient; |
| 22 | + |
| 23 | + public constructor(config: { collectionName: string }) { |
| 24 | + super(); |
| 25 | + this.#collectionName = config.collectionName; |
| 26 | + } |
| 27 | + |
| 28 | + protected async _deleteRecord(record: IdempotencyRecord): Promise<void> { |
| 29 | + await ( |
| 30 | + await this.#getClient() |
| 31 | + ).delete(this.#collectionName, record.idempotencyKey); |
| 32 | + } |
| 33 | + |
| 34 | + protected async _getRecord( |
| 35 | + idempotencyKey: string |
| 36 | + ): Promise<IdempotencyRecord> { |
| 37 | + try { |
| 38 | + const item = await ( |
| 39 | + await this.#getClient() |
| 40 | + ).get(this.#collectionName, idempotencyKey); |
| 41 | + |
| 42 | + return new IdempotencyRecord({ |
| 43 | + ...(item as unknown as IdempotencyRecordOptions), |
| 44 | + }); |
| 45 | + } catch (error) { |
| 46 | + throw new IdempotencyItemNotFoundError(); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + protected async _putRecord(record: IdempotencyRecord): Promise<void> { |
| 51 | + const item: Partial<ProviderItem> = { |
| 52 | + status: record.getStatus(), |
| 53 | + }; |
| 54 | + |
| 55 | + if (record.inProgressExpiryTimestamp !== undefined) { |
| 56 | + item.in_progress_expiration = record.inProgressExpiryTimestamp; |
| 57 | + } |
| 58 | + |
| 59 | + if (this.isPayloadValidationEnabled() && record.payloadHash !== undefined) { |
| 60 | + item.validation = record.payloadHash; |
| 61 | + } |
| 62 | + |
| 63 | + const ttl = record.expiryTimestamp |
| 64 | + ? Math.floor(new Date(record.expiryTimestamp * 1000).getTime() / 1000) - |
| 65 | + Math.floor(new Date().getTime() / 1000) |
| 66 | + : this.getExpiresAfterSeconds(); |
| 67 | + |
| 68 | + let existingItem: ProviderItem | undefined; |
| 69 | + try { |
| 70 | + existingItem = await ( |
| 71 | + await this.#getClient() |
| 72 | + ).put(this.#collectionName, record.idempotencyKey, item, { |
| 73 | + ttl, |
| 74 | + }); |
| 75 | + } catch (error) { |
| 76 | + if (error instanceof ProviderItemAlreadyExists) { |
| 77 | + if ( |
| 78 | + existingItem && |
| 79 | + existingItem.status !== IdempotencyRecordStatus.INPROGRESS && |
| 80 | + (existingItem.in_progress_expiration || 0) < Date.now() |
| 81 | + ) { |
| 82 | + throw new IdempotencyItemAlreadyExistsError( |
| 83 | + `Failed to put record for already existing idempotency key: ${record.idempotencyKey}` |
| 84 | + ); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + protected async _updateRecord(record: IdempotencyRecord): Promise<void> { |
| 91 | + const value: Partial<ProviderItem> = { |
| 92 | + data: JSON.stringify(record.responseData), |
| 93 | + status: record.getStatus(), |
| 94 | + }; |
| 95 | + |
| 96 | + if (this.isPayloadValidationEnabled()) { |
| 97 | + value.validation = record.payloadHash; |
| 98 | + } |
| 99 | + |
| 100 | + await ( |
| 101 | + await this.#getClient() |
| 102 | + ).update(this.#collectionName, record.idempotencyKey, value); |
| 103 | + } |
| 104 | + |
| 105 | + async #getClient(): Promise<ProviderClient> { |
| 106 | + if (this.#client) return this.#client; |
| 107 | + |
| 108 | + const secretName = process.env.API_SECRET; |
| 109 | + if (!secretName) { |
| 110 | + throw new Error('API_SECRET environment variable is not set'); |
| 111 | + } |
| 112 | + |
| 113 | + const apiSecret = await getSecret<ApiSecret>(secretName, { |
| 114 | + transform: Transform.JSON, |
| 115 | + }); |
| 116 | + |
| 117 | + if (!apiSecret) { |
| 118 | + throw new Error(`Could not retrieve secret ${secretName}`); |
| 119 | + } |
| 120 | + |
| 121 | + this.#client = new ProviderClient({ |
| 122 | + apiKey: apiSecret.apiKey, |
| 123 | + defaultTtlSeconds: this.getExpiresAfterSeconds(), |
| 124 | + }); |
| 125 | + |
| 126 | + return this.#client; |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +export { CustomPersistenceLayer }; |
0 commit comments