1 | import type { TypedDocumentNode } from '@graphql-typed-document-node/core'
|
2 | import { GraphQLClient } from '../classes/GraphQLClient.js'
|
3 | import type { RequestDocument, RequestOptions, Variables, VariablesAndRequestHeadersArgs } from '../helpers/types.js'
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | export async function request<T, V extends Variables = Variables>(options: RequestExtendedOptions<V, T>): Promise<T>
|
44 | // dprint-ignore
|
45 | export async function request<T, V extends Variables = Variables>(url: string, document: RequestDocument | TypedDocumentNode<T, V>, ...variablesAndRequestHeaders: VariablesAndRequestHeadersArgs<V>): Promise<T>
|
46 | // dprint-ignore
|
47 | // eslint-disable-next-line
|
48 | export async function request<T, V extends Variables = Variables>(urlOrOptions: string | RequestExtendedOptions<V, T>, document?: RequestDocument | TypedDocumentNode<T, V>, ...variablesAndRequestHeaders: VariablesAndRequestHeadersArgs<V>): Promise<T> {
|
49 | const requestOptions = parseRequestExtendedArgs<V>(urlOrOptions, document, ...variablesAndRequestHeaders)
|
50 | const client = new GraphQLClient(requestOptions.url)
|
51 | return client.request<T, V>({
|
52 | ...requestOptions,
|
53 | })
|
54 | }
|
55 |
|
56 | export const parseRequestArgs = <V extends Variables = Variables>(
|
57 | documentOrOptions: RequestDocument | RequestOptions<V>,
|
58 | variables?: V,
|
59 | requestHeaders?: HeadersInit,
|
60 | ): RequestOptions<V> => {
|
61 | return (documentOrOptions as RequestOptions<V>).document
|
62 | ? (documentOrOptions as RequestOptions<V>)
|
63 | : ({
|
64 | document: documentOrOptions as RequestDocument,
|
65 | variables: variables,
|
66 | requestHeaders: requestHeaders,
|
67 | signal: undefined,
|
68 | } as unknown as RequestOptions<V>)
|
69 | }
|
70 |
|
71 | export type RequestExtendedOptions<V extends Variables = Variables, T = unknown> = {
|
72 | url: string
|
73 | } & RequestOptions<V, T>
|
74 |
|
75 | export const parseRequestExtendedArgs = <V extends Variables = Variables>(
|
76 | urlOrOptions: string | RequestExtendedOptions<V>,
|
77 | document?: RequestDocument,
|
78 | ...variablesAndRequestHeaders: VariablesAndRequestHeadersArgs<V>
|
79 | ): RequestExtendedOptions<V> => {
|
80 | const [variables, requestHeaders] = variablesAndRequestHeaders
|
81 | return typeof urlOrOptions === `string`
|
82 | ? ({
|
83 | url: urlOrOptions,
|
84 | document: document as RequestDocument,
|
85 | variables,
|
86 | requestHeaders,
|
87 | signal: undefined,
|
88 | } as unknown as RequestExtendedOptions<V>)
|
89 | : urlOrOptions
|
90 | }
|