Update Swell Provider (#359)

* fix update cart item

* update types

* update checkout

* update get-page, cleanup types

* revert change to incorrect file

* cleanup

Co-authored-by: Greg Hoskin <greghoskin@Gregs-MacBook-Pro.local>
This commit is contained in:
ghoskin
2021-06-14 13:37:18 -07:00
committed by GitHub
parent ddd4631ade
commit 4d85b43a30
55 changed files with 503 additions and 545 deletions

View File

@@ -1,28 +0,0 @@
import { CollectionEdge } from '../schema'
import { getConfig, SwellConfig } from '../api'
const getAllCollections = async (options?: {
variables?: any
config: SwellConfig
preview?: boolean
}) => {
let { config, variables = { limit: 25 } } = options ?? {}
config = getConfig(config)
const response = await config.fetch('categories', 'list', { variables })
const edges = response.results ?? []
const categories = edges.map(
({ node: { id: entityId, title: name, handle } }: CollectionEdge) => ({
entityId,
name,
path: `/${handle}`,
})
)
return {
categories,
}
}
export default getAllCollections

View File

@@ -1,39 +0,0 @@
import { SwellProduct } from '../types'
import { getConfig, SwellConfig } from '../api'
type ProductPath = {
path: string
}
export type ProductPathNode = {
node: ProductPath
}
type ReturnType = {
products: ProductPathNode[]
}
const getAllProductPaths = async (options?: {
variables?: any
config?: SwellConfig
preview?: boolean
}): Promise<ReturnType> => {
let { config, variables = [{ limit: 100 }] } = options ?? {}
config = getConfig(config)
const { results } = await config.fetch('products', 'list', [
{
limit: variables.first,
},
])
return {
products: results?.map(({ slug: handle }: SwellProduct) => ({
node: {
path: `/${handle}`,
},
})),
}
}
export default getAllProductPaths

View File

@@ -1,36 +0,0 @@
import { getConfig, SwellConfig } from '../api'
import { normalizeProduct } from '../utils/normalize'
import { Product } from '@commerce/types'
import { SwellProduct } from '../types'
type Variables = {
first?: number
field?: string
}
type ReturnType = {
products: Product[]
}
const getAllProducts = async (options: {
variables?: Variables
config?: SwellConfig
preview?: boolean
}): Promise<ReturnType> => {
let { config, variables = { first: 250 } } = options ?? {}
config = getConfig(config)
const { results } = await config.fetch('products', 'list', [
{
limit: variables.first,
},
])
const products = results.map((product: SwellProduct) =>
normalizeProduct(product)
)
return {
products,
}
}
export default getAllProducts

View File

@@ -1,32 +0,0 @@
import { GraphQLFetcherResult } from '@commerce/api'
import { getConfig, SwellConfig } from '../api'
import { normalizeProduct } from '../utils'
type Variables = {
slug: string
}
type ReturnType = {
product: any
}
const getProduct = async (options: {
variables: Variables
config: SwellConfig
preview?: boolean
}): Promise<ReturnType> => {
let { config, variables } = options ?? {}
config = getConfig(config)
const product = await config.fetch('products', 'get', [variables.slug])
if (product && product.variants) {
product.variants = product.variants?.results
}
return {
product: product ? normalizeProduct(product) : null,
}
}
export default getProduct

View File

@@ -0,0 +1,2 @@
export { default as usePrice } from './use-price'
export { default as useSearch } from './use-search'

View File

@@ -1,11 +1,8 @@
import { SWRHook } from '@commerce/utils/types'
import useSearch, { UseSearch } from '@commerce/product/use-search'
import { normalizeProduct } from '../utils'
import { Product } from '@commerce/types'
import { SwellProduct } from '../types'
import type { SearchProductsHook } from '../types/product'
export default useSearch as UseSearch<typeof handler>
@@ -16,18 +13,9 @@ export type SearchProductsInput = {
sort?: string
}
export type SearchProductsData = {
products: Product[]
found: boolean
}
export const handler: SWRHook<
SearchProductsData,
SearchProductsInput,
SearchProductsInput
> = {
export const handler: SWRHook<SearchProductsHook> = {
fetchOptions: {
query: 'products', // String(Math.random()),
query: 'products',
method: 'list',
},
async fetcher({ input, options, fetch }) {