mirror of
https://github.com/vercel/commerce.git
synced 2025-06-29 01:41:21 +00:00
* sync changes with latest changes Signed-off-by: Chloe <pinkcloudvnn@gmail.com> * fix typescripts Signed-off-by: Chloe <pinkcloudvnn@gmail.com> * fix all types issue and checkout process Signed-off-by: Chloe <pinkcloudvnn@gmail.com> * fix some types Signed-off-by: Chloe <pinkcloudvnn@gmail.com> * several fixes Signed-off-by: Chloe <pinkcloudvnn@gmail.com> * remove console log Signed-off-by: Chloe <pinkcloudvnn@gmail.com> --------- Signed-off-by: Chloe <pinkcloudvnn@gmail.com>
47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
import type { Fetcher } from '@vercel/commerce/utils/types'
|
|
import { FetcherError } from '@vercel/commerce/utils/errors'
|
|
import { API_URL } from './const'
|
|
|
|
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 fetcher: Fetcher = async ({
|
|
url = API_URL,
|
|
method = 'POST',
|
|
variables,
|
|
query,
|
|
body: bodyObj,
|
|
}) => {
|
|
const hasBody = Boolean(variables || bodyObj)
|
|
const body = hasBody
|
|
? JSON.stringify(variables ? { query, variables } : bodyObj)
|
|
: undefined
|
|
const headers = hasBody ? { 'Content-Type': 'application/json' } : undefined
|
|
const res = await fetch(url!, { method, body, headers })
|
|
|
|
if (res.ok) {
|
|
const { data, errors } = await res.json()
|
|
if (errors && errors.length) {
|
|
throw getError(res)
|
|
}
|
|
return data
|
|
}
|
|
|
|
throw await getError(res)
|
|
}
|
|
|
|
export default fetcher
|