-
Notifications
You must be signed in to change notification settings - Fork 524
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
103 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Util | ||
|
||
Utility API for third-party implementations of the dispatcher API. | ||
|
||
## `parseHeaders(headers, [obj])` | ||
|
||
Receives a header object and returns the parsed value. | ||
|
||
Arguments: | ||
|
||
- **headers** `Record<string, string | string[]> | (Buffer | string | (Buffer | string)[])[]` (required) - Header object. | ||
|
||
- **obj** `Record<string, string | string[]>` (optional) - Object to specify a proxy object. The parsed value is assigned to this object. But, if **headers** is an object, it is not used. | ||
|
||
Returns: `Record<string, string | string[]>` If **headers** is an object, it is **headers**. Otherwise, if **obj** is specified, it is equivalent to **obj**. | ||
|
||
## `headerNameToString(value)` | ||
|
||
Retrieves a header name and returns its lowercase value. | ||
|
||
Arguments: | ||
|
||
- **value** `string | Buffer` (required) - Header name. | ||
|
||
Returns: `string` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { expectAssignable } from 'tsd'; | ||
import { util } from '../../types/util'; | ||
|
||
expectAssignable<Record<string, string | string[]>>( | ||
util.parseHeaders({ 'content-type': 'text/plain' }) | ||
); | ||
|
||
expectAssignable<Record<string, string | string[]>>( | ||
//@ts-ignore | ||
util.parseHeaders({ 'content-type': 'text/plain' }, {}) | ||
); | ||
|
||
expectAssignable<Record<string, string | string[]>>( | ||
util.parseHeaders({} as Record<string, string> | string[], {}) | ||
); | ||
|
||
expectAssignable<Record<string, string | string[]>>( | ||
util.parseHeaders(['content-type', 'text/plain']) | ||
); | ||
|
||
expectAssignable<Record<string, string | string[]>>( | ||
util.parseHeaders([Buffer.from('content-type'), Buffer.from('text/plain')]) | ||
); | ||
|
||
expectAssignable<Record<string, string | string[]>>( | ||
util.parseHeaders( | ||
[Buffer.from('content-type'), Buffer.from('text/plain')], | ||
{} | ||
) | ||
); | ||
|
||
expectAssignable<Record<string, string | string[]>>( | ||
util.parseHeaders([Buffer.from('content-type'), [Buffer.from('text/plain')]]) | ||
); | ||
|
||
expectAssignable<string>(util.headerNameToString('content-type')); | ||
|
||
expectAssignable<string>(util.headerNameToString(Buffer.from('content-type'))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export namespace util { | ||
/** | ||
* Retrieves a header name and returns its lowercase value. | ||
* @param value Header name | ||
*/ | ||
export function headerNameToString(value: string | Buffer): string; | ||
|
||
/** | ||
* Receives a header object and returns the parsed value. | ||
* @param headers Header object | ||
*/ | ||
export function parseHeaders( | ||
headers: | ||
| Record<string, string | string[]> | ||
| (Buffer | string | (Buffer | string)[])[] | ||
): Record<string, string | string[]>; | ||
/** | ||
* Receives a header object and returns the parsed value. | ||
* @param headers Header object | ||
* @param obj Object to specify a proxy object. Used to assign parsed values. But, if `headers` is an object, it is not used. | ||
* @returns If `headers` is an object, it is `headers`. Otherwise, if `obj` is specified, it is equivalent to `obj`. | ||
*/ | ||
export function parseHeaders< | ||
H extends | ||
| Record<string, string | string[]> | ||
| (Buffer | string | (Buffer | string)[])[] | ||
>( | ||
headers: H, | ||
obj?: H extends any[] ? Record<string, string | string[]> : never | ||
): Record<string, string | string[]>; | ||
} |