Skip to content

Commit

Permalink
Fix build errors
Browse files Browse the repository at this point in the history
  • Loading branch information
cond0r committed May 13, 2021
1 parent e7d0f56 commit bff94e7
Show file tree
Hide file tree
Showing 22 changed files with 87 additions and 458 deletions.
8 changes: 3 additions & 5 deletions framework/swell/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
})

Expand Down
25 changes: 0 additions & 25 deletions framework/swell/api/utils/fetch-all-products.ts

This file was deleted.

10 changes: 3 additions & 7 deletions framework/swell/api/utils/fetch-swell-api.ts
Original file line number Diff line number Diff line change
@@ -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
4 changes: 0 additions & 4 deletions framework/swell/auth/use-signup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
24 changes: 17 additions & 7 deletions framework/swell/cart/use-cart.tsx
Original file line number Diff line number Diff line change
@@ -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]
)
},
}
3 changes: 1 addition & 2 deletions framework/swell/cart/use-update-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -79,7 +79,6 @@ export const handler = {
const data = await fetch({
input: {
item: {
itemId,
productId,
variantId,
quantity: input.quantity,
Expand Down
5 changes: 2 additions & 3 deletions framework/swell/cart/utils/checkout-to-cart.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Cart } from '../../types'
import { CommerceError, ValidationError } from '@commerce/utils/errors'
import { CommerceError } from '@commerce/utils/errors'

import {
CheckoutLineItemsAddPayload,
Expand All @@ -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
13 changes: 7 additions & 6 deletions framework/swell/common/get-all-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand Down
2 changes: 1 addition & 1 deletion framework/swell/common/get-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 8 additions & 3 deletions framework/swell/fetcher.ts
Original file line number Diff line number Diff line change
@@ -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!' })
}
}

Expand Down
2 changes: 1 addition & 1 deletion framework/swell/product/get-all-collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
15 changes: 7 additions & 8 deletions framework/swell/product/get-all-product-paths.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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}`,
},
Expand Down
2 changes: 1 addition & 1 deletion framework/swell/product/get-all-products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
4 changes: 3 additions & 1 deletion framework/swell/product/get-product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
2 changes: 1 addition & 1 deletion framework/swell/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export interface Cart extends Core.Cart {
}

export interface LineItem extends Core.LineItem {
options: any[]
options?: any[]
}

/**
Expand Down
8 changes: 4 additions & 4 deletions framework/swell/utils/get-categories.ts
Original file line number Diff line number Diff line change
@@ -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}`,
Expand Down
2 changes: 1 addition & 1 deletion framework/swell/utils/get-vendors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
7 changes: 1 addition & 6 deletions framework/swell/utils/normalize.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
3 changes: 1 addition & 2 deletions pages/[...pages].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ export async function getStaticProps({
const pageItem = pages.find((p) => (p.url ? getSlug(p.url) === slug : false))
const data =
pageItem &&
// TODO: Shopify - Fix this type
(await getPage({ variables: { id: pageItem.id! } as any, config, preview }))
(await getPage({ variables: { id: pageItem.id! }, config, preview }))
const page = data?.page

if (!page) {
Expand Down
3 changes: 1 addition & 2 deletions pages/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ export default function Search({

const { data } = useSearch({
search: typeof q === 'string' ? q : '',
// TODO: Shopify - Fix this type
// @ts-ignore Swell - Fix this types
categoryId: activeCategory?.entityId as any,
// TODO: Shopify - Fix this type
brandId: (activeBrand as any)?.entityId,
sort: typeof sort === 'string' ? sort : '',
})
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"@components/*": ["components/*"],
"@commerce": ["framework/commerce"],
"@commerce/*": ["framework/commerce/*"],
"@framework": ["framework/shopify"],
"@framework/*": ["framework/shopify/*"]
"@framework": ["framework/swell"],
"@framework/*": ["framework/swell/*"]
}
},
"include": ["next-env.d.ts", "**/*.d.ts", "**/*.ts", "**/*.tsx", "**/*.js"],
Expand Down
Loading

0 comments on commit bff94e7

Please sign in to comment.