Skip to content

Commit

Permalink
update docs & fix type
Browse files Browse the repository at this point in the history
  • Loading branch information
tsctx committed Dec 20, 2023
1 parent 7b0d7ce commit 0803473
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 23 deletions.
4 changes: 2 additions & 2 deletions docs/api/Util.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ 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. But, if **headers** is an object, it is not used.
- **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[]>`
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)`

Expand Down
10 changes: 0 additions & 10 deletions test/types/util.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@ expectAssignable<Record<string, string | string[]>>(
util.parseHeaders({ 'content-type': 'text/plain' }, {})
);

expectAssignable<Record<string, string | string[]>>(
util.parseHeaders(
{ 'content-type': 'text/plain' },
//@ts-ignore
{
'content-length': 0,
}
)
);

expectAssignable<Record<string, string | string[]>>(
util.parseHeaders({} as Record<string, string> | string[], {})
);
Expand Down
16 changes: 5 additions & 11 deletions types/util.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,15 @@ export namespace util {
/**
* Receives a header object and returns the parsed value.
* @param headers Header object
* @param obj Object to specify a proxy object. But, if `headers` is an object, it is not used.
* @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)[])[],
T extends Record<string, unknown>
| (Buffer | string | (Buffer | string)[])[]
>(
headers: H,
obj?: H extends any[] ? T : never
): T extends Record<string, infer V>
? unknown extends V
? Record<string, string | string[]>
: H extends any[]
? Record<string, string | string[] | V>
: Record<string, string | string[]>
: never;
obj?: H extends any[] ? Record<string, string | string[]> : never
): Record<string, string | string[]>;
}

0 comments on commit 0803473

Please sign in to comment.