mirror of
https://github.com/vercel/commerce.git
synced 2025-04-30 06:47:50 +00:00
Update config.ts
This commit is contained in:
parent
57ebfe42f5
commit
612392aaba
@ -12,6 +12,22 @@ export const STORE_DOMAIN = process.env.NEXT_PUBLIC_SHOPIFY_STORE_DOMAIN
|
|||||||
export const API_URL = `https://${STORE_DOMAIN}/api/2021-01/graphql.json`
|
export const API_URL = `https://${STORE_DOMAIN}/api/2021-01/graphql.json`
|
||||||
export const API_TOKEN = process.env.NEXT_PUBLIC_SHOPIFY_STOREFRONT_ACCESS_TOKEN
|
export const API_TOKEN = process.env.NEXT_PUBLIC_SHOPIFY_STOREFRONT_ACCESS_TOKEN
|
||||||
|
|
||||||
|
async function getText(res: Response) {
|
||||||
|
try {
|
||||||
|
return (await res.text()) || res.statusText
|
||||||
|
} catch (error) {
|
||||||
|
return res.statusText
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getError(res: Response) {
|
||||||
|
if (res.headers.get('Content-Type')?.includes('application/json')) {
|
||||||
|
const data = await res.json()
|
||||||
|
return new FetcherError({ errors: data.errors, status: res.status })
|
||||||
|
}
|
||||||
|
return new FetcherError({ message: await getText(res), status: res.status })
|
||||||
|
}
|
||||||
|
|
||||||
const shopifyConfig: ShopifyConfig = {
|
const shopifyConfig: ShopifyConfig = {
|
||||||
locale: 'en-us',
|
locale: 'en-us',
|
||||||
cartCookie: SHOPIFY_CHECKOUT_ID_COOKIE,
|
cartCookie: SHOPIFY_CHECKOUT_ID_COOKIE,
|
||||||
@ -26,18 +42,18 @@ const shopifyConfig: ShopifyConfig = {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
const json = await res.json()
|
if (res.ok) {
|
||||||
|
const { data, errors } = await res.json()
|
||||||
|
|
||||||
if (json.errors) {
|
if (errors && errors.length) {
|
||||||
throw new FetcherError({
|
throw new CommerceError({
|
||||||
errors: json.errors ?? [
|
message: errors[0].message,
|
||||||
{ message: 'Failed to fetch Shopify Storefront API' },
|
})
|
||||||
],
|
}
|
||||||
status: res.status,
|
return data
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return { data: json.data, res }
|
throw await getError(res)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user