-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetClientForServer.ts
40 lines (33 loc) · 941 Bytes
/
getClientForServer.ts
1
2
3
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
import { GraphQLClient } from "graphql-request"
import { getClientCredentialsToken } from "../lib/oauth"
let client: GraphQLClient
/**
* Gets a GraphQL client for use in the backend.
* @returns A GraphQL client.
*/
export async function getClientForServer() {
client =
client ||
new GraphQLClient(process.env.NEXT_PUBLIC_API_PATH, {
headers: {
"X-Niftory-API-Key": process.env.NEXT_PUBLIC_API_KEY,
},
})
const token = await getClientCredentialsToken()
client.setHeader("Authorization", `Bearer ${token}`)
return client
}
/**
* Gets a GraphQL client for use in the backend with out user credntial.
* @returns A GraphQL client.
*/
export async function getClientForServerWithoutCredentials() {
client =
client ||
new GraphQLClient(process.env.NEXT_PUBLIC_API_PATH, {
headers: {
"X-Niftory-API-Key": process.env.NEXT_PUBLIC_API_KEY,
},
})
return client
}