1 | import type { TypedDocumentNode } from '@graphql-typed-document-node/core'
|
2 | import type { GraphQLError } from 'graphql'
|
3 | import type { DocumentNode } from 'graphql'
|
4 | import type { MaybeLazy, MaybePromise, RemoveIndex } from '../../lib/prelude.js'
|
5 | import type { ClientError } from '../classes/ClientError.js'
|
6 |
|
7 | export type Fetch = typeof fetch
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | export type ErrorPolicy = 'none' | 'ignore' | 'all'
|
17 |
|
18 | export interface JsonSerializer {
|
19 | stringify: (obj: any) => string
|
20 | parse: (obj: string) => unknown
|
21 | }
|
22 |
|
23 | export interface AdditionalRequestOptions {
|
24 | jsonSerializer?: JsonSerializer
|
25 | |
26 |
|
27 |
|
28 | errorPolicy?: ErrorPolicy
|
29 | }
|
30 |
|
31 | export interface FetchOptions extends RequestInit, AdditionalRequestOptions {}
|
32 |
|
33 | export type { GraphQLError }
|
34 |
|
35 | export type Variables = object
|
36 | export type BatchVariables = (Variables | undefined)[]
|
37 |
|
38 | export interface GraphQLResponse<T = unknown> {
|
39 | data?: T
|
40 | errors?: GraphQLError[]
|
41 | extensions?: unknown
|
42 | status: number
|
43 | [key: string]: unknown
|
44 | }
|
45 |
|
46 | export interface GraphQLRequestContext<V extends Variables = Variables> {
|
47 | query: string | string[]
|
48 | variables?: V
|
49 | }
|
50 |
|
51 | export type RequestDocument = string | DocumentNode
|
52 |
|
53 | export interface GraphQLClientResponse<Data> {
|
54 | status: number
|
55 | headers: Headers
|
56 | data: Data
|
57 | extensions?: unknown
|
58 | errors?: GraphQLError[]
|
59 | }
|
60 |
|
61 | export type HTTPMethodInput = 'GET' | 'POST' | 'get' | 'post'
|
62 |
|
63 | export 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 |
|
73 | export 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 |
|
83 | export 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 |
|
93 | export type ResponseMiddleware = (
|
94 | response: GraphQLClientResponse<unknown> | ClientError | Error,
|
95 | request: RequestInitExtended,
|
96 | ) => MaybePromise<void>
|
97 |
|
98 | export type RequestMiddleware<V extends Variables = Variables> = (
|
99 | request: RequestInitExtended<V>,
|
100 | ) => RequestInitExtended | Promise<RequestInitExtended>
|
101 |
|
102 | export type RequestInitExtended<V extends Variables = Variables> = RequestInit & {
|
103 | url: string
|
104 | operationName?: string
|
105 | variables?: V
|
106 | }
|
107 |
|
108 | export type VariablesAndRequestHeadersArgs<V extends Variables> = V extends Record<any, never>
|
109 | ? [variables?: V, requestHeaders?: HeadersInit]
|
110 | : keyof RemoveIndex<V> extends never
|
111 | ? [variables?: V, requestHeaders?: HeadersInit]
|
112 | : [variables: V, requestHeaders?: HeadersInit]
|