mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
cleanup, add sorting
This commit is contained in:
@@ -1,12 +1,10 @@
|
||||
import createApiHandler, {
|
||||
ShopifyApiHandler,
|
||||
} from '../utils/create-api-handler'
|
||||
import createApiHandler, { SwellApiHandler } from '../utils/create-api-handler'
|
||||
|
||||
import { SWELL_CHECKOUT_URL_COOKIE } from '../../const'
|
||||
|
||||
import { getConfig } from '..'
|
||||
|
||||
const checkoutApi: ShopifyApiHandler<any> = async (req, res, config) => {
|
||||
const checkoutApi: SwellApiHandler<any> = async (req, res, config) => {
|
||||
config = getConfig()
|
||||
|
||||
const { cookies } = req
|
||||
|
@@ -1,26 +1,12 @@
|
||||
import type { CommerceAPIConfig } from '@commerce/api'
|
||||
|
||||
import {
|
||||
API_URL,
|
||||
API_TOKEN,
|
||||
SHOPIFY_CHECKOUT_ID_COOKIE,
|
||||
SHOPIFY_CUSTOMER_TOKEN_COOKIE,
|
||||
SHOPIFY_COOKIE_EXPIRE,
|
||||
SWELL_CHECKOUT_ID_COOKIE,
|
||||
SWELL_CUSTOMER_TOKEN_COOKIE,
|
||||
SWELL_COOKIE_EXPIRE,
|
||||
} from '../const'
|
||||
|
||||
if (!API_URL) {
|
||||
throw new Error(
|
||||
`The environment variable NEXT_PUBLIC_SHOPIFY_STORE_DOMAIN is missing and it's required to access your store`
|
||||
)
|
||||
}
|
||||
|
||||
if (!API_TOKEN) {
|
||||
throw new Error(
|
||||
`The environment variable NEXT_PUBLIC_SHOPIFY_STOREFRONT_ACCESS_TOKEN is missing and it's required to access your store`
|
||||
)
|
||||
}
|
||||
|
||||
import fetchGraphqlApi from './utils/fetch-graphql-api'
|
||||
import fetcher from '../fetcher'
|
||||
import fetchSwellApi from './utils/fetch-swell-api'
|
||||
|
||||
export interface SwellConfig extends CommerceAPIConfig {
|
||||
@@ -48,13 +34,13 @@ export class Config {
|
||||
|
||||
const config = new Config({
|
||||
locale: 'en-US',
|
||||
commerceUrl: API_URL,
|
||||
apiToken: API_TOKEN!,
|
||||
cartCookie: SHOPIFY_CHECKOUT_ID_COOKIE,
|
||||
cartCookieMaxAge: SHOPIFY_COOKIE_EXPIRE,
|
||||
commerceUrl: '',
|
||||
apiToken: ''!,
|
||||
cartCookie: SWELL_CHECKOUT_ID_COOKIE,
|
||||
cartCookieMaxAge: SWELL_COOKIE_EXPIRE,
|
||||
fetchSwell: fetchSwellApi,
|
||||
fetch: fetchGraphqlApi,
|
||||
customerCookie: SHOPIFY_CUSTOMER_TOKEN_COOKIE,
|
||||
fetch: fetcher,
|
||||
customerCookie: SWELL_CUSTOMER_TOKEN_COOKIE,
|
||||
})
|
||||
|
||||
export function getConfig(userConfig?: Partial<SwellConfig>) {
|
||||
|
@@ -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
|
||||
) {
|
||||
|
@@ -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) {
|
||||
|
@@ -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
|
@@ -6,7 +6,6 @@ const fetchSwellApi = async (
|
||||
variables: [] = []
|
||||
) => {
|
||||
const { swell } = swellConfig
|
||||
|
||||
return await swell[query][method](...variables)
|
||||
}
|
||||
export default fetchSwellApi
|
||||
|
Reference in New Issue
Block a user