mirror of
https://github.com/vercel/commerce.git
synced 2025-05-17 06:56:59 +00:00
add token in fetcher header
This commit is contained in:
parent
ad634950a8
commit
456568e942
@ -1,13 +1,25 @@
|
|||||||
import { Fetcher } from '@vercel/commerce/utils/types'
|
import { Fetcher } from '@vercel/commerce/utils/types'
|
||||||
|
import { getCustomerToken } from './utils/token/customer-token'
|
||||||
import { API_URL } from './const'
|
import { API_URL } from './const'
|
||||||
import { handleFetchResponse } from './utils'
|
import { handleFetchResponse } from './utils'
|
||||||
|
|
||||||
const fetcher: Fetcher = async ({ url = '', method = 'POST', body }) => {
|
const fetcher: Fetcher = async ({
|
||||||
|
url = '',
|
||||||
|
method = 'POST',
|
||||||
|
body,
|
||||||
|
variables = {
|
||||||
|
useToken: true,
|
||||||
|
},
|
||||||
|
}) => {
|
||||||
|
const token = getCustomerToken()
|
||||||
const res = await fetch(API_URL + url!, {
|
const res = await fetch(API_URL + url!, {
|
||||||
method: method,
|
method: method,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
accept: 'application/json, text/plain',
|
accept: 'application/json, text/plain',
|
||||||
|
...(token && variables.useToken
|
||||||
|
? { Authorization: `Bearer ${token}` }
|
||||||
|
: {}),
|
||||||
},
|
},
|
||||||
body: body ? JSON.stringify(body) : undefined,
|
body: body ? JSON.stringify(body) : undefined,
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user