handle empty cart, variants, options, errors

This commit is contained in:
Greg Hoskin
2021-04-30 18:35:19 -05:00
parent b83753f0b9
commit ffbcce2a9e
8 changed files with 33 additions and 75 deletions

View File

@@ -1,21 +0,0 @@
import Client from 'shopify-buy'
import { SwellConfig } from '../index'
type Options = {
config: SwellConfig
}
const getAllCollections = async (options: Options) => {
const { config } = options
const client = Client.buildClient({
storefrontAccessToken: config.apiToken,
domain: config.commerceUrl,
})
const res = await client.collection.fetchAllWithProducts()
return JSON.parse(JSON.stringify(res))
}
export default getAllCollections

View File

@@ -1,5 +1,5 @@
import { ProductEdge } from '../../schema'
import { SwellConfig } from '..'
import { SwellProduct } from '../../types'
const fetchAllProducts = async ({
config,
@@ -7,35 +7,17 @@ const fetchAllProducts = async ({
method,
variables,
acc = [],
cursor,
}: {
config: SwellConfig
query: string
acc?: ProductEdge[]
method: string
acc?: SwellProduct[]
variables?: any
cursor?: string
}): Promise<ProductEdge[]> => {
// const response = await config.fetch(query, {
// variables: { ...variables, cursor },
// })
const response = await config.fetchSwell('products', 'list', [{ limit: 100 }])
}): Promise<SwellProduct[]> => {
const response = await config.fetchSwell(query, method, variables)
const edges: ProductEdge[] = response.results ?? []
const hasNextPage = response.results.length < response.count
acc = acc.concat(edges)
if (hasNextPage) {
const cursor = edges.pop()?.cursor
if (cursor) {
return fetchAllProducts({
config,
query,
variables,
acc,
cursor,
})
}
}
acc = acc.concat(response.results)
return acc
}