Skip to content

Commit 3f67a5b

Browse files
committed
fix: require zod input type from getFreshValue
when using schema validation
1 parent fe0f6c7 commit 3f67a5b

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

src/cachified.spec.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,8 @@ describe('cachified', () => {
294294
cache,
295295
key: 'test-2',
296296
checkValue: z.string(),
297-
getFreshValue() {
297+
/* manually setting unknown here leaves the type-checking to zod during runtime */
298+
getFreshValue(): unknown {
298299
/* pretend API returns an unexpected value */
299300
return 1;
300301
},
@@ -326,19 +327,19 @@ describe('cachified', () => {
326327
cachified({
327328
cache,
328329
key: 'test',
329-
checkValue: z.string().transform((s) => s.toUpperCase()),
330+
checkValue: z.string().transform((s) => parseInt(s, 10)),
330331
getFreshValue() {
331-
return 'one';
332+
return '123';
332333
},
333334
});
334335

335-
expect(await getValue()).toBe('ONE');
336+
expect(await getValue()).toBe(123);
336337

337338
/* Stores original value in cache */
338-
expect(cache.get('test')?.value).toBe('one');
339+
expect(cache.get('test')?.value).toBe('123');
339340

340341
/* Gets transformed value from cache */
341-
expect(await getValue()).toBe('ONE');
342+
expect(await getValue()).toBe(123);
342343
});
343344

344345
it('supports migrating cached values', async () => {

src/cachified.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import { shouldRefresh } from './shouldRefresh';
1414
// Keys are unique per cache but may be used by multiple caches
1515
const pendingValuesByCache = new WeakMap<Cache, Map<string, any>>();
1616

17-
export async function cachified<Value>(
18-
options: CachifiedOptionsWithSchema<Value>,
17+
export async function cachified<Value, InternalValue>(
18+
options: CachifiedOptionsWithSchema<Value, InternalValue>,
1919
): Promise<Value>;
2020
export async function cachified<Value>(
2121
options: CachifiedOptions<Value>,

src/common.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ export type CheckValue<Value> = (
5555
value: unknown,
5656
migrate: (value: Value, updateCache?: boolean) => MigratedValue<Value>,
5757
) => ValueCheckResult<Value> | Promise<ValueCheckResult<Value>>;
58-
export interface Schema<Value> {
58+
export interface Schema<Value, InputValue> {
59+
_input: InputValue;
5960
parseAsync(value: unknown): Promise<Value>;
6061
}
6162

@@ -126,7 +127,7 @@ export interface CachifiedOptions<Value> {
126127
*
127128
* @type {function(): boolean | undefined | string | MigratedValue} Optional, default makes no value check
128129
*/
129-
checkValue?: CheckValue<Value> | Schema<Value>;
130+
checkValue?: CheckValue<Value> | Schema<Value, unknown>;
130131
/**
131132
* Set true to not even try reading the currently cached value
132133
*
@@ -163,12 +164,12 @@ export interface CachifiedOptions<Value> {
163164
}
164165
/* When using a schema validator, a strongly typed getFreshValue is not required
165166
and sometimes even sub-optimal */
166-
export type CachifiedOptionsWithSchema<Value> = Omit<
167+
export type CachifiedOptionsWithSchema<Value, InternalValue> = Omit<
167168
CachifiedOptions<Value>,
168169
'checkValue' | 'getFreshValue'
169170
> & {
170-
checkValue: Schema<Value>;
171-
getFreshValue: GetFreshValue<unknown>;
171+
checkValue: Schema<Value, InternalValue>;
172+
getFreshValue: GetFreshValue<InternalValue>;
172173
};
173174

174175
export interface Context<Value>

0 commit comments

Comments
 (0)