mirror of
https://github.com/vercel/commerce.git
synced 2025-07-26 19:51:23 +00:00
Add Login, Signup and Active Customer API Hook
This commit is contained in:
28
src/utils/fetcher.ts
Normal file
28
src/utils/fetcher.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { request } from 'graphql-request'
|
||||
import { RequestDocument, Variables } from 'graphql-request/dist/types'
|
||||
|
||||
interface QueryOptions {
|
||||
query: RequestDocument
|
||||
variables?: Variables
|
||||
onLoad?: (loading: boolean) => any
|
||||
key?: string
|
||||
}
|
||||
|
||||
const fetcher = async <T>(options: QueryOptions): Promise<T> => {
|
||||
const { query, variables } = options
|
||||
console.log('query')
|
||||
console.log(options)
|
||||
const token = localStorage.getItem('token')
|
||||
console.log('token')
|
||||
console.log(token)
|
||||
const res = await request<T>(
|
||||
process.env.NEXT_PUBLIC_VENDURE_SHOP_API_URL as string,
|
||||
query,
|
||||
variables,
|
||||
token ? { Authorization: 'Bearer ' + token } : {}
|
||||
)
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
export default fetcher
|
11
src/utils/gglFetcher.ts
Normal file
11
src/utils/gglFetcher.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { RequestDocument, Variables } from 'graphql-request/dist/types'
|
||||
import fetcher from './fetcher'
|
||||
|
||||
const gglFetcher = async <T>(
|
||||
...params: [RequestDocument, Variables]
|
||||
): Promise<T> => {
|
||||
const [query, variables] = params
|
||||
return fetcher<T>({ query, variables })
|
||||
}
|
||||
|
||||
export default gglFetcher
|
28
src/utils/rawFetcher.ts
Normal file
28
src/utils/rawFetcher.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { rawRequest } from 'graphql-request'
|
||||
import { RequestDocument, Variables } from 'graphql-request/dist/types'
|
||||
|
||||
interface QueryOptions {
|
||||
query: RequestDocument
|
||||
variables?: Variables
|
||||
onLoad?: (loading: boolean) => any
|
||||
key?: string
|
||||
}
|
||||
|
||||
const rawFetcher = <T>({
|
||||
query,
|
||||
variables,
|
||||
onLoad = () => true,
|
||||
}: QueryOptions): Promise<{ data: T; headers: any }> => {
|
||||
onLoad(true)
|
||||
return rawRequest<T>(
|
||||
process.env.NEXT_PUBLIC_VENDURE_SHOP_API_URL as string,
|
||||
query as string,
|
||||
variables
|
||||
)
|
||||
.then(({ data, headers }) => {
|
||||
return { data, headers }
|
||||
})
|
||||
.finally(() => onLoad(false))
|
||||
}
|
||||
|
||||
export default rawFetcher
|
Reference in New Issue
Block a user