commerce/framework/swell/utils/handle-fetch-response.ts
2021-03-27 16:06:07 -06:00

29 lines
695 B
TypeScript

import { FetcherError } from '@commerce/utils/errors'
export function getError(errors: any[], status: number) {
errors = errors ?? [{ message: 'Failed to fetch Shopify API' }]
return new FetcherError({ errors, status })
}
export async function getAsyncError(res: Response) {
const data = await res.json()
return getError(data.errors, res.status)
}
const handleFetchResponse = async (res: Response) => {
// if (res.ok) {
// const { data, errors } = await res.json()
// if (errors && errors.length) {
// throw getError(errors, res.status)
// }
// return data
// }
if (res) return res
throw await getAsyncError(res)
}
export default handleFetchResponse