klaemo opened a new issue, #380:
URL: https://github.com/apache/couchdb-nano/issues/380
## Expected Behavior
The types in `lib/nano.d.ts` should describe what nano 11 accepts and
produces at runtime:
1. `Configuration.agentOptions` should accept an undici Dispatcher
**instance** (`new Agent(...)`, `new MockAgent()`), because nano passes it
unchanged to `fetch` as `dispatcher` (`cfg.agent = cfg.agentOptions`, then
`fetchOptions.dispatcher = cfg.agent`). That is also what the README example
and `test/nano.agent.test.js` do.
2. `RequestError.request` should describe the object that nano attaches to
errors: the `fetchOptions` that `relax()` builds, with credentials scrubbed.
## Current Behavior
1. `agentOptions` is typed `AgentOptions | typeof undici.Agent | typeof
undici.MockAgent | typeof undici.Dispatcher`. Those are constructor types, so:
- `nano({ url, agentOptions: new Agent({ connections: 10 }) })` fails
with TS2322, and so does `new MockAgent()`. These are the values that work at
runtime.
- `nano({ url, agentOptions: Agent })` (the class) typechecks, but every
request then fails with `TypeError: agent.dispatch is not a function`.
- A plain `AgentOptions` object (e.g. `{ bodyTimeout: 1000 }`) fails at
runtime with the same `agent.dispatch is not a function`.
2. `RequestError.request` is typed `{ method?: 'GET' | 'POST' | 'PUT' |
'DELETE'; headers?; uri?: string; qs?: any }`, which looks like the shape from
the `request` era. In v11 there is no `uri` and no `qs`: the query string is
part of `url`, and `method` is lower case. This is what a 404 actually contains:
```js
{
url: 'http://localhost:39187/db/doc?revs_info=true',
method: 'get',
headers: { accept: 'application/json', /* ... */ Authorization:
'XXXXXXX', 'content-type': 'application/json' },
credentials: 'include',
body: undefined,
redirect: 'error',
signal: undefined,
bodyTimeout: 0,
dispatcher: Agent { /* only when agentOptions is set */ }
}
```
(`RequestError.headers` does contain a `uri`, which may be where the
confusion came from.)
## Possible Solution
Type-only changes in `lib/nano.d.ts`:
- `agentOptions?: Dispatcher` (instance type, via `import type { Dispatcher
} from "undici"`)
- `request?: { url?; method?: string; headers?; body?; credentials?;
redirect?; signal?; bodyTimeout?; dispatcher? }`
I have a PR ready that does this, together with the missing `parseUrl` from
#362.
Making the runtime accept a plain `AgentOptions` object (by building an
`undici.Agent` from it) would need undici as a runtime dependency, so I left
that out.
## Steps to Reproduce (for bugs)
```ts
import Nano from 'nano'
import { Agent, MockAgent } from 'undici'
const url = 'http://localhost:5984'
Nano({ url, agentOptions: new Agent({ connections: 10 }) }) // TS2322, works
at runtime
Nano({ url, agentOptions: new MockAgent() }) // TS2322, works
at runtime
Nano({ url, agentOptions: Agent }) // compiles;
runtime "agent.dispatch is not a function"
try {
await Nano(url).db.use('db').get('missing', { revs_info: true })
} catch (e) {
const err = e as Nano.RequestError
err.request?.uri // typed string, always undefined
err.request?.qs // typed any, always undefined
err.request?.url // TS2339, but this is where the URL is
}
```
## Context
Found while upgrading an app from nano 10 to 11.0.7. Error logging read
`err.request.uri` and got `undefined`, and passing an `Agent` instance needed a
cast.
## Your Environment
* Version used: nano 11.0.7 (same on `main` at 53dea92), TypeScript 6.0.3,
undici 7.29.0
* Node.js v24.21.0
* Operating System and version: Linux
* Link to your project: private app
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]