UNPKG

3.42 kBPlain TextView Raw
1import type { TypedDocumentNode } from '@graphql-typed-document-node/core'
2import type { GraphQLError } from 'graphql'
3import type { DocumentNode } from 'graphql'
4import type { MaybeLazy, MaybePromise, RemoveIndex } from '../../lib/prelude.js'
5import type { ClientError } from '../classes/ClientError.js'
6
7export type Fetch = typeof fetch
8
9/**
10 * 'None' will throw whenever the response contains errors
11 *
12 * 'Ignore' will ignore incoming errors and resolve like no errors occurred
13 *
14 * 'All' will return both the errors and data
15 */
16export type ErrorPolicy = 'none' | 'ignore' | 'all'
17
18export interface JsonSerializer {
19 stringify: (obj: any) => string
20 parse: (obj: string) => unknown
21}
22
23export interface AdditionalRequestOptions {
24 jsonSerializer?: JsonSerializer
25 /**
26 * Decide how to handle GraphQLErrors in response
27 */
28 errorPolicy?: ErrorPolicy
29}
30
31export interface FetchOptions extends RequestInit, AdditionalRequestOptions {}
32
33export type { GraphQLError }
34
35export type Variables = object
36export type BatchVariables = (Variables | undefined)[]
37
38export interface GraphQLResponse<T = unknown> {
39 data?: T
40 errors?: GraphQLError[]
41 extensions?: unknown
42 status: number
43 [key: string]: unknown
44}
45
46export interface GraphQLRequestContext<V extends Variables = Variables> {
47 query: string | string[]
48 variables?: V
49}
50
51export type RequestDocument = string | DocumentNode
52
53export interface GraphQLClientResponse<Data> {
54 status: number
55 headers: Headers
56 data: Data
57 extensions?: unknown
58 errors?: GraphQLError[]
59}
60
61export type HTTPMethodInput = 'GET' | 'POST' | 'get' | 'post'
62
63export interface RequestConfig extends Omit<RequestInit, 'headers' | 'method'>, AdditionalRequestOptions {
64 fetch?: Fetch
65 method?: HTTPMethodInput
66 headers?: MaybeLazy<HeadersInit>
67 requestMiddleware?: RequestMiddleware
68 responseMiddleware?: ResponseMiddleware
69 jsonSerializer?: JsonSerializer
70 excludeOperationName?: boolean
71}
72
73export type RawRequestOptions<V extends Variables = Variables> =
74 & {
75 query: string
76 requestHeaders?: HeadersInit
77 signal?: RequestInit['signal']
78 }
79 & (V extends Record<any, never> ? { variables?: V }
80 : keyof RemoveIndex<V> extends never ? { variables?: V }
81 : { variables: V })
82
83export type RequestOptions<V extends Variables = Variables, T = unknown> =
84 & {
85 document: RequestDocument | TypedDocumentNode<T, V>
86 requestHeaders?: HeadersInit
87 signal?: RequestInit['signal']
88 }
89 & (V extends Record<any, never> ? { variables?: V }
90 : keyof RemoveIndex<V> extends never ? { variables?: V }
91 : { variables: V })
92
93export type ResponseMiddleware = (
94 response: GraphQLClientResponse<unknown> | ClientError | Error,
95 request: RequestInitExtended,
96) => MaybePromise<void>
97
98export type RequestMiddleware<V extends Variables = Variables> = (
99 request: RequestInitExtended<V>,
100) => RequestInitExtended | Promise<RequestInitExtended>
101
102export type RequestInitExtended<V extends Variables = Variables> = RequestInit & {
103 url: string
104 operationName?: string
105 variables?: V
106}
107
108export type VariablesAndRequestHeadersArgs<V extends Variables> = V extends Record<any, never> // do we have explicitly no variables allowed?
109 ? [variables?: V, requestHeaders?: HeadersInit]
110 : keyof RemoveIndex<V> extends never // do we get an empty variables object?
111 ? [variables?: V, requestHeaders?: HeadersInit]
112 : [variables: V, requestHeaders?: HeadersInit]