4
0
forked from crowetic/commerce

update product search hook. whitelist cdn

This commit is contained in:
Greg Hoskin 2021-03-29 17:49:14 -06:00
parent e4c1050880
commit 1ebf458fb2
8 changed files with 31 additions and 56 deletions

View File

@ -12,13 +12,9 @@ export const handler: SWRHook<Customer | null> = {
method: 'get', method: 'get',
}, },
async fetcher({ options, fetch }) { async fetcher({ options, fetch }) {
// console.log('STORE_ID', STORE_ID, 'PUBLIC_KEY', PUBLIC_KEY);
// const data = await swell.account.get()
const data = await fetch<any | null>({ const data = await fetch<any | null>({
...options, ...options,
// variables: { customerAccessToken: getCustomerToken() },
}) })
console.log(`Customer data ${data}`)
return data ? normalizeCustomer(data) : null return data ? normalizeCustomer(data) : null
}, },
useHook: ({ useData }) => (input) => { useHook: ({ useData }) => (input) => {

View File

@ -9,15 +9,13 @@ const fetcher: Fetcher = async ({ method = 'get', variables, query }) => {
const arg1 = variables[0] const arg1 = variables[0]
const arg2 = variables[1] const arg2 = variables[1]
const response = await swell[query][method](arg1, arg2) const response = await swell[query][method](arg1, arg2)
console.log(response)
return handleFetchResponse(response) return handleFetchResponse(response)
} else { } else {
const response = await swell[query][method](variables) const response = await swell[query][method](variables)
console.log(response)
return handleFetchResponse(response) return handleFetchResponse(response)
} }
} }
if (query) { if (query in swell) {
return await callSwell() return await callSwell()
} }
} }

View File

@ -3,6 +3,6 @@ const commerce = require('./commerce.config.json')
module.exports = { module.exports = {
commerce, commerce,
images: { images: {
domains: ['cdn.shopify.com'], domains: ['cdn.schema.io'],
}, },
} }

View File

@ -21,16 +21,8 @@ const getAllProducts = async (options: {
}): Promise<ReturnType> => { }): Promise<ReturnType> => {
let { config, variables = { first: 250 } } = options ?? {} let { config, variables = { first: 250 } } = options ?? {}
config = getConfig(config) config = getConfig(config)
const { results } = await config.fetchSwell('products', 'get')
const { data }: GraphQLFetcherResult = await config.fetch( const products = results.map((product) => normalizeProduct(product)) ?? []
getAllProductsQuery,
{ variables }
)
const products =
data.products?.edges?.map(({ node: p }: ProductEdge) =>
normalizeProduct(p)
) ?? []
return { return {
products, products,

View File

@ -1,13 +1,7 @@
import { SWRHook } from '@commerce/utils/types' import { SWRHook } from '@commerce/utils/types'
import useSearch, { UseSearch } from '@commerce/product/use-search' import useSearch, { UseSearch } from '@commerce/product/use-search'
import { ProductEdge } from '../schema' import { getAllProductsQuery, normalizeProduct } from '../utils'
import {
getAllProductsQuery,
getCollectionProductsQuery,
getSearchVariables,
normalizeProduct,
} from '../utils'
import { Product } from '@commerce/types' import { Product } from '@commerce/types'
@ -36,28 +30,17 @@ export const handler: SWRHook<
async fetcher({ input, options, fetch }) { async fetcher({ input, options, fetch }) {
const { categoryId, brandId } = input const { categoryId, brandId } = input
const data = await fetch({ const { results, count: found } = await fetch({
query: categoryId ? getCollectionProductsQuery : options.query, query: 'products',
method: options?.method, method: 'get',
variables: getSearchVariables(input), // variables: { categoryId },
}) })
let edges const products = results.map((product) => normalizeProduct(product))
if (categoryId) {
edges = data.node?.products?.edges ?? []
if (brandId) {
edges = edges.filter(
({ node: { vendor } }: ProductEdge) => vendor === brandId
)
}
} else {
edges = data.products?.edges ?? []
}
return { return {
products: edges.map(({ node }: ProductEdge) => normalizeProduct(node)), products,
found: !!edges.length, found,
} }
}, },
useHook: ({ useData }) => (input = {}) => { useHook: ({ useData }) => (input = {}) => {

View File

@ -4147,7 +4147,7 @@ export type ProductOption = Node & {
/** The product options name. */ /** The product options name. */
name: Scalars['String'] name: Scalars['String']
/** The corresponding value to the product option name. */ /** The corresponding value to the product option name. */
values: Array<Scalars['String']> values: Array<Scalars['Object']>
} }
/** The price range of the product. */ /** The price range of the product. */

View File

@ -1,6 +1,11 @@
import * as Core from '@commerce/types' import * as Core from '@commerce/types'
import { CheckoutLineItem } from './schema' import { CheckoutLineItem } from './schema'
export interface SwellProduct extends Core.Product {
name: string
slug: string
}
export interface SwellCustomer extends Core.Customer { export interface SwellCustomer extends Core.Customer {
first_name: string first_name: string
last_name: string last_name: string

View File

@ -1,5 +1,6 @@
import { Product } from '@commerce/types' import { Product } from '@commerce/types'
import { Customer } from '@commerce/types' import { Customer } from '@commerce/types'
import { fileURLToPath } from 'node:url'
import { import {
Product as ShopifyProduct, Product as ShopifyProduct,
@ -12,7 +13,7 @@ import {
ProductOption, ProductOption,
} from '../schema' } from '../schema'
import type { Cart, LineItem, SwellCustomer } from '../types' import type { Cart, LineItem, SwellCustomer, SwellProduct } from '../types'
const money = ({ amount, currencyCode }: MoneyV2) => { const money = ({ amount, currencyCode }: MoneyV2) => {
return { return {
@ -32,7 +33,7 @@ const normalizeProductOption = ({
displayName, displayName,
values: values.map((value) => { values: values.map((value) => {
let output: any = { let output: any = {
label: value, label: value.name,
} }
if (displayName === 'Color') { if (displayName === 'Color') {
output = { output = {
@ -45,9 +46,9 @@ const normalizeProductOption = ({
} }
} }
const normalizeProductImages = ({ edges }: ImageConnection) => const normalizeProductImages = (images) =>
edges?.map(({ node: { originalSrc: url, ...rest } }) => ({ images?.map(({ file, ...rest }) => ({
url, url: file.url,
...rest, ...rest,
})) }))
@ -73,16 +74,16 @@ const normalizeProductVariants = ({ edges }: ProductVariantConnection) => {
) )
} }
export function normalizeProduct(productNode: ShopifyProduct): Product { export function normalizeProduct(productNode: SwellProduct): Product {
const { const {
id, id,
title: name, name,
vendor, vendor,
images, images,
variants, variants,
description, description,
handle, slug,
priceRange, price,
options, options,
...rest ...rest
} = productNode } = productNode
@ -92,9 +93,9 @@ export function normalizeProduct(productNode: ShopifyProduct): Product {
name, name,
vendor, vendor,
description, description,
path: `/${handle}`, path: `/${slug}`,
slug: handle?.replace(/^\/+|\/+$/g, ''), slug,
price: money(priceRange?.minVariantPrice), price,
images: normalizeProductImages(images), images: normalizeProductImages(images),
variants: variants ? normalizeProductVariants(variants) : [], variants: variants ? normalizeProductVariants(variants) : [],
options: options ? options.map((o) => normalizeProductOption(o)) : [], options: options ? options.map((o) => normalizeProductOption(o)) : [],