Loan Laux 09249045eb
Add Reaction Commerce provider
Signed-off-by: Loan Laux <loan@outgrow.io>
2021-03-30 20:07:48 +04:00

58 lines
1.5 KiB
TypeScript

import { FetcherError } from '@commerce/utils/errors'
import type { Fetcher } from '@commerce/utils/types'
import { handleFetchResponse } from './utils'
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,
method = 'GET',
variables,
body: bodyObj,
query,
}) => {
// if no URL is passed but we have a `query` param, we assume it's GraphQL
if (!url && query) {
return handleFetchResponse(
await fetch(API_URL, {
method: 'POST',
body: JSON.stringify({ query, variables }),
headers: {
'Content-Type': 'application/json',
},
})
)
}
const hasBody = Boolean(variables || bodyObj) && method !== 'GET'
const body = hasBody
? JSON.stringify(variables ? { variables } : bodyObj)
: undefined
const headers = hasBody ? { 'Content-Type': 'application/json' } : undefined
const res = await fetch(url!, { method, body, headers })
if (res.ok) {
const { data } = await res.json()
return data
}
throw await getError(res)
}
export default fetcher