diff --git a/framework/swell/customer/use-customer.tsx b/framework/swell/customer/use-customer.tsx index ec3dd99fd..7b84795aa 100644 --- a/framework/swell/customer/use-customer.tsx +++ b/framework/swell/customer/use-customer.tsx @@ -12,13 +12,9 @@ export const handler: SWRHook = { method: 'get', }, async fetcher({ options, fetch }) { - // console.log('STORE_ID', STORE_ID, 'PUBLIC_KEY', PUBLIC_KEY); - // const data = await swell.account.get() const data = await fetch({ ...options, - // variables: { customerAccessToken: getCustomerToken() }, }) - console.log(`Customer data ${data}`) return data ? normalizeCustomer(data) : null }, useHook: ({ useData }) => (input) => { diff --git a/framework/swell/fetcher.ts b/framework/swell/fetcher.ts index f10032957..e52d8fd54 100644 --- a/framework/swell/fetcher.ts +++ b/framework/swell/fetcher.ts @@ -9,15 +9,13 @@ const fetcher: Fetcher = async ({ method = 'get', variables, query }) => { const arg1 = variables[0] const arg2 = variables[1] const response = await swell[query][method](arg1, arg2) - console.log(response) return handleFetchResponse(response) } else { const response = await swell[query][method](variables) - console.log(response) return handleFetchResponse(response) } } - if (query) { + if (query in swell) { return await callSwell() } } diff --git a/framework/swell/next.config.js b/framework/swell/next.config.js index e9d48c02c..f6ac38345 100644 --- a/framework/swell/next.config.js +++ b/framework/swell/next.config.js @@ -3,6 +3,6 @@ const commerce = require('./commerce.config.json') module.exports = { commerce, images: { - domains: ['cdn.shopify.com'], + domains: ['cdn.schema.io'], }, } diff --git a/framework/swell/product/get-all-products.ts b/framework/swell/product/get-all-products.ts index 78e081238..8ca85eecd 100644 --- a/framework/swell/product/get-all-products.ts +++ b/framework/swell/product/get-all-products.ts @@ -21,16 +21,8 @@ const getAllProducts = async (options: { }): Promise => { let { config, variables = { first: 250 } } = options ?? {} config = getConfig(config) - - const { data }: GraphQLFetcherResult = await config.fetch( - getAllProductsQuery, - { variables } - ) - - const products = - data.products?.edges?.map(({ node: p }: ProductEdge) => - normalizeProduct(p) - ) ?? [] + const { results } = await config.fetchSwell('products', 'get') + const products = results.map((product) => normalizeProduct(product)) ?? [] return { products, diff --git a/framework/swell/product/use-search.tsx b/framework/swell/product/use-search.tsx index 425df9e83..a00e39dc9 100644 --- a/framework/swell/product/use-search.tsx +++ b/framework/swell/product/use-search.tsx @@ -1,13 +1,7 @@ import { SWRHook } from '@commerce/utils/types' import useSearch, { UseSearch } from '@commerce/product/use-search' -import { ProductEdge } from '../schema' -import { - getAllProductsQuery, - getCollectionProductsQuery, - getSearchVariables, - normalizeProduct, -} from '../utils' +import { getAllProductsQuery, normalizeProduct } from '../utils' import { Product } from '@commerce/types' @@ -36,28 +30,17 @@ export const handler: SWRHook< async fetcher({ input, options, fetch }) { const { categoryId, brandId } = input - const data = await fetch({ - query: categoryId ? getCollectionProductsQuery : options.query, - method: options?.method, - variables: getSearchVariables(input), + const { results, count: found } = await fetch({ + query: 'products', + method: 'get', + // variables: { categoryId }, }) - let edges - - if (categoryId) { - edges = data.node?.products?.edges ?? [] - if (brandId) { - edges = edges.filter( - ({ node: { vendor } }: ProductEdge) => vendor === brandId - ) - } - } else { - edges = data.products?.edges ?? [] - } + const products = results.map((product) => normalizeProduct(product)) return { - products: edges.map(({ node }: ProductEdge) => normalizeProduct(node)), - found: !!edges.length, + products, + found, } }, useHook: ({ useData }) => (input = {}) => { diff --git a/framework/swell/schema.d.ts b/framework/swell/schema.d.ts index b1b23a3e5..8ffa23f73 100644 --- a/framework/swell/schema.d.ts +++ b/framework/swell/schema.d.ts @@ -4147,7 +4147,7 @@ export type ProductOption = Node & { /** The product option’s name. */ name: Scalars['String'] /** The corresponding value to the product option name. */ - values: Array + values: Array } /** The price range of the product. */ diff --git a/framework/swell/types.ts b/framework/swell/types.ts index 579402f97..2878295e1 100644 --- a/framework/swell/types.ts +++ b/framework/swell/types.ts @@ -1,6 +1,11 @@ import * as Core from '@commerce/types' import { CheckoutLineItem } from './schema' +export interface SwellProduct extends Core.Product { + name: string + slug: string +} + export interface SwellCustomer extends Core.Customer { first_name: string last_name: string diff --git a/framework/swell/utils/normalize.ts b/framework/swell/utils/normalize.ts index 17579c2d3..6dbb6d708 100644 --- a/framework/swell/utils/normalize.ts +++ b/framework/swell/utils/normalize.ts @@ -1,5 +1,6 @@ import { Product } from '@commerce/types' import { Customer } from '@commerce/types' +import { fileURLToPath } from 'node:url' import { Product as ShopifyProduct, @@ -12,7 +13,7 @@ import { ProductOption, } from '../schema' -import type { Cart, LineItem, SwellCustomer } from '../types' +import type { Cart, LineItem, SwellCustomer, SwellProduct } from '../types' const money = ({ amount, currencyCode }: MoneyV2) => { return { @@ -32,7 +33,7 @@ const normalizeProductOption = ({ displayName, values: values.map((value) => { let output: any = { - label: value, + label: value.name, } if (displayName === 'Color') { output = { @@ -45,9 +46,9 @@ const normalizeProductOption = ({ } } -const normalizeProductImages = ({ edges }: ImageConnection) => - edges?.map(({ node: { originalSrc: url, ...rest } }) => ({ - url, +const normalizeProductImages = (images) => + images?.map(({ file, ...rest }) => ({ + url: file.url, ...rest, })) @@ -73,16 +74,16 @@ const normalizeProductVariants = ({ edges }: ProductVariantConnection) => { ) } -export function normalizeProduct(productNode: ShopifyProduct): Product { +export function normalizeProduct(productNode: SwellProduct): Product { const { id, - title: name, + name, vendor, images, variants, description, - handle, - priceRange, + slug, + price, options, ...rest } = productNode @@ -92,9 +93,9 @@ export function normalizeProduct(productNode: ShopifyProduct): Product { name, vendor, description, - path: `/${handle}`, - slug: handle?.replace(/^\/+|\/+$/g, ''), - price: money(priceRange?.minVariantPrice), + path: `/${slug}`, + slug, + price, images: normalizeProductImages(images), variants: variants ? normalizeProductVariants(variants) : [], options: options ? options.map((o) => normalizeProductOption(o)) : [],