Fix build errors

This commit is contained in:
cond0r
2021-05-13 16:10:09 +03:00
parent e7d0f56e85
commit bff94e73ae
22 changed files with 87 additions and 458 deletions

View File

@@ -6,11 +6,10 @@ import {
SWELL_COOKIE_EXPIRE,
} from '../const'
import fetcher from '../fetcher'
import fetchSwellApi from './utils/fetch-swell-api'
import fetchApi from './utils/fetch-swell-api'
export interface SwellConfig extends CommerceAPIConfig {
fetchSwell: any
fetch: any
}
export class Config {
@@ -38,8 +37,7 @@ const config = new Config({
apiToken: ''!,
cartCookie: SWELL_CHECKOUT_ID_COOKIE,
cartCookieMaxAge: SWELL_COOKIE_EXPIRE,
fetchSwell: fetchSwellApi,
fetch: fetcher,
fetch: fetchApi,
customerCookie: SWELL_CUSTOMER_TOKEN_COOKIE,
})

View File

@@ -1,25 +0,0 @@
import { SwellConfig } from '..'
import { SwellProduct } from '../../types'
const fetchAllProducts = async ({
config,
query,
method,
variables,
acc = [],
}: {
config: SwellConfig
query: string
method: string
acc?: SwellProduct[]
variables?: any
cursor?: string
}): Promise<SwellProduct[]> => {
const response = await config.fetchSwell(query, method, variables)
acc = acc.concat(response.results)
return acc
}
export default fetchAllProducts

View File

@@ -1,11 +1,7 @@
import { swellConfig } from '../..'
const fetchSwellApi = async (
query: string,
method: string,
variables: [] = []
) => {
const fetchApi = async (query: string, method: string, variables: [] = []) => {
const { swell } = swellConfig
return await swell[query][method](...variables)
return swell[query][method](...variables)
}
export default fetchSwellApi
export default fetchApi

View File

@@ -5,10 +5,6 @@ import useSignup, { UseSignup } from '@commerce/auth/use-signup'
import useCustomer from '../customer/use-customer'
import { CustomerCreateInput } from '../schema'
import {
customerCreateMutation,
customerAccessTokenCreateMutation,
} from '../utils/mutations'
import handleLogin from '../utils/handle-login'
export default useSignup as UseSignup<typeof handler>

View File

@@ -1,27 +1,37 @@
import useCart, { UseCart } from '@commerce/cart/use-cart'
import { Cart } from '@commerce/types'
import { SWRHook } from '@commerce/utils/types'
import { useMemo } from 'react'
import { normalizeCart } from '../utils/normalize'
import { checkoutCreate, checkoutToCart } from './utils'
export default useCart as UseCart<typeof handler>
export const handler: SWRHook<Cart | null> = {
export const handler: SWRHook<Cart | null, {}, any, { isEmpty?: boolean }> = {
fetchOptions: {
query: 'cart',
method: 'get',
},
async fetcher({ options, fetch }) {
async fetcher({ fetch }) {
const cart = await checkoutCreate(fetch)
return cart ? normalizeCart(cart) : null
},
useHook: ({ useData }) => (input) => {
return useData({
swrOptions: {
revalidateOnFocus: false,
...input?.swrOptions,
},
const response = useData({
swrOptions: { revalidateOnFocus: false, ...input?.swrOptions },
})
return useMemo(
() =>
Object.create(response, {
isEmpty: {
get() {
return (response.data?.lineItems.length ?? 0) <= 0
},
enumerable: true,
},
}),
[response]
)
},
}

View File

@@ -47,7 +47,7 @@ export const handler = {
}
const response = await fetch({
...options,
variables: [item.itemId, { quantity: item.quantity }],
variables: [itemId, { quantity: item.quantity }],
})
return checkoutToCart(response)
@@ -79,7 +79,6 @@ export const handler = {
const data = await fetch({
input: {
item: {
itemId,
productId,
variantId,
quantity: input.quantity,

View File

@@ -1,5 +1,5 @@
import { Cart } from '../../types'
import { CommerceError, ValidationError } from '@commerce/utils/errors'
import { CommerceError } from '@commerce/utils/errors'
import {
CheckoutLineItemsAddPayload,
@@ -20,8 +20,7 @@ const checkoutToCart = (checkoutPayload?: Maybe<CheckoutPayload>): Cart => {
message: 'Invalid response from Swell',
})
}
return normalizeCart(checkoutPayload)
return normalizeCart(checkoutPayload as any)
}
export default checkoutToCart

View File

@@ -23,12 +23,13 @@ const getAllPages = async (options?: {
}): Promise<ReturnType> => {
let { config, variables = { first: 250 } } = options ?? {}
config = getConfig(config)
const { locale, fetchSwell } = config
const { results } = await fetchSwell('content', 'list', ['pages'])
const pages = results.map(({ slug, ...rest }: { slug: string }) => ({
url: `/${locale}/${slug}`,
...rest,
}))
const { locale, fetch } = config
const data = await fetch('content', 'list', ['pages'])
const pages =
data?.results?.map(({ slug, ...rest }: { slug: string }) => ({
url: `/${locale}/${slug}`,
...rest,
})) ?? []
return { pages }
}

View File

@@ -17,7 +17,7 @@ const getPage = async (options: {
config = getConfig(config)
const { locale } = config
const { id } = variables
const result = await config.fetchSwell('content', 'get', ['pages', id])
const result = await config.fetch('content', 'get', ['pages', id])
const page = result
return {

View File

@@ -1,22 +1,27 @@
import { Fetcher } from '@commerce/utils/types'
import { handleFetchResponse } from './utils'
import { swellConfig } from './index'
import { CommerceError } from '@commerce/utils/errors'
const fetcher: Fetcher = async ({ method = 'get', variables, query }) => {
const { swell } = swellConfig
async function callSwell() {
if (Array.isArray(variables)) {
const arg1 = variables[0]
const arg2 = variables[1]
const response = await swell[query][method](arg1, arg2)
const response = await swell[query!][method](arg1, arg2)
return handleFetchResponse(response)
} else {
const response = await swell[query][method](variables)
const response = await swell[query!][method](variables)
return handleFetchResponse(response)
}
}
if (query in swell) {
if (query && query in swell) {
return await callSwell()
} else {
throw new CommerceError({ message: 'Invalid query argument!' })
}
}

View File

@@ -9,7 +9,7 @@ const getAllCollections = async (options?: {
let { config, variables = { limit: 25 } } = options ?? {}
config = getConfig(config)
const response = await config.fetchSwell('categories', 'list', { variables })
const response = await config.fetch('categories', 'list', { variables })
const edges = response.results ?? []
const categories = edges.map(

View File

@@ -1,5 +1,5 @@
import { SwellProduct } from '@framework/types'
import { getConfig, SwellConfig } from '../api'
import fetchAllProducts from '../api/utils/fetch-all-products'
type ProductPath = {
path: string
@@ -21,15 +21,14 @@ const getAllProductPaths = async (options?: {
let { config, variables = [{ limit: 100 }] } = options ?? {}
config = getConfig(config)
const products = await fetchAllProducts({
config,
query: 'products',
method: 'list',
variables,
})
const { results } = await config.fetch('products', 'list', [
{
limit: variables.first,
},
])
return {
products: products?.map(({ slug: handle }) => ({
products: results?.map(({ slug: handle }: SwellProduct) => ({
node: {
path: `/${handle}`,
},

View File

@@ -19,7 +19,7 @@ const getAllProducts = async (options: {
}): Promise<ReturnType> => {
let { config, variables = { first: 250 } } = options ?? {}
config = getConfig(config)
const { results } = await config.fetchSwell('products', 'list', [
const { results } = await config.fetch('products', 'list', [
{
limit: variables.first,
},

View File

@@ -18,10 +18,12 @@ const getProduct = async (options: {
let { config, variables } = options ?? {}
config = getConfig(config)
const product = await config.fetchSwell('products', 'get', [variables.slug])
const product = await config.fetch('products', 'get', [variables.slug])
if (product && product.variants) {
product.variants = product.variants?.results
}
return {
product: product ? normalizeProduct(product) : null,
}

View File

@@ -90,7 +90,7 @@ export interface Cart extends Core.Cart {
}
export interface LineItem extends Core.LineItem {
options: any[]
options?: any[]
}
/**

View File

@@ -1,15 +1,15 @@
import { SwellConfig } from '../api'
export type Category = {
id: string
entityId: string
name: string
slug: string
path: string
}
const getCategories = async (config: SwellConfig): Promise<Category[]> => {
const data = await config.fetchSwell('categories', 'get')
const data = await config.fetch('categories', 'get')
return (
data.results.map(({ id: entityId, name, slug }: Category) => ({
data.results.map(({ id: entityId, name, slug }: any) => ({
entityId,
name,
path: `/${slug}`,

View File

@@ -13,7 +13,7 @@ export type Brands = BrandEdge[]
const getVendors = async (config: SwellConfig) => {
const vendors: [string] =
(await config.fetchSwell('attributes', 'get', ['brand']))?.values ?? []
(await config.fetch('attributes', 'get', ['brand']))?.values ?? []
return [...new Set(vendors)].map((v) => ({
node: {

View File

@@ -1,11 +1,6 @@
import { Product, Customer } from '@commerce/types'
import {
Checkout,
CheckoutLineItemEdge,
MoneyV2,
ProductOption,
} from '../schema'
import { MoneyV2, ProductOption } from '../schema'
import type {
Cart,