mirror of
https://github.com/vercel/commerce.git
synced 2025-07-25 19:21:23 +00:00
Enable text search for the Spree Framework
This commit is contained in:
@@ -10,8 +10,9 @@ import type { RelationType } from '@spree/storefront-api-v2-sdk/types/interfaces
|
||||
import SpreeResponseContentError from '../errors/SpreeResponseContentError'
|
||||
import { findIncluded } from './jsonApi'
|
||||
|
||||
const isColorProductOption = (productOption: ProductOption) =>
|
||||
productOption.displayName === 'Color'
|
||||
const isColorProductOption = (productOption: ProductOption) => {
|
||||
return productOption.displayName === 'Color'
|
||||
}
|
||||
|
||||
const expandOptions = (
|
||||
spreeSuccessResponse: JsonApiResponse,
|
||||
|
7
framework/spree/utils/getCartToken.ts
Normal file
7
framework/spree/utils/getCartToken.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { requireConfigValue } from '@framework/isomorphicConfig'
|
||||
import Cookies from 'js-cookie'
|
||||
|
||||
const getCartToken = () =>
|
||||
Cookies.get(requireConfigValue('cartCookieName') as string)
|
||||
|
||||
export default getCartToken
|
202
framework/spree/utils/normalizeCart.ts
Normal file
202
framework/spree/utils/normalizeCart.ts
Normal file
@@ -0,0 +1,202 @@
|
||||
import type {
|
||||
Cart,
|
||||
LineItem,
|
||||
ProductVariant,
|
||||
SelectedOption,
|
||||
} from '@commerce/types/cart'
|
||||
import MissingLineItemVariantError from '@framework/errors/MissingLineItemVariantError'
|
||||
import { requireConfigValue } from '@framework/isomorphicConfig'
|
||||
import type {
|
||||
JsonApiDocument,
|
||||
JsonApiListResponse,
|
||||
JsonApiSingleResponse,
|
||||
} from '@spree/storefront-api-v2-sdk/types/interfaces/JsonApi'
|
||||
import type { OrderAttr } from '@spree/storefront-api-v2-sdk/types/interfaces/Order'
|
||||
import { ProductAttr } from '@spree/storefront-api-v2-sdk/types/interfaces/Product'
|
||||
import type { RelationType } from '@spree/storefront-api-v2-sdk/types/interfaces/Relationships'
|
||||
import createGetAbsoluteImageUrl from './createGetAbsoluteImageUrl'
|
||||
import getMediaGallery from './getMediaGallery'
|
||||
import { findIncluded, findIncludedOfType } from './jsonApi'
|
||||
|
||||
const isColorProductOption = (productOptionType: any) => {
|
||||
// TODO: Fix type and merge with isColorProductOption in framework/spree/utils/expandOptions.ts
|
||||
return productOptionType.attributes.presentation === 'Color'
|
||||
}
|
||||
|
||||
const normalizeVariant = (
|
||||
spreeSuccessResponse: JsonApiSingleResponse | JsonApiListResponse,
|
||||
spreeVariant: JsonApiDocument
|
||||
): ProductVariant => {
|
||||
const productIdentifier = spreeVariant.relationships.product
|
||||
.data as RelationType
|
||||
const spreeProduct = findIncluded<ProductAttr>(
|
||||
spreeSuccessResponse,
|
||||
productIdentifier.type,
|
||||
productIdentifier.id
|
||||
)
|
||||
|
||||
if (spreeProduct === null) {
|
||||
throw new MissingLineItemVariantError(
|
||||
`Couldn't find product with id ${productIdentifier.id}.`
|
||||
)
|
||||
}
|
||||
|
||||
const spreeVariantImageRecords = findIncludedOfType(
|
||||
spreeSuccessResponse,
|
||||
spreeVariant,
|
||||
'images'
|
||||
)
|
||||
|
||||
let lineItemImage
|
||||
|
||||
const variantImage = getMediaGallery(
|
||||
spreeVariantImageRecords,
|
||||
createGetAbsoluteImageUrl(requireConfigValue('spreeImageHost') as string)
|
||||
)[0]
|
||||
|
||||
if (variantImage) {
|
||||
lineItemImage = variantImage
|
||||
} else {
|
||||
const spreeProductImageRecords = findIncludedOfType(
|
||||
spreeSuccessResponse,
|
||||
spreeProduct,
|
||||
'images'
|
||||
)
|
||||
|
||||
const productImage = getMediaGallery(
|
||||
spreeProductImageRecords,
|
||||
createGetAbsoluteImageUrl(requireConfigValue('spreeImageHost') as string)
|
||||
)[0]
|
||||
|
||||
lineItemImage = productImage
|
||||
}
|
||||
|
||||
return {
|
||||
id: spreeVariant.id,
|
||||
sku: spreeVariant.attributes.sku,
|
||||
name: spreeProduct.attributes.name,
|
||||
requiresShipping: true,
|
||||
price: parseFloat(spreeVariant.attributes.price),
|
||||
listPrice: parseFloat(spreeVariant.attributes.price),
|
||||
image: lineItemImage,
|
||||
isInStock: spreeVariant.attributes.in_stock,
|
||||
availableForSale: spreeVariant.attributes.purchasable,
|
||||
weight: spreeVariant.attributes.weight,
|
||||
height: spreeVariant.attributes.height,
|
||||
width: spreeVariant.attributes.width,
|
||||
depth: spreeVariant.attributes.depth,
|
||||
}
|
||||
}
|
||||
|
||||
const normalizeLineItem = (
|
||||
spreeSuccessResponse: JsonApiSingleResponse | JsonApiListResponse,
|
||||
spreeLineItem: JsonApiDocument
|
||||
): LineItem => {
|
||||
//TODO: Replace JsonApiDocument type in spreeLineItem with more specific, new Spree line item item
|
||||
const variantIdentifier = spreeLineItem.relationships.variant
|
||||
.data as RelationType
|
||||
const variant = findIncluded(
|
||||
spreeSuccessResponse,
|
||||
variantIdentifier.type,
|
||||
variantIdentifier.id
|
||||
)
|
||||
|
||||
if (variant === null) {
|
||||
throw new MissingLineItemVariantError(
|
||||
`Couldn't find variant with id ${variantIdentifier.id}.`
|
||||
)
|
||||
}
|
||||
|
||||
const productIdentifier = variant.relationships.product.data as RelationType
|
||||
const product = findIncluded<ProductAttr>(
|
||||
spreeSuccessResponse,
|
||||
productIdentifier.type,
|
||||
productIdentifier.id
|
||||
)
|
||||
|
||||
if (product === null) {
|
||||
throw new MissingLineItemVariantError(
|
||||
`Couldn't find product with id ${productIdentifier.id}.`
|
||||
)
|
||||
}
|
||||
|
||||
const path = `/${product.attributes.slug}`
|
||||
|
||||
const spreeOptionValues = findIncludedOfType(
|
||||
spreeSuccessResponse,
|
||||
variant,
|
||||
'option_values'
|
||||
)
|
||||
|
||||
const options: SelectedOption[] = spreeOptionValues.map(
|
||||
(spreeOptionValue) => {
|
||||
const spreeOptionTypeIdentifier = spreeOptionValue.relationships
|
||||
.option_type.data as RelationType
|
||||
|
||||
const spreeOptionType = findIncluded(
|
||||
spreeSuccessResponse,
|
||||
spreeOptionTypeIdentifier.type,
|
||||
spreeOptionTypeIdentifier.id
|
||||
)
|
||||
|
||||
if (spreeOptionType === null) {
|
||||
throw new MissingLineItemVariantError(
|
||||
`Couldn't find option type with id ${spreeOptionTypeIdentifier.id}.`
|
||||
)
|
||||
}
|
||||
|
||||
const label = isColorProductOption(spreeOptionType)
|
||||
? spreeOptionValue.attributes.name
|
||||
: spreeOptionValue.attributes.presentation
|
||||
|
||||
return {
|
||||
id: spreeOptionValue.id,
|
||||
name: spreeOptionType.attributes.presentation,
|
||||
value: label,
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
return {
|
||||
id: spreeLineItem.id,
|
||||
variantId: variant.id,
|
||||
productId: productIdentifier.id,
|
||||
name: spreeLineItem.attributes.name,
|
||||
quantity: spreeLineItem.attributes.quantity,
|
||||
discounts: [], // TODO: Retrieve from Spree
|
||||
path,
|
||||
variant: normalizeVariant(spreeSuccessResponse, variant),
|
||||
options,
|
||||
}
|
||||
}
|
||||
|
||||
const normalizeCart = (
|
||||
spreeSuccessResponse: JsonApiSingleResponse | JsonApiListResponse,
|
||||
spreeCart: OrderAttr
|
||||
): Cart => {
|
||||
const lineItems = findIncludedOfType(
|
||||
spreeSuccessResponse,
|
||||
spreeCart,
|
||||
'line_items'
|
||||
).map((lineItem) => normalizeLineItem(spreeSuccessResponse, lineItem))
|
||||
|
||||
return {
|
||||
id: spreeCart.id,
|
||||
createdAt: spreeCart.attributes.created_at.toString(),
|
||||
currency: { code: spreeCart.attributes.currency },
|
||||
taxesIncluded: true,
|
||||
lineItems,
|
||||
lineItemsSubtotalPrice: parseFloat(spreeCart.attributes.item_total),
|
||||
// TODO: We need a value from Spree which includes item total and discounts in one value for subtotalPrice.
|
||||
subtotalPrice: parseFloat(spreeCart.attributes.item_total),
|
||||
totalPrice: parseFloat(spreeCart.attributes.total),
|
||||
customerId: spreeCart.attributes.token,
|
||||
email: spreeCart.attributes.email,
|
||||
discounts: [],
|
||||
// discounts: [{value: number}] // TODO: Retrieve from Spree
|
||||
}
|
||||
}
|
||||
|
||||
export { normalizeLineItem }
|
||||
|
||||
export default normalizeCart
|
@@ -1,4 +1,5 @@
|
||||
import type {
|
||||
Product,
|
||||
ProductOption,
|
||||
ProductPrice,
|
||||
ProductVariant,
|
||||
@@ -18,7 +19,7 @@ import { findIncludedOfType } from './jsonApi'
|
||||
const normalizeProduct = (
|
||||
spreeSuccessResponse: JsonApiSingleResponse | JsonApiListResponse,
|
||||
spreeProduct: ProductAttr
|
||||
) => {
|
||||
): Product => {
|
||||
const spreeImageRecords = findIncludedOfType(
|
||||
spreeSuccessResponse,
|
||||
spreeProduct,
|
||||
|
16
framework/spree/utils/setCartToken.ts
Normal file
16
framework/spree/utils/setCartToken.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { requireConfigValue } from '@framework/isomorphicConfig'
|
||||
import Cookies from 'js-cookie'
|
||||
|
||||
const setCartToken = (cartToken: string) => {
|
||||
const cookieOptions = {
|
||||
expires: requireConfigValue('cartCookieExpire') as number,
|
||||
}
|
||||
|
||||
Cookies.set(
|
||||
requireConfigValue('cartCookieName') as string,
|
||||
cartToken,
|
||||
cookieOptions
|
||||
)
|
||||
}
|
||||
|
||||
export default setCartToken
|
21
framework/spree/utils/validateCookieExpire.ts
Normal file
21
framework/spree/utils/validateCookieExpire.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
const validateCookieExpire = (expire: unknown) => {
|
||||
let expireInteger: number
|
||||
|
||||
if (typeof expire === 'string') {
|
||||
expireInteger = parseFloat(expire || '')
|
||||
} else if (typeof expire === 'number') {
|
||||
expireInteger = expire
|
||||
} else {
|
||||
throw new TypeError(
|
||||
'expire must be a string containing a number or an integer.'
|
||||
)
|
||||
}
|
||||
|
||||
if (expireInteger < 0) {
|
||||
throw new RangeError('expire must be non-negative.')
|
||||
}
|
||||
|
||||
return expireInteger
|
||||
}
|
||||
|
||||
export default validateCookieExpire
|
Reference in New Issue
Block a user