From f327c694eac99770ff8a8f723c077c052090cb06 Mon Sep 17 00:00:00 2001 From: Giuseppe Ciotola <30926550+gciotola@users.noreply.github.com> Date: Mon, 30 Jun 2025 17:42:13 +0200 Subject: [PATCH 1/2] fix: ignore api requests for mocked ids in `useCoreApi` --- .../providers/CoreSdkProvider/useCoreApi.tsx | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/app-elements/src/providers/CoreSdkProvider/useCoreApi.tsx b/packages/app-elements/src/providers/CoreSdkProvider/useCoreApi.tsx index 81139ca68..3d259502c 100644 --- a/packages/app-elements/src/providers/CoreSdkProvider/useCoreApi.tsx +++ b/packages/app-elements/src/providers/CoreSdkProvider/useCoreApi.tsx @@ -1,6 +1,8 @@ +import { isMockedId } from '#helpers/mocks' import { type ResourceEndpoint } from '#helpers/resources' import { useTokenProvider } from '#providers/TokenProvider' import type { CommerceLayerClient } from '@commercelayer/sdk' +import { isString } from 'lodash-es' import { useCallback } from 'react' import useSWR, { type Fetcher, @@ -69,9 +71,25 @@ export function useCoreApi< [sdkClient] ) + const isMock = action === 'retrieve' && isArgsForMockedId(args) + return useSWR( - args !== null ? [resource, action, args, mode] : null, + args !== null && !isMock ? [resource, action, args, mode] : null, fetcher, config ?? {} ) } + +/** + * Check if the useCoreApi args contain a request for a mocked resource id. + * This is used to determine if the request should be ignored (when is for a mocked id). + */ +function isArgsForMockedId(args: any): boolean { + return ( + args != null && + Array.isArray(args) && + args.length > 0 && + isString(args[0]) && + isMockedId(args[0]) + ) +} From 30ac71c4d527a315c26e258296ef2f3f38325a41 Mon Sep 17 00:00:00 2001 From: Giuseppe Ciotola <30926550+gciotola@users.noreply.github.com> Date: Wed, 2 Jul 2025 09:11:21 +0200 Subject: [PATCH 2/2] fix: remove check for action retrieve when identifying mocked id --- .../src/providers/CoreSdkProvider/useCoreApi.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/app-elements/src/providers/CoreSdkProvider/useCoreApi.tsx b/packages/app-elements/src/providers/CoreSdkProvider/useCoreApi.tsx index 3d259502c..903a32771 100644 --- a/packages/app-elements/src/providers/CoreSdkProvider/useCoreApi.tsx +++ b/packages/app-elements/src/providers/CoreSdkProvider/useCoreApi.tsx @@ -71,10 +71,10 @@ export function useCoreApi< [sdkClient] ) - const isMock = action === 'retrieve' && isArgsForMockedId(args) - return useSWR( - args !== null && !isMock ? [resource, action, args, mode] : null, + args == null || isArgsForMockedId(args) + ? null + : [resource, action, args, mode], fetcher, config ?? {} )