-
Notifications
You must be signed in to change notification settings - Fork 217
Expand file tree
/
Copy pathmutation.ts
More file actions
66 lines (61 loc) · 2.67 KB
/
mutation.ts
File metadata and controls
66 lines (61 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* Copyright (c) 2025, 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 {ApiClients, ApiMethod, Argument, CacheUpdateGetter, DataType, MergedOptions} from '../types'
import {useMutation} from '../useMutation'
import {UseMutationResult} from '@tanstack/react-query'
import {NotImplementedError} from '../utils'
import {cacheUpdateMatrix} from './cache'
import {CLIENT_KEYS} from '../../constant'
import useCommerceApi from '../useCommerceApi'
const CLIENT_KEY = CLIENT_KEYS.SHOPPER_AGENTS
type Client = NonNullable<ApiClients[typeof CLIENT_KEY]>
/**
* Mutations available for Shopper Agents
* @group ShopperAgents
* @category Mutation
* @enum
*/
export const ShopperAgentsMutations = {
/**
* Initializes an Agentforce session. The request body must include the sessionInitKey field.
* Missing or invalid sessionInitKey information results in INVALID_REQUEST_PARAMETERS (400).
* @returns A TanStack Query mutation hook for interacting with the Shopper Agents `postSessionInit` endpoint.
*/
PostSessionInit: 'postSessionInit'
} as const
/**
* Mutation for Shopper Agents.
* @group ShopperAgents
* @category Mutation
*/
export type ShopperAgentsMutation =
(typeof ShopperAgentsMutations)[keyof typeof ShopperAgentsMutations]
/**
* Mutation hook for Shopper Agents.
* @group ShopperAgents
* @category Mutation
*/
export function useShopperAgentsMutation<Mutation extends ShopperAgentsMutation>(
mutation: Mutation
): UseMutationResult<DataType<Client[Mutation]>, unknown, Argument<Client[Mutation]>> {
const getCacheUpdates = cacheUpdateMatrix[mutation]
// TODO: Remove this check when all mutations are implemented.
if (!getCacheUpdates) throw new NotImplementedError(`The '${mutation}' mutation`)
// The `Options` and `Data` types for each mutation are similar, but distinct, and the union
// type generated from `Client[Mutation]` seems to be too complex for TypeScript to handle.
// I'm not sure if there's a way to avoid the type assertions in here for the methods that
// use them. However, I'm fairly confident that they are safe to do, as they seem to be simply
// re-asserting what we already have.
const client = useCommerceApi(CLIENT_KEY)
type Options = Argument<Client[Mutation]>
type Data = DataType<Client[Mutation]>
return useMutation({
client,
method: (opts: Options) => (client[mutation] as ApiMethod<Options, Data>)(opts),
getCacheUpdates: getCacheUpdates as unknown as CacheUpdateGetter<MergedOptions<Client, Options>, Data>
})
}