cleanup, add sorting

This commit is contained in:
Greg Hoskin
2021-04-25 14:20:58 -05:00
parent 6a9c6c3bca
commit a409c373c4
52 changed files with 74 additions and 618 deletions

View File

@@ -1,41 +1,41 @@
import type { NextApiHandler, NextApiRequest, NextApiResponse } from 'next'
import { SwellConfig, getConfig } from '..'
export type ShopifyApiHandler<
export type SwellApiHandler<
T = any,
H extends ShopifyHandlers = {},
H extends SwellHandlers = {},
Options extends {} = {}
> = (
req: NextApiRequest,
res: NextApiResponse<ShopifyApiResponse<T>>,
res: NextApiResponse<SwellApiResponse<T>>,
config: SwellConfig,
handlers: H,
// Custom configs that may be used by a particular handler
options: Options
) => void | Promise<void>
export type ShopifyHandler<T = any, Body = null> = (options: {
export type SwellHandler<T = any, Body = null> = (options: {
req: NextApiRequest
res: NextApiResponse<ShopifyApiResponse<T>>
res: NextApiResponse<SwellApiResponse<T>>
config: SwellConfig
body: Body
}) => void | Promise<void>
export type ShopifyHandlers<T = any> = {
[k: string]: ShopifyHandler<T, any>
export type SwellHandlers<T = any> = {
[k: string]: SwellHandler<T, any>
}
export type ShopifyApiResponse<T> = {
export type SwellApiResponse<T> = {
data: T | null
errors?: { message: string; code?: string }[]
}
export default function createApiHandler<
T = any,
H extends ShopifyHandlers = {},
H extends SwellHandlers = {},
Options extends {} = {}
>(
handler: ShopifyApiHandler<T, H, Options>,
handler: SwellApiHandler<T, H, Options>,
handlers: H,
defaultOptions: Options
) {

View File

@@ -4,6 +4,7 @@ import { SwellConfig } from '..'
const fetchAllProducts = async ({
config,
query,
method,
variables,
acc = [],
cursor,
@@ -14,12 +15,13 @@ const fetchAllProducts = async ({
variables?: any
cursor?: string
}): Promise<ProductEdge[]> => {
const { data } = await config.fetch(query, {
variables: { ...variables, cursor },
})
// const response = await config.fetch(query, {
// variables: { ...variables, cursor },
// })
const response = await config.fetchSwell('products', 'list', [{ limit: 100 }])
const edges: ProductEdge[] = data.products?.edges ?? []
const hasNextPage = data.products?.pageInfo?.hasNextPage
const edges: ProductEdge[] = response.results ?? []
const hasNextPage = response.results.length < response.count
acc = acc.concat(edges)
if (hasNextPage) {

View File

@@ -1,34 +0,0 @@
import type { GraphQLFetcher } from '@commerce/api'
import fetch from './fetch'
import { API_URL, API_TOKEN } from '../../const'
import { getError } from '../../utils/handle-fetch-response'
const fetchGraphqlApi: GraphQLFetcher = async (
query: string,
{ variables } = {},
fetchOptions
) => {
const res = await fetch(API_URL, {
...fetchOptions,
method: 'POST',
headers: {
'X-Shopify-Storefront-Access-Token': API_TOKEN!,
...fetchOptions?.headers,
'Content-Type': 'application/json',
},
body: JSON.stringify({
query,
variables,
}),
})
const { data, errors, status } = await res.json()
if (errors) {
throw getError(errors, status)
}
return { data, res }
}
export default fetchGraphqlApi

View File

@@ -6,7 +6,6 @@ const fetchSwellApi = async (
variables: [] = []
) => {
const { swell } = swellConfig
return await swell[query][method](...variables)
}
export default fetchSwellApi