-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathmutation.ts
More file actions
149 lines (144 loc) · 7.48 KB
/
mutation.ts
File metadata and controls
149 lines (144 loc) · 7.48 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* 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 {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_CUSTOMERS
type Client = NonNullable<ApiClients[typeof CLIENT_KEY]>
/**
* Mutations available for Shopper Customers.
* @group ShopperCustomers
* @category Mutation
* @enum
*/
export const ShopperCustomersMutations = {
/**
* Registers a new customer. The mandatory data are the credentials, profile last name, and email. This requires a JSON Web Token (JWT) which needs to be obtained using the POST /customers/auth API with type \"guest\".
* @returns A TanStack Query mutation hook for interacting with the Shopper Customers `registerCustomer` endpoint.
*/
RegisterCustomer: 'registerCustomer',
/**
* Reset customer password, after obtaining a reset token. This is the second step in the reset customer password flow, where a customer password is reset by providing the new credentials along with a reset token. This call should be preceded by a call to the /create-reset-token endpoint.
* @returns A TanStack Query mutation hook for interacting with the Shopper Customers `resetPassword` endpoint.
*/
ResetPassword: 'resetPassword',
/**
* Get reset password token. This is the first step in the reset customer password flow, where a password reset token is requested for future use to reset a customer password. This call should be followed by a call to the /reset endpoint.
* @returns A TanStack Query mutation hook for interacting with the Shopper Customers `getResetPasswordToken` endpoint.
*/
GetResetPasswordToken: 'getResetPasswordToken',
// TODO: Re-implement (and update description from RAML spec) when the endpoint exits closed beta.
// /**
// * Registers a new external profile for a customer. This endpoint is in closed beta, available to select few customers. Please get in touch with your Account Team if you'd like to participate in the beta program
// * @returns A TanStack Query mutation hook for interacting with the Shopper Customers `registerExternalProfile` endpoint.
// */
// RegisterExternalProfile: 'registerExternalProfile',
/**
* Updates a customer.
* @returns A TanStack Query mutation hook for interacting with the Shopper Customers `updateCustomer` endpoint.
*/
UpdateCustomer: 'updateCustomer',
/**
* Creates a new address with the given name for the given customer.
* @returns A TanStack Query mutation hook for interacting with the Shopper Customers `createCustomerAddress` endpoint.
*/
CreateCustomerAddress: 'createCustomerAddress',
/**
* Deletes a customer's address by address name.
* @returns A TanStack Query mutation hook for interacting with the Shopper Customers `removeCustomerAddress` endpoint.
*/
RemoveCustomerAddress: 'removeCustomerAddress',
/**
* Updates a customer's address by address name.
* @returns A TanStack Query mutation hook for interacting with the Shopper Customers `updateCustomerAddress` endpoint.
*/
UpdateCustomerAddress: 'updateCustomerAddress',
/**
* Updates the customer's password.
* @returns A TanStack Query mutation hook for interacting with the Shopper Customers `updateCustomerPassword` endpoint.
*/
UpdateCustomerPassword: 'updateCustomerPassword',
/**
* Adds a payment instrument to the customer information.
* @returns A TanStack Query mutation hook for interacting with the Shopper Customers `createCustomerPaymentInstrument` endpoint.
*/
CreateCustomerPaymentInstrument: 'createCustomerPaymentInstrument',
/**
* Updates a customer's payment instrument.
* @returns A TanStack Query mutation hook for interacting with the Shopper Customers `updateCustomerPaymentInstrument` endpoint.
*/
UpdateCustomerPaymentInstrument: 'updateCustomerPaymentInstrument',
/**
* Deletes a customer's payment instrument.
* @returns A TanStack Query mutation hook for interacting with the Shopper Customers `deleteCustomerPaymentInstrument` endpoint.
*/
DeleteCustomerPaymentInstrument: 'deleteCustomerPaymentInstrument',
/**
* Creates a customer product list.
* @returns A TanStack Query mutation hook for interacting with the Shopper Customers `createCustomerProductList` endpoint.
*/
CreateCustomerProductList: 'createCustomerProductList',
/**
* Deletes a customer product list.
* @returns A TanStack Query mutation hook for interacting with the Shopper Customers `deleteCustomerProductList` endpoint.
*/
DeleteCustomerProductList: 'deleteCustomerProductList',
/**
* Changes a product list. Changeable properties are the name, description, and if the list is public.
* @returns A TanStack Query mutation hook for interacting with the Shopper Customers `updateCustomerProductList` endpoint.
*/
UpdateCustomerProductList: 'updateCustomerProductList',
/**
* @returns A TanStack Query mutation hook for interacting with the Shopper Customers `createCustomerProductListItem` endpoint.
*/
CreateCustomerProductListItem: 'createCustomerProductListItem',
/**
* Removes an item from a customer product list.
* @returns A TanStack Query mutation hook for interacting with the Shopper Customers `deleteCustomerProductListItem` endpoint.
*/
DeleteCustomerProductListItem: 'deleteCustomerProductListItem',
/**
* Updates an item of a customer's product list.
*/
UpdateCustomerProductListItem: 'updateCustomerProductListItem'
} as const
/**
* Mutation for Shopper Customers.
* @group ShopperCustomers
* @category Mutation
*/
export type ShopperCustomersMutation =
(typeof ShopperCustomersMutations)[keyof typeof ShopperCustomersMutations]
/**
* Mutation hook for Shopper Customers.
* @group ShopperCustomers
* @category Mutation
*/
export function useShopperCustomersMutation<Mutation extends ShopperCustomersMutation>(
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 CacheUpdateGetter<MergedOptions<Client, Options>, Data>
})
}