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:
@@ -111,7 +111,7 @@ export default function getSiteInfoOperation({
|
||||
id: spreeTaxon.id,
|
||||
name: spreeTaxon.attributes.name,
|
||||
slug: spreeTaxon.id,
|
||||
path: spreeTaxon.id,
|
||||
path: `/${spreeTaxon.id}`,
|
||||
}
|
||||
})
|
||||
|
||||
@@ -120,11 +120,10 @@ export default function getSiteInfoOperation({
|
||||
.sort(taxonsSort)
|
||||
.map((spreeTaxon: TaxonAttr) => {
|
||||
return {
|
||||
node: {
|
||||
entityId: spreeTaxon.id,
|
||||
path: `brands/${spreeTaxon.id}`,
|
||||
name: spreeTaxon.attributes.name,
|
||||
},
|
||||
id: spreeTaxon.id,
|
||||
path: `/${spreeTaxon.id}`,
|
||||
slug: spreeTaxon.id,
|
||||
name: spreeTaxon.attributes.name,
|
||||
}
|
||||
})
|
||||
|
||||
|
@@ -8,10 +8,9 @@ import type { ResultResponse } from '@spree/storefront-api-v2-sdk/types/interfac
|
||||
import type { Response } from '@vercel/fetch'
|
||||
import type { ProductOption, Product } from '@vercel/commerce/types/product'
|
||||
import type {
|
||||
AddItemHook,
|
||||
RemoveItemHook,
|
||||
WishlistItemBody,
|
||||
WishlistTypes,
|
||||
Wishlist as CoreWishlist,
|
||||
WishlistItemBody as CoreWishlistItemBody,
|
||||
RemoveItemHook as CoreRemoveItemHook,
|
||||
} from '@vercel/commerce/types/wishlist'
|
||||
|
||||
export type UnknownObjectValues = Record<string, unknown>
|
||||
@@ -134,31 +133,22 @@ export type UserOAuthTokens = {
|
||||
accessToken: string
|
||||
}
|
||||
|
||||
// TODO: ExplicitCommerceWishlist is a temporary type
|
||||
// derived from tsx views. It will be removed once
|
||||
// Wishlist in @vercel/commerce/types/wishlist is updated
|
||||
// to a more specific type than `any`.
|
||||
export type ExplicitCommerceWishlist = {
|
||||
id: string
|
||||
export interface Wishlist extends CoreWishlist {
|
||||
token: string
|
||||
items: {
|
||||
id: string
|
||||
product_id: number
|
||||
variant_id: number
|
||||
product: Product
|
||||
}[]
|
||||
}
|
||||
|
||||
export type ExplicitWishlistAddItemHook = AddItemHook<
|
||||
WishlistTypes & {
|
||||
wishlist: ExplicitCommerceWishlist
|
||||
itemBody: WishlistItemBody & {
|
||||
wishlistToken?: string
|
||||
}
|
||||
}
|
||||
>
|
||||
|
||||
export type ExplicitWishlistRemoveItemHook = RemoveItemHook & {
|
||||
fetcherInput: { wishlistToken?: string }
|
||||
body: { wishlistToken?: string }
|
||||
export interface WishlistItemBody extends CoreWishlistItemBody {
|
||||
wishlistToken: string
|
||||
}
|
||||
|
||||
export type AddItemHook = {
|
||||
data: Wishlist | null | undefined
|
||||
body: { item: WishlistItemBody }
|
||||
fetcherInput: { item: WishlistItemBody }
|
||||
actionInput: WishlistItemBody
|
||||
}
|
||||
|
||||
export type RemoveItemHook = CoreRemoveItemHook & {
|
||||
fetcherInput: { itemId: string; wishlistToken?: string }
|
||||
body: { temId: string; wishlistToken?: string }
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
// Based on https://github.com/spark-solutions/spree2vuestorefront/blob/d88d85ae1bcd2ec99b13b81cd2e3c25600a0216e/src/utils/index.ts
|
||||
|
||||
import type { ProductImage } from '@vercel/commerce/types/product'
|
||||
import type { Image } from '@vercel/commerce/types/common'
|
||||
import type { SpreeProductImage } from '../types'
|
||||
|
||||
const getMediaGallery = (
|
||||
@@ -11,7 +11,7 @@ const getMediaGallery = (
|
||||
minHeight: number
|
||||
) => string | null
|
||||
) => {
|
||||
return images.reduce<ProductImage[]>((productImages, _, imageIndex) => {
|
||||
return images.reduce<Image[]>((productImages, _, imageIndex) => {
|
||||
const url = getImageUrl(images[imageIndex], 800, 800)
|
||||
|
||||
if (url) {
|
||||
|
@@ -88,7 +88,6 @@ const normalizeVariant = (
|
||||
price: parseFloat(spreeVariant.attributes.price),
|
||||
listPrice: parseFloat(spreeVariant.attributes.price),
|
||||
image,
|
||||
isInStock: spreeVariant.attributes.in_stock,
|
||||
availableForSale: spreeVariant.attributes.purchasable,
|
||||
...(spreeVariant.attributes.weight === '0.0'
|
||||
? {}
|
||||
|
@@ -35,7 +35,7 @@ const normalizePage = (
|
||||
id: spreePage.id,
|
||||
name: spreePage.attributes.title,
|
||||
url: `/${usedCommerceLocale}/${spreePage.attributes.slug}`,
|
||||
body: spreePage.attributes.content,
|
||||
body: spreePage.attributes.content ?? '',
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,9 +1,9 @@
|
||||
import type {
|
||||
Product,
|
||||
ProductImage,
|
||||
ProductPrice,
|
||||
ProductVariant,
|
||||
} from '@vercel/commerce/types/product'
|
||||
import type { Image } from '@vercel/commerce/types/common'
|
||||
import type { ProductAttr } from '@spree/storefront-api-v2-sdk/types/interfaces/Product'
|
||||
import type { RelationType } from '@spree/storefront-api-v2-sdk/types/interfaces/Relationships'
|
||||
import { jsonApi } from '@spree/storefront-api-v2-sdk'
|
||||
@@ -105,6 +105,7 @@ const normalizeProduct = (
|
||||
|
||||
return {
|
||||
id: spreeVariantRecord.id,
|
||||
sku: spreeVariantRecord.attributes.sku || spreeVariantRecord.id,
|
||||
options: variantOptions,
|
||||
}
|
||||
}
|
||||
@@ -213,7 +214,7 @@ const normalizeProduct = (
|
||||
createGetAbsoluteImageUrl(requireConfigValue('imageHost') as string)
|
||||
)
|
||||
|
||||
const images: ProductImage[] =
|
||||
const images: Image[] =
|
||||
productImages.length === 0
|
||||
? placeholderImage === false
|
||||
? []
|
||||
|
@@ -6,10 +6,11 @@ const normalizeUser = (
|
||||
_spreeSuccessResponse: SpreeSdkResponse,
|
||||
spreeUser: AccountAttr
|
||||
): Customer => {
|
||||
const email = spreeUser.attributes.email
|
||||
|
||||
return {
|
||||
email,
|
||||
id: spreeUser.id,
|
||||
email: spreeUser.attributes.email,
|
||||
firstName: spreeUser.attributes.firstname,
|
||||
lastName: spreeUser.attributes.lastname,
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -4,59 +4,54 @@ import { jsonApi } from '@spree/storefront-api-v2-sdk'
|
||||
import type { ProductAttr } from '@spree/storefront-api-v2-sdk/types/interfaces/Product'
|
||||
import type { WishedItemAttr } from '@spree/storefront-api-v2-sdk/types/interfaces/WishedItem'
|
||||
import type { WishlistAttr } from '@spree/storefront-api-v2-sdk/types/interfaces/Wishlist'
|
||||
import type {
|
||||
ExplicitCommerceWishlist,
|
||||
SpreeSdkResponse,
|
||||
VariantAttr,
|
||||
} from '../../types'
|
||||
import type { SpreeSdkResponse, VariantAttr } from '../../types'
|
||||
import normalizeProduct from './normalize-product'
|
||||
import { Wishlist } from '@vercel/commerce/types/wishlist'
|
||||
|
||||
const normalizeWishlist = (
|
||||
spreeSuccessResponse: SpreeSdkResponse,
|
||||
spreeWishlist: WishlistAttr
|
||||
): ExplicitCommerceWishlist => {
|
||||
): Wishlist => {
|
||||
const spreeWishedItems = jsonApi.findRelationshipDocuments<WishedItemAttr>(
|
||||
spreeSuccessResponse,
|
||||
spreeWishlist,
|
||||
'wished_items'
|
||||
)
|
||||
|
||||
const items: ExplicitCommerceWishlist['items'] = spreeWishedItems.map(
|
||||
(spreeWishedItem) => {
|
||||
const spreeWishedVariant =
|
||||
jsonApi.findSingleRelationshipDocument<VariantAttr>(
|
||||
spreeSuccessResponse,
|
||||
spreeWishedItem,
|
||||
'variant'
|
||||
)
|
||||
const items: Wishlist['items'] = spreeWishedItems.map((spreeWishedItem) => {
|
||||
const spreeWishedVariant =
|
||||
jsonApi.findSingleRelationshipDocument<VariantAttr>(
|
||||
spreeSuccessResponse,
|
||||
spreeWishedItem,
|
||||
'variant'
|
||||
)
|
||||
|
||||
if (spreeWishedVariant === null) {
|
||||
throw new MissingVariantError(
|
||||
`Couldn't find variant for wished item with id ${spreeWishedItem.id}.`
|
||||
)
|
||||
}
|
||||
|
||||
const spreeWishedProduct =
|
||||
jsonApi.findSingleRelationshipDocument<ProductAttr>(
|
||||
spreeSuccessResponse,
|
||||
spreeWishedVariant,
|
||||
'product'
|
||||
)
|
||||
|
||||
if (spreeWishedProduct === null) {
|
||||
throw new MissingProductError(
|
||||
`Couldn't find product for variant with id ${spreeWishedVariant.id}.`
|
||||
)
|
||||
}
|
||||
|
||||
return {
|
||||
id: spreeWishedItem.id,
|
||||
product_id: parseInt(spreeWishedProduct.id, 10),
|
||||
variant_id: parseInt(spreeWishedVariant.id, 10),
|
||||
product: normalizeProduct(spreeSuccessResponse, spreeWishedProduct),
|
||||
}
|
||||
if (spreeWishedVariant === null) {
|
||||
throw new MissingVariantError(
|
||||
`Couldn't find variant for wished item with id ${spreeWishedItem.id}.`
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
const spreeWishedProduct =
|
||||
jsonApi.findSingleRelationshipDocument<ProductAttr>(
|
||||
spreeSuccessResponse,
|
||||
spreeWishedVariant,
|
||||
'product'
|
||||
)
|
||||
|
||||
if (spreeWishedProduct === null) {
|
||||
throw new MissingProductError(
|
||||
`Couldn't find product for variant with id ${spreeWishedVariant.id}.`
|
||||
)
|
||||
}
|
||||
|
||||
return {
|
||||
id: spreeWishedItem.id,
|
||||
productId: spreeWishedProduct.id,
|
||||
variantId: spreeWishedVariant.id,
|
||||
product: normalizeProduct(spreeSuccessResponse, spreeWishedProduct),
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
id: spreeWishlist.id,
|
||||
|
@@ -2,8 +2,9 @@ import { useCallback } from 'react'
|
||||
import type { MutationHook } from '@vercel/commerce/utils/types'
|
||||
import useAddItem from '@vercel/commerce/wishlist/use-add-item'
|
||||
import type { UseAddItem } from '@vercel/commerce/wishlist/use-add-item'
|
||||
import type { AddItemHook } from '@vercel/commerce/types/wishlist'
|
||||
import useWishlist from './use-wishlist'
|
||||
import type { ExplicitWishlistAddItemHook } from '../types'
|
||||
|
||||
import type {
|
||||
WishedItem,
|
||||
WishlistsAddWishedItem,
|
||||
@@ -11,12 +12,12 @@ import type {
|
||||
import type { GraphQLFetcherResult } from '@vercel/commerce/api'
|
||||
import ensureIToken from '../utils/tokens/ensure-itoken'
|
||||
import type { IToken } from '@spree/storefront-api-v2-sdk/types/interfaces/Token'
|
||||
import type { AddItemHook } from '@vercel/commerce/types/wishlist'
|
||||
|
||||
import isLoggedIn from '../utils/tokens/is-logged-in'
|
||||
|
||||
export default useAddItem as UseAddItem<typeof handler>
|
||||
|
||||
export const handler: MutationHook<ExplicitWishlistAddItemHook> = {
|
||||
export const handler: MutationHook<AddItemHook> = {
|
||||
fetchOptions: {
|
||||
url: 'wishlists',
|
||||
query: 'addWishedItem',
|
||||
@@ -31,7 +32,7 @@ export const handler: MutationHook<ExplicitWishlistAddItemHook> = {
|
||||
)
|
||||
|
||||
const {
|
||||
item: { productId, variantId, wishlistToken },
|
||||
item: { variantId, wishlistToken },
|
||||
} = input
|
||||
|
||||
if (!isLoggedIn() || !wishlistToken) {
|
||||
|
@@ -2,8 +2,8 @@ import { useCallback } from 'react'
|
||||
import type { MutationHook } from '@vercel/commerce/utils/types'
|
||||
import useRemoveItem from '@vercel/commerce/wishlist/use-remove-item'
|
||||
import type { UseRemoveItem } from '@vercel/commerce/wishlist/use-remove-item'
|
||||
import type { RemoveItemHook } from '@vercel/commerce/types/wishlist'
|
||||
import useWishlist from './use-wishlist'
|
||||
import type { ExplicitWishlistRemoveItemHook } from '../types'
|
||||
import isLoggedIn from '../utils/tokens/is-logged-in'
|
||||
import ensureIToken from '../utils/tokens/ensure-itoken'
|
||||
import type { IToken } from '@spree/storefront-api-v2-sdk/types/interfaces/Token'
|
||||
@@ -12,7 +12,7 @@ import type { WishedItem } from '@spree/storefront-api-v2-sdk/types/interfaces/W
|
||||
|
||||
export default useRemoveItem as UseRemoveItem<typeof handler>
|
||||
|
||||
export const handler: MutationHook<ExplicitWishlistRemoveItemHook> = {
|
||||
export const handler: MutationHook<RemoveItemHook> = {
|
||||
fetchOptions: {
|
||||
url: 'wishlists',
|
||||
query: 'removeWishedItem',
|
||||
@@ -45,7 +45,7 @@ export const handler: MutationHook<ExplicitWishlistRemoveItemHook> = {
|
||||
},
|
||||
useHook: ({ fetch }) => {
|
||||
const useWrappedHook: ReturnType<
|
||||
MutationHook<ExplicitWishlistRemoveItemHook>['useHook']
|
||||
MutationHook<RemoveItemHook>['useHook']
|
||||
> = () => {
|
||||
const wishlist = useWishlist()
|
||||
|
||||
|
@@ -9,6 +9,7 @@ import type { Wishlist } from '@spree/storefront-api-v2-sdk/types/interfaces/Wis
|
||||
import ensureIToken from '../utils/tokens/ensure-itoken'
|
||||
import normalizeWishlist from '../utils/normalizations/normalize-wishlist'
|
||||
import isLoggedIn from '../utils/tokens/is-logged-in'
|
||||
import { ValidationError } from '@vercel/commerce/utils/errors'
|
||||
|
||||
export default useWishlist as UseWishlist<typeof handler>
|
||||
|
||||
@@ -28,7 +29,9 @@ export const handler: SWRHook<GetWishlistHook> = {
|
||||
)
|
||||
|
||||
if (!isLoggedIn()) {
|
||||
return null
|
||||
throw new ValidationError({
|
||||
message: 'Not logged in',
|
||||
})
|
||||
}
|
||||
|
||||
// TODO: Optimize with includeProducts.
|
||||
|
Reference in New Issue
Block a user