Flatten fetcher responses

This commit is contained in:
tniezg
2021-09-03 09:52:59 +02:00
parent b9f8ed659d
commit 60894d32c5
15 changed files with 88 additions and 83 deletions

View File

@@ -71,14 +71,12 @@ export default function getAllProductPathsOperation({
const config = commerce.getConfig(userConfig) const config = commerce.getConfig(userConfig)
const { fetch: apiFetch } = config // TODO: Send config.locale to Spree. const { fetch: apiFetch } = config // TODO: Send config.locale to Spree.
const { const { data: spreeSuccessResponse } = await apiFetch<
data: { data: spreeSuccessResponse }, IProductsSlugs,
} = await apiFetch<{ data: IProductsSlugs }, SpreeSdkVariables>( SpreeSdkVariables
'__UNUSED__', >('__UNUSED__', {
{ variables,
variables, })
}
)
const normalizedProductsPaths: Pick<Product, 'path'>[] = const normalizedProductsPaths: Pick<Product, 'path'>[] =
spreeSuccessResponse.data.map((spreeProduct) => ({ spreeSuccessResponse.data.map((spreeProduct) => ({

View File

@@ -57,9 +57,10 @@ export default function getAllProductsOperation({
const config = commerce.getConfig(userConfig) const config = commerce.getConfig(userConfig)
const { fetch: apiFetch } = config // TODO: Send config.locale to Spree. const { fetch: apiFetch } = config // TODO: Send config.locale to Spree.
const { const { data: spreeSuccessResponse } = await apiFetch<
data: { data: spreeSuccessResponse }, IProducts,
} = await apiFetch<{ data: IProducts }, SpreeSdkVariables>('__UNUSED__', { SpreeSdkVariables
>('__UNUSED__', {
variables, variables,
}) })

View File

@@ -63,9 +63,10 @@ export default function getProductOperation({
const config = commerce.getConfig(userConfig) const config = commerce.getConfig(userConfig)
const { fetch: apiFetch } = config // TODO: Send config.locale to Spree. const { fetch: apiFetch } = config // TODO: Send config.locale to Spree.
const { const { data: spreeSuccessResponse } = await apiFetch<
data: { data: spreeSuccessResponse }, IProduct,
} = await apiFetch<{ data: IProduct }, SpreeSdkVariables>('__UNUSED__', { SpreeSdkVariables
>('__UNUSED__', {
variables, variables,
}) })

View File

@@ -82,12 +82,8 @@ export default function getSiteInfoOperation({
const config = commerce.getConfig(userConfig) const config = commerce.getConfig(userConfig)
const { fetch: apiFetch } = config // TODO: Send config.locale to Spree. const { fetch: apiFetch } = config // TODO: Send config.locale to Spree.
const { const { data: spreeCategoriesSuccessResponse } = await apiFetch<
data: { data: spreeCategoriesSuccessResponse }, ITaxons,
} = await apiFetch<
{
data: ITaxons
},
SpreeSdkVariables SpreeSdkVariables
>('__UNUSED__', { >('__UNUSED__', {
variables: createVariables( variables: createVariables(
@@ -95,12 +91,8 @@ export default function getSiteInfoOperation({
), ),
}) })
const { const { data: spreeBrandsSuccessResponse } = await apiFetch<
data: { data: spreeBrandsSuccessResponse }, ITaxons,
} = await apiFetch<
{
data: ITaxons
},
SpreeSdkVariables SpreeSdkVariables
>('__UNUSED__', { >('__UNUSED__', {
variables: createVariables( variables: createVariables(

View File

@@ -3,15 +3,14 @@ import { errors, makeClient } from '@spree/storefront-api-v2-sdk'
import { requireConfigValue } from '../../isomorphic-config' import { requireConfigValue } from '../../isomorphic-config'
import convertSpreeErrorToGraphQlError from '../../utils/convert-spree-error-to-graph-ql-error' 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 { 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 getSpreeSdkMethodFromEndpointPath from '../../utils/get-spree-sdk-method-from-endpoint-path'
import SpreeSdkMethodFromEndpointPathError from 'framework/spree/errors/SpreeSdkMethodFromEndpointPathError' import SpreeSdkMethodFromEndpointPathError from 'framework/spree/errors/SpreeSdkMethodFromEndpointPathError'
import { GraphQLFetcher, GraphQLFetcherResult } from '@commerce/api' 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 fetch, { Request } from 'node-fetch'
import type { SpreeSdkResponseWithRawResponse } from '@framework/types'
export type CreateApiFetch = ( export type CreateApiFetch = (
getConfig: () => SpreeApiConfig getConfig: () => SpreeApiConfig
@@ -52,20 +51,19 @@ const createApiFetch: CreateApiFetch = (_getConfig) => {
) )
} }
const storeResponse: ResultResponse< const storeResponse: ResultResponse<SpreeSdkResponseWithRawResponse> =
JsonApiSingleResponse | JsonApiListResponse await getSpreeSdkMethodFromEndpointPath(
> = await getSpreeSdkMethodFromEndpointPath( client,
client, variables.methodPath
variables.methodPath )(...variables.arguments)
)(...variables.arguments)
if (storeResponse.isSuccess()) { if (storeResponse.isSuccess()) {
const data = storeResponse.success() const data = storeResponse.success()
const rawFetchRespone = Object.getPrototypeOf(data).response const rawFetchResponse = data[fetchResponseKey]
return { return {
data, data,
res: rawFetchRespone, res: rawFetchResponse,
} }
} }

View File

@@ -46,9 +46,9 @@ export const handler: MutationHook<AddItemHook> = {
].join(','), ].join(','),
} }
const { const { data: spreeSuccessResponse } = await fetch<
data: { data: spreeSuccessResponse }, GraphQLFetcherResult<IOrder>
} = await fetch<GraphQLFetcherResult<{ data: IOrder }>>({ >({
variables: { variables: {
methodPath: 'cart.addItem', methodPath: 'cart.addItem',
arguments: [token, addItemParameters], arguments: [token, addItemParameters],

View File

@@ -38,9 +38,9 @@ export const handler: SWRHook<GetCartHook> = {
const spreeToken: IToken = { orderToken: cartToken } const spreeToken: IToken = { orderToken: cartToken }
try { try {
const { const { data: spreeCartShowSuccessResponse } = await fetch<
data: { data: spreeCartShowSuccessResponse }, GraphQLFetcherResult<IOrder>
} = await fetch<GraphQLFetcherResult<{ data: IOrder }>>({ >({
variables: { variables: {
methodPath: 'cart.show', methodPath: 'cart.show',
arguments: [ arguments: [
@@ -74,9 +74,9 @@ export const handler: SWRHook<GetCartHook> = {
} }
if (!spreeCartResponse || spreeCartResponse?.data.attributes.completed_at) { if (!spreeCartResponse || spreeCartResponse?.data.attributes.completed_at) {
const { const { data: spreeCartCreateSuccessResponse } = await fetch<
data: { data: spreeCartCreateSuccessResponse }, GraphQLFetcherResult<IOrder>
} = await fetch<GraphQLFetcherResult<{ data: IOrder }>>({ >({
variables: { variables: {
methodPath: 'cart.create', methodPath: 'cart.create',
arguments: [], arguments: [],

View File

@@ -42,9 +42,9 @@ export const handler: MutationHook<RemoveItemHook> = {
].join(','), ].join(','),
} }
const { const { data: spreeSuccessResponse } = await fetch<
data: { data: spreeSuccessResponse }, GraphQLFetcherResult<IOrder>
} = await fetch<GraphQLFetcherResult<{ data: IOrder }>>({ >({
variables: { variables: {
methodPath: 'cart.removeItem', methodPath: 'cart.removeItem',
arguments: [token, lineItemId, removeItemParameters], arguments: [token, lineItemId, removeItemParameters],

View File

@@ -51,9 +51,9 @@ export const handler: MutationHook<UpdateItemHook> = {
].join(','), ].join(','),
} }
const { const { data: spreeSuccessResponse } = await fetch<
data: { data: spreeSuccessResponse }, GraphQLFetcherResult<IOrder>
} = await fetch<GraphQLFetcherResult<{ data: IOrder }>>({ >({
variables: { variables: {
methodPath: 'cart.setQuantity', methodPath: 'cart.setQuantity',
arguments: [token, setQuantityParameters], arguments: [token, setQuantityParameters],

View File

@@ -2,17 +2,19 @@ import type { Fetcher } from '@commerce/utils/types'
import convertSpreeErrorToGraphQlError from './utils/convert-spree-error-to-graph-ql-error' import convertSpreeErrorToGraphQlError from './utils/convert-spree-error-to-graph-ql-error'
import { makeClient } from '@spree/storefront-api-v2-sdk' import { makeClient } from '@spree/storefront-api-v2-sdk'
import type { ResultResponse } from '@spree/storefront-api-v2-sdk/types/interfaces/ResultResponse' 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 { errors } from '@spree/storefront-api-v2-sdk'
import { requireConfigValue } from './isomorphic-config' import { requireConfigValue } from './isomorphic-config'
import getSpreeSdkMethodFromEndpointPath from './utils/get-spree-sdk-method-from-endpoint-path' import getSpreeSdkMethodFromEndpointPath from './utils/get-spree-sdk-method-from-endpoint-path'
import SpreeSdkMethodFromEndpointPathError from './errors/SpreeSdkMethodFromEndpointPathError' import SpreeSdkMethodFromEndpointPathError from './errors/SpreeSdkMethodFromEndpointPathError'
import type { SpreeSdkVariables } from './types' import type {
SpreeSdkResponse,
SpreeSdkResponseWithRawResponse,
SpreeSdkVariables,
} from './types'
import type { GraphQLFetcherResult } from '@commerce/api' 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({ const client = makeClient({
host: requireConfigValue('apiHost') as string, host: requireConfigValue('apiHost') as string,
@@ -27,7 +29,7 @@ const client = makeClient({
}) })
const fetcher: Fetcher< const fetcher: Fetcher<
GraphQLFetcherResult<JsonApiSingleResponse | JsonApiListResponse>, GraphQLFetcherResult<SpreeSdkResponse>,
SpreeSdkVariables SpreeSdkVariables
> = async (requestOptions) => { > = async (requestOptions) => {
const { url, method, variables, query } = requestOptions const { url, method, variables, query } = requestOptions
@@ -47,20 +49,19 @@ const fetcher: Fetcher<
) )
} }
const storeResponse: ResultResponse< const storeResponse: ResultResponse<SpreeSdkResponseWithRawResponse> =
JsonApiSingleResponse | JsonApiListResponse await getSpreeSdkMethodFromEndpointPath(
> = await getSpreeSdkMethodFromEndpointPath( client,
client, variables.methodPath
variables.methodPath )(...variables.arguments)
)(...variables.arguments)
if (storeResponse.isSuccess()) { if (storeResponse.isSuccess()) {
const data = storeResponse.success() const data = storeResponse.success()
const rawFetchRespone = Object.getPrototypeOf(data).response const rawFetchResponse = data[fetchResponseKey]
return { return {
data, data,
res: rawFetchRespone, res: rawFetchResponse,
} }
} }

View File

@@ -40,9 +40,9 @@ export const handler: SWRHook<SearchProductsHook> = {
const sort = input.sort ? { sort: nextToSpreeSortMap[input.sort] } : {} const sort = input.sort ? { sort: nextToSpreeSortMap[input.sort] } : {}
const { const { data: spreeSuccessResponse } = await fetch<
data: { data: spreeSuccessResponse }, GraphQLFetcherResult<IProducts>
} = await fetch<GraphQLFetcherResult<{ data: IProducts }>>({ >({
variables: { variables: {
methodPath: 'products.list', methodPath: 'products.list',
arguments: [ arguments: [

View File

@@ -1,9 +1,11 @@
import type { fetchResponseKey } from '@framework/utils/create-customized-fetch-fetcher'
import type { import type {
JsonApiDocument, JsonApiDocument,
JsonApiListResponse, JsonApiListResponse,
JsonApiSingleResponse, JsonApiSingleResponse,
} from '@spree/storefront-api-v2-sdk/types/interfaces/JsonApi' } from '@spree/storefront-api-v2-sdk/types/interfaces/JsonApi'
import type { ResultResponse } from '@spree/storefront-api-v2-sdk/types/interfaces/ResultResponse' import type { ResultResponse } from '@spree/storefront-api-v2-sdk/types/interfaces/ResultResponse'
import type { Response } from '@vercel/fetch'
export type UnknownObjectValues = Record<string, unknown> 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 ValueOf<T> = T[keyof T]
export type SpreeSdkResponse = JsonApiSingleResponse | JsonApiListResponse
export type SpreeSdkResponseWithRawResponse = SpreeSdkResponse & {
[fetchResponseKey]: Response
}
export type SpreeSdkMethodReturnType = Promise< export type SpreeSdkMethodReturnType = Promise<
ResultResponse<JsonApiSingleResponse | JsonApiListResponse> ResultResponse<SpreeSdkResponseWithRawResponse>
> >
export type SpreeSdkMethod = (...args: any[]) => SpreeSdkMethodReturnType export type SpreeSdkMethod = (...args: any[]) => SpreeSdkMethodReturnType

View File

@@ -2,6 +2,8 @@ import * as qs from 'qs'
import { errors } from '@spree/storefront-api-v2-sdk' import { errors } from '@spree/storefront-api-v2-sdk'
import type { CreateCustomizedFetchFetcher } from '@spree/storefront-api-v2-sdk/types/interfaces/CreateCustomizedFetchFetcher' import type { CreateCustomizedFetchFetcher } from '@spree/storefront-api-v2-sdk/types/interfaces/CreateCustomizedFetchFetcher'
export const fetchResponseKey = Symbol('fetch-response-key')
const createCustomizedFetchFetcher: CreateCustomizedFetchFetcher = ( const createCustomizedFetchFetcher: CreateCustomizedFetchFetcher = (
fetcherOptions fetcherOptions
) => { ) => {
@@ -65,11 +67,9 @@ const createCustomizedFetchFetcher: CreateCustomizedFetchFetcher = (
throw new FetchError(response, request, data) throw new FetchError(response, request, data)
} }
return { data[fetchResponseKey] = response
// 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. return { data }
data: Object.setPrototypeOf({ data }, { response }),
}
} catch (error) { } catch (error) {
if (error instanceof FetchError) { if (error instanceof FetchError) {
throw error throw error

View File

@@ -16,7 +16,12 @@ import type { RelationType } from '@spree/storefront-api-v2-sdk/types/interfaces
import createGetAbsoluteImageUrl from './create-get-absolute-image-url' import createGetAbsoluteImageUrl from './create-get-absolute-image-url'
import getMediaGallery from './get-media-gallery' import getMediaGallery from './get-media-gallery'
import { findIncluded, findIncludedOfType } from './find-json-api-documents' 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' import type { Image } from '@commerce/types/common'
const placeholderImage = requireConfigValue('lineItemPlaceholderImageUrl') as const placeholderImage = requireConfigValue('lineItemPlaceholderImageUrl') as
@@ -28,7 +33,7 @@ const isColorProductOption = (productOptionType: OptionTypeAttr) => {
} }
const normalizeVariant = ( const normalizeVariant = (
spreeSuccessResponse: JsonApiSingleResponse | JsonApiListResponse, spreeSuccessResponse: SpreeSdkResponse,
spreeVariant: VariantAttr spreeVariant: VariantAttr
): ProductVariant => { ): ProductVariant => {
const productIdentifier = spreeVariant.relationships.product const productIdentifier = spreeVariant.relationships.product
@@ -102,7 +107,7 @@ const normalizeVariant = (
} }
const normalizeLineItem = ( const normalizeLineItem = (
spreeSuccessResponse: JsonApiSingleResponse | JsonApiListResponse, spreeSuccessResponse: SpreeSdkResponse,
spreeLineItem: LineItemAttr spreeLineItem: LineItemAttr
): LineItem => { ): LineItem => {
const variantIdentifier = spreeLineItem.relationships.variant const variantIdentifier = spreeLineItem.relationships.variant
@@ -183,7 +188,7 @@ const normalizeLineItem = (
} }
const normalizeCart = ( const normalizeCart = (
spreeSuccessResponse: JsonApiSingleResponse | JsonApiListResponse, spreeSuccessResponse: SpreeSdkResponse,
spreeCart: OrderAttr spreeCart: OrderAttr
): Cart => { ): Cart => {
const lineItems = findIncludedOfType( const lineItems = findIncludedOfType(

View File

@@ -18,13 +18,14 @@ import getMediaGallery from './get-media-gallery'
import { findIncluded, findIncludedOfType } from './find-json-api-documents' import { findIncluded, findIncludedOfType } from './find-json-api-documents'
import getProductPath from './get-product-path' import getProductPath from './get-product-path'
import MissingPrimaryVariantError from '../errors/MissingPrimaryVariantError' import MissingPrimaryVariantError from '../errors/MissingPrimaryVariantError'
import type { SpreeSdkResponse } from '@framework/types'
const placeholderImage = requireConfigValue('productPlaceholderImageUrl') as const placeholderImage = requireConfigValue('productPlaceholderImageUrl') as
| string | string
| false | false
const normalizeProduct = ( const normalizeProduct = (
spreeSuccessResponse: JsonApiSingleResponse | JsonApiListResponse, spreeSuccessResponse: SpreeSdkResponse,
spreeProduct: ProductAttr spreeProduct: ProductAttr
): Product => { ): Product => {
const primaryVariantIdentifier = spreeProduct.relationships.primary_variant const primaryVariantIdentifier = spreeProduct.relationships.primary_variant