diff --git a/framework/swell/api/operations/get-all-collections.ts b/framework/swell/api/operations/get-all-collections.ts deleted file mode 100644 index ed906ffcc..000000000 --- a/framework/swell/api/operations/get-all-collections.ts +++ /dev/null @@ -1,21 +0,0 @@ -import Client from 'shopify-buy' -import { SwellConfig } from '../index' - -type Options = { - config: SwellConfig -} - -const getAllCollections = async (options: Options) => { - const { config } = options - - const client = Client.buildClient({ - storefrontAccessToken: config.apiToken, - domain: config.commerceUrl, - }) - - const res = await client.collection.fetchAllWithProducts() - - return JSON.parse(JSON.stringify(res)) -} - -export default getAllCollections diff --git a/framework/swell/api/utils/fetch-all-products.ts b/framework/swell/api/utils/fetch-all-products.ts index 859ec0212..38ba5f6e3 100644 --- a/framework/swell/api/utils/fetch-all-products.ts +++ b/framework/swell/api/utils/fetch-all-products.ts @@ -1,5 +1,5 @@ -import { ProductEdge } from '../../schema' import { SwellConfig } from '..' +import { SwellProduct } from '../../types' const fetchAllProducts = async ({ config, @@ -7,35 +7,17 @@ const fetchAllProducts = async ({ method, variables, acc = [], - cursor, }: { config: SwellConfig query: string - acc?: ProductEdge[] + method: string + acc?: SwellProduct[] variables?: any cursor?: string -}): Promise => { - // const response = await config.fetch(query, { - // variables: { ...variables, cursor }, - // }) - const response = await config.fetchSwell('products', 'list', [{ limit: 100 }]) +}): Promise => { + const response = await config.fetchSwell(query, method, variables) - const edges: ProductEdge[] = response.results ?? [] - const hasNextPage = response.results.length < response.count - acc = acc.concat(edges) - - if (hasNextPage) { - const cursor = edges.pop()?.cursor - if (cursor) { - return fetchAllProducts({ - config, - query, - variables, - acc, - cursor, - }) - } - } + acc = acc.concat(response.results) return acc } diff --git a/framework/swell/cart/utils/checkout-create.ts b/framework/swell/cart/utils/checkout-create.ts index cc08007f4..5ee3833c5 100644 --- a/framework/swell/cart/utils/checkout-create.ts +++ b/framework/swell/cart/utils/checkout-create.ts @@ -8,6 +8,14 @@ export const checkoutCreate = async (fetch: any) => { method: 'get', }) + if (!cart) { + const cart = await fetch({ + query: 'cart', + method: 'setItems', + variables: [[]], + }) + } + const checkoutUrl = cart?.checkout_url if (checkoutUrl) { diff --git a/framework/swell/product/get-all-product-paths.ts b/framework/swell/product/get-all-product-paths.ts index fad11f8c2..3b95798e4 100644 --- a/framework/swell/product/get-all-product-paths.ts +++ b/framework/swell/product/get-all-product-paths.ts @@ -1,7 +1,5 @@ -import { Product } from '@commerce/types' import { getConfig, SwellConfig } from '../api' import fetchAllProducts from '../api/utils/fetch-all-products' -import { ProductEdge } from '../schema' type ProductPath = { path: string @@ -20,7 +18,7 @@ const getAllProductPaths = async (options?: { config?: SwellConfig preview?: boolean }): Promise => { - let { config, variables = { limit: 100 } } = options ?? {} + let { config, variables = [{ limit: 100 }] } = options ?? {} config = getConfig(config) const products = await fetchAllProducts({ diff --git a/framework/swell/product/get-product.ts b/framework/swell/product/get-product.ts index 8cd59f6e0..15fde1cfa 100644 --- a/framework/swell/product/get-product.ts +++ b/framework/swell/product/get-product.ts @@ -19,11 +19,11 @@ const getProduct = async (options: { config = getConfig(config) const product = await config.fetchSwell('products', 'get', [variables.slug]) - if (product.variants) { + if (product && product.variants) { product.variants = product.variants?.results } return { - product: normalizeProduct(product), + product: product ? normalizeProduct(product) : null, } } diff --git a/framework/swell/utils/get-vendors.ts b/framework/swell/utils/get-vendors.ts index a96d9dccd..9f2184fc3 100644 --- a/framework/swell/utils/get-vendors.ts +++ b/framework/swell/utils/get-vendors.ts @@ -13,7 +13,7 @@ export type Brands = BrandEdge[] const getVendors = async (config: SwellConfig) => { const vendors: [string] = - (await config.fetchSwell('attributes', 'get', ['brand'])).values ?? [] + (await config.fetchSwell('attributes', 'get', ['brand']))?.values ?? [] return [...new Set(vendors)].map((v) => ({ node: { diff --git a/framework/swell/utils/handle-fetch-response.ts b/framework/swell/utils/handle-fetch-response.ts index 96ceb2469..2688c9c70 100644 --- a/framework/swell/utils/handle-fetch-response.ts +++ b/framework/swell/utils/handle-fetch-response.ts @@ -1,28 +1,19 @@ -import { FetcherError } from '@commerce/utils/errors' +import { CommerceError } from '@commerce/utils/errors' -export function getError(errors: any[], status: number) { - errors = errors ?? [{ message: 'Failed to fetch Swell API' }] - return new FetcherError({ errors, status }) +type SwellFetchResponse = { + error: { + message: string + code?: string + } } -export async function getAsyncError(res: Response) { - const data = await res.json() - return getError(data.errors, res.status) -} - -const handleFetchResponse = async (res: Response) => { - // if (res.ok) { - // const { data, errors } = await res.json() - - // if (errors && errors.length) { - // throw getError(errors, res.status) - // } - - // return data - // } - if (res) return res - - throw await getAsyncError(res) +const handleFetchResponse = async (res: SwellFetchResponse) => { + if (res) { + if (res.error) { + throw new CommerceError(res.error) + } + return res + } } export default handleFetchResponse diff --git a/framework/swell/utils/normalize.ts b/framework/swell/utils/normalize.ts index 57215d485..a7a7be1ce 100644 --- a/framework/swell/utils/normalize.ts +++ b/framework/swell/utils/normalize.ts @@ -76,7 +76,7 @@ const normalizeProductVariants = ( productOptions: normalizedProductOption[] ) => { return variants?.map( - ({ id, name, price, option_value_ids: optionValueIds }) => { + ({ id, name, price, option_value_ids: optionValueIds = [] }) => { const values = name .split(',') .map((i) => ({ name: i.trim(), label: i.trim() })) @@ -167,7 +167,7 @@ export function normalizeCart({ createdAt: date_created, currency: { code: currency }, taxesIncluded: tax_included_total > 0, - lineItems: items?.map(normalizeLineItem), + lineItems: items?.map(normalizeLineItem) ?? [], lineItemsSubtotalPrice: +sub_total, subtotalPrice: +sub_total, totalPrice: grand_total,