From 514d6d0ec215226c16d1c8889b6c197950e8d6f6 Mon Sep 17 00:00:00 2001 From: Brian Birtles Date: Sat, 17 Feb 2018 15:17:13 +0900 Subject: [PATCH] Fix the return type of pouchdb-core's bulkDocs to include Error values As per [1]: > The response contains an array of the familiar ok/rev/id from the > put()/post() API. If there are any errors, they will be provided > individually Demo: https://square-shape.glitch.me/ [1] https://pouchdb.com/api.html#batch_create --- types/pouchdb-core/index.d.ts | 4 ++-- types/pouchdb-core/pouchdb-core-tests.ts | 11 ++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/types/pouchdb-core/index.d.ts b/types/pouchdb-core/index.d.ts index 59e5650e62..01c7a0045c 100644 --- a/types/pouchdb-core/index.d.ts +++ b/types/pouchdb-core/index.d.ts @@ -594,7 +594,7 @@ declare namespace PouchDB { */ bulkDocs(docs: Array>, options: Core.BulkDocsOptions | null, - callback: Core.Callback): void; + callback: Core.Callback>): void; /** * Create, update or delete multiple documents. The docs argument is an array of documents. @@ -604,7 +604,7 @@ declare namespace PouchDB { * Finally, to delete a document, include a _deleted parameter with the value true. */ bulkDocs(docs: Array>, - options?: Core.BulkDocsOptions): Promise; + options?: Core.BulkDocsOptions): Promise>; /** Compact the database */ compact(options?: Core.CompactOptions): Promise; diff --git a/types/pouchdb-core/pouchdb-core-tests.ts b/types/pouchdb-core/pouchdb-core-tests.ts index 79a151aa3a..fe79997461 100644 --- a/types/pouchdb-core/pouchdb-core-tests.ts +++ b/types/pouchdb-core/pouchdb-core-tests.ts @@ -47,10 +47,19 @@ function testBulkDocs() { const model = { property: 'test' }; const model2 = { property: 'test' }; + const isError = ( + result: PouchDB.Core.Response | PouchDB.Core.Error + ): result is PouchDB.Core.Error => { + return !!( result).error; + }; + db.bulkDocs([model, model2]).then((result) => { - result.forEach(({ ok, id, rev }) => { + result.forEach(result => { + if (!isError(result)) { + const { ok, id, rev } = result; isString(id); isString(rev); + } }); });