Add Login, Signup and Active Customer API Hook

This commit is contained in:
Peter
2021-09-25 17:57:05 +07:00
parent 91b5f7d901
commit dc00a4ad7a
11 changed files with 410 additions and 96 deletions

28
src/utils/fetcher.ts Normal file
View 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
View 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
View 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