diff --git a/packages/bigcommerce/src/api/utils/fetch-graphql-api.ts b/packages/bigcommerce/src/api/utils/fetch-graphql-api.ts index 9072b2432..e860cb714 100644 --- a/packages/bigcommerce/src/api/utils/fetch-graphql-api.ts +++ b/packages/bigcommerce/src/api/utils/fetch-graphql-api.ts @@ -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 => { // 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, }), diff --git a/packages/commerce/src/api/index.ts b/packages/commerce/src/api/index.ts index 95b5804e2..dc4aeda40 100644 --- a/packages/commerce/src/api/index.ts +++ b/packages/commerce/src/api/index.ts @@ -73,6 +73,12 @@ export type EndpointHandlers< > } +export type FetchOptions = { + method?: string + body?: Body + headers?: HeadersInit +} + export type APIProvider = { config: CommerceAPIConfig operations: APIOperations @@ -165,9 +171,7 @@ export interface CommerceAPIConfig { fetch( query: string, queryData?: CommerceAPIFetchOptions, - options?: { - headers: HeadersInit - } + options?: FetchOptions ): Promise> } diff --git a/packages/kibocommerce/src/api/utils/fetch-graphql-api.ts b/packages/kibocommerce/src/api/utils/fetch-graphql-api.ts index 245e59971..9f07ef210 100644 --- a/packages/kibocommerce/src/api/utils/fetch-graphql-api.ts +++ b/packages/kibocommerce/src/api/utils/fetch-graphql-api.ts @@ -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, }), diff --git a/packages/kibocommerce/src/api/utils/get-customer-id.ts b/packages/kibocommerce/src/api/utils/get-customer-id.ts index 1d93ade4f..f96000976 100644 --- a/packages/kibocommerce/src/api/utils/get-customer-id.ts +++ b/packages/kibocommerce/src/api/utils/get-customer-id.ts @@ -13,7 +13,9 @@ async function getCustomerId({ : null const accessToken = token ? JSON.parse(token).accessToken : null const { data } = await config.fetch(getCustomerAccountQuery, undefined, { - 'x-vol-user-claims': accessToken, + headers: { + 'x-vol-user-claims': accessToken, + }, }) return data?.customerAccount?.id diff --git a/packages/saleor/src/api/operations/get-all-pages.ts b/packages/saleor/src/api/operations/get-all-pages.ts index 0da523a88..e53cc8f31 100644 --- a/packages/saleor/src/api/operations/get-all-pages.ts +++ b/packages/saleor/src/api/operations/get-all-pages.ts @@ -30,7 +30,9 @@ export default function getAllPagesOperation({ { variables }, { ...(locale && { - 'Accept-Language': locale, + headers: { + 'Accept-Language': locale, + }, }), } ) diff --git a/packages/saleor/src/api/operations/get-all-products.ts b/packages/saleor/src/api/operations/get-all-products.ts index 9a92e2f7a..1be61b4e0 100644 --- a/packages/saleor/src/api/operations/get-all-products.ts +++ b/packages/saleor/src/api/operations/get-all-products.ts @@ -38,9 +38,11 @@ export default function getAllProductsOperation({ query, { variables }, { - ...(locale && { - 'Accept-Language': locale, - }), + headers: { + ...(locale && { + 'Accept-Language': locale, + }), + }, } ) diff --git a/packages/saleor/src/api/operations/get-page.ts b/packages/saleor/src/api/operations/get-page.ts index 9d260713d..6fa427691 100644 --- a/packages/saleor/src/api/operations/get-page.ts +++ b/packages/saleor/src/api/operations/get-page.ts @@ -28,7 +28,9 @@ export default function getPageOperation({ { variables }, { ...(locale && { - 'Accept-Language': locale, + headers: { + 'Accept-Language': locale, + }, }), } ) diff --git a/packages/saleor/src/api/operations/get-product.ts b/packages/saleor/src/api/operations/get-product.ts index c9bdf364b..cc2bf9a7a 100644 --- a/packages/saleor/src/api/operations/get-product.ts +++ b/packages/saleor/src/api/operations/get-product.ts @@ -32,7 +32,9 @@ export default function getProductOperation({ { variables }, { ...(locale && { - 'Accept-Language': locale, + headers: { + 'Accept-Language': locale, + }, }), } ) diff --git a/packages/saleor/src/api/utils/fetch-graphql-api.ts b/packages/saleor/src/api/utils/fetch-graphql-api.ts index 45f5326d5..ba2183797 100644 --- a/packages/saleor/src/api/utils/fetch-graphql-api.ts +++ b/packages/saleor/src/api/utils/fetch-graphql-api.ts @@ -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, }), diff --git a/packages/sfcc/src/api/utils/fetch-local.ts b/packages/sfcc/src/api/utils/fetch-local.ts index 58e801517..89c1288d8 100644 --- a/packages/sfcc/src/api/utils/fetch-local.ts +++ b/packages/sfcc/src/api/utils/fetch-local.ts @@ -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, }), diff --git a/packages/shopify/src/api/operations/get-all-pages.ts b/packages/shopify/src/api/operations/get-all-pages.ts index f8cde77f8..55c1d7d69 100644 --- a/packages/shopify/src/api/operations/get-all-pages.ts +++ b/packages/shopify/src/api/operations/get-all-pages.ts @@ -51,7 +51,9 @@ export default function getAllPagesOperation({ }, { ...(locale && { - 'Accept-Language': locale, + headers: { + 'Accept-Language': locale, + }, }), } ) diff --git a/packages/shopify/src/api/operations/get-all-products.ts b/packages/shopify/src/api/operations/get-all-products.ts index 6ae4885ed..fcab20128 100644 --- a/packages/shopify/src/api/operations/get-all-products.ts +++ b/packages/shopify/src/api/operations/get-all-products.ts @@ -49,7 +49,9 @@ export default function getAllProductsOperation({ { variables }, { ...(locale && { - 'Accept-Language': locale, + headers: { + 'Accept-Language': locale, + }, }), } ) diff --git a/packages/shopify/src/api/operations/get-page.ts b/packages/shopify/src/api/operations/get-page.ts index a98cae79b..e6ba19ce5 100644 --- a/packages/shopify/src/api/operations/get-page.ts +++ b/packages/shopify/src/api/operations/get-page.ts @@ -50,7 +50,9 @@ export default function getPageOperation({ }, { ...(locale && { - 'Accept-Language': locale, + headers: { + 'Accept-Language': locale, + }, }), } ) diff --git a/packages/shopify/src/api/operations/get-product.ts b/packages/shopify/src/api/operations/get-product.ts index e8aa28120..76a3c865f 100644 --- a/packages/shopify/src/api/operations/get-product.ts +++ b/packages/shopify/src/api/operations/get-product.ts @@ -48,7 +48,9 @@ export default function getProductOperation({ }, { ...(locale && { - 'Accept-Language': locale, + headers: { + 'Accept-Language': locale, + }, }), } ) diff --git a/packages/shopify/src/api/utils/fetch-graphql-api.ts b/packages/shopify/src/api/utils/fetch-graphql-api.ts index 1eac16ef1..39d6b3eb0 100644 --- a/packages/shopify/src/api/utils/fetch-graphql-api.ts +++ b/packages/shopify/src/api/utils/fetch-graphql-api.ts @@ -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, }), diff --git a/packages/shopify/src/utils/get-categories.ts b/packages/shopify/src/utils/get-categories.ts index e21592ee1..2bae3c3b5 100644 --- a/packages/shopify/src/utils/get-categories.ts +++ b/packages/shopify/src/utils/get-categories.ts @@ -17,7 +17,9 @@ const getCategories = async ({ }, { ...(locale && { - 'Accept-Language': locale, + headers: { + 'Accept-Language': locale, + }, }), } ) diff --git a/packages/vendure/src/api/utils/fetch-graphql-api.ts b/packages/vendure/src/api/utils/fetch-graphql-api.ts index f8377f7ae..61183ada3 100644 --- a/packages/vendure/src/api/utils/fetch-graphql-api.ts +++ b/packages/vendure/src/api/utils/fetch-graphql-api.ts @@ -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, }),