Skip to content

Commit

Permalink
feat(headers): added (non-standard) toJSON to the Headers class
Browse files Browse the repository at this point in the history
  • Loading branch information
grantila committed Aug 20, 2020
1 parent ee5c42c commit a93a54a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ These are features in `fetch-h2`, that don't exist in the Fetch API. Some things
* The `Request` class (options to `fetch`) has an extra property `allowForbiddenHeaders`, which defaults to `false`.
* The `Response` class also has an extra property `allowForbiddenHeaders`, which defaults to `false` (or to the value of the `Request` if it was constructed through a `fetch` call, which is the common case).
* The response object has an extra property `httpVersion` which is either `1` or `2` (numbers), depending on what was negotiated with the server.
* The `Headers` class (e.g. retried by `{response}.headers`) has a `toJSON` function which converts the headers to a simple JavaScript object.


## Contexts
Expand Down
10 changes: 10 additions & 0 deletions lib/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,16 @@ export class Headers
for ( const value of this._data.values( ) )
yield value.join( "," );
}

// This is non-standard, but useful
public toJSON( )
{
return [ ...this.entries( ) ]
.reduce( ( prev, [ key, val ] ) =>
Object.assign( prev, { [ key ]: val } ),
{ }
);
}
}

export class GuardedHeaders extends Headers
Expand Down

0 comments on commit a93a54a

Please sign in to comment.