Skip to content

Commit

Permalink
Fix/align ts types with code (#143)
Browse files Browse the repository at this point in the history
* fix(types): align types with code

* fix(style): use spaces in types

* fix(style): tabs to spaces

* fix(ts): export types that might be useful
  • Loading branch information
SkeLLLa committed Apr 29, 2024
1 parent 594eeab commit 2607cfb
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 66 deletions.
105 changes: 55 additions & 50 deletions index.d.ts
@@ -1,85 +1,91 @@
import { Redis } from "ioredis";

type StorageOptionsType = "redis" | "memory";
export type StorageOptionsType = "redis" | "memory";

export type StorageOptions = {
type: StorageOptionsType,
options: StorageRedisOptions | StorageMemoryOptions,
}

type References = string | string[];

interface LoggerInput {
msg: string;
[key: string]: any;
msg: string;
[key: string]: any;
}

interface Logger {
debug: (input: LoggerInput) => void;
warn: (input: LoggerInput) => void;
error: (input: LoggerInput) => void;
debug: (input: LoggerInput) => void;
warn: (input: LoggerInput) => void;
error: (input: LoggerInput) => void;
}
interface StorageRedisOptions {
client: Redis;
log?: Logger;
invalidation?: { referencesTTL: number } | boolean;
export interface StorageRedisOptions {
client: Redis;
log?: Logger;
invalidation?: { referencesTTL: number } | boolean;
}

interface StorageMemoryOptions {
size?: number;
log?: Logger;
invalidation?: boolean;
export interface StorageMemoryOptions {
size?: number;
log?: Logger;
invalidation?: boolean;
}

interface DataTransformer {
serialize: (data: any) => any;
deserialize: (data: any) => any;
serialize: (data: any) => any;
deserialize: (data: any) => any;
}

type Events = {
onDedupe?: (key: string) => void;
onError?: (err: any) => void;
onHit?: (key: string) => void;
onMiss?: (key: string) => void;
onDedupe?: (key: string) => void;
onError?: (err: any) => void;
onHit?: (key: string) => void;
onMiss?: (key: string) => void;
};

type StorageInputRedis = {
type: "redis";
options?: StorageRedisOptions;
export type StorageInputRedis = {
type: "redis";
options?: StorageRedisOptions;
};

type StorageInputMemory = {
type: "memory";
options?: StorageMemoryOptions;
export type StorageInputMemory = {
type: "memory";
options?: StorageMemoryOptions;
};

declare class StorageInterface {
constructor(options: any);
export declare class StorageInterface {
constructor(options: any);

get(key: string): Promise<undefined | any>;
set(key: string, value: any, ttl: number, references?: References): Promise<void>;
remove(key: string): Promise<void>;
invalidate(references: References): Promise<void>;
clear(name: string): Promise<void>;
refresh(): Promise<void>;
get(key: string): Promise<undefined | any>;
set(key: string, value: any, ttl: number, references?: References): Promise<void>;
remove(key: string): Promise<void>;
invalidate(references: References): Promise<void>;
clear(name: string): Promise<void>;
refresh(): Promise<void>;
}

declare function createCache(
options?: {
storage?: StorageInputRedis | StorageInputMemory;
ttl?: number | ((result: unknown) => number);
transformer?: DataTransformer;
stale?: number | ((result: unknown) => number);
} & Events,
export declare function createCache(
options?: {
storage?: StorageInputRedis | StorageInputMemory;
ttl?: number | ((result: unknown) => number);
transformer?: DataTransformer;
stale?: number | ((result: unknown) => number);
} & Events,
): Cache;

declare class Cache {
export declare class Cache {
constructor(
options: {
ttl: number | ((result: unknown) => number);
storage: StorageOptionsType;
stale?: number | ((result: unknown) => number);
storage: StorageInterface;
} & Events
);

define<T extends (args: any) => any, N extends string, S extends this>(
name: N,
opts: {
storage?: StorageOptionsType;
storage?: StorageOptions;
transformer?: DataTransformer;
ttl?: number | ((result: Awaited<ReturnType<T>>) => number);
stale?: number | ((result: Awaited<ReturnType<T>>) => number);
Expand Down Expand Up @@ -119,11 +125,10 @@ declare class Cache {
): Promise<void>;
}

declare function createStorage(type: "redis", options: StorageRedisOptions): StorageInterface;
declare function createStorage(type: "memory", options: StorageMemoryOptions): StorageInterface;
declare function createStorage(
type: StorageOptionsType,
options: StorageRedisOptions | StorageMemoryOptions,
export declare function createStorage(type: "redis", options: StorageRedisOptions): StorageInterface;
export declare function createStorage(type: "memory", options: StorageMemoryOptions): StorageInterface;
export declare function createStorage(
type: StorageOptionsType,
options: StorageRedisOptions | StorageMemoryOptions,
): StorageInterface;

export { createCache, Cache, createStorage, StorageInterface, StorageMemoryOptions };
47 changes: 31 additions & 16 deletions index.test-d.ts
Expand Up @@ -6,7 +6,7 @@ import { StorageInterface, StorageMemoryOptions } from "./index.js";
// Testing internal types

const storageOptions: StorageMemoryOptions = {
size: 1000,
size: 1000,
};

const cache = createCache();
Expand All @@ -16,30 +16,37 @@ const storage = createStorage("memory", storageOptions);
expectType<StorageInterface>(storage);

const memoryCache = createCache({
storage: {
type: "memory",
options: storageOptions,
},
storage: {
type: "memory",
options: storageOptions,
},
});
expectType<Cache>(memoryCache);

const cacheWithTtlAndStale = createCache({
ttl: 1000,
stale: 1000,
ttl: 1000,
stale: 1000,
});
expectType<Cache>(cacheWithTtlAndStale);

const cacheClass = new Cache({
ttl: 1000,
stale: 1000,
storage: createStorage('memory', {})
});
expectType<Cache>(cacheClass);

// Testing Union Types

const fetchSomething = async (k: any) => {
console.log("query", k);
return { k };
console.log("query", k);
return { k };
};

export type CachedFunctions = {
fetchSomething: typeof fetchSomething;
fetchSomethingElse: typeof fetchSomething;
fetchSomethingElseWithTtlFunction: typeof fetchSomething;
fetchSomething: typeof fetchSomething;
fetchSomethingElse: typeof fetchSomething;
fetchSomethingElseWithTtlFunction: typeof fetchSomething;
};

const unionMemoryCache = createCache({
Expand All @@ -60,12 +67,20 @@ const currentCacheInstance = unionMemoryCache
"fetchSomethingElseWithTtlFunction",
{ ttl: (result) => (result.k ? 1000 : 5), stale: 1000 },
fetchSomething
)
.define(
"fetchSomethingElseWithCustomStorage",
{storage: {'type': 'memory', options: {size: 10}}, stale: 1000 },
fetchSomething
);
expectType<typeof fetchSomething>(currentCacheInstance.fetchSomething);
expectType<typeof fetchSomething>(currentCacheInstance.fetchSomethingElse);
expectType<typeof fetchSomething>(
currentCacheInstance.fetchSomethingElseWithTtlFunction
);
expectType<typeof fetchSomething>(
currentCacheInstance.fetchSomethingElseWithCustomStorage
);

expectType<Promise<void>>(cache.clear());
expectType<Promise<void>>(cache.clear("fetchSomething"));
Expand All @@ -80,14 +95,14 @@ await unionMemoryCache.invalidateAll(["test:1", "test:2", "test:3"], "memory");

// Testing define.func only accepts one argument
const fetchFuncSingleArgument = async (args: {k1: string, k2:string}) => {
console.log("query", args.k1, args.k2);
return { k1: args.k1, k2: args.k2 };
console.log("query", args.k1, args.k2);
return { k1: args.k1, k2: args.k2 };
};


const fetchFuncMultipleArguments = async (k1: string, k2:string) => {
console.log("query", k1, k2);
return { k1, k2 };
console.log("query", k1, k2);
return { k1, k2 };
};

expectAssignable<Parameters<typeof unionMemoryCache.define>>(["fetchFuncSingleArgument", fetchFuncSingleArgument]);
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -6,6 +6,7 @@
"types": "index.d.ts",
"scripts": {
"test": "standard | snazzy && c8 --100 node --test test/*test.js && tsd",
"test:types": "tsd",
"test:browser": "node test/browser/helpers/runner-browser.mjs",
"lint:fix": "standard --fix",
"redis": "docker run --rm -p 6379:6379 redis redis-server"
Expand Down

0 comments on commit 2607cfb

Please sign in to comment.