-
Notifications
You must be signed in to change notification settings - Fork 212
@W-20276592: Address autocompletion feature #3484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ec72cff
@W-20276592: Address autocompletion feature
dannyphan2000 0cc84e4
@W-20276592: clean up
dannyphan2000 9d9ebf6
update API key rules
dannyphan2000 2eece5d
quick fix
dannyphan2000 fea5418
Merge branch 'develop' into feature/address-autocompletion-MAIN
dannyphan2000 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages/commerce-sdk-react/src/hooks/ShopperConfigurations/cache.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| /* | ||
| * Copyright (c) 2023, Salesforce, Inc. | ||
| * All rights reserved. | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
| */ | ||
| import {CLIENT_KEYS} from '../../constant' | ||
| import {ApiClients, CacheUpdateMatrix} from '../types' | ||
|
|
||
| const CLIENT_KEY = CLIENT_KEYS.SHOPPER_CONFIGURATIONS | ||
| type Client = NonNullable<ApiClients[typeof CLIENT_KEY]> | ||
|
|
||
| // ShopperConfigurations API is primarily for reading configuration data | ||
| // No mutations are currently supported | ||
| export const cacheUpdateMatrix: CacheUpdateMatrix<Client> = {} |
15 changes: 15 additions & 0 deletions
15
packages/commerce-sdk-react/src/hooks/ShopperConfigurations/index.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| /* | ||
| * Copyright (c) 2023, Salesforce, Inc. | ||
| * All rights reserved. | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
| */ | ||
| import {useConfigurations} from './query' | ||
|
|
||
| describe('ShopperConfigurations', () => { | ||
| describe('useConfigurations', () => { | ||
| it('should be defined', () => { | ||
| expect(useConfigurations).toBeDefined() | ||
| }) | ||
| }) | ||
| }) |
7 changes: 7 additions & 0 deletions
7
packages/commerce-sdk-react/src/hooks/ShopperConfigurations/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| /* | ||
| * Copyright (c) 2023, Salesforce, Inc. | ||
| * All rights reserved. | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
| */ | ||
| export * from './query' |
89 changes: 89 additions & 0 deletions
89
packages/commerce-sdk-react/src/hooks/ShopperConfigurations/query.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| /* | ||
| * Copyright (c) 2023, Salesforce, Inc. | ||
| * All rights reserved. | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
| */ | ||
| import nock from 'nock' | ||
| import { | ||
| mockQueryEndpoint, | ||
| renderHookWithProviders, | ||
| waitAndExpectError, | ||
| waitAndExpectSuccess, | ||
| createQueryClient | ||
| } from '../../test-utils' | ||
|
|
||
| import {Argument} from '../types' | ||
| import * as queries from './query' | ||
|
|
||
| jest.mock('../../auth/index.ts', () => { | ||
| const {default: mockAuth} = jest.requireActual('../../auth/index.ts') | ||
| mockAuth.prototype.ready = jest.fn().mockResolvedValue({access_token: 'access_token'}) | ||
| return mockAuth | ||
| }) | ||
|
|
||
| type Queries = typeof queries | ||
| const configurationsEndpoint = '/organizations/' | ||
| // Not all endpoints use all parameters, but unused parameters are safely discarded | ||
| const OPTIONS: Argument<Queries[keyof Queries]> = { | ||
| parameters: {organizationId: 'f_ecom_zzrmy_orgf_001'} | ||
| } | ||
|
|
||
| // Mock data for configurations | ||
| const mockConfigurationsData = { | ||
| configurations: [ | ||
| { | ||
| id: 'gcp', | ||
| value: 'test-gcp-api-key' | ||
| }, | ||
| { | ||
| id: 'einstein', | ||
| value: 'test-einstein-api-key' | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| describe('Shopper Configurations query hooks', () => { | ||
| beforeEach(() => nock.cleanAll()) | ||
| afterEach(() => { | ||
| expect(nock.pendingMocks()).toHaveLength(0) | ||
| }) | ||
|
|
||
| test('`useConfigurations` has meta.displayName defined', async () => { | ||
| mockQueryEndpoint(configurationsEndpoint, mockConfigurationsData) | ||
| const queryClient = createQueryClient() | ||
| const {result} = renderHookWithProviders( | ||
| () => { | ||
| return queries.useConfigurations(OPTIONS) | ||
| }, | ||
| {queryClient} | ||
| ) | ||
| await waitAndExpectSuccess(() => result.current) | ||
| expect(queryClient.getQueryCache().getAll()[0].meta?.displayName).toBe('useConfigurations') | ||
| }) | ||
|
|
||
| test('`useConfigurations` returns data on success', async () => { | ||
| mockQueryEndpoint(configurationsEndpoint, mockConfigurationsData) | ||
| const {result} = renderHookWithProviders(() => { | ||
| return queries.useConfigurations(OPTIONS) | ||
| }) | ||
| await waitAndExpectSuccess(() => result.current) | ||
| expect(result.current.data).toEqual(mockConfigurationsData) | ||
| }) | ||
|
|
||
| test('`useConfigurations` returns error on error', async () => { | ||
| mockQueryEndpoint(configurationsEndpoint, {}, 400) | ||
| const {result} = renderHookWithProviders(() => { | ||
| return queries.useConfigurations(OPTIONS) | ||
| }) | ||
| await waitAndExpectError(() => result.current) | ||
| }) | ||
|
|
||
| test('`useConfigurations` handles 500 server error', async () => { | ||
| mockQueryEndpoint(configurationsEndpoint, {}, 500) | ||
| const {result} = renderHookWithProviders(() => { | ||
| return queries.useConfigurations(OPTIONS) | ||
| }) | ||
| await waitAndExpectError(() => result.current) | ||
| }) | ||
| }) |
64 changes: 64 additions & 0 deletions
64
packages/commerce-sdk-react/src/hooks/ShopperConfigurations/query.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * Copyright (c) 2023, Salesforce, Inc. | ||
| * All rights reserved. | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
| */ | ||
| import {UseQueryResult} from '@tanstack/react-query' | ||
| import {ShopperConfigurations} from 'commerce-sdk-isomorphic' | ||
| import {ApiClients, ApiQueryOptions, Argument, DataType, NullableParameters} from '../types' | ||
| import {useQuery} from '../useQuery' | ||
| import {mergeOptions, omitNullableParameters, pickValidParams} from '../utils' | ||
| import * as queryKeyHelpers from './queryKeyHelpers' | ||
| import {CLIENT_KEYS} from '../../constant' | ||
| import useCommerceApi from '../useCommerceApi' | ||
|
|
||
| const CLIENT_KEY = CLIENT_KEYS.SHOPPER_CONFIGURATIONS | ||
| type Client = NonNullable<ApiClients[typeof CLIENT_KEY]> | ||
|
|
||
| /** | ||
| * Gets configuration information that encompasses toggles, preferences, and configuration that allow the application to be reactive to changes performed by the merchant, admin, or support engineer. | ||
| * | ||
| * @group ShopperConfigurations | ||
| * @category Query | ||
| * @parameter apiOptions - Options to pass through to `commerce-sdk-isomorphic`, with `null` accepted for unset API parameters. | ||
| * @parameter queryOptions - TanStack Query query options, with `enabled` by default set to check that all required API parameters have been set. | ||
| * @returns A TanStack Query query hook with data from the Shopper Configurations `getConfigurations` endpoint. | ||
| * @see {@link https://developer.salesforce.com/docs/commerce/commerce-api/references/shopper-configurations?meta=getConfigurations| Salesforce Developer Center} for more information about the API endpoint. | ||
| * @see {@link https://salesforcecommercecloud.github.io/commerce-sdk-isomorphic/classes/shopperconfigurations.shopperconfigurations-1.html#getconfigurations | `commerce-sdk-isomorphic` documentation} for more information on the parameters and returned data type. | ||
| * @see {@link https://tanstack.com/query/latest/docs/react/reference/useQuery | TanStack Query `useQuery` reference} for more information about the return value. | ||
| */ | ||
| export const useConfigurations = ( | ||
| apiOptions: NullableParameters<Argument<Client['getConfigurations']>>, | ||
| queryOptions: ApiQueryOptions<Client['getConfigurations']> = {} | ||
| ): UseQueryResult<DataType<Client['getConfigurations']>> => { | ||
| type Options = Argument<Client['getConfigurations']> | ||
| type Data = DataType<Client['getConfigurations']> | ||
| const client = useCommerceApi(CLIENT_KEY) | ||
| const methodName = 'getConfigurations' | ||
| const requiredParameters = ShopperConfigurations.paramKeys[`${methodName}Required`] | ||
|
|
||
| // Parameters can be set in `apiOptions` or `client.clientConfig` | ||
| // we must merge them in order to generate the correct query key. | ||
| const netOptions = omitNullableParameters(mergeOptions(client, apiOptions || {})) | ||
| const parameters = pickValidParams( | ||
| netOptions.parameters, | ||
| ShopperConfigurations.paramKeys[methodName] | ||
| ) | ||
| const queryKey = queryKeyHelpers[methodName].queryKey(netOptions.parameters) | ||
| // We don't use `netOptions` here because we manipulate the options in `useQuery`. | ||
| const method = async (options: Options) => await client[methodName](options) | ||
|
|
||
| queryOptions.meta = { | ||
| displayName: 'useConfigurations', | ||
| ...queryOptions.meta | ||
| } | ||
|
|
||
| // For some reason, if we don't explicitly set these generic parameters, the inferred type for | ||
| // `Data` sometimes, but not always, includes `Response`, which is incorrect. I don't know why. | ||
| return useQuery<Client, Options, Data>({...netOptions, parameters}, queryOptions, { | ||
| method, | ||
| queryKey, | ||
| requiredParameters | ||
| }) | ||
| } |
46 changes: 46 additions & 0 deletions
46
packages/commerce-sdk-react/src/hooks/ShopperConfigurations/queryKeyHelpers.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * Copyright (c) 2023, Salesforce, Inc. | ||
| * All rights reserved. | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
| */ | ||
| import {ShopperConfigurations} from 'commerce-sdk-isomorphic' | ||
| import {Argument, ExcludeTail} from '../types' | ||
| import {pickValidParams} from '../utils' | ||
|
|
||
| // We must use a client with no parameters in order to have required/optional match the API spec | ||
| type Client = ShopperConfigurations<{shortCode: string}> | ||
| type Params<T extends keyof QueryKeys> = Partial<Argument<Client[T]>['parameters']> | ||
| export type QueryKeys = { | ||
| getConfigurations: [ | ||
| '/commerce-sdk-react', | ||
| '/organizations/', | ||
| string | undefined, | ||
| '/configurations', | ||
| Params<'getConfigurations'> | ||
| ] | ||
| } | ||
|
|
||
| // This is defined here, rather than `types.ts`, because it relies on `Client` and `QueryKeys`, | ||
| // and making those generic would add too much complexity. | ||
| type QueryKeyHelper<T extends keyof QueryKeys> = { | ||
| /** Generates the path component of the query key for an endpoint. */ | ||
| path: (params: Params<T>) => ExcludeTail<QueryKeys[T]> | ||
| /** Generates the full query key for an endpoint. */ | ||
| queryKey: (params: Params<T>) => QueryKeys[T] | ||
| } | ||
|
|
||
| export const getConfigurations: QueryKeyHelper<'getConfigurations'> = { | ||
| path: (params) => [ | ||
| '/commerce-sdk-react', | ||
| '/organizations/', | ||
| params?.organizationId, | ||
| '/configurations' | ||
| ], | ||
| queryKey: (params: Params<'getConfigurations'>) => { | ||
| return [ | ||
| ...getConfigurations.path(params), | ||
| pickValidParams(params || {}, ShopperConfigurations.paramKeys.getConfigurations) | ||
| ] | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the reason for the changes to auth.index.ts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sf-mkosak mentioned that this was needed with the new SCAPI changes in updating to the latest
commerce-sdk-isomorphicversion