apache / couchdb-nano

Nano: The official Apache CouchDB library for Node.js
https://www.npmjs.com/package/nano
Apache License 2.0
651 stars 165 forks source link

requestDefaults behavior has changed in 9.0.x #240

Closed dsebastien closed 3 years ago

dsebastien commented 3 years ago

Expected Behavior

Expected the following to still compile / work using nano with TypeScript:

export const databaseConnection: ServerScope = nano({
  url: environment.databaseConfiguration.url,
  requestDefaults: {
   ...
    auth: {
      username: environment.databaseConfiguration.username,
      password: environment.databaseConfiguration.password,
    },
  },
});

Current Behavior

With 9.0.x installed, the above code fails to compile because the auth key does not exist anymore. After this commit: https://github.com/apache/couchdb-nano/commit/8655d40d213a3a3c2bd73d64953ce065628cbb12#diff-2dd1f9d8d0579eec00aede543000fb3cd7ee90d47953c9f4d2fb07353f7fa59eR19

There is a new interface:

interface requestDefaultsOptions {
    timeout: number;
    agent: any;
    headers: object;
  }

With a weird naming convention (camelCase instead of PascalCase) and with very basic types. The auth option does not seem to exist anymore and I couldn't find a lot of information about this change. Is it still possible to pass these somehow?

Possible Solution

It would be nice for the new requestDefaults interface to be more complete and more clearly indicate which options can be passed (parameter names / types).

In any case, being able to pass the authentication information once is also nice to have.

Steps to Reproduce (for bugs)

Upgrade to nano 9.0.x & compile using TypeScript

Context

Cannot centrally manage authentication for the nano http client.

Your Environment

glynnbird commented 3 years ago

As version 9.x of Nano doesn't use the request library anymore then the requestDefaults configuration is not the same. As it happens, axios & request both accept a "auth" object, so this code does work:

const Nano = require('.')
const nano = Nano({url: 'http://localhost:5984', requestDefaults: { auth: { username: 'MYUSER', password: 'MYPASS'}}})
nano.db.list().then(console.log)

so "it doesn't compile" is purely a Typescript thing easily solved by adding to the requestDefaultsOptions interface.

dsebastien commented 3 years ago

Good news then. Thanks for looking into this.