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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -32,7 +32,9 @@ export default function getProductOperation({
{ variables }, { variables },
{ {
...(locale && { ...(locale && {
headers: {
'Accept-Language': locale, '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 { API_URL } from '../../const'
import { getError } from '../../utils/handle-fetch-response' import { getError } from '../../utils/handle-fetch-response'
@ -7,20 +7,21 @@ import { getToken } from '../../utils/index'
const fetchGraphqlApi: GraphQLFetcher = async ( const fetchGraphqlApi: GraphQLFetcher = async (
query: string, query: string,
{ variables } = {}, { variables } = {},
headers?: HeadersInit options?: FetchOptions
) => { ) => {
const token = getToken() const token = getToken()
const res = await fetch(API_URL!, { const res = await fetch(API_URL!, {
method: 'POST', method: options?.method || 'POST',
headers: { headers: {
...(token && { ...(token && {
Authorization: `Bearer ${token}`, Authorization: `Bearer ${token}`,
}), }),
...headers, ...options?.headers,
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
body: JSON.stringify({ body: JSON.stringify({
...options?.body,
query, query,
variables, variables,
}), }),

View File

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

View File

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

View File

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

View File

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

View File

@ -48,7 +48,9 @@ export default function getProductOperation({
}, },
{ {
...(locale && { ...(locale && {
headers: {
'Accept-Language': locale, '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 { API_URL, API_TOKEN } from '../../const'
import { getError } from '../../utils/handle-fetch-response' import { getError } from '../../utils/handle-fetch-response'
@ -6,17 +6,18 @@ import { getError } from '../../utils/handle-fetch-response'
const fetchGraphqlApi: GraphQLFetcher = async ( const fetchGraphqlApi: GraphQLFetcher = async (
query: string, query: string,
{ variables } = {}, { variables } = {},
headers?: HeadersInit options?: FetchOptions
) => { ) => {
try { try {
const res = await fetch(API_URL, { const res = await fetch(API_URL, {
method: 'POST', method: options?.method || 'POST',
headers: { headers: {
'X-Shopify-Storefront-Access-Token': API_TOKEN!, 'X-Shopify-Storefront-Access-Token': API_TOKEN!,
...headers, ...options?.headers,
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
body: JSON.stringify({ body: JSON.stringify({
...options?.body,
query, query,
variables, variables,
}), }),

View File

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

View File

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