Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: replace GraphQLClientRequestHeaders with built-in HeadersInit type #616

Merged
merged 1 commit into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { resolveRequestDocument } from './resolveRequestDocument.js'
import type {
BatchRequestDocument,
FetchOptions,
GraphQLClientRequestHeaders,
GraphQLClientResponse,
HTTPMethodInput,
JsonSerializer,
Expand All @@ -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<string, string> => {
const resolveHeaders = (headers?: HeadersInit): Record<string, string> => {
let oHeaders: Record<string, string> = {}
if (headers) {
if (headers instanceof Headers) {
Expand Down Expand Up @@ -124,7 +123,7 @@ interface RequestVerbParams<V extends Variables = Variables> {
fetch: Fetch
fetchOptions: FetchOptions
variables?: V
headers?: GraphQLClientRequestHeaders
headers?: HeadersInit
operationName?: string
middleware?: RequestMiddleware<V>
}
Expand Down Expand Up @@ -297,11 +296,11 @@ class GraphQLClient {
* Send GraphQL documents in batch to the server.
*/
// prettier-ignore
batchRequests<T extends BatchResult, V extends Variables = Variables>(documents: BatchRequestDocument<V>[], requestHeaders?: GraphQLClientRequestHeaders): Promise<T>
batchRequests<T extends BatchResult, V extends Variables = Variables>(documents: BatchRequestDocument<V>[], requestHeaders?: HeadersInit): Promise<T>
// prettier-ignore
batchRequests<T extends BatchResult, V extends Variables = Variables>(options: BatchRequestsOptions<V>): Promise<T>
// prettier-ignore
batchRequests<T extends BatchResult, V extends Variables = Variables>(documentsOrOptions: BatchRequestDocument<V>[] | BatchRequestsOptions<V>, requestHeaders?: GraphQLClientRequestHeaders): Promise<T> {
batchRequests<T extends BatchResult, V extends Variables = Variables>(documentsOrOptions: BatchRequestDocument<V>[] | BatchRequestsOptions<V>, requestHeaders?: HeadersInit): Promise<T> {
const batchRequestOptions = parseBatchRequestArgs<V>(documentsOrOptions, requestHeaders)
const { headers, ...fetchOptions } = this.requestConfig

Expand Down Expand Up @@ -343,7 +342,7 @@ class GraphQLClient {
})
}

setHeaders(headers: GraphQLClientRequestHeaders): GraphQLClient {
setHeaders(headers: HeadersInit): GraphQLClient {
this.requestConfig.headers = headers
return this
}
Expand Down Expand Up @@ -378,7 +377,7 @@ const makeRequest = async <T = unknown, V extends Variables = Variables>(params:
url: string
query: string | string[]
variables?: V
headers?: GraphQLClientRequestHeaders
headers?: HeadersInit
operationName?: string
fetch: Fetch
method?: HTTPMethodInput
Expand Down Expand Up @@ -431,13 +430,13 @@ const makeRequest = async <T = unknown, V extends Variables = Variables>(params:

// prettier-ignore
interface RawRequestMethod {
<T, V extends Variables = Variables>(query: string, variables?: V, requestHeaders?: GraphQLClientRequestHeaders): Promise<GraphQLClientResponse<T>>
<T, V extends Variables = Variables>(query: string, variables?: V, requestHeaders?: HeadersInit): Promise<GraphQLClientResponse<T>>
<T, V extends Variables = Variables>(options: RawRequestOptions<V>): Promise<GraphQLClientResponse<T>>
}

// prettier-ignore
type RawRequestMethodArgs<V extends Variables> =
| [query: string, variables?: V, requestHeaders?: GraphQLClientRequestHeaders]
| [query: string, variables?: V, requestHeaders?: HeadersInit]
| [RawRequestOptions<V>]

// prettier-ignore
Expand Down Expand Up @@ -563,12 +562,12 @@ type BatchResult = [Result, ...Result[]]

// prettier-ignore
interface BatchRequests {
<T extends BatchResult, V extends Variables = Variables>(url: string, documents: BatchRequestDocument<V>[], requestHeaders?: GraphQLClientRequestHeaders): Promise<T>
<T extends BatchResult, V extends Variables = Variables>(url: string, documents: BatchRequestDocument<V>[], requestHeaders?: HeadersInit): Promise<T>
<T extends BatchResult, V extends Variables = Variables>(options: BatchRequestsExtendedOptions<V>): Promise<T>
}

type BatchRequestsArgs =
| [url: string, documents: BatchRequestDocument[], requestHeaders?: GraphQLClientRequestHeaders]
| [url: string, documents: BatchRequestDocument[], requestHeaders?: HeadersInit]
| [options: BatchRequestsExtendedOptions]

const parseBatchRequestsArgsExtended = (args: BatchRequestsArgs): BatchRequestsExtendedOptions => {
Expand Down Expand Up @@ -674,7 +673,6 @@ export {
BatchRequestsOptions,
ClientError,
GraphQLClient,
GraphQLClientRequestHeaders,
rawRequest,
RawRequestExtendedOptions,
RawRequestOptions,
Expand Down
7 changes: 3 additions & 4 deletions src/parseArgs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type {
BatchRequestDocument,
BatchRequestsOptions,
GraphQLClientRequestHeaders,
RawRequestExtendedOptions,
RawRequestOptions,
RequestDocument,
Expand All @@ -14,7 +13,7 @@ import type {
export const parseRequestArgs = <V extends Variables = Variables>(
documentOrOptions: RequestDocument | RequestOptions<V>,
variables?: V,
requestHeaders?: GraphQLClientRequestHeaders,
requestHeaders?: HeadersInit,
): RequestOptions<V> => {
return (documentOrOptions as RequestOptions<V>).document
? (documentOrOptions as RequestOptions<V>)
Expand All @@ -29,7 +28,7 @@ export const parseRequestArgs = <V extends Variables = Variables>(
export const parseRawRequestArgs = <V extends Variables = Variables>(
queryOrOptions: string | RawRequestOptions<V>,
variables?: V,
requestHeaders?: GraphQLClientRequestHeaders,
requestHeaders?: HeadersInit,
): RawRequestOptions<V> => {
return (queryOrOptions as RawRequestOptions<V>).query
? (queryOrOptions as RawRequestOptions<V>)
Expand All @@ -43,7 +42,7 @@ export const parseRawRequestArgs = <V extends Variables = Variables>(

export const parseBatchRequestArgs = <V extends Variables = Variables>(
documentsOrOptions: BatchRequestDocument<V>[] | BatchRequestsOptions<V>,
requestHeaders?: GraphQLClientRequestHeaders,
requestHeaders?: HeadersInit,
): BatchRequestsOptions<V> => {
return (documentsOrOptions as BatchRequestsOptions<V>).documents
? (documentsOrOptions as BatchRequestsOptions<V>)
Expand Down
18 changes: 7 additions & 11 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export type HTTPMethodInput = 'GET' | 'POST' | 'get' | 'post'
export interface RequestConfig extends Omit<RequestInit, 'headers' | 'method'>, AdditionalRequestOptions {
fetch?: Fetch
method?: HTTPMethodInput
headers?: MaybeLazy<GraphQLClientRequestHeaders>
headers?: MaybeLazy<HeadersInit>
requestMiddleware?: RequestMiddleware
responseMiddleware?: ResponseMiddleware
jsonSerializer?: JsonSerializer
Expand All @@ -106,7 +106,7 @@ export type BatchRequestDocument<V extends Variables = Variables> = {

export type RawRequestOptions<V extends Variables = Variables> = {
query: string
requestHeaders?: GraphQLClientRequestHeaders
requestHeaders?: HeadersInit
signal?: RequestInit['signal']
} & (V extends Record<any, never>
? { variables?: V }
Expand All @@ -116,7 +116,7 @@ export type RawRequestOptions<V extends Variables = Variables> = {

export type RequestOptions<V extends Variables = Variables, T = unknown> = {
document: RequestDocument | TypedDocumentNode<T, V>
requestHeaders?: GraphQLClientRequestHeaders
requestHeaders?: HeadersInit
signal?: RequestInit['signal']
} & (V extends Record<any, never>
? { variables?: V }
Expand All @@ -126,7 +126,7 @@ export type RequestOptions<V extends Variables = Variables, T = unknown> = {

export interface BatchRequestsOptions<V extends Variables = Variables> {
documents: BatchRequestDocument<V>[]
requestHeaders?: GraphQLClientRequestHeaders
requestHeaders?: HeadersInit
signal?: RequestInit['signal']
}

Expand Down Expand Up @@ -154,14 +154,10 @@ type RequestExtendedInit<V extends Variables = Variables> = 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<string, string>

// prettier-ignore
export type VariablesAndRequestHeadersArgs<V extends Variables> =
V extends Record<any, never> // do we have explicitly no variables allowed?
? [variables?: V, requestHeaders?: GraphQLClientRequestHeaders]
? [variables?: V, requestHeaders?: HeadersInit]
: keyof RemoveIndex<V> 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]
6 changes: 3 additions & 3 deletions tests/headers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe(`using class`, () => {
})

describe(`custom header in the request`, () => {
describe.each([
describe.each<HeadersInit[]>([
[new Headers({ 'x-request-foo': `request-bar` })],
[{ 'x-request-foo': `request-bar` }],
[[[`x-request-foo`, `request-bar`]]],
Expand Down Expand Up @@ -66,7 +66,7 @@ describe(`using class`, () => {
})
})

describe.each([
describe.each<HeadersInit[]>([
[new Headers({ 'x-foo': `request-bar` })],
[{ 'x-foo': `request-bar` }],
[[[`x-foo`, `request-bar`]]],
Expand Down Expand Up @@ -129,7 +129,7 @@ describe(`using class`, () => {
})

describe(`using request function`, () => {
describe.each([
describe.each<HeadersInit[]>([
[new Headers({ 'x-request-foo': `request-bar` })],
[{ 'x-request-foo': `request-bar` }],
[[[`x-request-foo`, `request-bar`]]],
Expand Down