mirror of
https://github.com/vercel/commerce.git
synced 2025-07-25 19:21:23 +00:00
Flatten fetcher responses
This commit is contained in:
@@ -2,6 +2,8 @@ import * as qs from 'qs'
|
||||
import { errors } from '@spree/storefront-api-v2-sdk'
|
||||
import type { CreateCustomizedFetchFetcher } from '@spree/storefront-api-v2-sdk/types/interfaces/CreateCustomizedFetchFetcher'
|
||||
|
||||
export const fetchResponseKey = Symbol('fetch-response-key')
|
||||
|
||||
const createCustomizedFetchFetcher: CreateCustomizedFetchFetcher = (
|
||||
fetcherOptions
|
||||
) => {
|
||||
@@ -65,11 +67,9 @@ const createCustomizedFetchFetcher: CreateCustomizedFetchFetcher = (
|
||||
throw new FetchError(response, request, data)
|
||||
}
|
||||
|
||||
return {
|
||||
// Add response key to the prototype so it can be passed inside the GraphQLFetcherResult type.
|
||||
// TODO: Search for a better solution than adding response to the prototype.
|
||||
data: Object.setPrototypeOf({ data }, { response }),
|
||||
}
|
||||
data[fetchResponseKey] = response
|
||||
|
||||
return { data }
|
||||
} catch (error) {
|
||||
if (error instanceof FetchError) {
|
||||
throw error
|
||||
|
@@ -16,7 +16,12 @@ import type { RelationType } from '@spree/storefront-api-v2-sdk/types/interfaces
|
||||
import createGetAbsoluteImageUrl from './create-get-absolute-image-url'
|
||||
import getMediaGallery from './get-media-gallery'
|
||||
import { findIncluded, findIncludedOfType } from './find-json-api-documents'
|
||||
import type { LineItemAttr, OptionTypeAttr, VariantAttr } from '../types'
|
||||
import type {
|
||||
LineItemAttr,
|
||||
OptionTypeAttr,
|
||||
SpreeSdkResponse,
|
||||
VariantAttr,
|
||||
} from '../types'
|
||||
import type { Image } from '@commerce/types/common'
|
||||
|
||||
const placeholderImage = requireConfigValue('lineItemPlaceholderImageUrl') as
|
||||
@@ -28,7 +33,7 @@ const isColorProductOption = (productOptionType: OptionTypeAttr) => {
|
||||
}
|
||||
|
||||
const normalizeVariant = (
|
||||
spreeSuccessResponse: JsonApiSingleResponse | JsonApiListResponse,
|
||||
spreeSuccessResponse: SpreeSdkResponse,
|
||||
spreeVariant: VariantAttr
|
||||
): ProductVariant => {
|
||||
const productIdentifier = spreeVariant.relationships.product
|
||||
@@ -102,7 +107,7 @@ const normalizeVariant = (
|
||||
}
|
||||
|
||||
const normalizeLineItem = (
|
||||
spreeSuccessResponse: JsonApiSingleResponse | JsonApiListResponse,
|
||||
spreeSuccessResponse: SpreeSdkResponse,
|
||||
spreeLineItem: LineItemAttr
|
||||
): LineItem => {
|
||||
const variantIdentifier = spreeLineItem.relationships.variant
|
||||
@@ -183,7 +188,7 @@ const normalizeLineItem = (
|
||||
}
|
||||
|
||||
const normalizeCart = (
|
||||
spreeSuccessResponse: JsonApiSingleResponse | JsonApiListResponse,
|
||||
spreeSuccessResponse: SpreeSdkResponse,
|
||||
spreeCart: OrderAttr
|
||||
): Cart => {
|
||||
const lineItems = findIncludedOfType(
|
||||
|
@@ -18,13 +18,14 @@ import getMediaGallery from './get-media-gallery'
|
||||
import { findIncluded, findIncludedOfType } from './find-json-api-documents'
|
||||
import getProductPath from './get-product-path'
|
||||
import MissingPrimaryVariantError from '../errors/MissingPrimaryVariantError'
|
||||
import type { SpreeSdkResponse } from '@framework/types'
|
||||
|
||||
const placeholderImage = requireConfigValue('productPlaceholderImageUrl') as
|
||||
| string
|
||||
| false
|
||||
|
||||
const normalizeProduct = (
|
||||
spreeSuccessResponse: JsonApiSingleResponse | JsonApiListResponse,
|
||||
spreeSuccessResponse: SpreeSdkResponse,
|
||||
spreeProduct: ProductAttr
|
||||
): Product => {
|
||||
const primaryVariantIdentifier = spreeProduct.relationships.primary_variant
|
||||
|
Reference in New Issue
Block a user