Revert fetch options

This commit is contained in:
cond0r 2023-01-10 11:50:00 +02:00
parent 3bc0cbdf48
commit 890ab716d7
17 changed files with 77 additions and 39 deletions

View File

@ -1,5 +1,5 @@
import { FetcherError } from '@vercel/commerce/utils/errors'
import type { GraphQLFetcher } from '@vercel/commerce/api'
import type { FetchOptions, GraphQLFetcher } from '@vercel/commerce/api'
import type { BigcommerceConfig } from '../index'
const fetchGraphqlApi: (getConfig: () => BigcommerceConfig) => GraphQLFetcher =
@ -7,19 +7,20 @@ const fetchGraphqlApi: (getConfig: () => BigcommerceConfig) => GraphQLFetcher =
async (
query: string,
{ variables, preview } = {},
options: { headers?: HeadersInit } = {}
options?: FetchOptions
): Promise<any> => {
// log.warn(query)
const config = getConfig()
const res = await fetch(config.commerceUrl + (preview ? '/preview' : ''), {
method: 'POST',
method: options?.method || 'POST',
headers: {
Authorization: `Bearer ${config.apiToken}`,
...options.headers,
...options?.headers,
'Content-Type': 'application/json',
},
body: JSON.stringify({
...options?.body,
query,
variables,
}),

View File

@ -73,6 +73,12 @@ export type EndpointHandlers<
>
}
export type FetchOptions<Body = any> = {
method?: string
body?: Body
headers?: HeadersInit
}
export type APIProvider = {
config: CommerceAPIConfig
operations: APIOperations<any>
@ -165,9 +171,7 @@ export interface CommerceAPIConfig {
fetch<Data = any, Variables = any>(
query: string,
queryData?: CommerceAPIFetchOptions<Variables>,
options?: {
headers: HeadersInit
}
options?: FetchOptions
): Promise<GraphQLFetcherResult<Data>>
}

View File

@ -1,5 +1,5 @@
import { FetcherError } from '@vercel/commerce/utils/errors'
import type { GraphQLFetcher } from '@vercel/commerce/api'
import type { FetchOptions, GraphQLFetcher } from '@vercel/commerce/api'
import type { KiboCommerceConfig } from '../index'
import { APIAuthenticationHelper } from './api-auth-helper'
@ -8,18 +8,23 @@ const fetchGraphqlApi: (
getConfig: () => KiboCommerceConfig
) => GraphQLFetcher =
(getConfig) =>
async (query: string, { variables, preview } = {}, headers?: HeadersInit) => {
async (
query: string,
{ variables, preview } = {},
options?: FetchOptions
) => {
const config = getConfig()
const authHelper = new APIAuthenticationHelper(config)
const apiToken = await authHelper.getAccessToken()
const res = await fetch(config.commerceUrl + (preview ? '/preview' : ''), {
method: 'POST',
method: options?.method || 'POST',
headers: {
...headers,
...options?.headers,
Authorization: `Bearer ${apiToken}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
...options?.body,
query,
variables,
}),

View File

@ -13,7 +13,9 @@ async function getCustomerId({
: null
const accessToken = token ? JSON.parse(token).accessToken : null
const { data } = await config.fetch(getCustomerAccountQuery, undefined, {
headers: {
'x-vol-user-claims': accessToken,
},
})
return data?.customerAccount?.id

View File

@ -30,7 +30,9 @@ export default function getAllPagesOperation({
{ variables },
{
...(locale && {
headers: {
'Accept-Language': locale,
},
}),
}
)

View File

@ -38,9 +38,11 @@ export default function getAllProductsOperation({
query,
{ variables },
{
headers: {
...(locale && {
'Accept-Language': locale,
}),
},
}
)

View File

@ -28,7 +28,9 @@ export default function getPageOperation({
{ variables },
{
...(locale && {
headers: {
'Accept-Language': locale,
},
}),
}
)

View File

@ -32,7 +32,9 @@ export default function getProductOperation({
{ variables },
{
...(locale && {
headers: {
'Accept-Language': locale,
},
}),
}
)

View File

@ -1,4 +1,4 @@
import type { GraphQLFetcher } from '@vercel/commerce/api'
import type { FetchOptions, GraphQLFetcher } from '@vercel/commerce/api'
import { API_URL } from '../../const'
import { getError } from '../../utils/handle-fetch-response'
@ -7,20 +7,21 @@ import { getToken } from '../../utils/index'
const fetchGraphqlApi: GraphQLFetcher = async (
query: string,
{ variables } = {},
headers?: HeadersInit
options?: FetchOptions
) => {
const token = getToken()
const res = await fetch(API_URL!, {
method: 'POST',
method: options?.method || 'POST',
headers: {
...(token && {
Authorization: `Bearer ${token}`,
}),
...headers,
...options?.headers,
'Content-Type': 'application/json',
},
body: JSON.stringify({
...options?.body,
query,
variables,
}),

View File

@ -1,18 +1,23 @@
import { FetcherError } from '@vercel/commerce/utils/errors'
import type { GraphQLFetcher } from '@vercel/commerce/api'
import type { FetchOptions, GraphQLFetcher } from '@vercel/commerce/api'
import type { SFCCConfig } from '../index'
const fetchGraphqlApi: (getConfig: () => SFCCConfig) => GraphQLFetcher =
(getConfig) =>
async (query: string, { variables, preview } = {}, headers?: HeadersInit) => {
async (
query: string,
{ variables, preview } = {},
options?: FetchOptions
) => {
const config = getConfig()
const res = await fetch(config.commerceUrl, {
method: 'POST',
method: options?.method || 'POST',
headers: {
...headers,
...options?.headers,
'Content-Type': 'application/json',
},
body: JSON.stringify({
...options?.body,
query,
variables,
}),

View File

@ -51,7 +51,9 @@ export default function getAllPagesOperation({
},
{
...(locale && {
headers: {
'Accept-Language': locale,
},
}),
}
)

View File

@ -49,7 +49,9 @@ export default function getAllProductsOperation({
{ variables },
{
...(locale && {
headers: {
'Accept-Language': locale,
},
}),
}
)

View File

@ -50,7 +50,9 @@ export default function getPageOperation({
},
{
...(locale && {
headers: {
'Accept-Language': locale,
},
}),
}
)

View File

@ -48,7 +48,9 @@ export default function getProductOperation({
},
{
...(locale && {
headers: {
'Accept-Language': locale,
},
}),
}
)

View File

@ -1,4 +1,4 @@
import type { GraphQLFetcher } from '@vercel/commerce/api'
import type { FetchOptions, GraphQLFetcher } from '@vercel/commerce/api'
import { API_URL, API_TOKEN } from '../../const'
import { getError } from '../../utils/handle-fetch-response'
@ -6,17 +6,18 @@ import { getError } from '../../utils/handle-fetch-response'
const fetchGraphqlApi: GraphQLFetcher = async (
query: string,
{ variables } = {},
headers?: HeadersInit
options?: FetchOptions
) => {
try {
const res = await fetch(API_URL, {
method: 'POST',
method: options?.method || 'POST',
headers: {
'X-Shopify-Storefront-Access-Token': API_TOKEN!,
...headers,
...options?.headers,
'Content-Type': 'application/json',
},
body: JSON.stringify({
...options?.body,
query,
variables,
}),

View File

@ -17,7 +17,9 @@ const getCategories = async ({
},
{
...(locale && {
headers: {
'Accept-Language': locale,
},
}),
}
)

View File

@ -1,21 +1,22 @@
import { FetcherError } from '@vercel/commerce/utils/errors'
import type { GraphQLFetcher } from '@vercel/commerce/api'
import type { FetchOptions, GraphQLFetcher } from '@vercel/commerce/api'
import { getCommerceApi } from '../'
const fetchGraphqlApi: GraphQLFetcher = async (
query: string,
{ variables } = {},
headers?: HeadersInit
options?: FetchOptions
) => {
const config = getCommerceApi().getConfig()
const res = await fetch(config.commerceUrl, {
method: 'POST',
method: options?.method || 'POST',
headers: {
...headers,
...options?.headers,
'Content-Type': 'application/json',
},
body: JSON.stringify({
...options?.body,
query,
variables,
}),