From 011941d5c8518d51043ba370857dfb210e49b05c Mon Sep 17 00:00:00 2001 From: Arylo Date: Mon, 19 Mar 2018 15:01:04 +0800 Subject: [PATCH 1/4] Add type definition for keyv --- types/keyv/index.d.ts | 30 ++++++++++++++++++++++++++++++ types/keyv/keyv-tests.ts | 20 ++++++++++++++++++++ types/keyv/tsconfig.json | 23 +++++++++++++++++++++++ types/keyv/tslint.json | 1 + 4 files changed, 74 insertions(+) create mode 100644 types/keyv/index.d.ts create mode 100644 types/keyv/keyv-tests.ts create mode 100644 types/keyv/tsconfig.json create mode 100644 types/keyv/tslint.json diff --git a/types/keyv/index.d.ts b/types/keyv/index.d.ts new file mode 100644 index 0000000000..4de9e75fff --- /dev/null +++ b/types/keyv/index.d.ts @@ -0,0 +1,30 @@ +// Type definitions for keyv 3.0 +// Project: https://github.com/lukechilds/keyv +// Definitions by: AryloYeung +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.1 + +/// + +// import * as events from 'events'; + +interface KeyvOptions { + namespace?: string; + serialize: (data: any) => string; + deserialize: (data: string) => any; + uri?: string; + store?: any; + ttl?: number; + adapter?: string; +} + +declare class Keyv extends NodeJS.EventEmitter { + constructor(uri?: string, opts?: KeyvOptions); + constructor(opts?: KeyvOptions); + get(key: string): Promise; + set(key: string, value: any, ttl?: number): Promise; + delete(key: string): Promise; + clear(): Promise; +} + +export = Keyv; diff --git a/types/keyv/keyv-tests.ts b/types/keyv/keyv-tests.ts new file mode 100644 index 0000000000..fc0c2f3ff1 --- /dev/null +++ b/types/keyv/keyv-tests.ts @@ -0,0 +1,20 @@ +import Keyv = require("keyv"); + +new Keyv('mongodb://user:pass@localhost:27017/dbname'); +new Keyv('redis://user:pass@localhost:6379'); +new Keyv('sqlite://path/to/database.sqlite'); +new Keyv('postgresql://user:pass@localhost:5432/dbname'); +new Keyv('mysql://user:pass@localhost:3306/dbname'); +new Keyv(); + +(async () => { + const keyv = new Keyv(); + + keyv.on('error', err => console.log('Connection Error', err)); + + await keyv.set('foo', 'expires in 1 second', 1000); // true + await keyv.set('foo', 'never expires'); // true + await keyv.get('foo'); // 'never expires' + await keyv.delete('foo'); // true + await keyv.clear(); // undefined +})(); diff --git a/types/keyv/tsconfig.json b/types/keyv/tsconfig.json new file mode 100644 index 0000000000..21b4bcb7f1 --- /dev/null +++ b/types/keyv/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": false, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "keyv-tests.ts" + ] +} diff --git a/types/keyv/tslint.json b/types/keyv/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/keyv/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From d98e689d3fe0264aacc90f1ba1e347df21209309 Mon Sep 17 00:00:00 2001 From: Arylo Date: Mon, 19 Mar 2018 15:13:43 +0800 Subject: [PATCH 2/4] Add Annotations --- types/keyv/index.d.ts | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/types/keyv/index.d.ts b/types/keyv/index.d.ts index 4de9e75fff..11e1895c9a 100644 --- a/types/keyv/index.d.ts +++ b/types/keyv/index.d.ts @@ -6,24 +6,50 @@ /// -// import * as events from 'events'; - interface KeyvOptions { + /** Namespace for the current instance. */ namespace?: string; + /** A custom serialization function. */ serialize: (data: any) => string; + /** A custom deserialization function. */ deserialize: (data: string) => any; + /** The connection string URI. */ uri?: string; + /** The storage adapter instance to be used by Keyv. */ store?: any; + /** Default TTL. Can be overridden by specififying a TTL on `.set()`. */ ttl?: number; + /** Specify an adapter to use. e.g `'redis'` or `'mongodb'`. */ adapter?: string; } declare class Keyv extends NodeJS.EventEmitter { + /** + * @param uri The connection string URI. + * + * Merged into the options object as options.uri. + * @param opts The options object is also passed through to the storage adapter. Check your storage adapter docs for any extra options. + */ constructor(uri?: string, opts?: KeyvOptions); + /** + * @param opts The options object is also passed through to the storage adapter. Check your storage adapter docs for any extra options. + */ constructor(opts?: KeyvOptions); + /** Returns the value. */ get(key: string): Promise; + /** + * Set a value. + * + * By default keys are persistent. You can set an expiry TTL in milliseconds. + */ set(key: string, value: any, ttl?: number): Promise; + /** + * Deletes an entry. + * + * Returns `true` if the key existed, `false` if not. + */ delete(key: string): Promise; + /** Delete all entries in the current namespace. */ clear(): Promise; } From f32382e9fd85e3cb351f1a5bccb1909c9c6ce293 Mon Sep 17 00:00:00 2001 From: Arylo Date: Mon, 19 Mar 2018 15:17:36 +0800 Subject: [PATCH 3/4] Modify `strictFunctionTypes` to `true` --- types/keyv/tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/keyv/tsconfig.json b/types/keyv/tsconfig.json index 21b4bcb7f1..47eab45180 100644 --- a/types/keyv/tsconfig.json +++ b/types/keyv/tsconfig.json @@ -6,7 +6,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictFunctionTypes": false, + "strictFunctionTypes": true, "strictNullChecks": true, "baseUrl": "../", "typeRoots": [ From 47742853c94a95edc5ce70596adcaadf2fe3d849 Mon Sep 17 00:00:00 2001 From: Arylo Date: Mon, 19 Mar 2018 15:29:01 +0800 Subject: [PATCH 4/4] Fix KeyvOptions --- types/keyv/index.d.ts | 12 ++++++------ types/keyv/keyv-tests.ts | 5 +++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/types/keyv/index.d.ts b/types/keyv/index.d.ts index 11e1895c9a..9d0e2c6404 100644 --- a/types/keyv/index.d.ts +++ b/types/keyv/index.d.ts @@ -10,9 +10,9 @@ interface KeyvOptions { /** Namespace for the current instance. */ namespace?: string; /** A custom serialization function. */ - serialize: (data: any) => string; + serialize?: (data: any) => string; /** A custom deserialization function. */ - deserialize: (data: string) => any; + deserialize?: (data: string) => any; /** The connection string URI. */ uri?: string; /** The storage adapter instance to be used by Keyv. */ @@ -24,6 +24,10 @@ interface KeyvOptions { } declare class Keyv extends NodeJS.EventEmitter { + /** + * @param opts The options object is also passed through to the storage adapter. Check your storage adapter docs for any extra options. + */ + constructor(opts?: KeyvOptions); /** * @param uri The connection string URI. * @@ -31,10 +35,6 @@ declare class Keyv extends NodeJS.EventEmitter { * @param opts The options object is also passed through to the storage adapter. Check your storage adapter docs for any extra options. */ constructor(uri?: string, opts?: KeyvOptions); - /** - * @param opts The options object is also passed through to the storage adapter. Check your storage adapter docs for any extra options. - */ - constructor(opts?: KeyvOptions); /** Returns the value. */ get(key: string): Promise; /** diff --git a/types/keyv/keyv-tests.ts b/types/keyv/keyv-tests.ts index fc0c2f3ff1..7ee4cac90e 100644 --- a/types/keyv/keyv-tests.ts +++ b/types/keyv/keyv-tests.ts @@ -1,5 +1,10 @@ import Keyv = require("keyv"); +new Keyv({ + uri: 'redis://user:pass@localhost:6379', + namespace: "redis" +}); + new Keyv('mongodb://user:pass@localhost:27017/dbname'); new Keyv('redis://user:pass@localhost:6379'); new Keyv('sqlite://path/to/database.sqlite');