diff --git a/src/index.ts b/src/index.ts index 015542956..e68dad7c5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,7 +11,6 @@ import { resolveRequestDocument } from './resolveRequestDocument.js' import type { BatchRequestDocument, FetchOptions, - GraphQLClientRequestHeaders, GraphQLClientResponse, HTTPMethodInput, JsonSerializer, @@ -37,7 +36,7 @@ import type { TypedDocumentNode } from '@graphql-typed-document-node/core' /** * Convert the given headers configuration into a plain object. */ -const resolveHeaders = (headers?: GraphQLClientRequestHeaders): Record => { +const resolveHeaders = (headers?: HeadersInit): Record => { let oHeaders: Record = {} if (headers) { if (headers instanceof Headers) { @@ -124,7 +123,7 @@ interface RequestVerbParams { fetch: Fetch fetchOptions: FetchOptions variables?: V - headers?: GraphQLClientRequestHeaders + headers?: HeadersInit operationName?: string middleware?: RequestMiddleware } @@ -297,11 +296,11 @@ class GraphQLClient { * Send GraphQL documents in batch to the server. */ // prettier-ignore - batchRequests(documents: BatchRequestDocument[], requestHeaders?: GraphQLClientRequestHeaders): Promise + batchRequests(documents: BatchRequestDocument[], requestHeaders?: HeadersInit): Promise // prettier-ignore batchRequests(options: BatchRequestsOptions): Promise // prettier-ignore - batchRequests(documentsOrOptions: BatchRequestDocument[] | BatchRequestsOptions, requestHeaders?: GraphQLClientRequestHeaders): Promise { + batchRequests(documentsOrOptions: BatchRequestDocument[] | BatchRequestsOptions, requestHeaders?: HeadersInit): Promise { const batchRequestOptions = parseBatchRequestArgs(documentsOrOptions, requestHeaders) const { headers, ...fetchOptions } = this.requestConfig @@ -343,7 +342,7 @@ class GraphQLClient { }) } - setHeaders(headers: GraphQLClientRequestHeaders): GraphQLClient { + setHeaders(headers: HeadersInit): GraphQLClient { this.requestConfig.headers = headers return this } @@ -378,7 +377,7 @@ const makeRequest = async (params: url: string query: string | string[] variables?: V - headers?: GraphQLClientRequestHeaders + headers?: HeadersInit operationName?: string fetch: Fetch method?: HTTPMethodInput @@ -431,13 +430,13 @@ const makeRequest = async (params: // prettier-ignore interface RawRequestMethod { - (query: string, variables?: V, requestHeaders?: GraphQLClientRequestHeaders): Promise> + (query: string, variables?: V, requestHeaders?: HeadersInit): Promise> (options: RawRequestOptions): Promise> } // prettier-ignore type RawRequestMethodArgs = - | [query: string, variables?: V, requestHeaders?: GraphQLClientRequestHeaders] + | [query: string, variables?: V, requestHeaders?: HeadersInit] | [RawRequestOptions] // prettier-ignore @@ -563,12 +562,12 @@ type BatchResult = [Result, ...Result[]] // prettier-ignore interface BatchRequests { - (url: string, documents: BatchRequestDocument[], requestHeaders?: GraphQLClientRequestHeaders): Promise + (url: string, documents: BatchRequestDocument[], requestHeaders?: HeadersInit): Promise (options: BatchRequestsExtendedOptions): Promise } type BatchRequestsArgs = - | [url: string, documents: BatchRequestDocument[], requestHeaders?: GraphQLClientRequestHeaders] + | [url: string, documents: BatchRequestDocument[], requestHeaders?: HeadersInit] | [options: BatchRequestsExtendedOptions] const parseBatchRequestsArgsExtended = (args: BatchRequestsArgs): BatchRequestsExtendedOptions => { @@ -674,7 +673,6 @@ export { BatchRequestsOptions, ClientError, GraphQLClient, - GraphQLClientRequestHeaders, rawRequest, RawRequestExtendedOptions, RawRequestOptions, diff --git a/src/parseArgs.ts b/src/parseArgs.ts index 49a5a81e0..474735bc8 100644 --- a/src/parseArgs.ts +++ b/src/parseArgs.ts @@ -1,7 +1,6 @@ import type { BatchRequestDocument, BatchRequestsOptions, - GraphQLClientRequestHeaders, RawRequestExtendedOptions, RawRequestOptions, RequestDocument, @@ -14,7 +13,7 @@ import type { export const parseRequestArgs = ( documentOrOptions: RequestDocument | RequestOptions, variables?: V, - requestHeaders?: GraphQLClientRequestHeaders, + requestHeaders?: HeadersInit, ): RequestOptions => { return (documentOrOptions as RequestOptions).document ? (documentOrOptions as RequestOptions) @@ -29,7 +28,7 @@ export const parseRequestArgs = ( export const parseRawRequestArgs = ( queryOrOptions: string | RawRequestOptions, variables?: V, - requestHeaders?: GraphQLClientRequestHeaders, + requestHeaders?: HeadersInit, ): RawRequestOptions => { return (queryOrOptions as RawRequestOptions).query ? (queryOrOptions as RawRequestOptions) @@ -43,7 +42,7 @@ export const parseRawRequestArgs = ( export const parseBatchRequestArgs = ( documentsOrOptions: BatchRequestDocument[] | BatchRequestsOptions, - requestHeaders?: GraphQLClientRequestHeaders, + requestHeaders?: HeadersInit, ): BatchRequestsOptions => { return (documentsOrOptions as BatchRequestsOptions).documents ? (documentsOrOptions as BatchRequestsOptions) diff --git a/src/types.ts b/src/types.ts index b4b7775a2..71a394801 100644 --- a/src/types.ts +++ b/src/types.ts @@ -93,7 +93,7 @@ export type HTTPMethodInput = 'GET' | 'POST' | 'get' | 'post' export interface RequestConfig extends Omit, AdditionalRequestOptions { fetch?: Fetch method?: HTTPMethodInput - headers?: MaybeLazy + headers?: MaybeLazy requestMiddleware?: RequestMiddleware responseMiddleware?: ResponseMiddleware jsonSerializer?: JsonSerializer @@ -106,7 +106,7 @@ export type BatchRequestDocument = { export type RawRequestOptions = { query: string - requestHeaders?: GraphQLClientRequestHeaders + requestHeaders?: HeadersInit signal?: RequestInit['signal'] } & (V extends Record ? { variables?: V } @@ -116,7 +116,7 @@ export type RawRequestOptions = { export type RequestOptions = { document: RequestDocument | TypedDocumentNode - requestHeaders?: GraphQLClientRequestHeaders + requestHeaders?: HeadersInit signal?: RequestInit['signal'] } & (V extends Record ? { variables?: V } @@ -126,7 +126,7 @@ export type RequestOptions = { export interface BatchRequestsOptions { documents: BatchRequestDocument[] - requestHeaders?: GraphQLClientRequestHeaders + requestHeaders?: HeadersInit signal?: RequestInit['signal'] } @@ -154,14 +154,10 @@ type RequestExtendedInit = RequestInit & { variables?: V } -// TODO: Replace this type with the built-in `HeadersInit` type. -// See: https://github.com/jasonkuhrt/graphql-request/issues/608 -export type GraphQLClientRequestHeaders = Headers | string[][] | Record - // prettier-ignore export type VariablesAndRequestHeadersArgs = V extends Record // do we have explicitly no variables allowed? - ? [variables?: V, requestHeaders?: GraphQLClientRequestHeaders] + ? [variables?: V, requestHeaders?: HeadersInit] : keyof RemoveIndex extends never // do we get an empty variables object? - ? [variables?: V, requestHeaders?: GraphQLClientRequestHeaders] - : [variables: V, requestHeaders?: GraphQLClientRequestHeaders] + ? [variables?: V, requestHeaders?: HeadersInit] + : [variables: V, requestHeaders?: HeadersInit] diff --git a/tests/headers.test.ts b/tests/headers.test.ts index 445068d3d..c297084ad 100644 --- a/tests/headers.test.ts +++ b/tests/headers.test.ts @@ -38,7 +38,7 @@ describe(`using class`, () => { }) describe(`custom header in the request`, () => { - describe.each([ + describe.each([ [new Headers({ 'x-request-foo': `request-bar` })], [{ 'x-request-foo': `request-bar` }], [[[`x-request-foo`, `request-bar`]]], @@ -66,7 +66,7 @@ describe(`using class`, () => { }) }) - describe.each([ + describe.each([ [new Headers({ 'x-foo': `request-bar` })], [{ 'x-foo': `request-bar` }], [[[`x-foo`, `request-bar`]]], @@ -129,7 +129,7 @@ describe(`using class`, () => { }) describe(`using request function`, () => { - describe.each([ + describe.each([ [new Headers({ 'x-request-foo': `request-bar` })], [{ 'x-request-foo': `request-bar` }], [[[`x-request-foo`, `request-bar`]]],