Skip to content

Commit a93a54a

Browse files
committed
feat(headers): added (non-standard) toJSON to the Headers class
1 parent ee5c42c commit a93a54a

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ These are features in `fetch-h2`, that don't exist in the Fetch API. Some things
167167
* The `Request` class (options to `fetch`) has an extra property `allowForbiddenHeaders`, which defaults to `false`.
168168
* 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).
169169
* The response object has an extra property `httpVersion` which is either `1` or `2` (numbers), depending on what was negotiated with the server.
170+
* The `Headers` class (e.g. retried by `{response}.headers`) has a `toJSON` function which converts the headers to a simple JavaScript object.
170171

171172

172173
## Contexts

lib/headers.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,16 @@ export class Headers
237237
for ( const value of this._data.values( ) )
238238
yield value.join( "," );
239239
}
240+
241+
// This is non-standard, but useful
242+
public toJSON( )
243+
{
244+
return [ ...this.entries( ) ]
245+
.reduce( ( prev, [ key, val ] ) =>
246+
Object.assign( prev, { [ key ]: val } ),
247+
{ }
248+
);
249+
}
240250
}
241251

242252
export class GuardedHeaders extends Headers

0 commit comments

Comments
 (0)