mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 07:26:59 +00:00
30 lines
802 B
TypeScript
30 lines
802 B
TypeScript
import { Fetcher } from '@commerce/utils/types'
|
|
import handleFetchResponse from './utils/handle-fetch-response'
|
|
import { ENDPOINT, CLIENTID, SCOPE } from './const'
|
|
import { getSalesChannelToken } from '@commercelayer/js-auth'
|
|
|
|
export const fetcher: Fetcher = async ({ url, method, variables, query }) => {
|
|
const token = await getSalesChannelToken({
|
|
endpoint: ENDPOINT,
|
|
clientId: CLIENTID,
|
|
scope: SCOPE,
|
|
})
|
|
|
|
return handleFetchResponse(
|
|
await fetch(url!, {
|
|
method,
|
|
headers: {
|
|
Accept: 'application/vnd.api+json',
|
|
Authorization: `Bearer ${token.accessToken}`,
|
|
'Content-Type': 'application/vnd.api+json',
|
|
},
|
|
body: JSON.stringify({
|
|
data: {
|
|
type: query,
|
|
attributes: variables,
|
|
},
|
|
}),
|
|
})
|
|
)
|
|
}
|