Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/core/src/idempotency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ export class Idempotency {
if (await this.isEnabled(reqInternal)) {
const fingerPrint = this.getFingerPrint(reqInternal);
const cacheKey = this.getIdempotencyCacheKey(reqInternal);

if (res.error && req.options?.skipErrorsCache) {
// do not cache the error itself, clear the cache key and allow subsequent requests to retry
await this.storage.delete(cacheKey);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you miss a return statement here? because the set below will set the response anyway.
https://github.com/mahendraHegde/node-idempotency/pull/36/files#diff-3c306f162d9ea758a7d4323df8bfe9476e1a263a7138ea8e22e531e30152b8fdR177-R184

also would you mind adding some tests like we have for other cases so that we could catch bugs like this early(I'd appreciate it if you could add tests for adapters too, if not i can add them too)?

}

const payload: StoragePayload = {
status: RequestStatusEnum.COMPLETE,
fingerPrint,
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ export interface IdempotencyOptions {
* if set to `true` requests without idempotency key header will be rejected
*/
enforceIdempotency?: boolean;
/**
* @defaultValue `false`
*
* if set to `true` responses with errors will not be cached
*/
skipErrorsCache?: boolean;

/**
* @defaultValue `undefined`
Expand Down
4 changes: 4 additions & 0 deletions packages/storage-adapter-memory/src/adapter-memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@ export class MemoryStorageAdapter implements StorageAdapter {
}
return val?.item;
}

async delete(key: string): Promise<void> {
this.cache.delete(key);
}
}
4 changes: 4 additions & 0 deletions packages/storage-adapter-redis/src/adapter-redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,8 @@ export class RedisStorageAdapter implements StorageAdapter {
const val = await this.client.get(key);
return val ?? undefined;
}

async delete(key: string): Promise<void> {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to go inside postgres adapter too, if you are not familiar with postgres, no worries i can take care of it too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

await this.client.del(key);
}
}
1 change: 1 addition & 0 deletions packages/storage/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface StorageAdapter {
) => Promise<boolean>;
set: (key: string, val: string, { ttl }: { ttl?: number }) => Promise<void>;
get: (key: string) => Promise<string | undefined>;
delete: (key: string) => Promise<void>;
connect?: () => Promise<void>;
disconnect?: () => Promise<void>;
}