diff --git a/pouchdb-tests.ts b/pouchdb-tests.ts index e17f9f9..8964a87 100644 --- a/pouchdb-tests.ts +++ b/pouchdb-tests.ts @@ -9,32 +9,34 @@ /// /// -module promise { - class Foo { - foo: string; - } +module PouchDBTest { + module promise { + class Foo { + foo: string; + } - class fakePromise implements pouchdb.async.Thenable { - new() { } + class fakePromise implements Promise { + new() { } - then(onFulfilled?: (value: T) => pouchdb.async.Thenable | R, onRejected?: (error: any) => pouchdb.async.Thenable | R) { - return new fakePromise(); - } + then(onFulfilled?: (value: T) => Promise | R, onRejected?: (error: any) => Promise | R) { + return new fakePromise(); + } - catch(onRejected: (error: any) => pouchdb.async.Thenable | R) { - return undefined; + catch(onRejected: (error: any) => Promise | R) { + return undefined; + } + + [Symbol.toStringTag]: string; } - } - function chainedThen() { - var fooOut: string; - new PouchDB("dbname") - .then((db) => new fakePromise()) - .then((value: Foo) => { fooOut = value.foo; }); + function chainedThen() { + var fooOut: string; + new PouchDB("dbname") + .then((db) => new fakePromise()) + .then((value: Foo) => { fooOut = value.foo; }); + } } -} -module PouchDBTest { module localDb { // common variables var dbt: pouchdb.thenable.PouchDB; diff --git a/pouchdb.d.ts b/pouchdb.d.ts index a454e21..2ae36f7 100644 --- a/pouchdb.d.ts +++ b/pouchdb.d.ts @@ -126,7 +126,7 @@ declare module pouchdb { } } /** - * Contains the standard pouchdb promises + * Contains the standard pouchdb promise utilities * @todo what is the error shape? looks like they contain status/reason/message and id(s)? */ module async { @@ -140,14 +140,16 @@ declare module pouchdb { message?: string; status?: number; } - /** An interface to represent a promise object */ - interface Thenable { + /** Overrides a supplied interface to represent a promise object with custom error typings for the first pass */ + export interface PouchPromise extends Promise { /** A Promises/A+ `then` implementation */ - then(onFulfilled?: (value: T) => Thenable|R, onRejected?: (error: Error) => Thenable|R): Thenable; + then(onFulfilled?: (value: T) => Promise | R, onRejected?: (error: pouchdb.async.Error) => Promise | R): Promise; + then(onFulfilled?: (value: T) => Promise | R, onRejected?: (error: pouchdb.async.Error) => void): Promise; /** `catch` implementation as per the pouchdb example docs */ - catch(onRejected: (error: Error) => Thenable|R): Thenable; + catch(onRejected?: (error: pouchdb.async.Error) => Promise | R): Promise; + catch(onRejected?: (error: pouchdb.async.Error) => void): Promise; } - /** Callback alternatives to promised */ + /** Callback alternatives to promises */ interface Callback { /** * @param error the error object @@ -336,15 +338,15 @@ declare module pouchdb { allDocs(options: FilterOptions, callback?: async.Callback): void; } /** Promise pattern for allDocs() */ - interface Promise { + interface Promisable { /** Fetch multiple documents, indexed and sorted by the `_id`. */ - allDocs(): async.Thenable; + allDocs(): async.PouchPromise; /** Fetch multiple documents, indexed and sorted by the `_id`. */ - allDocs(options: RangeOptions): async.Thenable; + allDocs(options: RangeOptions): async.PouchPromise; /** Fetch multiple documents, indexed and sorted by the `_id`. */ - allDocs(options: PaginationOptions): async.Thenable; + allDocs(options: PaginationOptions): async.PouchPromise; /** Fetch multiple documents, indexed and sorted by the `_id`. */ - allDocs(options: FilterOptions): async.Thenable; + allDocs(options: FilterOptions): async.PouchPromise; } } @@ -473,47 +475,47 @@ declare module pouchdb { bulkDocs(docs: MixedDoc[], options: BulkDocsOptions, callback?: async.Callback): void; } /** Promise pattern for bulkDocs() */ - interface Promise { + interface Promisable { /** * Update/Delete each doc in an array of documents. * @param folder the documents storage object * @param options * @todo define options shape - docs don't make it clear what this is */ - bulkDocs(folder: DocumentPouch, options?: BulkDocsOptions): async.Thenable; + bulkDocs(folder: DocumentPouch, options?: BulkDocsOptions): async.PouchPromise; /** * Update/Delete each doc in an array of documents. * @param doc the doc * @param options * @todo define options shape - docs don't make it clear what this is */ - bulkDocs(docs: ExistingDoc[], options?: BulkDocsOptions): async.Thenable; + bulkDocs(docs: ExistingDoc[], options?: BulkDocsOptions): async.PouchPromise; /** * Create multiple documents. * @param doc the doc * @param options */ - bulkDocs(folder: DocumentPouch): async.Thenable; + bulkDocs(folder: DocumentPouch): async.PouchPromise; /** * Create multiple documents. * @param doc the doc * @param options * @todo define options shape - docs don't make it clear what this is */ - bulkDocs(docs: NewDoc[], options?: BulkDocsOptions): async.Thenable; + bulkDocs(docs: NewDoc[], options?: BulkDocsOptions): async.PouchPromise; /** * Perform mixed Create/Update/Delete operations on multiple documents. * @param docs the documents to act on * @param options */ - bulkDocs(folder: DocumentPouch): async.Thenable; + bulkDocs(folder: DocumentPouch): async.PouchPromise; /** * Perform mixed Create/Update/Delete operations on multiple documents. * @param docs the documents to act on * @param options * @todo define options shape - docs don't make it clear what this is */ - bulkDocs(docs: MixedDoc[], options?: BulkDocsOptions): async.Thenable; + bulkDocs(docs: MixedDoc[], options?: BulkDocsOptions): async.PouchPromise; } } @@ -677,7 +679,7 @@ declare module pouchdb { /** Callback pattern for changes() */ interface Callback { } /** Promise pattern for changes() */ - interface Promise { } + interface Promisable { } } /** Contains the method and call/return types for close() */ @@ -688,9 +690,9 @@ declare module pouchdb { close(callback?: async.Callback): void; } /** Promise pattern for close() */ - interface Promise { + interface Promisable { /** Closes the pouchdb */ - close(): async.Thenable; + close(): async.PouchPromise; } } @@ -719,12 +721,12 @@ declare module pouchdb { /** * Promise pattern for destroy */ - interface Promise { + interface Promisable { /** * Deletes a database * @param options ajax options */ - destroy(options?: options.OptionsWithAjax): async.Thenable; + destroy(options?: options.OptionsWithAjax): async.PouchPromise; } } @@ -797,13 +799,13 @@ declare module pouchdb { get(docId: string, options: Options, callback?: async.Callback): void; } /** Promise pattern for remove */ - interface Promise { + interface Promisable { /** * Retrieves a document, specified by `docId`. * @param docId the doc id * @param options */ - get(docId: string, options?: Options): async.Thenable; + get(docId: string, options?: Options): async.PouchPromise; } } @@ -815,9 +817,9 @@ declare module pouchdb { id(callback?: async.Callback): void; } /** Promise pattern for `id()` */ - interface Promise { + interface Promisable { /** Returns the instance id for the pouchdb */ - id(): async.Thenable; + id(): async.PouchPromise; } } @@ -850,9 +852,9 @@ declare module pouchdb { info(callback?: async.Callback): void; } /** Promise pattern for `info()` */ - interface Promise { + interface Promisable { /** Returns the instance info for the pouchdb */ - info(): async.Thenable; + info(): async.PouchPromise; } } @@ -882,7 +884,7 @@ declare module pouchdb { post(doc: BaseDoc, options: options.EmptyOptions, callback?: async.Callback): void; } /** Promise pattern for post */ - interface Promise { + interface Promisable { /** * Create a new document and let PouchDB auto-generate an _id for it. * (tip: use `put()` instead for better indexing) @@ -890,7 +892,7 @@ declare module pouchdb { * @param options ajax options * @todo define options shape - docs don't make it clear what this is */ - post(doc: BaseDoc, options?: options.EmptyOptions): async.Thenable; + post(doc: BaseDoc, options?: options.EmptyOptions): async.PouchPromise; } } @@ -964,21 +966,21 @@ declare module pouchdb { put(doc: BaseDoc, docId: string, options: options.EmptyOptions, callback?: async.Callback): void; } /** Promise pattern for put */ - interface Promise { + interface Promisable { /** * Update an existing document. * @param doc the doc * @param options * @todo define options shape - docs don't make it clear what this is */ - put(doc: ExistingDoc, options?: options.EmptyOptions): async.Thenable; + put(doc: ExistingDoc, options?: options.EmptyOptions): async.PouchPromise; /** * Create a new document. * @param doc the doc * @param options * @todo define options shape - docs don't make it clear what this is */ - put(doc: NewDoc, options?: options.EmptyOptions): async.Thenable; + put(doc: NewDoc, options?: options.EmptyOptions): async.PouchPromise; /** * Update an existing document. * @param doc the doc @@ -987,7 +989,7 @@ declare module pouchdb { * @param options * @todo define options shape - docs don't make it clear what this is */ - put(doc: BaseDoc, docId: string, docRev: string, options?: options.EmptyOptions): async.Thenable; + put(doc: BaseDoc, docId: string, docRev: string, options?: options.EmptyOptions): async.PouchPromise; /** * Create a new document. If the document already exists, * you must use the update overload otherwise a conflict will occur. @@ -996,7 +998,7 @@ declare module pouchdb { * @param options * @todo define options shape - docs don't make it clear what this is */ - put(doc: BaseDoc, docId: string, options?: options.EmptyOptions): async.Thenable; + put(doc: BaseDoc, docId: string, options?: options.EmptyOptions): async.PouchPromise; } } @@ -1060,7 +1062,7 @@ declare module pouchdb { remove(doc: NewDoc, options: RevOptions, callback?: async.Callback): void; } /** Promise pattern for remove */ - interface Promise { + interface Promisable { /** * Deletes the document. * `doc` is required to be a document with at least an `_id` and a `_rev` property. @@ -1070,7 +1072,7 @@ declare module pouchdb { * @param options * @todo define options shape - docs don't make it clear what this is */ - remove(docId: string, docRev: string, options?: options.EmptyOptions): async.Thenable; + remove(docId: string, docRev: string, options?: options.EmptyOptions): async.PouchPromise; /** * Deletes the document. * `doc` is required to be a document with at least an `_id` and a `_rev` property. @@ -1079,14 +1081,14 @@ declare module pouchdb { * @param options * @todo define options shape - docs don't make it clear what this is */ - remove(doc: ExistingDoc, options?: options.EmptyOptions): async.Thenable; + remove(doc: ExistingDoc, options?: options.EmptyOptions): async.PouchPromise; /** * Deletes the document. * `doc` is required to be a document with at least an `_id` property, `rev` is specified in the `options`. * @param doc the doc (with only an `id` property) * @param options options that specify */ - remove(doc: NewDoc, options: RevOptions): async.Thenable; + remove(doc: NewDoc, options: RevOptions): async.PouchPromise; } } } @@ -1107,19 +1109,19 @@ declare module pouchdb { , methods.put.Callback , methods.remove.Callback { } /** pouchDB api: promise based */ - interface Promise extends + interface Promisable extends PouchInstance - , methods.allDocs.Promise - , methods.bulkDocs.Promise + , methods.allDocs.Promisable + , methods.bulkDocs.Promisable , methods.changes.Overloads - , methods.close.Promise - , methods.destroy.Promise - , methods.get.Promise - , methods.id.Promise - , methods.info.Promise - , methods.post.Promise - , methods.put.Promise - , methods.remove.Promise { } + , methods.close.Promisable + , methods.destroy.Promisable + , methods.get.Promisable + , methods.id.Promisable + , methods.info.Promisable + , methods.post.Promisable + , methods.put.Promisable + , methods.remove.Promisable { } } /** The main pouchDB interface */ @@ -1232,7 +1234,7 @@ declare module pouchdb { /** The api module for the pouchdb promise pattern */ module promise { /** The main pouchDB interface (promise pattern) */ - interface PouchDB extends api.db.Promise { } + interface PouchDB extends api.db.Promisable { } } /** The api module for the pouchdb promise pattern (constructor only) */ module thenable { @@ -1241,7 +1243,7 @@ declare module pouchdb { * Usually only a `pouchdb.promise.PouchDB` reference would be kept, assigned by * the `then` of the constructor. */ - interface PouchDB extends promise.PouchDB, async.Thenable { } + interface PouchDB extends promise.PouchDB, async.PouchPromise { } } /** Static-side interface for PouchDB */ @@ -1259,7 +1261,7 @@ declare module pouchdb { * Creates a new local pouchDb with the name specified and * all the default options * @param name the database name - * @returns a Thenable + * @returns a thenable.PouchDB */ new (name: string): thenable.PouchDB; /** @@ -1277,7 +1279,7 @@ declare module pouchdb { * Creates a new local SQLite pouchDb with the name and options provided * @param name the database name * @param options the SQlite database options - * @returns a Thenable + * @returns a thenable.PouchDB */ new (name: string, options: options.ctor.LocalSQLiteDb): thenable.PouchDB; /** @@ -1292,7 +1294,7 @@ declare module pouchdb { /** * Creates a new local SQLite pouchDb with the options provided * @param options the SQlite database options, including the name - * @returns a Thenable + * @returns a thenable.PouchDB */ new (options: options.ctor.LocalSQLiteDbWithName): thenable.PouchDB; /** @@ -1308,7 +1310,7 @@ declare module pouchdb { * Creates a new local WebSQL pouchDb with the name and options provided * @param name A string value that specifies the database name * @param options An object that specifies the local database options - * @returns a Thenable + * @returns a thenable.PouchDB */ new (name: string, options: options.ctor.LocalWebSQLDb): thenable.PouchDB; /** @@ -1323,7 +1325,7 @@ declare module pouchdb { /** * Creates a new local WebSQL pouchDb with the options provided * @param options An object that specifies the local database options - * @returns a Thenable + * @returns a thenable.PouchDB */ new (options: options.ctor.LocalWebSQLDbWithName): thenable.PouchDB; /** @@ -1339,7 +1341,7 @@ declare module pouchdb { * Creates a new local pouchDb with the name and options provided * @param name the database name * @param options the local database options - * @returns a Thenable + * @returns a thenable.PouchDB */ new (name: string, options: options.ctor.LocalDb): thenable.PouchDB; /** @@ -1354,7 +1356,7 @@ declare module pouchdb { /** * Creates a new local pouchDb with the options provided * @param options the local database options, including the name - * @returns a Thenable + * @returns a thenable.PouchDB */ new (options: options.ctor.LocalDbWithName): thenable.PouchDB; /** @@ -1370,12 +1372,14 @@ declare module pouchdb { * A fallback constructor if none of the typed constructors cover a use case * @todo if you find yourself using this, consider contributing a patch * to add/improve the necessary typed overload instead of `options: any` + * @returns a thenable.PouchDB */ new (name: string, options: any): thenable.PouchDB; /** * A fallback constructor if none of the typed constructors cover a use case * @todo if you find yourself using this, consider contributing a patch * to add/improve the necessary typed overload instead of `options: pouchdb.options.DbName` + * @returns a thenable.PouchDB */ new (options: options.ctor.DbName): thenable.PouchDB; }