Created
October 21, 2021 16:17
-
-
Save bibekgupta3333/6ecc205e7a002693e9d51d6edcc680f8 to your computer and use it in GitHub Desktop.
custom register next js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Fetcher } from '@commerce/utils/types' | |
import { FetcherError } from '@commerce/utils/errors' | |
import type { Response } from '@vercel/fetch' | |
import { getCustomerToken } from '@framework/utils' | |
async function getText(res: any) { | |
try { | |
return (await res.text()) || res.statusText | |
} catch (error) { | |
return res.statusText | |
} | |
} | |
export const fetcher: Fetcher = async ({ | |
url = process.env.NEXT_PUBLIC_STRAPI_LOCAL_API_URL || | |
process.env.NEXT_PUBLIC_STRAPI_API_URL, | |
method = 'POST', | |
variables, | |
query, | |
body: bodyObj, | |
}) => { | |
const token = getCustomerToken() | |
const hasBody = Boolean(variables || query) | |
const body = hasBody ? JSON.stringify({ query, variables }) : undefined | |
const contentTypeJson = hasBody | |
? { 'Content-Type': 'application/json' } | |
: undefined | |
const headers = token | |
? { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` } | |
: contentTypeJson | |
const res = await fetch(url + '/graphql', { | |
method, | |
body, | |
headers, | |
}) | |
if (res.ok) { | |
const { data, errors } = await res.json() | |
if (errors) { | |
throw await new FetcherError({ status: res.status, errors }) | |
} | |
return data[`${Object.keys(data)[0]}`] | |
} | |
throw await new FetcherError({ | |
message: await getText(res), | |
status: res.status, | |
}) | |
} | |
// setCustomerToken(null) | |
// const data = await fetcher({ | |
// variables: { | |
// input: { username: email, email, password }, | |
// }, | |
// query: registerMutation, | |
// }) | |
// if (data.jwt) { | |
// setCustomerToken(data.jwt) | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment