Skip to content

Commit

Permalink
feat: allow all of HeadersInit for HttpRequestOptions.headers
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Jan 9, 2024
1 parent e9dffe0 commit a5fe73c
Show file tree
Hide file tree
Showing 19 changed files with 173 additions and 132 deletions.
5 changes: 2 additions & 3 deletions docs/interfaces/ClientCredentialsGrantRequestOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ ___

### headers

`Optional` **headers**: [`Headers`]( https://developer.mozilla.org/docs/Web/API/Headers )
`Optional` **headers**: [`Record`]( https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type )\<`string`, `string`\> \| [`string`, `string`][] \| [`Headers`]( https://developer.mozilla.org/docs/Web/API/Headers )

A Headers instance to additionally send with the HTTP Request(s) triggered by this function's
invocation.
Headers to additionally send with the HTTP Request(s) triggered by this function's invocation.

___

Expand Down
5 changes: 2 additions & 3 deletions docs/interfaces/DeviceAuthorizationRequestOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ ___

### headers

`Optional` **headers**: [`Headers`]( https://developer.mozilla.org/docs/Web/API/Headers )
`Optional` **headers**: [`Record`]( https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type )\<`string`, `string`\> \| [`string`, `string`][] \| [`Headers`]( https://developer.mozilla.org/docs/Web/API/Headers )

A Headers instance to additionally send with the HTTP Request(s) triggered by this function's
invocation.
Headers to additionally send with the HTTP Request(s) triggered by this function's invocation.

___

Expand Down
5 changes: 2 additions & 3 deletions docs/interfaces/DiscoveryRequestOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ ___

### headers

`Optional` **headers**: [`Headers`]( https://developer.mozilla.org/docs/Web/API/Headers )
`Optional` **headers**: [`Record`]( https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type )\<`string`, `string`\> \| [`string`, `string`][] \| [`Headers`]( https://developer.mozilla.org/docs/Web/API/Headers )

A Headers instance to additionally send with the HTTP Request(s) triggered by this function's
invocation.
Headers to additionally send with the HTTP Request(s) triggered by this function's invocation.

___

Expand Down
5 changes: 2 additions & 3 deletions docs/interfaces/HttpRequestOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@

### headers

`Optional` **headers**: [`Headers`]( https://developer.mozilla.org/docs/Web/API/Headers )
`Optional` **headers**: [`Record`]( https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type )\<`string`, `string`\> \| [`string`, `string`][] \| [`Headers`]( https://developer.mozilla.org/docs/Web/API/Headers )

A Headers instance to additionally send with the HTTP Request(s) triggered by this function's
invocation.
Headers to additionally send with the HTTP Request(s) triggered by this function's invocation.

___

Expand Down
5 changes: 2 additions & 3 deletions docs/interfaces/IntrospectionRequestOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ ___

### headers

`Optional` **headers**: [`Headers`]( https://developer.mozilla.org/docs/Web/API/Headers )
`Optional` **headers**: [`Record`]( https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type )\<`string`, `string`\> \| [`string`, `string`][] \| [`Headers`]( https://developer.mozilla.org/docs/Web/API/Headers )

A Headers instance to additionally send with the HTTP Request(s) triggered by this function's
invocation.
Headers to additionally send with the HTTP Request(s) triggered by this function's invocation.

___

Expand Down
5 changes: 2 additions & 3 deletions docs/interfaces/PushedAuthorizationRequestOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ ___

### headers

`Optional` **headers**: [`Headers`]( https://developer.mozilla.org/docs/Web/API/Headers )
`Optional` **headers**: [`Record`]( https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type )\<`string`, `string`\> \| [`string`, `string`][] \| [`Headers`]( https://developer.mozilla.org/docs/Web/API/Headers )

A Headers instance to additionally send with the HTTP Request(s) triggered by this function's
invocation.
Headers to additionally send with the HTTP Request(s) triggered by this function's invocation.

___

Expand Down
5 changes: 2 additions & 3 deletions docs/interfaces/RevocationRequestOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ ___

### headers

`Optional` **headers**: [`Headers`]( https://developer.mozilla.org/docs/Web/API/Headers )
`Optional` **headers**: [`Record`]( https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type )\<`string`, `string`\> \| [`string`, `string`][] \| [`Headers`]( https://developer.mozilla.org/docs/Web/API/Headers )

A Headers instance to additionally send with the HTTP Request(s) triggered by this function's
invocation.
Headers to additionally send with the HTTP Request(s) triggered by this function's invocation.

___

Expand Down
5 changes: 2 additions & 3 deletions docs/interfaces/TokenEndpointRequestOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ ___

### headers

`Optional` **headers**: [`Headers`]( https://developer.mozilla.org/docs/Web/API/Headers )
`Optional` **headers**: [`Record`]( https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type )\<`string`, `string`\> \| [`string`, `string`][] \| [`Headers`]( https://developer.mozilla.org/docs/Web/API/Headers )

A Headers instance to additionally send with the HTTP Request(s) triggered by this function's
invocation.
Headers to additionally send with the HTTP Request(s) triggered by this function's invocation.

___

Expand Down
5 changes: 2 additions & 3 deletions docs/interfaces/UserInfoRequestOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ ___

### headers

`Optional` **headers**: [`Headers`]( https://developer.mozilla.org/docs/Web/API/Headers )
`Optional` **headers**: [`Record`]( https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type )\<`string`, `string`\> \| [`string`, `string`][] \| [`Headers`]( https://developer.mozilla.org/docs/Web/API/Headers )

A Headers instance to additionally send with the HTTP Request(s) triggered by this function's
invocation.
Headers to additionally send with the HTTP Request(s) triggered by this function's invocation.

___

Expand Down
13 changes: 5 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -727,11 +727,8 @@ export interface HttpRequestOptions {
*/
signal?: (() => AbortSignal) | AbortSignal

/**
* A Headers instance to additionally send with the HTTP Request(s) triggered by this function's
* invocation.
*/
headers?: Headers
/** Headers to additionally send with the HTTP Request(s) triggered by this function's invocation. */
headers?: [string, string][] | Record<string, string> | Headers
}

export interface DiscoveryRequestOptions extends HttpRequestOptions {
Expand Down Expand Up @@ -762,9 +759,9 @@ function isJsonObject<T = JsonObject>(input: unknown): input is T {
return true
}

function prepareHeaders(input: unknown): Headers {
if (input !== undefined && !(input instanceof Headers)) {
throw new TypeError('"options.headers" must be an instance of Headers')
function prepareHeaders(input?: [string, string][] | Record<string, string> | Headers): Headers {
if (input instanceof Headers) {
input = Object.fromEntries(input.entries())
}
const headers = new Headers(input)
if (USER_AGENT && !headers.has('user-agent')) {
Expand Down
38 changes: 22 additions & 16 deletions test/authorization_code.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,23 +213,29 @@ test('authorizationCodeGrantRequest() w/ Custom Headers', async (t) => {
},
})
.reply(200, { access_token: 'token', token_type: 'Bearer' })
.times(3)

await t.notThrowsAsync(
lib.authorizationCodeGrantRequest(
tIssuer,
tClient,
cb('code=authorization_code'),
'redirect_uri',
'verifier',
{
headers: new Headers([
['accept', 'will be overwritten'],
['user-agent', 'foo'],
['foo', 'bar'],
]),
},
),
)
const entries = [
['accept', 'will be overwritten'],
['user-agent', 'foo'],
['foo', 'bar'],
]
for (const headers of [
entries,
Object.fromEntries(entries),
new Headers(Object.fromEntries(entries)),
]) {
await t.notThrowsAsync(
lib.authorizationCodeGrantRequest(
tIssuer,
tClient,
cb('code=authorization_code'),
'redirect_uri',
'verifier',
{ headers },
),
)
}
})

test('authorizationCodeGrantRequest() w/ DPoP', async (t) => {
Expand Down
24 changes: 15 additions & 9 deletions test/client_credentials.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,22 @@ test('clientCredentialsGrantRequest() w/ Custom Headers', async (t) => {
},
})
.reply(200, { access_token: 'token', token_type: 'Bearer' })
.times(3)

await t.notThrowsAsync(
lib.clientCredentialsGrantRequest(tIssuer, tClient, new URLSearchParams(), {
headers: new Headers([
['accept', 'will be overwritten'],
['user-agent', 'foo'],
['foo', 'bar'],
]),
}),
)
const entries = [
['accept', 'will be overwritten'],
['user-agent', 'foo'],
['foo', 'bar'],
]
for (const headers of [
entries,
Object.fromEntries(entries),
new Headers(Object.fromEntries(entries)),
]) {
await t.notThrowsAsync(
lib.clientCredentialsGrantRequest(tIssuer, tClient, new URLSearchParams(), { headers }),
)
}
})

test('clientCredentialsGrantRequest() w/ DPoP', async (t) => {
Expand Down
46 changes: 28 additions & 18 deletions test/device_flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,22 @@ test('deviceAuthorizationRequest() w/ Custom Headers', async (t) => {
},
})
.reply(200, '')
.times(3)

await t.notThrowsAsync(
lib.deviceAuthorizationRequest(tIssuer, tClient, new URLSearchParams(), {
headers: new Headers([
['accept', 'will be overwritten'],
['user-agent', 'foo'],
['foo', 'bar'],
]),
}),
)
const entries = [
['accept', 'will be overwritten'],
['user-agent', 'foo'],
['foo', 'bar'],
]
for (const headers of [
entries,
Object.fromEntries(entries),
new Headers(Object.fromEntries(entries)),
]) {
await t.notThrowsAsync(
lib.deviceAuthorizationRequest(tIssuer, tClient, new URLSearchParams(), { headers }),
)
}
})

test('processDeviceAuthorizationResponse()', async (t) => {
Expand Down Expand Up @@ -320,16 +326,20 @@ test('deviceCodeGrantRequest() w/ Custom Headers', async (t) => {
},
})
.reply(200, { access_token: 'token', token_type: 'Bearer' })
.times(3)

await t.notThrowsAsync(
lib.deviceCodeGrantRequest(tIssuer, tClient, 'device_code', {
headers: new Headers([
['accept', 'will be overwritten'],
['user-agent', 'foo'],
['foo', 'bar'],
]),
}),
)
const entries = [
['accept', 'will be overwritten'],
['user-agent', 'foo'],
['foo', 'bar'],
]
for (const headers of [
entries,
Object.fromEntries(entries),
new Headers(Object.fromEntries(entries)),
]) {
await t.notThrowsAsync(lib.deviceCodeGrantRequest(tIssuer, tClient, 'device_code', { headers }))
}
})

test('deviceCodeGrantRequest() w/ DPoP', async (t) => {
Expand Down
24 changes: 15 additions & 9 deletions test/discovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,21 @@ test('discoveryRequest() w/ Custom Headers', async (t) => {
},
})
.reply(200, data)

const response = await lib.discoveryRequest(new URL(issuer.issuer), {
headers: new Headers([
['accept', 'will be overwritten'],
['user-agent', 'foo'],
['foo', 'bar'],
]),
})
t.true(response instanceof Response)
.times(3)

const entries = [
['accept', 'will be overwritten'],
['user-agent', 'foo'],
['foo', 'bar'],
]
for (const headers of [
entries,
Object.fromEntries(entries),
new Headers(Object.fromEntries(entries)),
]) {
const response = await lib.discoveryRequest(new URL(issuer.issuer), { headers })
t.true(response instanceof Response)
}
})

test('discoveryRequest() - oidc with a pathname', async (t) => {
Expand Down
22 changes: 13 additions & 9 deletions test/introspection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,20 @@ test('introspectionRequest() w/ Custom Headers', async (t) => {
},
})
.reply(200, { access_token: 'token', token_type: 'Bearer' })
.times(3)

await t.notThrowsAsync(
lib.introspectionRequest(tIssuer, tClient, 'token', {
headers: new Headers([
['accept', 'will be overwritten'],
['user-agent', 'foo'],
['foo', 'bar'],
]),
}),
)
const entries = [
['accept', 'will be overwritten'],
['user-agent', 'foo'],
['foo', 'bar'],
]
for (const headers of [
entries,
Object.fromEntries(entries),
new Headers(Object.fromEntries(entries)),
]) {
await t.notThrowsAsync(lib.introspectionRequest(tIssuer, tClient, 'token', { headers }))
}
})

test('introspectionRequest() forced jwt', async (t) => {
Expand Down
25 changes: 15 additions & 10 deletions test/par.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,21 @@ test('pushedAuthorizationRequest() w/ Custom Headers', async (t) => {
},
})
.reply(200, { request_uri: 'urn:example:uri', expires_in: 60 })

await t.notThrowsAsync(
lib.pushedAuthorizationRequest(tIssuer, tClient, new URLSearchParams(), {
headers: new Headers([
['accept', 'will be overwritten'],
['user-agent', 'foo'],
['foo', 'bar'],
]),
}),
)
.times(3)
const entries = [
['accept', 'will be overwritten'],
['user-agent', 'foo'],
['foo', 'bar'],
]
for (const headers of [
entries,
Object.fromEntries(entries),
new Headers(Object.fromEntries(entries)),
]) {
await t.notThrowsAsync(
lib.pushedAuthorizationRequest(tIssuer, tClient, new URLSearchParams(), { headers }),
)
}
})

test('pushedAuthorizationRequest() w/ DPoP', async (t) => {
Expand Down

0 comments on commit a5fe73c

Please sign in to comment.