Skip to content

Commit ae870c1

Browse files
authored
Request headers types (#2879)
* Mention iterable in request headers type * Update type tests with iterable headers * Mention iterables in documentation
1 parent 2a27dc1 commit ae870c1

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

docs/docs/api/Dispatcher.md

+39-3
Original file line numberDiff line numberDiff line change
@@ -855,10 +855,12 @@ Emitted when dispatcher is no longer busy.
855855

856856
## Parameter: `UndiciHeaders`
857857

858-
* `Record<string, string | string[] | undefined> | string[] | null`
859-
860-
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<string, string | string[] | undefined>` (`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.
858+
* `Record<string, string | string[] | undefined> | string[] | Iterable<[string, string | string[] | undefined]> | null`
861859

860+
Header arguments such as `options.headers` in [`Client.dispatch`](Client.md#clientdispatchoptions-handlers) can be specified in three forms:
861+
* As an object specified by the `Record<string, string | string[] | undefined>` (`IncomingHttpHeaders`) type.
862+
* As an array of strings. An array representation of a header list must have an even length, or an `InvalidArgumentError` will be thrown.
863+
* As an iterable that can encompass `Headers`, `Map`, or a custom iterator returning key-value pairs.
862864
Keys are lowercase and values are not modified.
863865

864866
Response headers will derive a `host` from the `url` of the [Client](Client.md#class-client) instance if no `host` header was previously specified.
@@ -886,3 +888,37 @@ Response headers will derive a `host` from the `url` of the [Client](Client.md#c
886888
'accept', '*/*'
887889
]
888890
```
891+
892+
### Example 3 - Iterable
893+
894+
```js
895+
new Headers({
896+
'content-length': '123',
897+
'content-type': 'text/plain',
898+
connection: 'keep-alive',
899+
host: 'mysite.com',
900+
accept: '*/*'
901+
})
902+
```
903+
or
904+
```js
905+
new Map([
906+
['content-length', '123'],
907+
['content-type', 'text/plain'],
908+
['connection', 'keep-alive'],
909+
['host', 'mysite.com'],
910+
['accept', '*/*']
911+
])
912+
```
913+
or
914+
```js
915+
{
916+
*[Symbol.iterator] () {
917+
yield ['content-length', '123']
918+
yield ['content-type', 'text/plain']
919+
yield ['connection', 'keep-alive']
920+
yield ['host', 'mysite.com']
921+
yield ['accept', '*/*']
922+
}
923+
}
924+
```

test/types/dispatcher.test-d.ts

+11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ expectAssignable<Dispatcher>(new Dispatcher())
1515
['content-type']: 'application/json'
1616
} satisfies IncomingHttpHeaders;
1717

18+
const headerInstanceHeaders = new Headers({ hello: 'world' })
19+
const mapHeaders = new Map([['hello', 'world']])
20+
const iteratorHeaders = {
21+
*[Symbol.iterator]() {
22+
yield ['hello', 'world']
23+
}
24+
}
25+
1826
// dispatch
1927
expectAssignable<boolean>(dispatcher.dispatch({ path: '', method: 'GET' }, {}))
2028
expectAssignable<boolean>(dispatcher.dispatch({ origin: '', path: '', method: 'GET' }, {}))
@@ -23,6 +31,9 @@ expectAssignable<Dispatcher>(new Dispatcher())
2331
expectAssignable<boolean>(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: {} }, {}))
2432
expectAssignable<boolean>(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: nodeCoreHeaders }, {}))
2533
expectAssignable<boolean>(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: null, reset: true }, {}))
34+
expectAssignable<boolean>(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: headerInstanceHeaders, reset: true }, {}))
35+
expectAssignable<boolean>(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: mapHeaders, reset: true }, {}))
36+
expectAssignable<boolean>(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: iteratorHeaders, reset: true }, {}))
2637
expectAssignable<boolean>(dispatcher.dispatch({ origin: new URL('http://localhost'), path: '', method: 'GET' }, {}))
2738

2839
// connect

types/dispatcher.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ declare namespace Dispatcher {
100100
/** Default: `null` */
101101
body?: string | Buffer | Uint8Array | Readable | null | FormData;
102102
/** Default: `null` */
103-
headers?: IncomingHttpHeaders | string[] | null;
103+
headers?: IncomingHttpHeaders | string[] | Iterable<[string, string | string[] | undefined]> | null;
104104
/** Query string params to be embedded in the request URL. Default: `null` */
105105
query?: Record<string, any>;
106106
/** 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`. */

0 commit comments

Comments
 (0)