From a9dabd54ae1a7894e65d1da13e103edb22be9444 Mon Sep 17 00:00:00 2001 From: pan93412 Date: Sun, 29 Jan 2023 16:43:16 +0800 Subject: [PATCH] Correct the type of DispatchOptions["headers"] (#1896) --- docs/api/Dispatcher.md | 12 ++++++------ types/dispatcher.d.ts | 2 +- types/errors.d.ts | 2 +- types/header.d.ts | 4 ++++ types/mock-interceptor.d.ts | 2 +- types/proxy-agent.d.ts | 3 +-- 6 files changed, 14 insertions(+), 11 deletions(-) create mode 100644 types/header.d.ts diff --git a/docs/api/Dispatcher.md b/docs/api/Dispatcher.md index 6bb97b47208..25c980d612b 100644 --- a/docs/api/Dispatcher.md +++ b/docs/api/Dispatcher.md @@ -74,7 +74,7 @@ Returns: `void | Promise` - Only returns a `Promise` if no `callbac #### Parameter: `ConnectData` * **statusCode** `number` -* **headers** `http.IncomingHttpHeaders` +* **headers** `Record` * **socket** `stream.Duplex` * **opaque** `unknown` @@ -383,7 +383,7 @@ Extends: [`RequestOptions`](#parameter-requestoptions) #### Parameter: PipelineHandlerData * **statusCode** `number` -* **headers** `IncomingHttpHeaders` +* **headers** `Record` * **opaque** `unknown` * **body** `stream.Readable` * **context** `object` @@ -477,7 +477,7 @@ The `RequestOptions.method` property should not be value `'CONNECT'`. #### Parameter: `ResponseData` * **statusCode** `number` -* **headers** `http.IncomingHttpHeaders` - Note that all header keys are lower-cased, e. g. `content-type`. +* **headers** `Record` - Note that all header keys are lower-cased, e. g. `content-type`. * **body** `stream.Readable` which also implements [the body mixin from the Fetch Standard](https://fetch.spec.whatwg.org/#body-mixin). * **trailers** `Record` - This object starts out as empty and will be mutated to contain trailers after `body` has emitted `'end'`. @@ -644,7 +644,7 @@ Returns: `void | Promise` - Only returns a `Promise` if no `callback #### Parameter: `StreamFactoryData` * **statusCode** `number` -* **headers** `http.IncomingHttpHeaders` +* **headers** `Record` * **opaque** `unknown` * **onInfo** `({statusCode: number, headers: Record}) => void | null` (optional) - Default: `null` - Callback collecting all the info headers (HTTP 100-199) received. @@ -853,9 +853,9 @@ Emitted when dispatcher is no longer busy. ## Parameter: `UndiciHeaders` -* `http.IncomingHttpHeaders | string[] | null` +* `Record | string[] | null` -Header arguments such as `options.headers` in [`Client.dispatch`](Client.md#clientdispatchoptions-handlers) can be specified in two forms; either as an object specified by the `http.IncomingHttpHeaders` type, or an array of strings. An array representation of a header list must have an even length or an `InvalidArgumentError` will be thrown. +Header arguments such as `options.headers` in [`Client.dispatch`](Client.md#clientdispatchoptions-handlers) can be specified in two forms; either as an object specified by the `Record` (`IncomingHttpHeaders`) type, or an array of strings. An array representation of a header list must have an even length or an `InvalidArgumentError` will be thrown. Keys are lowercase and values are not modified. diff --git a/types/dispatcher.d.ts b/types/dispatcher.d.ts index 05a2ab5e76e..2e427a73dd4 100644 --- a/types/dispatcher.d.ts +++ b/types/dispatcher.d.ts @@ -1,8 +1,8 @@ import { URL } from 'url' import { Duplex, Readable, Writable } from 'stream' import { EventEmitter } from 'events' -import { IncomingHttpHeaders } from 'http' import { Blob } from 'buffer' +import { IncomingHttpHeaders } from './header' import BodyReadable from './readable' import { FormData } from './formdata' import Errors from './errors' diff --git a/types/errors.d.ts b/types/errors.d.ts index c4ef4b6f633..f05fae1e5b3 100644 --- a/types/errors.d.ts +++ b/types/errors.d.ts @@ -1,4 +1,4 @@ -import {IncomingHttpHeaders} from "http"; +import { IncomingHttpHeaders } from "./header"; import Client from './client' export default Errors diff --git a/types/header.d.ts b/types/header.d.ts new file mode 100644 index 00000000000..3fd9483e27b --- /dev/null +++ b/types/header.d.ts @@ -0,0 +1,4 @@ +/** + * The header type declaration of `undici`. + */ +export type IncomingHttpHeaders = Record; diff --git a/types/mock-interceptor.d.ts b/types/mock-interceptor.d.ts index 9dc423c0600..6b3961c0482 100644 --- a/types/mock-interceptor.d.ts +++ b/types/mock-interceptor.d.ts @@ -1,4 +1,4 @@ -import { IncomingHttpHeaders } from 'http' +import { IncomingHttpHeaders } from './header' import Dispatcher from './dispatcher'; import { BodyInit, Headers } from './fetch' diff --git a/types/proxy-agent.d.ts b/types/proxy-agent.d.ts index 55058734f78..d312cb3f9a5 100644 --- a/types/proxy-agent.d.ts +++ b/types/proxy-agent.d.ts @@ -1,8 +1,7 @@ -import { IncomingHttpHeaders } from 'http' - import Agent from './agent' import buildConnector from './connector'; import Dispatcher from './dispatcher' +import { IncomingHttpHeaders } from './header' export default ProxyAgent