1- import { generateErrorFromResponse } from 'pouchdb-errors' ;
1+ import { createError , generateErrorFromResponse } from 'pouchdb-errors' ;
22import { Headers } from 'pouchdb-fetch' ;
33import massageCreateIndexRequest from '../../massageCreateIndexRequest' ;
44import validateSelector from '../../validateSelector' ;
55
6- function dbFetch ( db , path , opts , callback ) {
7- var status , ok ;
8- opts . headers = new Headers ( { 'Content-type' : 'application/json' } ) ;
9- db . fetch ( path , opts ) . then ( function ( response ) {
10- status = response . status ;
11- ok = response . ok ;
12- return response . json ( ) ;
13- } ) . then ( function ( json ) {
14- if ( ! ok ) {
15- json . status = status ;
16- var err = generateErrorFromResponse ( json ) ;
17- callback ( err ) ;
18- } else {
19- callback ( null , json ) ;
20- }
21- } ) . catch ( callback ) ;
6+ async function dbFetch ( db , path , opts ) {
7+ if ( opts . body ) {
8+ opts . body = JSON . stringify ( opts . body ) ;
9+ opts . headers = new Headers ( { 'Content-type' : 'application/json' } ) ;
10+ }
11+
12+ const response = await db . fetch ( path , opts ) ;
13+ const json = await response . json ( ) ;
14+ if ( ! response . ok ) {
15+ json . status = response . status ;
16+ const pouchError = createError ( json ) ;
17+ throw generateErrorFromResponse ( pouchError ) ;
18+ }
19+ return json ;
2220}
2321
2422function createIndex ( db , requestDef , callback ) {
25- requestDef = massageCreateIndexRequest ( requestDef ) ;
2623 dbFetch ( db , '_index' , {
2724 method : 'POST' ,
28- body : JSON . stringify ( requestDef )
29- } , callback ) ;
25+ body : massageCreateIndexRequest ( requestDef )
26+ } )
27+ . then ( ( result ) => {
28+ callback ( null , result ) ;
29+ } )
30+ . catch ( callback ) ;
3031}
3132
3233function find ( db , requestDef , callback ) {
3334 validateSelector ( requestDef . selector , true ) ;
3435 dbFetch ( db , '_find' , {
3536 method : 'POST' ,
36- body : JSON . stringify ( requestDef )
37- } , callback ) ;
37+ body : requestDef
38+ } )
39+ . then ( ( result ) => {
40+ callback ( null , result ) ;
41+ } )
42+ . catch ( callback ) ;
3843}
3944
4045function explain ( db , requestDef , callback ) {
4146 dbFetch ( db , '_explain' , {
4247 method : 'POST' ,
43- body : JSON . stringify ( requestDef )
44- } , callback ) ;
48+ body : requestDef
49+ } )
50+ . then ( ( result ) => {
51+ callback ( null , result ) ;
52+ } )
53+ . catch ( callback ) ;
4554}
4655
4756function getIndexes ( db , callback ) {
48- dbFetch ( db , '_index' , {
49- method : 'GET'
50- } , callback ) ;
57+ return dbFetch ( db , '_index' , {
58+ method : 'GET' ,
59+ } )
60+ . then ( ( result ) => {
61+ callback ( null , result ) ;
62+ } )
63+ . catch ( callback ) ;
5164}
5265
5366function deleteIndex ( db , indexDef , callback ) {
54-
55-
56- var ddoc = indexDef . ddoc ;
57- var type = indexDef . type || 'json' ;
58- var name = indexDef . name ;
67+ const ddoc = indexDef . ddoc ;
68+ const type = indexDef . type || 'json' ;
69+ const name = indexDef . name ;
5970
6071 if ( ! ddoc ) {
6172 return callback ( new Error ( 'you must provide an index\'s ddoc' ) ) ;
@@ -65,9 +76,13 @@ function deleteIndex(db, indexDef, callback) {
6576 return callback ( new Error ( 'you must provide an index\'s name' ) ) ;
6677 }
6778
68- var url = '_index/' + [ ddoc , type , name ] . map ( encodeURIComponent ) . join ( '/' ) ;
79+ const url = '_index/' + [ ddoc , type , name ] . map ( encodeURIComponent ) . join ( '/' ) ;
6980
70- dbFetch ( db , url , { method : 'DELETE' } , callback ) ;
81+ dbFetch ( db , url , { method : 'DELETE' } )
82+ . then ( ( result ) => {
83+ callback ( null , result ) ;
84+ } )
85+ . catch ( callback ) ;
7186}
7287
7388export {
0 commit comments