4
0
forked from crowetic/commerce
Catalin Pinte 6c2610584d
Update types (#831)
* Update product types

* Cart types progress, add zod & initial schema validator

* Update normalize.ts

* Update with-schema-parser.ts

* Updated types, schemas & providers

* Fix providers after schema parse errors

* Fix paths

* More provider fixes

* Fix kibocommerce & commercejs

* Add customer updated types & fixes

* Add checkout & customer types

* Import core types only from commerce

* Update tsconfig.json

* Convert hooks interfaces to types

* Requested changes

* Change to relative paths

* Move Zod dependency
2022-10-05 09:02:29 +03:00

54 lines
1.4 KiB
TypeScript

import { SWRHook } from '@vercel/commerce/utils/types'
import useSearch, { UseSearch } from '@vercel/commerce/product/use-search'
import { SearchProductsHook } from '@vercel/commerce/types/product'
import type { CommercejsProduct } from '../types'
import { getProductSearchVariables } from '../utils/product-search'
import { normalizeProduct } from '../utils/normalize-product'
export default useSearch as UseSearch<typeof handler>
export const handler: SWRHook<SearchProductsHook> = {
fetchOptions: {
query: 'products',
method: 'list',
},
async fetcher({ input, options, fetch }) {
const { data, meta } = await fetch<{
data: CommercejsProduct[]
meta: {
pagination: {
total: number
}
}
}>({
query: options.query,
method: options.method,
variables: getProductSearchVariables(input),
})
const formattedProducts =
data?.map((product) => normalizeProduct(product)) || []
return {
products: formattedProducts,
found: meta.pagination.total > 0,
}
},
useHook:
({ useData }) =>
(input = {}) => {
return useData({
input: [
['search', input.search],
['categoryId', input.categoryId],
['brandId', input.brandId],
['sort', input.sort],
],
swrOptions: {
revalidateOnFocus: false,
...input.swrOptions,
},
})
},
}