|
4 | 4 | * Define the structure of a GraphQL request body
|
5 | 5 | * `variables` are optional and can be any shape you need.
|
6 | 6 | */
|
7 |
| -interface GraphQLRequestBody { |
| 7 | +interface GraphQLRequestBody<RequestData> { |
8 | 8 | query: string
|
9 |
| - variables?: Record<string, unknown> |
| 9 | + variables?: RequestData | Record<string, unknown> |
10 | 10 | }
|
11 | 11 |
|
12 | 12 | /**
|
@@ -42,9 +42,9 @@ export const gql = String.raw
|
42 | 42 | * It accepts a request body, an optional auth token, and extra headers.
|
43 | 43 | * The function then returns the typed data (T).
|
44 | 44 | */
|
45 |
| -export async function gqlRequest<T>( |
| 45 | +export async function gqlRequest<T, RequestData>( |
46 | 46 | client: string,
|
47 |
| - data: GraphQLRequestBody, |
| 47 | + data: GraphQLRequestBody<RequestData>, |
48 | 48 | token?: string
|
49 | 49 | ): Promise<T> {
|
50 | 50 | // Build headers. If an auth token is provided, set the Authorization header.
|
@@ -87,15 +87,21 @@ export async function gqlRequest<T>(
|
87 | 87 | /**
|
88 | 88 | * Convenience function that sets web-client as the client,
|
89 | 89 | */
|
90 |
| -export async function pocketRequest<T>(body: GraphQLRequestBody, token?: string): Promise<T> { |
91 |
| - return gqlRequest<T>('web-client', body, token) |
| 90 | +export async function pocketRequest<T, RequestData>( |
| 91 | + body: GraphQLRequestBody<RequestData>, |
| 92 | + token?: string |
| 93 | +): Promise<T> { |
| 94 | + return gqlRequest<T, RequestData>('web-client', body, token) |
92 | 95 | }
|
93 | 96 |
|
94 | 97 | /**
|
95 | 98 | * Convenience function that sets web-client as the client,
|
96 | 99 | */
|
97 |
| -export async function extensionRequest<T>(body: GraphQLRequestBody, token?: string): Promise<T> { |
98 |
| - return gqlRequest<T>('web-extension', body, token) |
| 100 | +export async function extensionRequest<T, RequestData>( |
| 101 | + body: GraphQLRequestBody<RequestData>, |
| 102 | + token?: string |
| 103 | +): Promise<T> { |
| 104 | + return gqlRequest<T, RequestData>('web-extension', body, token) |
99 | 105 | }
|
100 | 106 |
|
101 | 107 | class GraphQLRequestError extends Error {
|
|
0 commit comments