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

feat: 🎸 bring in typings from DT #90

Merged
merged 4 commits into from
Jun 12, 2024
Merged
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
33 changes: 33 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ClientApi, ClientOptions } from '@hapi/catbox';

interface Engine<T> extends ClientApi<T> {}

declare class Engine<T> implements ClientApi<T> {
constructor(options?: Engine.Options);
}

declare namespace Engine {
interface Options extends ClientOptions {
/**
* Sets an upper limit on the number of bytes that can be stored in the cache.
* Once this limit is reached no additional items will be added to the cache until some expire.
* The utilized memory calculation is a rough approximation and must not be relied on.
* @default 104857600 (100MB).
*/
maxByteSize?: number;
/**
* The minimum number of milliseconds in between each cache cleanup.
* @default 1000 (1 second)
*/
minCleanupIntervalMsec?: number;
/**
* by default, buffers stored in the cache with allowMixedContent set to true are copied when they are set but not when they are retrieved.
* This means a change to the buffer returned by a get() will change the value in the cache. To prevent this,
* set cloneBuffersOnGet to true to always return a copy of the cached buffer.
* @default false
*/
cloneBuffersOnGet?: boolean;
}
}

export { Engine };
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"version": "6.0.1",
"repository": "git://github.com/hapijs/catbox-memory",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
"lib"
],
Expand All @@ -25,10 +26,12 @@
"@hapi/catbox": "^12.1.1",
"@hapi/code": "^9.0.3",
"@hapi/eslint-plugin": "*",
"@hapi/lab": "^25.1.2"
"@hapi/lab": "^25.1.2",
"@types/node": "^16.18.98",
"typescript": "^5.4.5"
},
"scripts": {
"test": "lab -a @hapi/code -L -t 100",
"test": "lab -a @hapi/code -L -t 100 -Y",
"test-cov-html": "lab -a @hapi/code -r html -o coverage.html"
},
"license": "BSD-3-Clause"
Expand Down
52 changes: 52 additions & 0 deletions test/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Client } from '@hapi/catbox';
import { Engine as CatboxMemory } from '..';

type MyValue = {
a: string;
b: number;
};

const client = new CatboxMemory<boolean>({
cloneBuffersOnGet: false,
maxByteSize: 1024,
minCleanupIntervalMsec: 1000,
});

const client2 = new CatboxMemory<MyValue>();

const catboxMemoryOptions: CatboxMemory.Options = {
cloneBuffersOnGet: false,
maxByteSize: 1024,
minCleanupIntervalMsec: 1000,
};

const client3 = new Client<string>(CatboxMemory, catboxMemoryOptions);

(async () => {

await Promise.all([client.start(), client2.start(), client3.start()]);

const x = await client.get({
id: 'x',
segment: 's',
})

x?.item === true;

const y = await client2.get({
id: 'y',
segment: 's',
})

y?.item.a === 'a';
y?.item.b === 1;

const z = await client3.get({
id: 'z',
segment: 's',
});

z?.item === 'z';

await Promise.all([client.stop(), client2.stop(), client3.stop()]);
})();