From 456568e94230a00b1916d1e928451d7376115f5d Mon Sep 17 00:00:00 2001 From: DuvCharles Date: Wed, 14 Dec 2022 14:54:39 +0100 Subject: [PATCH] add token in fetcher header --- packages/sylius/src/fetcher.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/sylius/src/fetcher.ts b/packages/sylius/src/fetcher.ts index 57d62dd46..73eecde27 100644 --- a/packages/sylius/src/fetcher.ts +++ b/packages/sylius/src/fetcher.ts @@ -1,13 +1,25 @@ import { Fetcher } from '@vercel/commerce/utils/types' +import { getCustomerToken } from './utils/token/customer-token' import { API_URL } from './const' 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!, { method: method, headers: { 'Content-Type': 'application/json', accept: 'application/json, text/plain', + ...(token && variables.useToken + ? { Authorization: `Bearer ${token}` } + : {}), }, body: body ? JSON.stringify(body) : undefined, })