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',
},
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>({
...options,
// variables: { customerAccessToken: getCustomerToken() },
})
console.log(`Customer data ${data}`)
return data ? normalizeCustomer(data) : null
},
useHook: ({ useData }) => (input) => {

View File

@ -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()
}
}

View File

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

View File

@ -21,16 +21,8 @@ const getAllProducts = async (options: {
}): Promise<ReturnType> => {
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,

View File

@ -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 = {}) => {

View File

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

View File

@ -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

View File

@ -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)) : [],