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:
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user