mirror of
https://github.com/vercel/commerce.git
synced 2025-07-23 04:36:49 +00:00
Update types (#831)
* Update product types * Cart types progress, add zod & initial schema validator * Update normalize.ts * Update with-schema-parser.ts * Updated types, schemas & providers * Fix providers after schema parse errors * Fix paths * More provider fixes * Fix kibocommerce & commercejs * Add customer updated types & fixes * Add checkout & customer types * Import core types only from commerce * Update tsconfig.json * Convert hooks interfaces to types * Requested changes * Change to relative paths * Move Zod dependency
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { Provider, SwellConfig } from '..'
|
||||
import type { Provider, SwellConfig } from '..'
|
||||
import type { OperationContext } from '@vercel/commerce/api/operations'
|
||||
import type { Page } from '../../types/page'
|
||||
import type { Page } from '@vercel/commerce/types/page'
|
||||
|
||||
export type GetAllPagesResult<T extends { pages: any[] } = { pages: Page[] }> =
|
||||
T
|
||||
@@ -31,10 +31,13 @@ export default function getAllPagesOperation({
|
||||
const { locale, fetch } = config
|
||||
const data = await fetch('content', 'list', ['pages'])
|
||||
const pages =
|
||||
data?.results?.map(({ slug, ...rest }: { slug: string }) => ({
|
||||
url: `/${locale}/${slug}`,
|
||||
...rest,
|
||||
})) ?? []
|
||||
data?.results?.map(
|
||||
({ slug, body, ...rest }: { slug: string; body: string }) => ({
|
||||
...rest,
|
||||
url: `/${locale}/${slug}`,
|
||||
body: body ?? '',
|
||||
})
|
||||
) ?? []
|
||||
return {
|
||||
pages,
|
||||
}
|
||||
|
@@ -1,10 +1,10 @@
|
||||
import { Page } from '../../../schema'
|
||||
import { SwellConfig, Provider } from '..'
|
||||
import {
|
||||
import type { Page } from '../../../schema'
|
||||
import type { SwellConfig, Provider } from '..'
|
||||
import type {
|
||||
OperationContext,
|
||||
OperationOptions,
|
||||
} from '@vercel/commerce/api/operations'
|
||||
import { GetPageOperation } from '../../types/page'
|
||||
import type { GetPageOperation } from '@vercel/commerce/types/page'
|
||||
|
||||
export type GetPageResult<T extends { page?: any } = { page?: Page }> = T
|
||||
|
||||
@@ -48,6 +48,7 @@ export default function getPageOperation({
|
||||
? {
|
||||
...page,
|
||||
url: `/${locale}/${page.slug}`,
|
||||
body: page.body ?? '',
|
||||
}
|
||||
: null,
|
||||
}
|
||||
|
@@ -1,13 +1,13 @@
|
||||
import getCategories from '../../utils/get-categories'
|
||||
import getVendors, { Brands } from '../../utils/get-vendors'
|
||||
import getVendors from '../../utils/get-vendors'
|
||||
import { Provider, SwellConfig } from '../'
|
||||
import type { OperationContext } from '@vercel/commerce/api/operations'
|
||||
import type { Category } from '@vercel/commerce/types/site'
|
||||
import type { Brand, Category } from '@vercel/commerce/types/site'
|
||||
|
||||
export type GetSiteInfoResult<
|
||||
T extends { categories: any[]; brands: any[] } = {
|
||||
categories: Category[]
|
||||
brands: Brands
|
||||
brands: Brand[]
|
||||
}
|
||||
> = T
|
||||
|
||||
|
@@ -3,7 +3,7 @@ import type {
|
||||
OperationContext,
|
||||
OperationOptions,
|
||||
} from '@vercel/commerce/api/operations'
|
||||
import type { LoginOperation } from '../../types/login'
|
||||
import type { LoginOperation } from '@vercel/commerce/types/login'
|
||||
import { Provider, SwellConfig } from '..'
|
||||
|
||||
export default function loginOperation({
|
||||
|
@@ -1,14 +1,15 @@
|
||||
import { useCallback } from 'react'
|
||||
import type { LoginHook } from '@vercel/commerce/types/login'
|
||||
import type { MutationHook } from '@vercel/commerce/utils/types'
|
||||
import { CommerceError, ValidationError } from '@vercel/commerce/utils/errors'
|
||||
import useCustomer from '../customer/use-customer'
|
||||
import {
|
||||
import type {
|
||||
CustomerUserError,
|
||||
Mutation,
|
||||
MutationCheckoutCreateArgs,
|
||||
} from '../../schema'
|
||||
import useLogin, { UseLogin } from '@vercel/commerce/auth/use-login'
|
||||
import { LoginHook } from '../types/login'
|
||||
|
||||
import { useCallback } from 'react'
|
||||
import { CommerceError, ValidationError } from '@vercel/commerce/utils/errors'
|
||||
import useCustomer from '../customer/use-customer'
|
||||
import useLogin, { type UseLogin } from '@vercel/commerce/auth/use-login'
|
||||
import { setCustomerToken } from '../utils'
|
||||
|
||||
export default useLogin as UseLogin<typeof handler>
|
||||
|
@@ -1,9 +1,10 @@
|
||||
import { useCallback } from 'react'
|
||||
import type { LogoutHook } from '@vercel/commerce/types/logout'
|
||||
import type { MutationHook } from '@vercel/commerce/utils/types'
|
||||
|
||||
import { useCallback } from 'react'
|
||||
import useLogout, { UseLogout } from '@vercel/commerce/auth/use-logout'
|
||||
import useCustomer from '../customer/use-customer'
|
||||
import { getCustomerToken, setCustomerToken } from '../utils/customer-token'
|
||||
import { LogoutHook } from '../types/logout'
|
||||
|
||||
export default useLogout as UseLogout<typeof handler>
|
||||
|
||||
|
@@ -1,9 +1,10 @@
|
||||
import { useCallback } from 'react'
|
||||
import type { SignupHook } from '@vercel/commerce/types/signup'
|
||||
import type { MutationHook } from '@vercel/commerce/utils/types'
|
||||
|
||||
import { useCallback } from 'react'
|
||||
import { CommerceError } from '@vercel/commerce/utils/errors'
|
||||
import useSignup, { UseSignup } from '@vercel/commerce/auth/use-signup'
|
||||
import useSignup, { type UseSignup } from '@vercel/commerce/auth/use-signup'
|
||||
import useCustomer from '../customer/use-customer'
|
||||
import { SignupHook } from '../types/signup'
|
||||
import handleLogin from '../utils/handle-login'
|
||||
|
||||
export default useSignup as UseSignup<typeof handler>
|
||||
|
@@ -1,11 +1,12 @@
|
||||
import type { AddItemHook } from '@vercel/commerce/types/cart'
|
||||
import type { MutationHook } from '@vercel/commerce/utils/types'
|
||||
|
||||
import { CommerceError } from '@vercel/commerce/utils/errors'
|
||||
import useAddItem, { UseAddItem } from '@vercel/commerce/cart/use-add-item'
|
||||
import useAddItem, { type UseAddItem } from '@vercel/commerce/cart/use-add-item'
|
||||
import useCart from './use-cart'
|
||||
import { checkoutToCart } from './utils'
|
||||
import { getCheckoutId } from '../utils'
|
||||
import { useCallback } from 'react'
|
||||
import { AddItemHook } from '../types/cart'
|
||||
|
||||
export default useAddItem as UseAddItem<typeof handler>
|
||||
|
||||
|
@@ -1,23 +1,25 @@
|
||||
import { useCallback } from 'react'
|
||||
import debounce from 'lodash.debounce'
|
||||
import type {
|
||||
HookFetcherContext,
|
||||
MutationHook,
|
||||
MutationHookContext,
|
||||
} from '@vercel/commerce/utils/types'
|
||||
|
||||
import type { UpdateItemHook, LineItem } from '@vercel/commerce/types/cart'
|
||||
|
||||
import { useCallback } from 'react'
|
||||
import debounce from 'lodash.debounce'
|
||||
|
||||
import { ValidationError } from '@vercel/commerce/utils/errors'
|
||||
// import useUpdateItem, {
|
||||
// UpdateItemInput as UpdateItemInputBase,
|
||||
// UseUpdateItem,
|
||||
// } from '@vercel/commerce/cart/use-update-item'
|
||||
import useUpdateItem, {
|
||||
UseUpdateItem,
|
||||
type UseUpdateItem,
|
||||
} from '@vercel/commerce/cart/use-update-item'
|
||||
import useCart from './use-cart'
|
||||
import { handler as removeItemHandler } from './use-remove-item'
|
||||
import { CartItemBody, LineItem } from '@vercel/commerce/types/cart'
|
||||
import { checkoutToCart } from './utils'
|
||||
import { UpdateItemHook } from '../types/cart'
|
||||
|
||||
// export type UpdateItemInput<T = any> = T extends LineItem
|
||||
// ? Partial<UpdateItemInputBase<LineItem>>
|
||||
// : UpdateItemInputBase<LineItem>
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import { Cart } from '../../types'
|
||||
import type { Cart } from '@vercel/commerce/types/cart'
|
||||
import { CommerceError } from '@vercel/commerce/utils/errors'
|
||||
|
||||
import {
|
||||
|
@@ -1,9 +1,10 @@
|
||||
import type { SWRHook } from '@vercel/commerce/utils/types'
|
||||
import type { CustomerHook } from '@vercel/commerce/types/customer'
|
||||
|
||||
import useCustomer, {
|
||||
UseCustomer,
|
||||
type UseCustomer,
|
||||
} from '@vercel/commerce/customer/use-customer'
|
||||
import { SWRHook } from '@vercel/commerce/utils/types'
|
||||
import { normalizeCustomer } from '../utils/normalize'
|
||||
import type { CustomerHook } from '../types/customer'
|
||||
|
||||
export default useCustomer as UseCustomer<typeof handler>
|
||||
|
||||
|
@@ -1,8 +1,9 @@
|
||||
import type { SwellProduct } from '../types'
|
||||
import type { SearchProductsHook } from '@vercel/commerce/types/product'
|
||||
|
||||
import { SWRHook } from '@vercel/commerce/utils/types'
|
||||
import useSearch, { UseSearch } from '@vercel/commerce/product/use-search'
|
||||
import useSearch, { type UseSearch } from '@vercel/commerce/product/use-search'
|
||||
import { normalizeProduct } from '../utils'
|
||||
import { SwellProduct } from '../types'
|
||||
import type { SearchProductsHook } from '../types/product'
|
||||
|
||||
export default useSearch as UseSearch<typeof handler>
|
||||
|
||||
|
@@ -1,6 +1,4 @@
|
||||
import * as Core from '@vercel/commerce/types/cart'
|
||||
import { Customer } from '@vercel/commerce/types'
|
||||
import { CheckoutLineItem } from '../schema'
|
||||
import type { CheckoutLineItem } from '../schema'
|
||||
|
||||
export type SwellImage = {
|
||||
file: {
|
||||
@@ -88,15 +86,6 @@ export type SwellCheckout = {
|
||||
lineItems: CheckoutLineItem[]
|
||||
}
|
||||
|
||||
export interface Cart extends Core.Cart {
|
||||
id: string
|
||||
lineItems: LineItem[]
|
||||
}
|
||||
|
||||
export interface LineItem extends Core.LineItem {
|
||||
options?: any[]
|
||||
}
|
||||
|
||||
/**
|
||||
* Cart mutations
|
||||
*/
|
||||
@@ -105,8 +94,3 @@ export type OptionSelections = {
|
||||
option_id: number
|
||||
option_value: number | string
|
||||
}
|
||||
|
||||
export type CartItemBody = Core.CartItemBody & {
|
||||
productId: string // The product id is always required for BC
|
||||
optionSelections?: OptionSelections
|
||||
}
|
||||
|
@@ -1,11 +1 @@
|
||||
import * as Core from '@vercel/commerce/types/login'
|
||||
import { LoginBody, LoginTypes } from '@vercel/commerce/types/login'
|
||||
|
||||
export * from '@vercel/commerce/types/login'
|
||||
|
||||
export type LoginHook<T extends LoginTypes = LoginTypes> = {
|
||||
data: null
|
||||
actionInput: LoginBody
|
||||
fetcherInput: LoginBody
|
||||
body: T['body']
|
||||
}
|
||||
|
@@ -1,5 +1,5 @@
|
||||
import { SwellConfig } from '../api'
|
||||
import { Category } from '../types/site'
|
||||
import type { Category } from '@vercel/commerce/types/site'
|
||||
|
||||
const getCategories = async (config: SwellConfig): Promise<Category[]> => {
|
||||
const data = await config.fetch('categories', 'get')
|
||||
|
@@ -1,26 +1,14 @@
|
||||
import { SwellConfig } from '../api'
|
||||
|
||||
export type BrandNode = {
|
||||
name: string
|
||||
path: string
|
||||
}
|
||||
|
||||
export type BrandEdge = {
|
||||
node: BrandNode
|
||||
}
|
||||
|
||||
export type Brands = BrandEdge[]
|
||||
|
||||
const getVendors = async (config: SwellConfig) => {
|
||||
const vendors: [string] =
|
||||
(await config.fetch('attributes', 'get', ['brand']))?.values ?? []
|
||||
|
||||
return [...new Set(vendors)].map((v) => ({
|
||||
node: {
|
||||
entityId: v,
|
||||
name: v,
|
||||
path: `brands/${v}`,
|
||||
},
|
||||
id: v,
|
||||
name: v,
|
||||
slug: v.replace(/\s+/g, '-').toLowerCase(),
|
||||
path: `/${v}`,
|
||||
}))
|
||||
}
|
||||
|
||||
|
@@ -1,9 +1,9 @@
|
||||
import { Customer } from '../types/customer'
|
||||
import { Product, ProductOption } from '../types/product'
|
||||
import { MoneyV2 } from '../../schema'
|
||||
import type { Cart, LineItem } from '@vercel/commerce/types/cart'
|
||||
import type { Customer } from '@vercel/commerce/types/customer'
|
||||
import type { Product, ProductOption } from '@vercel/commerce/types/product'
|
||||
import type { MoneyV2 } from '../../schema'
|
||||
|
||||
import type {
|
||||
Cart,
|
||||
CartLineItem,
|
||||
SwellCustomer,
|
||||
SwellProduct,
|
||||
@@ -12,7 +12,6 @@ import type {
|
||||
ProductOptionValue,
|
||||
SwellProductOptionValue,
|
||||
SwellCart,
|
||||
LineItem,
|
||||
} from '../types'
|
||||
|
||||
const money = ({ amount, currencyCode }: MoneyV2) => {
|
||||
@@ -121,7 +120,6 @@ export function normalizeProduct(swellProduct: SwellProduct): Product {
|
||||
currency: currencyCode,
|
||||
} = swellProduct
|
||||
// ProductView accesses variants for each product
|
||||
const emptyVariants = [{ options: [], id, name }]
|
||||
|
||||
const productOptions = options
|
||||
? options.map((o) => normalizeProductOption(o))
|
||||
@@ -138,10 +136,7 @@ export function normalizeProduct(swellProduct: SwellProduct): Product {
|
||||
vendor: '',
|
||||
path: `/${slug}`,
|
||||
images: productImages,
|
||||
variants:
|
||||
productVariants && productVariants.length
|
||||
? productVariants
|
||||
: emptyVariants,
|
||||
variants: productVariants && productVariants.length ? productVariants : [],
|
||||
options: productOptions,
|
||||
price: {
|
||||
value,
|
||||
@@ -214,11 +209,12 @@ function normalizeLineItem({
|
||||
price: price,
|
||||
listPrice: price,
|
||||
},
|
||||
path: '',
|
||||
path: `/${product.slug}`,
|
||||
discounts: [],
|
||||
options: [
|
||||
{
|
||||
value: variant?.name,
|
||||
name: variant?.name!,
|
||||
value: variant?.name!,
|
||||
},
|
||||
],
|
||||
}
|
||||
|
@@ -1,22 +1,11 @@
|
||||
// TODO: replace this hook and other wishlist hooks with a handler, or remove them if
|
||||
// Swell doesn't have a wishlist
|
||||
|
||||
import type { Wishlist } from '@vercel/commerce/types/wishlist'
|
||||
import { HookFetcher } from '@vercel/commerce/utils/types'
|
||||
import { Product } from '../../schema'
|
||||
|
||||
const defaultOpts = {}
|
||||
|
||||
export type Wishlist = {
|
||||
items: [
|
||||
{
|
||||
product_id: number
|
||||
variant_id: number
|
||||
id: number
|
||||
product: Product
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
export interface UseWishlistOptions {
|
||||
includeProducts?: boolean
|
||||
}
|
||||
|
Reference in New Issue
Block a user