Skip to content

Commit

Permalink
fix: Add the headers object to the rawRequest's result
Browse files Browse the repository at this point in the history
Add the headers object to the rawRequest's result
  • Loading branch information
schickling authored Apr 27, 2018
2 parents 4624856 + 1f8d7fb commit d365229
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ClientError, GraphQLError, Headers, Options, Variables } from './types'
import { ClientError, GraphQLError, Headers as HttpHeaders, Options, Variables } from './types'
export { ClientError } from './types'
import 'cross-fetch/polyfill'

Expand All @@ -14,7 +14,7 @@ export class GraphQLClient {
async rawRequest<T extends any>(
query: string,
variables?: Variables,
): Promise<{ data?: T, extensions?: any, errors?: GraphQLError[] }> {
): Promise<{ data?: T, extensions?: any, headers: Headers, errors?: GraphQLError[] }> {
const { headers, ...others } = this.options

const body = JSON.stringify({
Expand All @@ -32,12 +32,12 @@ export class GraphQLClient {
const result = await getResult(response)

if (response.ok && !result.errors && result.data) {
return result
return { ...result, headers: response.headers }
} else {
const errorResult =
typeof result === 'string' ? { error: result } : result
throw new ClientError(
{ ...errorResult, status: response.status },
{ ...errorResult, status: response.status, headers: response.headers },
{ query, variables },
)
}
Expand Down Expand Up @@ -75,7 +75,7 @@ export class GraphQLClient {
}
}

setHeaders(headers: Headers): GraphQLClient {
setHeaders(headers: HttpHeaders): GraphQLClient {
this.options.headers = headers

return this
Expand All @@ -97,7 +97,7 @@ export async function rawRequest<T extends any>(
url: string,
query: string,
variables?: Variables,
): Promise<{ data?: T, extensions?: any, errors?: GraphQLError[] }> {
): Promise<{ data?: T, extensions?: any, headers: Headers, errors?: GraphQLError[] }> {
const client = new GraphQLClient(url)

return client.rawRequest<T>(query, variables)
Expand Down
26 changes: 25 additions & 1 deletion tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,31 @@ test('minimal raw query', async (t) => {
}

await mock({body: {data, extensions}}, async () => {
t.deepEqual(await rawRequest('https://mock-api.com/graphql', `{ viewer { id } }`), {data, extensions})
const { headers, ...result } = await rawRequest('https://mock-api.com/graphql', `{ viewer { id } }`)
t.deepEqual(result, {data, extensions})
})
})

test('minimal raw query with response headers', async (t) => {
const data = {
viewer: {
id: 'some-id',
},
}

const extensions = {
version: '1',
}

const reqHeaders = {
'Content-Type': 'application/json',
'X-Custom-Header': 'test-custom-header',
}

await mock({headers: reqHeaders, body: {data, extensions}}, async () => {
const { headers, ...result } = await rawRequest('https://mock-api.com/graphql', `{ viewer { id } }`)
t.deepEqual(result, {data, extensions})
t.deepEqual(headers.get('X-Custom-Header'), reqHeaders['X-Custom-Header'])
})
})

Expand Down

0 comments on commit d365229

Please sign in to comment.