diff --git a/lib/index.d.ts b/lib/index.d.ts new file mode 100644 index 0000000..b95eb62 --- /dev/null +++ b/lib/index.d.ts @@ -0,0 +1,33 @@ +import { ClientApi, ClientOptions } from '@hapi/catbox'; + +interface Engine extends ClientApi {} + +declare class Engine implements ClientApi { + 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 }; \ No newline at end of file diff --git a/package.json b/package.json index c8dc693..29b74d4 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,10 @@ { "name": "@hapi/catbox-memory", "description": "Memory adapter for catbox", - "version": "6.0.1", + "version": "6.0.2", "repository": "git://github.com/hapijs/catbox-memory", "main": "lib/index.js", + "types": "lib/index.d.ts", "files": [ "lib" ], diff --git a/test/types.ts b/test/types.ts new file mode 100644 index 0000000..42d906d --- /dev/null +++ b/test/types.ts @@ -0,0 +1,48 @@ +import { Client } from '@hapi/catbox'; +import { Engine as CatboxMemory } from '..'; + +type MyValue = { + a: string; + b: number; +}; + +const client = new CatboxMemory({ + cloneBuffersOnGet: false, + maxByteSize: 1024, + minCleanupIntervalMsec: 1000, +}); + +const client2 = new CatboxMemory(); + +const catboxMemoryOptions: CatboxMemory.Options = { + cloneBuffersOnGet: false, + maxByteSize: 1024, + minCleanupIntervalMsec: 1000, +}; + +const client3 = new Client(CatboxMemory, catboxMemoryOptions); + +(async () => { + + 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'; +})() \ No newline at end of file