import type { TypedDocumentNode } from '@graphql-typed-document-node/core' import type { GraphQLError } from 'graphql' import type { DocumentNode } from 'graphql' import type { MaybeLazy, MaybePromise, RemoveIndex } from '../../lib/prelude.js' import type { ClientError } from '../classes/ClientError.js' export type Fetch = typeof fetch /** * 'None' will throw whenever the response contains errors * * 'Ignore' will ignore incoming errors and resolve like no errors occurred * * 'All' will return both the errors and data */ export type ErrorPolicy = 'none' | 'ignore' | 'all' export interface JsonSerializer { stringify: (obj: any) => string parse: (obj: string) => unknown } export interface AdditionalRequestOptions { jsonSerializer?: JsonSerializer /** * Decide how to handle GraphQLErrors in response */ errorPolicy?: ErrorPolicy } export interface FetchOptions extends RequestInit, AdditionalRequestOptions {} export type { GraphQLError } export type Variables = object export type BatchVariables = (Variables | undefined)[] export interface GraphQLResponse { data?: T errors?: GraphQLError[] extensions?: unknown status: number [key: string]: unknown } export interface GraphQLRequestContext { query: string | string[] variables?: V } export type RequestDocument = string | DocumentNode export interface GraphQLClientResponse { status: number headers: Headers data: Data extensions?: unknown errors?: GraphQLError[] } export type HTTPMethodInput = 'GET' | 'POST' | 'get' | 'post' export interface RequestConfig extends Omit, AdditionalRequestOptions { fetch?: Fetch method?: HTTPMethodInput headers?: MaybeLazy requestMiddleware?: RequestMiddleware responseMiddleware?: ResponseMiddleware jsonSerializer?: JsonSerializer excludeOperationName?: boolean } export type RawRequestOptions = & { query: string requestHeaders?: HeadersInit signal?: RequestInit['signal'] } & (V extends Record ? { variables?: V } : keyof RemoveIndex extends never ? { variables?: V } : { variables: V }) export type RequestOptions = & { document: RequestDocument | TypedDocumentNode requestHeaders?: HeadersInit signal?: RequestInit['signal'] } & (V extends Record ? { variables?: V } : keyof RemoveIndex extends never ? { variables?: V } : { variables: V }) export type ResponseMiddleware = ( response: GraphQLClientResponse | ClientError | Error, request: RequestInitExtended, ) => MaybePromise export type RequestMiddleware = ( request: RequestInitExtended, ) => RequestInitExtended | Promise export type RequestInitExtended = RequestInit & { url: string operationName?: string variables?: V } export type VariablesAndRequestHeadersArgs = V extends Record // do we have explicitly no variables allowed? ? [variables?: V, requestHeaders?: HeadersInit] : keyof RemoveIndex extends never // do we get an empty variables object? ? [variables?: V, requestHeaders?: HeadersInit] : [variables: V, requestHeaders?: HeadersInit]