Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache-manager - fix: return types of mget to handle Array<T | null> #1005

Merged
merged 6 commits into from
Feb 13, 2025
Merged
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
13 changes: 8 additions & 5 deletions packages/cache-manager/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ type WrapOptionsRaw<T> = WrapOptions<T> & {
export type Cache = {
// eslint-disable-next-line @typescript-eslint/ban-types
get: <T>(key: string) => Promise<T | null>;
mget: <T>(keys: string[]) => Promise<[T]>;
// eslint-disable-next-line @typescript-eslint/ban-types
mget: <T>(keys: string[]) => Promise<Array<T | null>>;
// eslint-disable-next-line @typescript-eslint/ban-types
ttl: (key: string) => Promise<number | null>;
set: <T>(key: string, value: T, ttl?: number) => Promise<T>;
Expand Down Expand Up @@ -120,15 +121,17 @@ export const createCache = (options?: CreateCacheOptions): Cache => {
return result as T;
};

const mget = async <T>(keys: string[]) => {
const result = [];
// eslint-disable-next-line @typescript-eslint/ban-types
const mget = async <T>(keys: string[]): Promise<Array<T | null>> => {
// eslint-disable-next-line @typescript-eslint/ban-types
const result: Array<T | null> = [];

for (const key of keys) {
const data = await get<T>(key);
result.push(data);
}

return result as [T];
return result;
};

// eslint-disable-next-line @typescript-eslint/ban-types
Expand Down Expand Up @@ -166,7 +169,7 @@ export const createCache = (options?: CreateCacheOptions): Cache => {
return null;
};

const set = async <T>(stores: Keyv[], key: string, value: T, ttl?: number) => {
const set = async <T>(stores: Keyv[], key: string, value: T, ttl?: number): Promise<T> => {
try {
if (nonBlocking) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
Expand Down