Skip to content

Commit

Permalink
feat: add configure helper (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiphe authored Feb 7, 2025
1 parent b038f3a commit 55b45a8
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,28 @@ console.log(await getUserById(1));
// Cache was filled an valid. `getFreshValue` was not invoked
```

### Pre-configuring cachified

We can create versions of cachified with defaults so that we don't have to
specify the same options every time.

<!-- pre-configured-cachified -->

```ts
import { configure } from '@epic-web/cachified';
import { LRUCache } from 'lru-cache';

/* lruCachified now has a default cache */
const lruCachified = configure({
cache: new LRUCache<string, CacheEntry>({ max: 1000 }),
});

const value = await lruCachified({
key: 'user-1',
getFreshValue: async () => 'ONE',
});
```

### Manually working with the cache

During normal app lifecycle there usually is no need for this but for
Expand Down
17 changes: 17 additions & 0 deletions src/cachified.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from './index';
import { Deferred } from './createBatch';
import { delay, report } from './testHelpers';
import { configure } from './configure';

jest.mock('./index', () => {
if (process.version.startsWith('v20')) {
Expand Down Expand Up @@ -1536,6 +1537,22 @@ describe('cachified', () => {
});
expect(await getValue(() => () => {})).toBe('FOUR');
});

it('supports creating pre-configured cachified functions', async () => {
const configuredCachified = configure({
cache: new Map(),
});

const value = await configuredCachified({
key: 'test',
// look mom, no cache!
getFreshValue() {
return 'ONE';
},
});

expect(value).toBe('ONE');
});
});

function createReporter() {
Expand Down
43 changes: 43 additions & 0 deletions src/configure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { cachified } from './cachified';
import { CachifiedOptions, CachifiedOptionsWithSchema } from './common';
import { CreateReporter, mergeReporters } from './reporter';

type PartialOptions<
Options extends CachifiedOptions<any>,
OptionalKeys extends string | number | symbol,
> = Omit<Options, OptionalKeys> &
Partial<Pick<Options, Extract<OptionalKeys, keyof Options>>>;

/**
* create a pre-configured version of cachified
*/
export function configure<
ConfigureValue extends unknown,
Opts extends Partial<CachifiedOptions<ConfigureValue>>,
>(defaultOptions: Opts, defaultReporter?: CreateReporter<ConfigureValue>) {
function configuredCachified<Value, InternalValue>(
options: PartialOptions<
CachifiedOptionsWithSchema<Value, InternalValue>,
keyof Opts
>,
reporter?: CreateReporter<Value>,
): Promise<Value>;
async function configuredCachified<Value>(
options: PartialOptions<CachifiedOptions<Value>, keyof Opts>,
reporter?: CreateReporter<Value>,
): Promise<Value>;
function configuredCachified<Value>(
options: PartialOptions<CachifiedOptions<Value>, keyof Opts>,
reporter?: CreateReporter<Value>,
) {
return cachified(
{
...defaultOptions,
...options,
} as any as CachifiedOptions<Value>,
mergeReporters(defaultReporter as any as CreateReporter<Value>, reporter),
);
}

return configuredCachified;
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export { cachified as default } from './cachified';
export { shouldRefresh } from './shouldRefresh';
export { assertCacheEntry } from './assertCacheEntry';
export { softPurge } from './softPurge';
export { configure } from './configure';

0 comments on commit 55b45a8

Please sign in to comment.