Skip to content

Commit

Permalink
feat: passthrough headers on exported request function (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
TJTorola authored May 12, 2021
1 parent 2940c49 commit 8587e4d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,11 @@ export async function rawRequest<T = any, V = Variables>(
export async function request<T = any, V = Variables>(
url: string,
document: RequestDocument,
variables?: V
variables?: V,
requestHeaders?: Dom.RequestInit['headers']
): Promise<T> {
const client = new GraphQLClient(url)
return client.request<T, V>(document, variables)
return client.request<T, V>(document, variables, requestHeaders)
}

export default request
Expand Down
17 changes: 16 additions & 1 deletion tests/headers.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as CrossFetch from 'cross-fetch'
import * as Dom from '../src/types.dom'
import { GraphQLClient } from '../src'
import { GraphQLClient, request } from '../src'
import { setupTestServer } from './__helpers'

const ctx = setupTestServer()
Expand Down Expand Up @@ -94,3 +94,18 @@ describe('using class', () => {
})
})
})

describe('using request function', () => {
describe.each([
[new H({ 'x-request-foo': 'request-bar' })],
[{ 'x-request-foo': 'request-bar' }],
[[['x-request-foo', 'request-bar']]]
])('request unique header with request', (headerCase: Dom.RequestInit['headers']) => {
test('sets header', async () => {
const mock = ctx.res()
await request(ctx.url, `{ me { id } }`, {}, headerCase)

expect(mock.requests[0].headers['x-request-foo']).toEqual('request-bar')
});
})
})

0 comments on commit 8587e4d

Please sign in to comment.