From a0923ddb45bc7b04bbd34375921ca219446b203c Mon Sep 17 00:00:00 2001 From: Daniel Byrne Date: Mon, 17 Sep 2018 11:02:23 -0700 Subject: [PATCH] adds typings for version 3.1 of the 'levelup' package --- types/level-sublevel/tsconfig.json | 7 +- types/levelup/index.d.ts | 159 ++++++++++++++++++----------- types/levelup/levelup-tests.ts | 66 +++++------- types/levelup/tsconfig.json | 4 +- types/levelup/tslint.json | 78 +------------- types/levelup/v1/index.d.ts | 82 +++++++++++++++ types/levelup/v1/levelup-tests.ts | 84 +++++++++++++++ types/levelup/v1/tsconfig.json | 31 ++++++ types/levelup/v1/tslint.json | 79 ++++++++++++++ 9 files changed, 411 insertions(+), 179 deletions(-) create mode 100644 types/levelup/v1/index.d.ts create mode 100644 types/levelup/v1/levelup-tests.ts create mode 100644 types/levelup/v1/tsconfig.json create mode 100644 types/levelup/v1/tslint.json diff --git a/types/level-sublevel/tsconfig.json b/types/level-sublevel/tsconfig.json index 4dad69a329..ef1d6dbbf0 100644 --- a/types/level-sublevel/tsconfig.json +++ b/types/level-sublevel/tsconfig.json @@ -13,6 +13,11 @@ "../" ], "types": [], + "paths": { + "levelup": [ + "levelup/v1", + ], + }, "noEmit": true, "forceConsistentCasingInFileNames": true }, @@ -20,4 +25,4 @@ "index.d.ts", "level-sublevel-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/levelup/index.d.ts b/types/levelup/index.d.ts index 69c816638b..77ad3330a1 100644 --- a/types/levelup/index.d.ts +++ b/types/levelup/index.d.ts @@ -1,82 +1,123 @@ -// Type definitions for LevelUp +// Type definitions for levelup 3.1 // Project: https://github.com/Level/levelup -// Definitions by: Bret Little , Thiago de Arruda +// Definitions by: Meirion Hughes +// Daniel Byrne // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.8 /// -import * as leveldown from "leveldown"; +import { EventEmitter } from 'events'; +import { AbstractLevelDOWN, AbstractIteratorOptions, AbstractBatch, ErrorCallback, AbstractOptions, ErrorValueCallback, AbstractGetOptions } from 'abstract-leveldown'; -export = levelup; +type LevelUpPut = + ((key: K, value: V, callback: ErrorCallback) => void) & + ((key: K, value: V, options: O, callback: ErrorCallback) => void) & + ((key: K, value: V, options?: O) => Promise); -declare var levelup: levelup.LevelUpConstructor; +type LevelUpGet = + ((key: K, callback: ErrorValueCallback) => void) & + ((key: K, options: O, callback: ErrorValueCallback) => void) & + ((key: K, options?: O) => Promise); -declare namespace levelup { +type LevelUpDel = + ((key: K, callback: ErrorCallback) => void) & + ((key: K, options: O, callback: ErrorCallback) => void) & + ((key: K, options?: O) => Promise); -interface CustomEncoding { - encode(val: any): Buffer| string; - decode(val: Buffer | string): any; - buffer: boolean; - type: string; -} +type LevelUpBatch = + ((key: K, callback: ErrorCallback) => void) & + ((key: K, options: O, callback: ErrorCallback) => void) & + ((key: K, options?: O) => Promise); -type Encoding = string | CustomEncoding; +type InferDBPut = + DB extends { put: (key: infer K, value: infer V, options: infer O, cb: any) => void } ? + LevelUpPut : + LevelUpPut; -interface Batch { - type: string; - key: any; - value?: any; - keyEncoding?: Encoding; - valueEncoding?: Encoding; -} +type InferDBGet = + DB extends { get: (key: infer K, options: infer O, callback: ErrorValueCallback) => void } ? + LevelUpGet : + LevelUpGet; -interface LevelUpBase { - open(callback ?: (error : any) => any): void; - close(callback ?: (error : any) => any): void; - put(key: any, value: any, callback ?: (error: any) => any): void; - put(key: any, value: any, options?: { sync?: boolean }, callback ?: (error: any) => any): void; - get(key: any, callback ?: (error: any, value: any) => any): void; +type InferDBDel = + DB extends { del: (key: infer K, options: infer O, callback: ErrorCallback) => void } ? + LevelUpDel : + LevelUpDel; - get(key: any, options ?: { keyEncoding?: Encoding; fillCache?: boolean }, callback ?: (error: any, value: any) => any): void; - del(key: any, callback ?: (error: any) => any): void; - del(key: any, options ?: { keyEncoding?: Encoding; sync?: boolean }, callback ?: (error: any) => any): void; +export interface LevelUp extends EventEmitter { + open(): Promise; + open(callback?: ErrorCallback): void; + close(): Promise; + close(callback?: ErrorCallback): void; + put: InferDBPut; + get: InferDBGet; + del: InferDBDel; - batch(array: BatchType[], options?: { keyEncoding?: Encoding; valueEncoding?: Encoding; sync?: boolean }, callback?: (error?: any)=>any): void; - batch(array: BatchType[], callback?: (error?: any)=>any): void; - batch():LevelUpChain; - isOpen():boolean; - isClosed():boolean; - createReadStream(options?: any): any; - createKeyStream(options?: any): any; - createValueStream(options?: any): any; - createWriteStream(options?: any): any; - destroy(location: string, callback?: Function): void; - repair(location: string, callback?: Function): void; -} + batch(array: AbstractBatch[], options?: any): Promise; + batch(array: AbstractBatch[], options: any, callback: (err?: any) => any): void; + batch(array: AbstractBatch[], callback: (err?: any) => any): void; -type LevelUp = LevelUpBase + batch(): LevelUpChain; -interface LevelUpChain { - put(key: any, value: any): LevelUpChain; - put(key: any, value: any, options?: { sync?: boolean }): LevelUpChain; - del(key: any): LevelUpChain; - del(key: any, options ?: { keyEncoding?: Encoding; sync?: boolean }): LevelUpChain; - clear(): LevelUpChain; - write(callback?: (error?: any)=>any) : LevelUpChain; -} + isOpen(): boolean; + isClosed(): boolean; -interface levelupOptions { - createIfMissing?: boolean; - errorIfExists?: boolean; - compression?: boolean; - cacheSize?: number; - keyEncoding?: Encoding; - valueEncoding?: Encoding; - db?: leveldown.Constructor; + createReadStream(options?: AbstractIteratorOptions): NodeJS.ReadableStream; + createKeyStream(options?: AbstractIteratorOptions): NodeJS.ReadableStream; + createValueStream(options?: AbstractIteratorOptions): NodeJS.ReadableStream; + + /* + emitted when a new value is 'put' + */ + on(event: 'put', cb: (key: any, value: any) => void): this; + /* + emitted when a value is deleted + */ + on(event: 'del', cb: (key: any) => void): this; + /* + emitted when a batch operation has executed + */ + on(event: 'batch', cb: (ary: any[]) => void): this; + /* + emitted on given event + */ + on(event: 'open' | 'ready' | 'closed' | 'opening' | 'closing', cb: () => void): this; } interface LevelUpConstructor { - (hostname: string, options?: levelupOptions): LevelUp; + ( + db: DB, + options: any, + cb?: ErrorCallback): LevelUp; + + ( + db: DB, + cb?: ErrorCallback): LevelUp; + + new ( + db: DB, + options: any, + cb?: ErrorCallback): LevelUp; + + new ( + db: DB, + cb?: ErrorCallback): LevelUp; + + errors: /*typeof levelerrors*/ any; // ? level-errors is not in DT } + +export interface LevelUpChain { + readonly length: number; + put(key: K, value: V): this; + del(key: K): this; + clear(): this; + write(callback: ErrorCallback): this; + write(): Promise; } + +export const errors: /*typeof levelerrors*/ any; // ? level-errors is not in DT + +declare const LevelUp: LevelUpConstructor; +export default LevelUp; diff --git a/types/levelup/levelup-tests.ts b/types/levelup/levelup-tests.ts index 2d3867cdd3..bb7fbd0518 100644 --- a/types/levelup/levelup-tests.ts +++ b/types/levelup/levelup-tests.ts @@ -1,11 +1,5 @@ -import levelup = require("levelup"); - -interface BufferEncoding { - encode(val: any): Buffer; - decode(val: Buffer): any; - buffer: boolean; - type: string; -} +import levelup from 'levelup'; +import { AbstractLevelDOWN } from 'abstract-leveldown'; interface StringEncoding { encode(val: any): string; @@ -14,50 +8,39 @@ interface StringEncoding { type: string; } -declare const bufferEncoding: BufferEncoding; declare const stringEncoding: StringEncoding; -var db1 = levelup("db1", { - keyEncoding: bufferEncoding, - valueEncoding: bufferEncoding -}); -var db2 = levelup("db2", { +const db = levelup(new AbstractLevelDOWN('here'), { keyEncoding: stringEncoding, valueEncoding: stringEncoding }); -var db = levelup('./mydb') db.open(); db.close(); -db.open((error)=> { - +db.open((error) => { }); -db.close((error)=> { - +db.close((error) => { }); - db.put("key", {}); -db.put("key", {}, (error)=>{}); -db.put("key", {}, { sync: true}, (error)=>{}); +db.put("key", {}, (error) => {}); +db.put("key", {}, { sync: true}, (error) => {}); db.get("key", {keyEncoding: "json"}, (error, val) => {}); db.get("key", {fillCache: true}, (error, val) => {}); db.get("key", (error, val) => {}); db.del("key"); -db.del("key", (error)=>{}); -db.del("key", {keyEncoding: "json"}, (error)=>{}); -db.del("key", {sync: true}, (error)=>{}); +db.del("key", (error) => {}); +db.del("key", {keyEncoding: "json"}, (error) => {}); +db.del("key", {sync: true}, (error) => {}); db.batch([{ type : 'put' , key : ([1, 2, 3]) , value : { some: 'json' } - , keyEncoding : 'binary' - , valueEncoding : 'json' -}], (error)=> {}); +}], (error: Error | undefined) => {}); db.batch() .del('father') @@ -65,20 +48,23 @@ db.batch() .put('dob', '16 February 1941') .put('spouse', 'Kim Young-sook') .put('occupation', 'Clown') - .write(function () { console.log('Done!') }) + .write(() => { console.log('Done!'); }); + +// $ExpectType boolean +db.isOpen(); +// $ExpectType boolean +db.isClosed(); -var open:boolean = db.isOpen(); -var closed:boolean = db.isClosed(); db.createReadStream() - .on('data', function (data: any) { - console.log(data.key, '=', data.value) + .on('data', (data: any) => { + console.log(data.key, '=', data.value); }) - .on('error', function (err: any) { - console.log('Oh my!', err) + .on('error', (err: any) => { + console.log('Oh my!', err); }) - .on('close', function () { - console.log('Stream closed') - }) - .on('end', function () { - console.log('Stream closed') + .on('close', () => { + console.log('Stream closed'); }) + .on('end', () => { + console.log('Stream closed'); + }); diff --git a/types/levelup/tsconfig.json b/types/levelup/tsconfig.json index 8825ba9b79..35349bd607 100644 --- a/types/levelup/tsconfig.json +++ b/types/levelup/tsconfig.json @@ -6,7 +6,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ @@ -20,4 +20,4 @@ "index.d.ts", "levelup-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/levelup/tslint.json b/types/levelup/tslint.json index a41bf5d19a..d88586e5bd 100644 --- a/types/levelup/tslint.json +++ b/types/levelup/tslint.json @@ -1,79 +1,3 @@ { - "extends": "dtslint/dt.json", - "rules": { - "adjacent-overload-signatures": false, - "array-type": false, - "arrow-return-shorthand": false, - "ban-types": false, - "callable-types": false, - "comment-format": false, - "dt-header": false, - "eofline": false, - "export-just-namespace": false, - "import-spacing": false, - "interface-name": false, - "interface-over-type-literal": false, - "jsdoc-format": false, - "max-line-length": false, - "member-access": false, - "new-parens": false, - "no-any-union": false, - "no-boolean-literal-compare": false, - "no-conditional-assignment": false, - "no-consecutive-blank-lines": false, - "no-construct": false, - "no-declare-current-package": false, - "no-duplicate-imports": false, - "no-duplicate-variable": false, - "no-empty-interface": false, - "no-for-in-array": false, - "no-inferrable-types": false, - "no-internal-module": false, - "no-irregular-whitespace": false, - "no-mergeable-namespace": false, - "no-misused-new": false, - "no-namespace": false, - "no-object-literal-type-assertion": false, - "no-padding": false, - "no-redundant-jsdoc": false, - "no-redundant-jsdoc-2": false, - "no-redundant-undefined": false, - "no-reference-import": false, - "no-relative-import-in-test": false, - "no-self-import": false, - "no-single-declare-module": false, - "no-string-throw": false, - "no-unnecessary-callback-wrapper": false, - "no-unnecessary-class": false, - "no-unnecessary-generics": false, - "no-unnecessary-qualifier": false, - "no-unnecessary-type-assertion": false, - "no-useless-files": false, - "no-var-keyword": false, - "no-var-requires": false, - "no-void-expression": false, - "no-trailing-whitespace": false, - "object-literal-key-quotes": false, - "object-literal-shorthand": false, - "one-line": false, - "one-variable-per-declaration": false, - "only-arrow-functions": false, - "prefer-conditional-expression": false, - "prefer-const": false, - "prefer-declare-function": false, - "prefer-for-of": false, - "prefer-method-signature": false, - "prefer-template": false, - "radix": false, - "semicolon": false, - "space-before-function-paren": false, - "space-within-parens": false, - "strict-export-declare-modifiers": false, - "trim-file": false, - "triple-equals": false, - "typedef-whitespace": false, - "unified-signatures": false, - "void-return": false, - "whitespace": false - } + "extends": "dtslint/dt.json" } diff --git a/types/levelup/v1/index.d.ts b/types/levelup/v1/index.d.ts new file mode 100644 index 0000000000..6435730778 --- /dev/null +++ b/types/levelup/v1/index.d.ts @@ -0,0 +1,82 @@ +// Type definitions for LevelUp 1 +// Project: https://github.com/Level/levelup +// Definitions by: Bret Little , Thiago de Arruda +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +import * as leveldown from "leveldown"; + +export = levelup; + +declare var levelup: levelup.LevelUpConstructor; + +declare namespace levelup { + + interface CustomEncoding { + encode(val: any): Buffer| string; + decode(val: Buffer | string): any; + buffer: boolean; + type: string; + } + + type Encoding = string | CustomEncoding; + + interface Batch { + type: string; + key: any; + value?: any; + keyEncoding?: Encoding; + valueEncoding?: Encoding; + } + + interface LevelUpBase { + open(callback ?: (error : any) => any): void; + close(callback ?: (error : any) => any): void; + put(key: any, value: any, callback ?: (error: any) => any): void; + put(key: any, value: any, options?: { sync?: boolean }, callback ?: (error: any) => any): void; + get(key: any, callback ?: (error: any, value: any) => any): void; + + get(key: any, options ?: { keyEncoding?: Encoding; fillCache?: boolean }, callback ?: (error: any, value: any) => any): void; + del(key: any, callback ?: (error: any) => any): void; + del(key: any, options ?: { keyEncoding?: Encoding; sync?: boolean }, callback ?: (error: any) => any): void; + + + batch(array: BatchType[], options?: { keyEncoding?: Encoding; valueEncoding?: Encoding; sync?: boolean }, callback?: (error?: any)=>any): void; + batch(array: BatchType[], callback?: (error?: any)=>any): void; + batch():LevelUpChain; + isOpen():boolean; + isClosed():boolean; + createReadStream(options?: any): any; + createKeyStream(options?: any): any; + createValueStream(options?: any): any; + createWriteStream(options?: any): any; + destroy(location: string, callback?: Function): void; + repair(location: string, callback?: Function): void; + } + + type LevelUp = LevelUpBase + + interface LevelUpChain { + put(key: any, value: any): LevelUpChain; + put(key: any, value: any, options?: { sync?: boolean }): LevelUpChain; + del(key: any): LevelUpChain; + del(key: any, options ?: { keyEncoding?: Encoding; sync?: boolean }): LevelUpChain; + clear(): LevelUpChain; + write(callback?: (error?: any)=>any) : LevelUpChain; + } + + interface levelupOptions { + createIfMissing?: boolean; + errorIfExists?: boolean; + compression?: boolean; + cacheSize?: number; + keyEncoding?: Encoding; + valueEncoding?: Encoding; + db?: leveldown.Constructor; + } + + interface LevelUpConstructor { + (hostname: string, options?: levelupOptions): LevelUp; + } +} diff --git a/types/levelup/v1/levelup-tests.ts b/types/levelup/v1/levelup-tests.ts new file mode 100644 index 0000000000..c32c240357 --- /dev/null +++ b/types/levelup/v1/levelup-tests.ts @@ -0,0 +1,84 @@ +import levelup = require("levelup"); + +interface BufferEncoding { + encode(val: any): Buffer; + decode(val: Buffer): any; + buffer: boolean; + type: string; +} + +interface StringEncoding { + encode(val: any): string; + decode(val: string): any; + buffer: boolean; + type: string; +} + +declare const bufferEncoding: BufferEncoding; +declare const stringEncoding: StringEncoding; + +var db1 = levelup("db1", { + keyEncoding: bufferEncoding, + valueEncoding: bufferEncoding +}); +var db2 = levelup("db2", { + keyEncoding: stringEncoding, + valueEncoding: stringEncoding +}); + +var db = levelup('./mydb') +db.open(); +db.close(); +db.open((error) => { + +}); + +db.close((error) => { + +}); + + +db.put("key", {}); +db.put("key", {}, (error) => { }); +db.put("key", {}, { sync: true }, (error) => { }); + +db.get("key", { keyEncoding: "json" }, (error, val) => { }); +db.get("key", { fillCache: true }, (error, val) => { }); +db.get("key", (error, val) => { }); + +db.del("key"); +db.del("key", (error) => { }); +db.del("key", { keyEncoding: "json" }, (error) => { }); +db.del("key", { sync: true }, (error) => { }); + +db.batch([{ + type: 'put' + , key: ([1, 2, 3]) + , value: { some: 'json' } + , keyEncoding: 'binary' + , valueEncoding: 'json' +}], (error) => { }); + +db.batch() + .del('father') + .put('name', 'Yuri Irsenovich Kim') + .put('dob', '16 February 1941') + .put('spouse', 'Kim Young-sook') + .put('occupation', 'Clown') + .write(function () { console.log('Done!') }) + +var open: boolean = db.isOpen(); +var closed: boolean = db.isClosed(); +db.createReadStream() + .on('data', function (data: any) { + console.log(data.key, '=', data.value) + }) + .on('error', function (err: any) { + console.log('Oh my!', err) + }) + .on('close', function () { + console.log('Stream closed') + }) + .on('end', function () { + console.log('Stream closed') + }) diff --git a/types/levelup/v1/tsconfig.json b/types/levelup/v1/tsconfig.json new file mode 100644 index 0000000000..1e7069b209 --- /dev/null +++ b/types/levelup/v1/tsconfig.json @@ -0,0 +1,31 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "strictFunctionTypes": true, + "baseUrl": "../../", + "typeRoots": [ + "../../" + ], + "types": [], + "paths": { + "levelup": [ + "levelup/v1" + ], + "levelup/*": [ + "levelup/v1/*" + ] + }, + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "levelup-tests.ts" + ] +} diff --git a/types/levelup/v1/tslint.json b/types/levelup/v1/tslint.json new file mode 100644 index 0000000000..a41bf5d19a --- /dev/null +++ b/types/levelup/v1/tslint.json @@ -0,0 +1,79 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "adjacent-overload-signatures": false, + "array-type": false, + "arrow-return-shorthand": false, + "ban-types": false, + "callable-types": false, + "comment-format": false, + "dt-header": false, + "eofline": false, + "export-just-namespace": false, + "import-spacing": false, + "interface-name": false, + "interface-over-type-literal": false, + "jsdoc-format": false, + "max-line-length": false, + "member-access": false, + "new-parens": false, + "no-any-union": false, + "no-boolean-literal-compare": false, + "no-conditional-assignment": false, + "no-consecutive-blank-lines": false, + "no-construct": false, + "no-declare-current-package": false, + "no-duplicate-imports": false, + "no-duplicate-variable": false, + "no-empty-interface": false, + "no-for-in-array": false, + "no-inferrable-types": false, + "no-internal-module": false, + "no-irregular-whitespace": false, + "no-mergeable-namespace": false, + "no-misused-new": false, + "no-namespace": false, + "no-object-literal-type-assertion": false, + "no-padding": false, + "no-redundant-jsdoc": false, + "no-redundant-jsdoc-2": false, + "no-redundant-undefined": false, + "no-reference-import": false, + "no-relative-import-in-test": false, + "no-self-import": false, + "no-single-declare-module": false, + "no-string-throw": false, + "no-unnecessary-callback-wrapper": false, + "no-unnecessary-class": false, + "no-unnecessary-generics": false, + "no-unnecessary-qualifier": false, + "no-unnecessary-type-assertion": false, + "no-useless-files": false, + "no-var-keyword": false, + "no-var-requires": false, + "no-void-expression": false, + "no-trailing-whitespace": false, + "object-literal-key-quotes": false, + "object-literal-shorthand": false, + "one-line": false, + "one-variable-per-declaration": false, + "only-arrow-functions": false, + "prefer-conditional-expression": false, + "prefer-const": false, + "prefer-declare-function": false, + "prefer-for-of": false, + "prefer-method-signature": false, + "prefer-template": false, + "radix": false, + "semicolon": false, + "space-before-function-paren": false, + "space-within-parens": false, + "strict-export-declare-modifiers": false, + "trim-file": false, + "triple-equals": false, + "typedef-whitespace": false, + "unified-signatures": false, + "void-return": false, + "whitespace": false + } +}