mirror of
https://github.com/vercel/commerce.git
synced 2025-07-21 11:51:20 +00:00
Flatten fetcher responses
This commit is contained in:
parent
b9f8ed659d
commit
60894d32c5
@ -71,14 +71,12 @@ export default function getAllProductPathsOperation({
|
||||
const config = commerce.getConfig(userConfig)
|
||||
const { fetch: apiFetch } = config // TODO: Send config.locale to Spree.
|
||||
|
||||
const {
|
||||
data: { data: spreeSuccessResponse },
|
||||
} = await apiFetch<{ data: IProductsSlugs }, SpreeSdkVariables>(
|
||||
'__UNUSED__',
|
||||
{
|
||||
variables,
|
||||
}
|
||||
)
|
||||
const { data: spreeSuccessResponse } = await apiFetch<
|
||||
IProductsSlugs,
|
||||
SpreeSdkVariables
|
||||
>('__UNUSED__', {
|
||||
variables,
|
||||
})
|
||||
|
||||
const normalizedProductsPaths: Pick<Product, 'path'>[] =
|
||||
spreeSuccessResponse.data.map((spreeProduct) => ({
|
||||
|
@ -57,9 +57,10 @@ export default function getAllProductsOperation({
|
||||
const config = commerce.getConfig(userConfig)
|
||||
const { fetch: apiFetch } = config // TODO: Send config.locale to Spree.
|
||||
|
||||
const {
|
||||
data: { data: spreeSuccessResponse },
|
||||
} = await apiFetch<{ data: IProducts }, SpreeSdkVariables>('__UNUSED__', {
|
||||
const { data: spreeSuccessResponse } = await apiFetch<
|
||||
IProducts,
|
||||
SpreeSdkVariables
|
||||
>('__UNUSED__', {
|
||||
variables,
|
||||
})
|
||||
|
||||
|
@ -63,9 +63,10 @@ export default function getProductOperation({
|
||||
const config = commerce.getConfig(userConfig)
|
||||
const { fetch: apiFetch } = config // TODO: Send config.locale to Spree.
|
||||
|
||||
const {
|
||||
data: { data: spreeSuccessResponse },
|
||||
} = await apiFetch<{ data: IProduct }, SpreeSdkVariables>('__UNUSED__', {
|
||||
const { data: spreeSuccessResponse } = await apiFetch<
|
||||
IProduct,
|
||||
SpreeSdkVariables
|
||||
>('__UNUSED__', {
|
||||
variables,
|
||||
})
|
||||
|
||||
|
@ -82,12 +82,8 @@ export default function getSiteInfoOperation({
|
||||
const config = commerce.getConfig(userConfig)
|
||||
const { fetch: apiFetch } = config // TODO: Send config.locale to Spree.
|
||||
|
||||
const {
|
||||
data: { data: spreeCategoriesSuccessResponse },
|
||||
} = await apiFetch<
|
||||
{
|
||||
data: ITaxons
|
||||
},
|
||||
const { data: spreeCategoriesSuccessResponse } = await apiFetch<
|
||||
ITaxons,
|
||||
SpreeSdkVariables
|
||||
>('__UNUSED__', {
|
||||
variables: createVariables(
|
||||
@ -95,12 +91,8 @@ export default function getSiteInfoOperation({
|
||||
),
|
||||
})
|
||||
|
||||
const {
|
||||
data: { data: spreeBrandsSuccessResponse },
|
||||
} = await apiFetch<
|
||||
{
|
||||
data: ITaxons
|
||||
},
|
||||
const { data: spreeBrandsSuccessResponse } = await apiFetch<
|
||||
ITaxons,
|
||||
SpreeSdkVariables
|
||||
>('__UNUSED__', {
|
||||
variables: createVariables(
|
||||
|
@ -3,15 +3,14 @@ import { errors, makeClient } from '@spree/storefront-api-v2-sdk'
|
||||
import { requireConfigValue } from '../../isomorphic-config'
|
||||
import convertSpreeErrorToGraphQlError from '../../utils/convert-spree-error-to-graph-ql-error'
|
||||
import type { ResultResponse } from '@spree/storefront-api-v2-sdk/types/interfaces/ResultResponse'
|
||||
import type {
|
||||
JsonApiListResponse,
|
||||
JsonApiSingleResponse,
|
||||
} from '@spree/storefront-api-v2-sdk/types/interfaces/JsonApi'
|
||||
import getSpreeSdkMethodFromEndpointPath from '../../utils/get-spree-sdk-method-from-endpoint-path'
|
||||
import SpreeSdkMethodFromEndpointPathError from 'framework/spree/errors/SpreeSdkMethodFromEndpointPathError'
|
||||
import { GraphQLFetcher, GraphQLFetcherResult } from '@commerce/api'
|
||||
import createCustomizedFetchFetcher from '../../utils/create-customized-fetch-fetcher'
|
||||
import createCustomizedFetchFetcher, {
|
||||
fetchResponseKey,
|
||||
} from '../../utils/create-customized-fetch-fetcher'
|
||||
import fetch, { Request } from 'node-fetch'
|
||||
import type { SpreeSdkResponseWithRawResponse } from '@framework/types'
|
||||
|
||||
export type CreateApiFetch = (
|
||||
getConfig: () => SpreeApiConfig
|
||||
@ -52,20 +51,19 @@ const createApiFetch: CreateApiFetch = (_getConfig) => {
|
||||
)
|
||||
}
|
||||
|
||||
const storeResponse: ResultResponse<
|
||||
JsonApiSingleResponse | JsonApiListResponse
|
||||
> = await getSpreeSdkMethodFromEndpointPath(
|
||||
client,
|
||||
variables.methodPath
|
||||
)(...variables.arguments)
|
||||
const storeResponse: ResultResponse<SpreeSdkResponseWithRawResponse> =
|
||||
await getSpreeSdkMethodFromEndpointPath(
|
||||
client,
|
||||
variables.methodPath
|
||||
)(...variables.arguments)
|
||||
|
||||
if (storeResponse.isSuccess()) {
|
||||
const data = storeResponse.success()
|
||||
const rawFetchRespone = Object.getPrototypeOf(data).response
|
||||
const rawFetchResponse = data[fetchResponseKey]
|
||||
|
||||
return {
|
||||
data,
|
||||
res: rawFetchRespone,
|
||||
res: rawFetchResponse,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -46,9 +46,9 @@ export const handler: MutationHook<AddItemHook> = {
|
||||
].join(','),
|
||||
}
|
||||
|
||||
const {
|
||||
data: { data: spreeSuccessResponse },
|
||||
} = await fetch<GraphQLFetcherResult<{ data: IOrder }>>({
|
||||
const { data: spreeSuccessResponse } = await fetch<
|
||||
GraphQLFetcherResult<IOrder>
|
||||
>({
|
||||
variables: {
|
||||
methodPath: 'cart.addItem',
|
||||
arguments: [token, addItemParameters],
|
||||
|
@ -38,9 +38,9 @@ export const handler: SWRHook<GetCartHook> = {
|
||||
const spreeToken: IToken = { orderToken: cartToken }
|
||||
|
||||
try {
|
||||
const {
|
||||
data: { data: spreeCartShowSuccessResponse },
|
||||
} = await fetch<GraphQLFetcherResult<{ data: IOrder }>>({
|
||||
const { data: spreeCartShowSuccessResponse } = await fetch<
|
||||
GraphQLFetcherResult<IOrder>
|
||||
>({
|
||||
variables: {
|
||||
methodPath: 'cart.show',
|
||||
arguments: [
|
||||
@ -74,9 +74,9 @@ export const handler: SWRHook<GetCartHook> = {
|
||||
}
|
||||
|
||||
if (!spreeCartResponse || spreeCartResponse?.data.attributes.completed_at) {
|
||||
const {
|
||||
data: { data: spreeCartCreateSuccessResponse },
|
||||
} = await fetch<GraphQLFetcherResult<{ data: IOrder }>>({
|
||||
const { data: spreeCartCreateSuccessResponse } = await fetch<
|
||||
GraphQLFetcherResult<IOrder>
|
||||
>({
|
||||
variables: {
|
||||
methodPath: 'cart.create',
|
||||
arguments: [],
|
||||
|
@ -42,9 +42,9 @@ export const handler: MutationHook<RemoveItemHook> = {
|
||||
].join(','),
|
||||
}
|
||||
|
||||
const {
|
||||
data: { data: spreeSuccessResponse },
|
||||
} = await fetch<GraphQLFetcherResult<{ data: IOrder }>>({
|
||||
const { data: spreeSuccessResponse } = await fetch<
|
||||
GraphQLFetcherResult<IOrder>
|
||||
>({
|
||||
variables: {
|
||||
methodPath: 'cart.removeItem',
|
||||
arguments: [token, lineItemId, removeItemParameters],
|
||||
|
@ -51,9 +51,9 @@ export const handler: MutationHook<UpdateItemHook> = {
|
||||
].join(','),
|
||||
}
|
||||
|
||||
const {
|
||||
data: { data: spreeSuccessResponse },
|
||||
} = await fetch<GraphQLFetcherResult<{ data: IOrder }>>({
|
||||
const { data: spreeSuccessResponse } = await fetch<
|
||||
GraphQLFetcherResult<IOrder>
|
||||
>({
|
||||
variables: {
|
||||
methodPath: 'cart.setQuantity',
|
||||
arguments: [token, setQuantityParameters],
|
||||
|
@ -2,17 +2,19 @@ import type { Fetcher } from '@commerce/utils/types'
|
||||
import convertSpreeErrorToGraphQlError from './utils/convert-spree-error-to-graph-ql-error'
|
||||
import { makeClient } from '@spree/storefront-api-v2-sdk'
|
||||
import type { ResultResponse } from '@spree/storefront-api-v2-sdk/types/interfaces/ResultResponse'
|
||||
import type {
|
||||
JsonApiListResponse,
|
||||
JsonApiSingleResponse,
|
||||
} from '@spree/storefront-api-v2-sdk/types/interfaces/JsonApi'
|
||||
import { errors } from '@spree/storefront-api-v2-sdk'
|
||||
import { requireConfigValue } from './isomorphic-config'
|
||||
import getSpreeSdkMethodFromEndpointPath from './utils/get-spree-sdk-method-from-endpoint-path'
|
||||
import SpreeSdkMethodFromEndpointPathError from './errors/SpreeSdkMethodFromEndpointPathError'
|
||||
import type { SpreeSdkVariables } from './types'
|
||||
import type {
|
||||
SpreeSdkResponse,
|
||||
SpreeSdkResponseWithRawResponse,
|
||||
SpreeSdkVariables,
|
||||
} from './types'
|
||||
import type { GraphQLFetcherResult } from '@commerce/api'
|
||||
import createCustomizedFetchFetcher from './utils/create-customized-fetch-fetcher'
|
||||
import createCustomizedFetchFetcher, {
|
||||
fetchResponseKey,
|
||||
} from './utils/create-customized-fetch-fetcher'
|
||||
|
||||
const client = makeClient({
|
||||
host: requireConfigValue('apiHost') as string,
|
||||
@ -27,7 +29,7 @@ const client = makeClient({
|
||||
})
|
||||
|
||||
const fetcher: Fetcher<
|
||||
GraphQLFetcherResult<JsonApiSingleResponse | JsonApiListResponse>,
|
||||
GraphQLFetcherResult<SpreeSdkResponse>,
|
||||
SpreeSdkVariables
|
||||
> = async (requestOptions) => {
|
||||
const { url, method, variables, query } = requestOptions
|
||||
@ -47,20 +49,19 @@ const fetcher: Fetcher<
|
||||
)
|
||||
}
|
||||
|
||||
const storeResponse: ResultResponse<
|
||||
JsonApiSingleResponse | JsonApiListResponse
|
||||
> = await getSpreeSdkMethodFromEndpointPath(
|
||||
client,
|
||||
variables.methodPath
|
||||
)(...variables.arguments)
|
||||
const storeResponse: ResultResponse<SpreeSdkResponseWithRawResponse> =
|
||||
await getSpreeSdkMethodFromEndpointPath(
|
||||
client,
|
||||
variables.methodPath
|
||||
)(...variables.arguments)
|
||||
|
||||
if (storeResponse.isSuccess()) {
|
||||
const data = storeResponse.success()
|
||||
const rawFetchRespone = Object.getPrototypeOf(data).response
|
||||
const rawFetchResponse = data[fetchResponseKey]
|
||||
|
||||
return {
|
||||
data,
|
||||
res: rawFetchRespone,
|
||||
res: rawFetchResponse,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -40,9 +40,9 @@ export const handler: SWRHook<SearchProductsHook> = {
|
||||
|
||||
const sort = input.sort ? { sort: nextToSpreeSortMap[input.sort] } : {}
|
||||
|
||||
const {
|
||||
data: { data: spreeSuccessResponse },
|
||||
} = await fetch<GraphQLFetcherResult<{ data: IProducts }>>({
|
||||
const { data: spreeSuccessResponse } = await fetch<
|
||||
GraphQLFetcherResult<IProducts>
|
||||
>({
|
||||
variables: {
|
||||
methodPath: 'products.list',
|
||||
arguments: [
|
||||
|
@ -1,9 +1,11 @@
|
||||
import type { fetchResponseKey } from '@framework/utils/create-customized-fetch-fetcher'
|
||||
import type {
|
||||
JsonApiDocument,
|
||||
JsonApiListResponse,
|
||||
JsonApiSingleResponse,
|
||||
} from '@spree/storefront-api-v2-sdk/types/interfaces/JsonApi'
|
||||
import type { ResultResponse } from '@spree/storefront-api-v2-sdk/types/interfaces/ResultResponse'
|
||||
import type { Response } from '@vercel/fetch'
|
||||
|
||||
export type UnknownObjectValues = Record<string, unknown>
|
||||
|
||||
@ -11,8 +13,14 @@ export type NonUndefined<T> = T extends undefined ? never : T
|
||||
|
||||
export type ValueOf<T> = T[keyof T]
|
||||
|
||||
export type SpreeSdkResponse = JsonApiSingleResponse | JsonApiListResponse
|
||||
|
||||
export type SpreeSdkResponseWithRawResponse = SpreeSdkResponse & {
|
||||
[fetchResponseKey]: Response
|
||||
}
|
||||
|
||||
export type SpreeSdkMethodReturnType = Promise<
|
||||
ResultResponse<JsonApiSingleResponse | JsonApiListResponse>
|
||||
ResultResponse<SpreeSdkResponseWithRawResponse>
|
||||
>
|
||||
|
||||
export type SpreeSdkMethod = (...args: any[]) => SpreeSdkMethodReturnType
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user