mirror of
https://github.com/vercel/commerce.git
synced 2025-08-13 04:11:23 +00:00
.vscode
assets
components
config
framework
bigcommerce
commerce
local
saleor
shopify
api
auth
cart
customer
product
types
utils
mutations
queries
checkout-create.ts
checkout-to-cart.ts
customer-token.ts
get-brands.ts
get-categories.ts
get-checkout-id.ts
get-search-variables.ts
get-sort-variables.ts
handle-account-activation.ts
handle-fetch-response.ts
handle-login.ts
index.ts
normalize.ts
throw-user-errors.ts
wishlist
.env.template
README.md
codegen.json
commerce.config.json
const.ts
fetcher.ts
index.tsx
next.config.js
provider.ts
schema.d.ts
schema.graphql
swell
vendure
lib
pages
public
.editorconfig
.env.template
.gitignore
.prettierignore
.prettierrc
README.md
codegen.bigcommerce.json
codegen.json
commerce.config.json
global.d.ts
license.md
next-env.d.ts
next.config.js
package-lock.json
package.json
postcss.config.js
swell-js.d.ts
tailwind.config.js
tsconfig.js
tsconfig.json
yarn.lock
28 lines
659 B
TypeScript
28 lines
659 B
TypeScript
import { FetcherError } from '@commerce/utils/errors'
|
|
|
|
export function getError(errors: any[] | null, 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
|
|
}
|
|
|
|
throw await getAsyncError(res)
|
|
}
|
|
|
|
export default handleFetchResponse
|