diff --git a/docs/docs/api/Dispatcher.md b/docs/docs/api/Dispatcher.md index d9f66d17d81..ec0565316ab 100644 --- a/docs/docs/api/Dispatcher.md +++ b/docs/docs/api/Dispatcher.md @@ -194,7 +194,7 @@ Returns: `Boolean` - `false` if dispatcher is busy and further dispatch calls wo * **method** `string` * **reset** `boolean` (optional) - Default: `false` - If `false`, the request will attempt to create a long-living connection by sending the `connection: keep-alive` header,otherwise will attempt to close it immediately after response by sending `connection: close` within the request and closing the socket afterwards. * **body** `string | Buffer | Uint8Array | stream.Readable | Iterable | AsyncIterable | null` (optional) - Default: `null` -* **headers** `UndiciHeaders | string[]` (optional) - Default: `null`. +* **headers** `UndiciHeaders` (optional) - Default: `null`. * **query** `Record | null` (optional) - Default: `null` - Query string params to be embedded in the request URL. Note that both keys and values of query are encoded using `encodeURIComponent`. If for some reason you need to send them unencoded, embed query params into path directly instead. * **idempotent** `boolean` (optional) - Default: `true` if `method` is `'HEAD'` or `'GET'` - Whether the requests can be safely retried or not. If `false` the request won't be sent until all preceding requests in the pipeline has completed. * **blocking** `boolean` (optional) - Default: `method !== 'HEAD'` - Whether the response is expected to take a long time and would end up blocking the pipeline. When this is set to `true` further pipelining will be avoided on the same connection until headers have been received. diff --git a/test/types/dispatcher.test-d.ts b/test/types/dispatcher.test-d.ts index 77f5320c1c4..4f26dc0adf1 100644 --- a/test/types/dispatcher.test-d.ts +++ b/test/types/dispatcher.test-d.ts @@ -22,15 +22,23 @@ expectAssignable(new Dispatcher()) yield ['hello', 'world'] } } + const recordHeadersString: Record = { hello: 'world' } + const recordHeadersStringArray: Record = { foo: ['hello', 'world'] } + const recordHeadersUndefined: Record = { bar: undefined } // dispatch expectAssignable(dispatcher.dispatch({ path: '', method: 'GET' }, {})) expectAssignable(dispatcher.dispatch({ origin: '', path: '', method: 'GET' }, {})) expectAssignable(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: { authorization: undefined } }, {})) expectAssignable(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: [] }, {})) + expectAssignable(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: ['hello', 'world'] }, {})) expectAssignable(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: {} }, {})) expectAssignable(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: nodeCoreHeaders }, {})) expectAssignable(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: null, reset: true }, {})) + expectAssignable(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: undefined, reset: true }, {})) + expectAssignable(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: recordHeadersString, reset: true }, {})) + expectAssignable(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: recordHeadersStringArray, reset: true }, {})) + expectAssignable(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: recordHeadersUndefined, reset: true }, {})) expectAssignable(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: headerInstanceHeaders, reset: true }, {})) expectAssignable(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: mapHeaders, reset: true }, {})) expectAssignable(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: iteratorHeaders, reset: true }, {})) @@ -50,6 +58,19 @@ expectAssignable(new Dispatcher()) })) expectAssignable>(dispatcher.connect({ origin: '', path: '', responseHeaders: 'raw' })) expectAssignable>(dispatcher.connect({ origin: '', path: '', responseHeaders: null })) + expectAssignable>(dispatcher.connect({ origin: '', path: '', headers: { authorization: undefined } })) + expectAssignable>(dispatcher.connect({ origin: '', path: '', headers: [] })) + expectAssignable>(dispatcher.connect({ origin: '', path: '', headers: ['hello', 'world'] })) + expectAssignable>(dispatcher.connect({ origin: '', path: '', headers: {} })) + expectAssignable>(dispatcher.connect({ origin: '', path: '', headers: nodeCoreHeaders })) + expectAssignable>(dispatcher.connect({ origin: '', path: '', headers: null })) + expectAssignable>(dispatcher.connect({ origin: '', path: '', headers: undefined })) + expectAssignable>(dispatcher.connect({ origin: '', path: '', headers: recordHeadersString })) + expectAssignable>(dispatcher.connect({ origin: '', path: '', headers: recordHeadersStringArray })) + expectAssignable>(dispatcher.connect({ origin: '', path: '', headers: recordHeadersUndefined })) + expectAssignable>(dispatcher.connect({ origin: '', path: '', headers: headerInstanceHeaders })) + expectAssignable>(dispatcher.connect({ origin: '', path: '', headers: mapHeaders })) + expectAssignable>(dispatcher.connect({ origin: '', path: '', headers: iteratorHeaders })) // request expectAssignable>(dispatcher.request({ origin: '', path: '', method: 'GET', maxRedirections: 0 })) @@ -156,6 +177,19 @@ expectAssignable(new Dispatcher()) })) expectAssignable>(dispatcher.upgrade({ path: '', responseHeaders: 'raw' })) expectAssignable>(dispatcher.upgrade({ path: '', responseHeaders: null })) + expectAssignable>(dispatcher.upgrade({ path: '', method: 'GET', headers: { authorization: undefined } })) + expectAssignable>(dispatcher.upgrade({ path: '', method: 'GET', headers: [] })) + expectAssignable>(dispatcher.upgrade({ path: '', method: 'GET', headers: ['hello', 'world'] })) + expectAssignable>(dispatcher.upgrade({ path: '', method: 'GET', headers: {} })) + expectAssignable>(dispatcher.upgrade({ path: '', method: 'GET', headers: nodeCoreHeaders })) + expectAssignable>(dispatcher.upgrade({ path: '', method: 'GET', headers: null })) + expectAssignable>(dispatcher.upgrade({ path: '', method: 'GET', headers: undefined })) + expectAssignable>(dispatcher.upgrade({ path: '', method: 'GET', headers: recordHeadersString })) + expectAssignable>(dispatcher.upgrade({ path: '', method: 'GET', headers: recordHeadersStringArray })) + expectAssignable>(dispatcher.upgrade({ path: '', method: 'GET', headers: recordHeadersUndefined })) + expectAssignable>(dispatcher.upgrade({ path: '', method: 'GET', headers: headerInstanceHeaders })) + expectAssignable>(dispatcher.upgrade({ path: '', method: 'GET', headers: mapHeaders })) + expectAssignable>(dispatcher.upgrade({ path: '', method: 'GET', headers: iteratorHeaders })) // close expectAssignable>(dispatcher.close()) diff --git a/types/dispatcher.d.ts b/types/dispatcher.d.ts index 17bb44166fa..0584e36f565 100644 --- a/types/dispatcher.d.ts +++ b/types/dispatcher.d.ts @@ -12,6 +12,8 @@ type AbortSignal = unknown export default Dispatcher +export type UndiciHeaders = Record | IncomingHttpHeaders | string[] | Iterable<[string, string | string[] | undefined]> | null + /** Dispatcher is the core API used to dispatch requests. */ declare class Dispatcher extends EventEmitter { /** Dispatches a request. This API is expected to evolve through semver-major versions and is less stable than the preceding higher level APIs. It is primarily intended for library developers who implement higher level APIs on top of this. */ @@ -103,7 +105,7 @@ declare namespace Dispatcher { /** Default: `null` */ body?: string | Buffer | Uint8Array | Readable | null | FormData; /** Default: `null` */ - headers?: Record | IncomingHttpHeaders | string[] | Iterable<[string, string | string[] | undefined]> | null; + headers?: UndiciHeaders; /** Query string params to be embedded in the request URL. Default: `null` */ query?: Record; /** Whether the requests can be safely retried or not. If `false` the request won't be sent until all preceding requests in the pipeline have completed. Default: `true` if `method` is `HEAD` or `GET`. */ @@ -127,7 +129,7 @@ declare namespace Dispatcher { origin: string | URL; path: string; /** Default: `null` */ - headers?: IncomingHttpHeaders | string[] | null; + headers?: UndiciHeaders; /** Default: `null` */ signal?: AbortSignal | EventEmitter | null; /** This argument parameter is passed through to `ConnectData` */ @@ -164,7 +166,7 @@ declare namespace Dispatcher { /** Default: `'GET'` */ method?: string; /** Default: `null` */ - headers?: IncomingHttpHeaders | string[] | null; + headers?: UndiciHeaders; /** A string of comma separated protocols, in descending preference order. Default: `'Websocket'` */ protocol?: string; /** Default: `null` */