Search code examples
couchdbpouchdb

Add custom header when create new remote database with pouchdb


I'm testing to send custom headers when creating a remote database with PouchDB. The fact is that according to the documentation the way to proceed is as follows (https://pouchdb.com/api.html):

var db = new PouchDB('http://example.com/dbname', {
  fetch: function (url, opts) {
    opts.headers.set('X-Some-Special-Header', 'foo');
    return PouchDB.fetch(url, opts);
  }
});

My code is almost copied and pasted

const db = new PouchDB(this.server, {
  fetch(url, opts) {
    opts.headers.set('xx-custom-xx', this.text);
    opts.credentials = 'include';
    return PouchDB.fetch(url, opts);
  }
});

When I try to launch it I get two errors:

  • Error1: "export 'fetch' (imported as 'PouchDB') was not found in 'pouchdb' (possible exports: default)" This error is being given by the line"return PouchDB.fetch(url, opts); "
  • Error2: "set does not exist on type headersinit"

I hope someone can help me. Thanks.


Solution

  • I get it.. the problem are two.

    First, I was importing Pouch like this

    import * as PouchDB from 'pouchdb'; 
    

    I have notice in other project I imported it by

    import PouchDB from 'pouchdb';
    

    In my new project with import PouchDB from 'pouchdb'; didn't work because I didn't set "allowSyntheticDefaultImports":true flag in tsconfig

    AllowSyntheticDefaultImports flag is specified in doc but It does't work (no for me) with import * as PouchDB from 'pouchdb';