diff --git a/framework/bigcommerce/api/checkout.ts b/framework/bigcommerce/api/checkout.ts deleted file mode 100644 index 530f4c40a..000000000 --- a/framework/bigcommerce/api/checkout.ts +++ /dev/null @@ -1,77 +0,0 @@ -import isAllowedMethod from './utils/is-allowed-method' -import createApiHandler, { - BigcommerceApiHandler, -} from './utils/create-api-handler' -import { BigcommerceApiError } from './utils/errors' - -const METHODS = ['GET'] -const fullCheckout = true - -// TODO: a complete implementation should have schema validation for `req.body` -const checkoutApi: BigcommerceApiHandler = async (req, res, config) => { - if (!isAllowedMethod(req, res, METHODS)) return - - const { cookies } = req - const cartId = cookies[config.cartCookie] - - try { - if (!cartId) { - res.redirect('/cart') - return - } - - const { data } = await config.storeApiFetch( - `/v3/carts/${cartId}/redirect_urls`, - { - method: 'POST', - } - ) - - if (fullCheckout) { - res.redirect(data.checkout_url) - return - } - - // TODO: make the embedded checkout work too! - const html = ` - - - - - - Checkout - - - - -
- - - ` - - res.status(200) - res.setHeader('Content-Type', 'text/html') - res.write(html) - res.end() - } catch (error) { - console.error(error) - - const message = - error instanceof BigcommerceApiError - ? 'An unexpected error ocurred with the Bigcommerce API' - : 'An unexpected error ocurred' - - res.status(500).json({ data: null, errors: [{ message }] }) - } -} - -export default createApiHandler(checkoutApi, {}, {}) diff --git a/framework/bigcommerce/api/index.ts b/framework/bigcommerce/api/index.ts index c9567dc9a..a6ee3f508 100644 --- a/framework/bigcommerce/api/index.ts +++ b/framework/bigcommerce/api/index.ts @@ -61,47 +61,9 @@ if (!(STORE_API_URL && STORE_API_TOKEN && STORE_API_CLIENT_ID)) { ) } -export class Config { - private config: BigcommerceConfig - - constructor(config: Omit) { - this.config = { - ...config, - // The customerCookie is not customizable for now, BC sets the cookie and it's - // not important to rename it - customerCookie: 'SHOP_TOKEN', - } - } - - getConfig(userConfig: Partial = {}) { - return Object.entries(userConfig).reduce( - (cfg, [key, value]) => Object.assign(cfg, { [key]: value }), - { ...this.config } - ) - } - - setConfig(newConfig: Partial) { - Object.assign(this.config, newConfig) - } -} - const ONE_DAY = 60 * 60 * 24 -const config = new Config({ - commerceUrl: API_URL, - apiToken: API_TOKEN, - cartCookie: process.env.BIGCOMMERCE_CART_COOKIE ?? 'bc_cartId', - cartCookieMaxAge: ONE_DAY * 30, - fetch: fetchGraphqlApi, - applyLocale: true, - // REST API only - storeApiUrl: STORE_API_URL, - storeApiToken: STORE_API_TOKEN, - storeApiClientId: STORE_API_CLIENT_ID, - storeChannelId: STORE_CHANNEL_ID, - storeApiFetch: fetchStoreApi, -}) -const config2: BigcommerceConfig = { +const config: BigcommerceConfig = { commerceUrl: API_URL, apiToken: API_TOKEN, customerCookie: 'SHOP_TOKEN', @@ -117,20 +79,19 @@ const config2: BigcommerceConfig = { storeApiFetch: fetchStoreApi, } -export const provider = { - config: config2, - operations: { - login, - getAllPages, - getPage, - getSiteInfo, - getCustomerWishlist, - getAllProductPaths, - getAllProducts, - getProduct, - }, +const operations = { + login, + getAllPages, + getPage, + getSiteInfo, + getCustomerWishlist, + getAllProductPaths, + getAllProducts, + getProduct, } +export const provider = { config, operations } + export type Provider = typeof provider export type APIs = @@ -149,11 +110,3 @@ export function getCommerceApi

( ): BigcommerceAPI

{ return commerceApi(customProvider) } - -export function getConfig(userConfig?: Partial) { - return config.getConfig(userConfig) -} - -export function setConfig(newConfig: Partial) { - return config.setConfig(newConfig) -} diff --git a/framework/bigcommerce/api/operations/get-all-pages.ts b/framework/bigcommerce/api/operations/get-all-pages.ts index 2f07670ff..3a9b64b1f 100644 --- a/framework/bigcommerce/api/operations/get-all-pages.ts +++ b/framework/bigcommerce/api/operations/get-all-pages.ts @@ -10,13 +10,13 @@ export default function getAllPagesOperation({ commerce, }: OperationContext) { async function getAllPages(opts?: { - config?: BigcommerceConfig + config?: Partial preview?: boolean }): Promise async function getAllPages( opts: { - config?: BigcommerceConfig + config?: Partial preview?: boolean } & OperationOptions ): Promise @@ -26,13 +26,13 @@ export default function getAllPagesOperation({ preview, }: { url?: string - config?: BigcommerceConfig + config?: Partial preview?: boolean } = {}): Promise { - config = commerce.getConfig(config) + const cfg = commerce.getConfig(config) // RecursivePartial forces the method to check for every prop in the data, which is // required in case there's a custom `url` - const { data } = await config.storeApiFetch< + const { data } = await cfg.storeApiFetch< RecursivePartial<{ data: Page[] }> >('/v3/content/pages') const pages = (data as RecursiveRequired) ?? [] diff --git a/framework/bigcommerce/api/operations/get-all-products.ts b/framework/bigcommerce/api/operations/get-all-products.ts index c5e16a276..c2652f5bf 100644 --- a/framework/bigcommerce/api/operations/get-all-products.ts +++ b/framework/bigcommerce/api/operations/get-all-products.ts @@ -76,14 +76,14 @@ export default function getAllProductsOperation({ }: OperationContext) { async function getAllProducts(opts?: { variables?: T['variables'] - config?: BigcommerceConfig + config?: Partial preview?: boolean }): Promise async function getAllProducts( opts: { variables?: T['variables'] - config?: BigcommerceConfig + config?: Partial preview?: boolean } & OperationOptions ): Promise @@ -91,15 +91,14 @@ export default function getAllProductsOperation({ async function getAllProducts({ query = getAllProductsQuery, variables: vars = {}, - config, + config: cfg, }: { query?: string variables?: T['variables'] - config?: BigcommerceConfig + config?: Partial preview?: boolean } = {}): Promise { - config = commerce.getConfig(config) - + const config = commerce.getConfig(cfg) const { locale } = config const field = getProductsType(vars.relevance) const variables: GetAllProductsQueryVariables = { diff --git a/framework/bigcommerce/api/operations/get-page.ts b/framework/bigcommerce/api/operations/get-page.ts index a5bca327c..e8f852e92 100644 --- a/framework/bigcommerce/api/operations/get-page.ts +++ b/framework/bigcommerce/api/operations/get-page.ts @@ -11,14 +11,14 @@ export default function getPageOperation({ }: OperationContext) { async function getPage(opts: { variables: T['variables'] - config?: BigcommerceConfig + config?: Partial preview?: boolean }): Promise async function getPage( opts: { variables: T['variables'] - config?: BigcommerceConfig + config?: Partial preview?: boolean } & OperationOptions ): Promise @@ -31,13 +31,13 @@ export default function getPageOperation({ }: { url?: string variables: T['variables'] - config?: BigcommerceConfig + config?: Partial preview?: boolean }): Promise { - config = commerce.getConfig(config) + const cfg = commerce.getConfig(config) // RecursivePartial forces the method to check for every prop in the data, which is // required in case there's a custom `url` - const { data } = await config.storeApiFetch< + const { data } = await cfg.storeApiFetch< RecursivePartial<{ data: Page[] }> >(url || `/v3/content/pages?id=${variables.id}&include=body`) const firstPage = data?.[0] diff --git a/framework/bigcommerce/api/operations/get-product.ts b/framework/bigcommerce/api/operations/get-product.ts index 31de42ef6..fb356e952 100644 --- a/framework/bigcommerce/api/operations/get-product.ts +++ b/framework/bigcommerce/api/operations/get-product.ts @@ -73,14 +73,14 @@ export default function getAllProductPathsOperation({ }: OperationContext) { async function getProduct(opts: { variables: T['variables'] - config?: BigcommerceConfig + config?: Partial preview?: boolean }): Promise async function getProduct( opts: { variables: T['variables'] - config?: BigcommerceConfig + config?: Partial preview?: boolean } & OperationOptions ): Promise @@ -88,15 +88,14 @@ export default function getAllProductPathsOperation({ async function getProduct({ query = getProductQuery, variables: { slug, ...vars }, - config, + config: cfg, }: { query?: string variables: T['variables'] - config?: BigcommerceConfig + config?: Partial preview?: boolean }): Promise { - config = commerce.getConfig(config) - + const config = commerce.getConfig(cfg) const { locale } = config const variables: GetProductQueryVariables = { locale, diff --git a/framework/bigcommerce/api/operations/get-site-info.ts b/framework/bigcommerce/api/operations/get-site-info.ts index cca11ed74..afe5f3626 100644 --- a/framework/bigcommerce/api/operations/get-site-info.ts +++ b/framework/bigcommerce/api/operations/get-site-info.ts @@ -53,13 +53,13 @@ export default function getSiteInfoOperation({ commerce, }: OperationContext) { async function getSiteInfo(opts?: { - config?: BigcommerceConfig + config?: Partial preview?: boolean }): Promise async function getSiteInfo( opts: { - config?: BigcommerceConfig + config?: Partial preview?: boolean } & OperationOptions ): Promise @@ -69,15 +69,13 @@ export default function getSiteInfoOperation({ config, }: { query?: string - config?: BigcommerceConfig + config?: Partial preview?: boolean } = {}): Promise { - config = commerce.getConfig(config) + const cfg = commerce.getConfig(config) // RecursivePartial forces the method to check for every prop in the data, which is // required in case there's a custom `query` - const { data } = await config.fetch>( - query - ) + const { data } = await cfg.fetch>(query) const categories = data.site?.categoryTree const brands = data.site?.brands?.edges diff --git a/framework/bigcommerce/api/utils/create-api-handler.ts b/framework/bigcommerce/api/utils/create-api-handler.ts deleted file mode 100644 index c1d651d9b..000000000 --- a/framework/bigcommerce/api/utils/create-api-handler.ts +++ /dev/null @@ -1,58 +0,0 @@ -import type { NextApiHandler, NextApiRequest, NextApiResponse } from 'next' -import { BigcommerceConfig, getConfig } from '..' - -export type BigcommerceApiHandler< - T = any, - H extends BigcommerceHandlers = {}, - Options extends {} = {} -> = ( - req: NextApiRequest, - res: NextApiResponse>, - config: BigcommerceConfig, - handlers: H, - // Custom configs that may be used by a particular handler - options: Options -) => void | Promise - -export type BigcommerceHandler = (options: { - req: NextApiRequest - res: NextApiResponse> - config: BigcommerceConfig - body: Body -}) => void | Promise - -export type BigcommerceHandlers = { - [k: string]: BigcommerceHandler -} - -export type BigcommerceApiResponse = { - data: T | null - errors?: { message: string; code?: string }[] -} - -export default function createApiHandler< - T = any, - H extends BigcommerceHandlers = {}, - Options extends {} = {} ->( - handler: BigcommerceApiHandler, - handlers: H, - defaultOptions: Options -) { - return function getApiHandler({ - config, - operations, - options, - }: { - config?: BigcommerceConfig - operations?: Partial - options?: Options extends {} ? Partial : never - } = {}): NextApiHandler { - const ops = { ...handlers, ...operations } - const opts = { ...defaultOptions, ...options } - - return function apiHandler(req, res) { - return handler(req, res, getConfig(config), ops, opts) - } - } -} diff --git a/framework/bigcommerce/api/utils/fetch-graphql-api.ts b/framework/bigcommerce/api/utils/fetch-graphql-api.ts index a449b81e0..9c2eaa2ac 100644 --- a/framework/bigcommerce/api/utils/fetch-graphql-api.ts +++ b/framework/bigcommerce/api/utils/fetch-graphql-api.ts @@ -1,15 +1,15 @@ import { FetcherError } from '@commerce/utils/errors' import type { GraphQLFetcher } from '@commerce/api' -import { getConfig } from '..' +import { provider } from '..' import fetch from './fetch' +const { config } = provider const fetchGraphqlApi: GraphQLFetcher = async ( query: string, { variables, preview } = {}, fetchOptions ) => { // log.warn(query) - const config = getConfig() const res = await fetch(config.commerceUrl + (preview ? '/preview' : ''), { ...fetchOptions, method: 'POST', diff --git a/framework/bigcommerce/api/utils/fetch-store-api.ts b/framework/bigcommerce/api/utils/fetch-store-api.ts index 7e59b9f06..68817417e 100644 --- a/framework/bigcommerce/api/utils/fetch-store-api.ts +++ b/framework/bigcommerce/api/utils/fetch-store-api.ts @@ -1,13 +1,14 @@ import type { RequestInit, Response } from '@vercel/fetch' -import { getConfig } from '..' +import { provider } from '..' import { BigcommerceApiError, BigcommerceNetworkError } from './errors' import fetch from './fetch' +const { config } = provider + export default async function fetchStoreApi( endpoint: string, options?: RequestInit ): Promise { - const config = getConfig() let res: Response try { diff --git a/pages/[...pages].tsx b/pages/[...pages].tsx index 2fd13d674..3e6ef65c9 100644 --- a/pages/[...pages].tsx +++ b/pages/[...pages].tsx @@ -8,7 +8,6 @@ import { Text } from '@components/ui' import { Layout } from '@components/common' import getSlug from '@lib/get-slug' import { missingLocaleInPages } from '@lib/usage-warns' -import { getConfig } from '@framework/api' import { defaultPageProps } from '@lib/defaults' export async function getStaticProps({ @@ -16,7 +15,7 @@ export async function getStaticProps({ params, locale, }: GetStaticPropsContext<{ pages: string[] }>) { - const config = getConfig({ locale }) + const config = { locale } const { pages } = await commerce.getAllPages({ preview, config }) const path = params?.pages.join('/') const slug = locale ? `${locale}/${path}` : path diff --git a/pages/blog.tsx b/pages/blog.tsx index 10dba8c56..c1bdc4624 100644 --- a/pages/blog.tsx +++ b/pages/blog.tsx @@ -1,5 +1,4 @@ import type { GetStaticPropsContext } from 'next' -import { getConfig } from '@framework/api' import commerce from '@lib/api/commerce' import { Layout } from '@components/common' import { Container } from '@components/ui' @@ -8,7 +7,7 @@ export async function getStaticProps({ preview, locale, }: GetStaticPropsContext) { - const config = getConfig({ locale }) + const config = { locale } const { pages } = await commerce.getAllPages({ config, preview }) return { props: { pages }, diff --git a/pages/cart.tsx b/pages/cart.tsx index e318ee6e9..d14a7bfd2 100644 --- a/pages/cart.tsx +++ b/pages/cart.tsx @@ -1,5 +1,4 @@ import type { GetStaticPropsContext } from 'next' -import { getConfig } from '@framework/api' import useCart from '@framework/cart/use-cart' import usePrice from '@framework/product/use-price' import commerce from '@lib/api/commerce' @@ -12,7 +11,7 @@ export async function getStaticProps({ preview, locale, }: GetStaticPropsContext) { - const config = getConfig({ locale }) + const config = { locale } const { pages } = await commerce.getAllPages({ config, preview }) return { props: { pages }, diff --git a/pages/index.tsx b/pages/index.tsx index b33ed585b..87881d2b9 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -5,14 +5,11 @@ import { ProductCard } from '@components/product' // import HomeAllProductsGrid from '@components/common/HomeAllProductsGrid' import type { GetStaticPropsContext, InferGetStaticPropsType } from 'next' -import { getConfig } from '@framework/api' - export async function getStaticProps({ preview, locale, }: GetStaticPropsContext) { - const config = getConfig({ locale }) - + const config = { locale } const { products } = await commerce.getAllProducts({ variables: { first: 12 }, config, diff --git a/pages/orders.tsx b/pages/orders.tsx index f1dd630ac..c43ff9e5a 100644 --- a/pages/orders.tsx +++ b/pages/orders.tsx @@ -3,13 +3,12 @@ import commerce from '@lib/api/commerce' import { Bag } from '@components/icons' import { Layout } from '@components/common' import { Container, Text } from '@components/ui' -import { getConfig } from '@framework/api' export async function getStaticProps({ preview, locale, }: GetStaticPropsContext) { - const config = getConfig({ locale }) + const config = { locale } const { pages } = await commerce.getAllPages({ config, preview }) return { props: { pages }, diff --git a/pages/product/[slug].tsx b/pages/product/[slug].tsx index 6f47e5dfa..bf71ac21e 100644 --- a/pages/product/[slug].tsx +++ b/pages/product/[slug].tsx @@ -7,14 +7,13 @@ import { useRouter } from 'next/router' import commerce from '@lib/api/commerce' import { Layout } from '@components/common' import { ProductView } from '@components/product' -import { getConfig } from '@framework/api' export async function getStaticProps({ params, locale, preview, }: GetStaticPropsContext<{ slug: string }>) { - const config = getConfig({ locale }) + const config = { locale } const { pages } = await commerce.getAllPages({ config, preview }) const { product } = await commerce.getProduct({ variables: { slug: params!.slug }, diff --git a/pages/profile.tsx b/pages/profile.tsx index 0c9d7b26d..b73469fa5 100644 --- a/pages/profile.tsx +++ b/pages/profile.tsx @@ -1,5 +1,4 @@ import type { GetStaticPropsContext } from 'next' -import { getConfig } from '@framework/api' import useCustomer from '@framework/customer/use-customer' import commerce from '@lib/api/commerce' import { Layout } from '@components/common' @@ -9,7 +8,7 @@ export async function getStaticProps({ preview, locale, }: GetStaticPropsContext) { - const config = getConfig({ locale }) + const config = { locale } const { pages } = await commerce.getAllPages({ config, preview }) return { props: { pages }, diff --git a/pages/search.tsx b/pages/search.tsx index 46311a5dc..53452b74a 100644 --- a/pages/search.tsx +++ b/pages/search.tsx @@ -8,7 +8,6 @@ import { Layout } from '@components/common' import { ProductCard } from '@components/product' import { Container, Grid, Skeleton } from '@components/ui' -import { getConfig } from '@framework/api' import useSearch from '@framework/product/use-search' import commerce from '@lib/api/commerce' import rangeMap from '@lib/range-map' @@ -36,7 +35,7 @@ export async function getStaticProps({ preview, locale, }: GetStaticPropsContext) { - const config = getConfig({ locale }) + const config = { locale } const { pages } = await commerce.getAllPages({ config, preview }) const { categories, brands } = await commerce.getSiteInfo({ config, preview }) return { diff --git a/pages/wishlist.tsx b/pages/wishlist.tsx index ef49dfff3..9927c536a 100644 --- a/pages/wishlist.tsx +++ b/pages/wishlist.tsx @@ -4,7 +4,6 @@ import { Heart } from '@components/icons' import { Layout } from '@components/common' import { Text, Container } from '@components/ui' import { defaultPageProps } from '@lib/defaults' -import { getConfig } from '@framework/api' import { useCustomer } from '@framework/customer' import { WishlistCard } from '@components/wishlist' import useWishlist from '@framework/wishlist/use-wishlist' @@ -20,7 +19,7 @@ export async function getStaticProps({ } } - const config = getConfig({ locale }) + const config = { locale } const { pages } = await commerce.getAllPages({ config, preview }) return { props: {