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:
@@ -33,6 +33,7 @@ export default function getProductOperation({
|
||||
name: product.name,
|
||||
description: product.description,
|
||||
slug: product.slug,
|
||||
path: `/${product.slug}`,
|
||||
images: product.assets.map((a) => ({
|
||||
url: a.preview,
|
||||
alt: a.name,
|
||||
|
@@ -33,8 +33,8 @@ export default function getSiteInfoOperation({
|
||||
})
|
||||
const collections = data.collections?.items.map((i) => ({
|
||||
...i,
|
||||
entityId: i.id,
|
||||
path: i.slug,
|
||||
id: i.id,
|
||||
path: `/${i.id}`,
|
||||
productCount: i.productVariants.totalItems,
|
||||
}))
|
||||
const categories = arrayToTree(collections).children
|
||||
|
@@ -4,7 +4,7 @@ import type {
|
||||
OperationOptions,
|
||||
} from '@vercel/commerce/api/operations'
|
||||
import { ValidationError } from '@vercel/commerce/utils/errors'
|
||||
import type { LoginOperation } from '../../types/login'
|
||||
import type { LoginOperation } from '@vercel/commerce/types/login'
|
||||
import type { LoginMutation } from '../../../schema'
|
||||
import { Provider, VendureConfig } from '..'
|
||||
import { loginMutation } from '../../utils/mutations/log-in-mutation'
|
||||
|
@@ -9,6 +9,7 @@ const fetchGraphqlApi: GraphQLFetcher = async (
|
||||
fetchOptions
|
||||
) => {
|
||||
const config = getCommerceApi().getConfig()
|
||||
|
||||
const res = await fetch(config.commerceUrl, {
|
||||
...fetchOptions,
|
||||
method: 'POST',
|
||||
@@ -23,6 +24,7 @@ const fetchGraphqlApi: GraphQLFetcher = async (
|
||||
})
|
||||
|
||||
const json = await res.json()
|
||||
|
||||
if (json.errors) {
|
||||
throw new FetcherError({
|
||||
errors: json.errors ?? [{ message: 'Failed to fetch Vendure API' }],
|
||||
|
@@ -1,10 +1,10 @@
|
||||
import { useCallback } from 'react'
|
||||
import { MutationHook } from '@vercel/commerce/utils/types'
|
||||
import useLogin, { UseLogin } from '@vercel/commerce/auth/use-login'
|
||||
import { LoginHook } from '../types/login'
|
||||
import useLogin, { type UseLogin } from '@vercel/commerce/auth/use-login'
|
||||
import type { LoginHook } from '@vercel/commerce/types/login'
|
||||
import { CommerceError, ValidationError } from '@vercel/commerce/utils/errors'
|
||||
import useCustomer from '../customer/use-customer'
|
||||
import { LoginMutation, LoginMutationVariables } from '../../schema'
|
||||
import type { LoginMutation, LoginMutationVariables } from '../../schema'
|
||||
import { loginMutation } from '../utils/mutations/log-in-mutation'
|
||||
|
||||
export default useLogin as UseLogin<typeof handler>
|
||||
|
@@ -1,10 +1,10 @@
|
||||
import { useCallback } from 'react'
|
||||
import { MutationHook } from '@vercel/commerce/utils/types'
|
||||
import useLogout, { UseLogout } from '@vercel/commerce/auth/use-logout'
|
||||
import type { MutationHook } from '@vercel/commerce/utils/types'
|
||||
import useLogout, { type UseLogout } from '@vercel/commerce/auth/use-logout'
|
||||
import useCustomer from '../customer/use-customer'
|
||||
import { LogoutMutation } from '../../schema'
|
||||
import type { LogoutMutation } from '../../schema'
|
||||
import { logoutMutation } from '../utils/mutations/log-out-mutation'
|
||||
import { LogoutHook } from '../types/logout'
|
||||
import type { LogoutHook } from '@vercel/commerce/types/logout'
|
||||
|
||||
export default useLogout as UseLogout<typeof handler>
|
||||
|
||||
|
@@ -1,15 +1,11 @@
|
||||
import { useCallback } from 'react'
|
||||
import { MutationHook } from '@vercel/commerce/utils/types'
|
||||
import { CommerceError, ValidationError } 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 {
|
||||
RegisterCustomerInput,
|
||||
SignupMutation,
|
||||
SignupMutationVariables,
|
||||
} from '../../schema'
|
||||
import type { SignupMutation, SignupMutationVariables } from '../../schema'
|
||||
import { signupMutation } from '../utils/mutations/sign-up-mutation'
|
||||
import { SignupHook } from '../types/signup'
|
||||
import type { SignupHook } from '@vercel/commerce/types/signup'
|
||||
|
||||
export default useSignup as UseSignup<typeof handler>
|
||||
|
||||
|
@@ -1,12 +1,12 @@
|
||||
import useAddItem, { UseAddItem } from '@vercel/commerce/cart/use-add-item'
|
||||
import { CommerceError } from '@vercel/commerce/utils/errors'
|
||||
import { MutationHook } from '@vercel/commerce/utils/types'
|
||||
import type { MutationHook } from '@vercel/commerce/utils/types'
|
||||
import { useCallback } from 'react'
|
||||
import useCart from './use-cart'
|
||||
import { AddItemToOrderMutation } from '../../schema'
|
||||
import { normalizeCart } from '../utils/normalize'
|
||||
import { addItemToOrderMutation } from '../utils/mutations/add-item-to-order-mutation'
|
||||
import { AddItemHook } from '../types/cart'
|
||||
import type { AddItemHook } from '@vercel/commerce/types/cart'
|
||||
|
||||
export default useAddItem as UseAddItem<typeof handler>
|
||||
|
||||
|
@@ -1,10 +1,10 @@
|
||||
import { SWRHook } from '@vercel/commerce/utils/types'
|
||||
import useCart, { UseCart } from '@vercel/commerce/cart/use-cart'
|
||||
import useCart, { type UseCart } from '@vercel/commerce/cart/use-cart'
|
||||
import { ActiveOrderQuery, CartFragment } from '../../schema'
|
||||
import { normalizeCart } from '../utils/normalize'
|
||||
import { useMemo } from 'react'
|
||||
import { getCartQuery } from '../utils/queries/get-cart-query'
|
||||
import { GetCartHook } from '../types/cart'
|
||||
import type { GetCartHook } from '@vercel/commerce/types/cart'
|
||||
|
||||
export type CartResult = {
|
||||
activeOrder?: CartFragment
|
||||
|
@@ -1,22 +1,17 @@
|
||||
import { useCallback } from 'react'
|
||||
import {
|
||||
HookFetcherContext,
|
||||
MutationHook,
|
||||
MutationHookContext,
|
||||
SWRHook,
|
||||
} from '@vercel/commerce/utils/types'
|
||||
import type { MutationHook } from '@vercel/commerce/utils/types'
|
||||
import useRemoveItem, {
|
||||
UseRemoveItem,
|
||||
} from '@vercel/commerce/cart/use-remove-item'
|
||||
import { CommerceError } from '@vercel/commerce/utils/errors'
|
||||
import { Cart } from '@vercel/commerce/types/cart'
|
||||
|
||||
import useCart from './use-cart'
|
||||
import {
|
||||
RemoveOrderLineMutation,
|
||||
RemoveOrderLineMutationVariables,
|
||||
} from '../../schema'
|
||||
import { normalizeCart } from '../utils/normalize'
|
||||
import { RemoveItemHook } from '../types/cart'
|
||||
import type { RemoveItemHook } from '@vercel/commerce/types/cart'
|
||||
import { removeOrderLineMutation } from '../utils/mutations/remove-order-line-mutation'
|
||||
|
||||
export default useRemoveItem as UseRemoveItem<typeof handler>
|
||||
|
@@ -8,7 +8,6 @@ import { CommerceError, ValidationError } from '@vercel/commerce/utils/errors'
|
||||
import useUpdateItem, {
|
||||
UseUpdateItem,
|
||||
} from '@vercel/commerce/cart/use-update-item'
|
||||
import { CartItemBody, LineItem } from '@vercel/commerce/types/cart'
|
||||
import useCart from './use-cart'
|
||||
import {
|
||||
AdjustOrderLineMutation,
|
||||
@@ -16,7 +15,7 @@ import {
|
||||
} from '../../schema'
|
||||
import { normalizeCart } from '../utils/normalize'
|
||||
import { adjustOrderLineMutation } from '../utils/mutations/adjust-order-line-mutation'
|
||||
import { UpdateItemHook } from '../types/cart'
|
||||
import type { UpdateItemHook, LineItem } from '@vercel/commerce/types/cart'
|
||||
|
||||
export type UpdateItemActionInput<T = any> = T extends LineItem
|
||||
? Partial<UpdateItemHook['actionInput']>
|
||||
|
@@ -4,7 +4,7 @@ import useCustomer, {
|
||||
} from '@vercel/commerce/customer/use-customer'
|
||||
import { ActiveCustomerQuery } from '../../schema'
|
||||
import { activeCustomerQuery } from '../utils/queries/active-customer-query'
|
||||
import { CustomerHook } from '../types/customer'
|
||||
import { CustomerHook } from '@vercel/commerce/types/customer'
|
||||
|
||||
export default useCustomer as UseCustomer<typeof handler>
|
||||
|
||||
|
@@ -44,7 +44,7 @@ export const fetcher: Fetcher = async ({
|
||||
if (res.ok) {
|
||||
const { data, errors } = await res.json()
|
||||
if (errors) {
|
||||
throw await new FetcherError({ status: res.status, errors })
|
||||
throw new FetcherError({ status: res.status, errors })
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
@@ -4,7 +4,7 @@ import { Product } from '@vercel/commerce/types/product'
|
||||
import { SearchQuery, SearchQueryVariables } from '../../schema'
|
||||
import { normalizeSearchResult } from '../utils/normalize'
|
||||
import { searchQuery } from '../utils/queries/search-query'
|
||||
import { SearchProductsHook } from '../types/product'
|
||||
import type { SearchProductsHook } from '@vercel/commerce/types/product'
|
||||
|
||||
export default useSearch as UseSearch<typeof handler>
|
||||
|
||||
|
@@ -1 +0,0 @@
|
||||
export * from '@vercel/commerce/types/cart'
|
@@ -1 +0,0 @@
|
||||
export * from '@vercel/commerce/types/checkout'
|
@@ -1 +0,0 @@
|
||||
export * from '@vercel/commerce/types/common'
|
@@ -1 +0,0 @@
|
||||
export * from '@vercel/commerce/types/customer'
|
@@ -1,25 +0,0 @@
|
||||
import * as Cart from './cart'
|
||||
import * as Checkout from './checkout'
|
||||
import * as Common from './common'
|
||||
import * as Customer from './customer'
|
||||
import * as Login from './login'
|
||||
import * as Logout from './logout'
|
||||
import * as Page from './page'
|
||||
import * as Product from './product'
|
||||
import * as Signup from './signup'
|
||||
import * as Site from './site'
|
||||
import * as Wishlist from './wishlist'
|
||||
|
||||
export type {
|
||||
Cart,
|
||||
Checkout,
|
||||
Common,
|
||||
Customer,
|
||||
Login,
|
||||
Logout,
|
||||
Page,
|
||||
Product,
|
||||
Signup,
|
||||
Site,
|
||||
Wishlist,
|
||||
}
|
@@ -1,12 +0,0 @@
|
||||
import * as Core from '@vercel/commerce/types/login'
|
||||
import type { LoginMutationVariables } from '../../schema'
|
||||
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 +0,0 @@
|
||||
export * from '@vercel/commerce/types/logout'
|
@@ -1 +0,0 @@
|
||||
export * from '@vercel/commerce/types/page'
|
@@ -1 +0,0 @@
|
||||
export * from '@vercel/commerce/types/product'
|
@@ -1 +0,0 @@
|
||||
export * from '@vercel/commerce/types/signup'
|
@@ -1 +0,0 @@
|
||||
export * from '@vercel/commerce/types/site'
|
@@ -1 +0,0 @@
|
||||
export * from '@vercel/commerce/types/wishlist'
|
@@ -8,7 +8,7 @@ export function normalizeSearchResult(item: SearchResultFragment): Product {
|
||||
name: item.productName,
|
||||
description: item.description,
|
||||
slug: item.slug,
|
||||
path: item.slug,
|
||||
path: `/${item.slug}`,
|
||||
images: [
|
||||
{
|
||||
url: item.productAsset?.preview
|
||||
@@ -45,7 +45,7 @@ export function normalizeCart(order: CartFragment): Cart {
|
||||
productId: l.productVariant.productId,
|
||||
images: [{ url: l.featuredAsset?.preview + '?preset=thumb' || '' }],
|
||||
discounts: l.discounts.map((d) => ({ value: d.amount / 100 })),
|
||||
path: '',
|
||||
path: `/${l.productVariant.product.slug}`,
|
||||
variant: {
|
||||
id: l.productVariant.id,
|
||||
name: l.productVariant.name,
|
||||
|
@@ -1,22 +1,11 @@
|
||||
// TODO: replace this hook and other wishlist hooks with a handler, or remove them if
|
||||
// Vendure doesn't have a built-in wishlist
|
||||
|
||||
import { HookFetcher } from '@vercel/commerce/utils/types'
|
||||
import { Product } from '../../schema'
|
||||
import type { HookFetcher } from '@vercel/commerce/utils/types'
|
||||
import type { Wishlist } from '@vercel/commerce/types/wishlist'
|
||||
|
||||
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