Skip to content

Commit a51ab39

Browse files
Merge branch 'idempotency-persistence-store' of github.com:jeffrey-baker-vg/aws-lambda-powertools-typescript into idempotency-persistence-store
2 parents 8f3f92b + dd881d5 commit a51ab39

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

packages/idempotency/src/persistence/IdempotencyRecord.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ class IdempotencyRecord {
77
private status: IdempotencyRecordStatus,
88
public expiryTimestamp: number | undefined,
99
public inProgressExpiryTimestamp: number | undefined,
10-
public responseData: Record<string, unknown> |undefined,
11-
public payloadHash: string | undefined) {}
12-
10+
public responseData: Record<string, unknown> | undefined,
11+
public payloadHash: string | undefined) { }
12+
13+
public getResponse(): Record<string, unknown> | undefined {
14+
return this.responseData;
15+
}
16+
1317
public getStatus(): IdempotencyRecordStatus {
14-
if (this.isExpired()){
18+
if (this.isExpired()) {
1519
return IdempotencyRecordStatus.EXPIRED;
16-
} else if (Object.values(IdempotencyRecordStatus).includes(this.status)){
20+
} else if (Object.values(IdempotencyRecordStatus).includes(this.status)) {
1721
return this.status;
1822
} else {
1923
throw new IdempotencyInvalidStatusError();
2024
}
2125
}
2226

23-
public responseJsonAsObject(): Record<string, unknown> | undefined {
24-
return this.responseData;
25-
}
26-
2727
private isExpired(): boolean {
2828
return this.expiryTimestamp !== undefined && (Date.now() > this.expiryTimestamp);
2929
}

packages/idempotency/tests/unit/persistence/IdempotencyRecord.test.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { IdempotencyInvalidStatusError } from '../../../src/Exceptions';
12
import { IdempotencyRecord } from '../../../src/persistence/IdempotencyRecord';
23
import { IdempotencyRecordStatus } from '../../../src/types/IdempotencyRecordStatus';
34
/**
@@ -38,15 +39,38 @@ describe('Given an idempotency record that is not expired', () => {
3839
Date.now = jest.fn(() => mockNowBeforeExiryTime);
3940
idempotencyRecord = new IdempotencyRecord(mockIdempotencyKey, IdempotencyRecordStatus.INPROGRESS, expiryTimeAfterNow, mockInProgressExpiry, mockData, mockPayloadHash);
4041
});
42+
describe('When checking the status of the idempotency record', () => {
43+
test('Then the status is EXPIRED', () => {
44+
expect(idempotencyRecord.getStatus()).toEqual(IdempotencyRecordStatus.INPROGRESS);
45+
});
46+
47+
test('Then the record is returned', () => {
48+
expect(idempotencyRecord.getResponse()).toEqual(mockData);
49+
});
50+
});
51+
});
52+
53+
describe('Given an idempotency record that has a status not in the IdempotencyRecordStatus enum', () => {
54+
let idempotencyRecord: IdempotencyRecord;
55+
beforeEach(() => {
56+
const mockNowBeforeExiryTime = 1487076707000;
57+
const expiryTimeAfterNow = 1487076708000;
58+
Date.now = jest.fn(() => mockNowBeforeExiryTime);
59+
idempotencyRecord = new IdempotencyRecord(mockIdempotencyKey, 'NOT_A_STATUS' as IdempotencyRecordStatus, expiryTimeAfterNow, mockInProgressExpiry, mockData, mockPayloadHash);
60+
});
4161
describe('When checking the status of the idempotency record', () => {
42-
let resultingStatus: IdempotencyRecordStatus;
62+
let resultingError: Error;
4363
beforeEach(() => {
44-
resultingStatus = idempotencyRecord.getStatus();
64+
try {
65+
idempotencyRecord.getStatus();
66+
} catch (e: unknown) {
67+
resultingError = e as Error;
68+
}
69+
4570
});
46-
47-
test('Then the status is EXPIRED', () => {
48-
expect(resultingStatus).toEqual(IdempotencyRecordStatus.INPROGRESS);
71+
72+
test('Then an IdempotencyInvalidStatusError is thrown ', () => {
73+
expect(resultingError).toBeInstanceOf(IdempotencyInvalidStatusError);
4974
});
5075
});
51-
});
52-
76+
});

0 commit comments

Comments
 (0)