adds typings for version 3.1 of the 'levelup' package

This commit is contained in:
Daniel Byrne
2018-09-17 12:21:33 -07:00
parent b641cd46ba
commit a0923ddb45
9 changed files with 411 additions and 179 deletions
+6 -1
View File
@@ -13,6 +13,11 @@
"../"
],
"types": [],
"paths": {
"levelup": [
"levelup/v1",
],
},
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
@@ -20,4 +25,4 @@
"index.d.ts",
"level-sublevel-tests.ts"
]
}
}
+100 -59
View File
@@ -1,82 +1,123 @@
// Type definitions for LevelUp
// Type definitions for levelup 3.1
// Project: https://github.com/Level/levelup
// Definitions by: Bret Little <https://github.com/blittle>, Thiago de Arruda <https://github.com/tarruda>
// Definitions by: Meirion Hughes <https://github.com/MeirionHughes>
// Daniel Byrne <https://github.com/danwbyrne>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
/// <reference types="node" />
import * as leveldown from "leveldown";
import { EventEmitter } from 'events';
import { AbstractLevelDOWN, AbstractIteratorOptions, AbstractBatch, ErrorCallback, AbstractOptions, ErrorValueCallback, AbstractGetOptions } from 'abstract-leveldown';
export = levelup;
type LevelUpPut<K, V, O> =
((key: K, value: V, callback: ErrorCallback) => void) &
((key: K, value: V, options: O, callback: ErrorCallback) => void) &
((key: K, value: V, options?: O) => Promise<void>);
declare var levelup: levelup.LevelUpConstructor;
type LevelUpGet<K, V, O> =
((key: K, callback: ErrorValueCallback<V>) => void) &
((key: K, options: O, callback: ErrorValueCallback<V>) => void) &
((key: K, options?: O) => Promise<V>);
declare namespace levelup {
type LevelUpDel<K, O> =
((key: K, callback: ErrorCallback) => void) &
((key: K, options: O, callback: ErrorCallback) => void) &
((key: K, options?: O) => Promise<void>);
interface CustomEncoding {
encode(val: any): Buffer| string;
decode(val: Buffer | string): any;
buffer: boolean;
type: string;
}
type LevelUpBatch<K, O> =
((key: K, callback: ErrorCallback) => void) &
((key: K, options: O, callback: ErrorCallback) => void) &
((key: K, options?: O) => Promise<void>);
type Encoding = string | CustomEncoding;
type InferDBPut<DB> =
DB extends { put: (key: infer K, value: infer V, options: infer O, cb: any) => void } ?
LevelUpPut<K, V, O> :
LevelUpPut<any, any, AbstractOptions>;
interface Batch {
type: string;
key: any;
value?: any;
keyEncoding?: Encoding;
valueEncoding?: Encoding;
}
type InferDBGet<DB> =
DB extends { get: (key: infer K, options: infer O, callback: ErrorValueCallback<infer V>) => void } ?
LevelUpGet<K, V, O> :
LevelUpGet<any, any, AbstractGetOptions>;
interface LevelUpBase<BatchType extends Batch> {
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> =
DB extends { del: (key: infer K, options: infer O, callback: ErrorCallback) => void } ?
LevelUpDel<K, O> :
LevelUpDel<any, AbstractOptions>;
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<DB = AbstractLevelDOWN> extends EventEmitter {
open(): Promise<void>;
open(callback?: ErrorCallback): void;
close(): Promise<void>;
close(callback?: ErrorCallback): void;
put: InferDBPut<DB>;
get: InferDBGet<DB>;
del: InferDBDel<DB>;
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<void>;
batch(array: AbstractBatch[], options: any, callback: (err?: any) => any): void;
batch(array: AbstractBatch[], callback: (err?: any) => any): void;
type LevelUp = LevelUpBase<Batch>
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 extends AbstractLevelDOWN = AbstractLevelDOWN>(
db: DB,
options: any,
cb?: ErrorCallback): LevelUp<DB>;
<DB extends AbstractLevelDOWN = AbstractLevelDOWN>(
db: DB,
cb?: ErrorCallback): LevelUp<DB>;
new <DB extends AbstractLevelDOWN = AbstractLevelDOWN>(
db: DB,
options: any,
cb?: ErrorCallback): LevelUp<DB>;
new <DB extends AbstractLevelDOWN = AbstractLevelDOWN>(
db: DB,
cb?: ErrorCallback): LevelUp<DB>;
errors: /*typeof levelerrors*/ any; // ? level-errors is not in DT
}
export interface LevelUpChain<K = any, V = any> {
readonly length: number;
put(key: K, value: V): this;
del(key: K): this;
clear(): this;
write(callback: ErrorCallback): this;
write(): Promise<this>;
}
export const errors: /*typeof levelerrors*/ any; // ? level-errors is not in DT
declare const LevelUp: LevelUpConstructor;
export default LevelUp;
+26 -40
View File
@@ -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');
});
+2 -2
View File
@@ -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"
]
}
}
+1 -77
View File
@@ -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"
}
+82
View File
@@ -0,0 +1,82 @@
// Type definitions for LevelUp 1
// Project: https://github.com/Level/levelup
// Definitions by: Bret Little <https://github.com/blittle>, Thiago de Arruda <https://github.com/tarruda>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
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<BatchType extends Batch> {
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<Batch>
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;
}
}
+84
View File
@@ -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')
})
+31
View File
@@ -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"
]
}
+79
View File
@@ -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
}
}