prezense 0.0.0-stage → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/accounts.js ADDED
@@ -0,0 +1,111 @@
1
+ import { operation, text } from './graphql.js';
2
+
3
+ // Every value goes in as a GraphQL variable, never spliced into the query
4
+ // text — emails and codes are typed by the shopper.
5
+
6
+ export class Accounts {
7
+ constructor(client) {
8
+ this.client = client;
9
+ }
10
+
11
+ async user({ email, data }) {
12
+ const args = { email: ['String!', text(email)] };
13
+ return this.client.sendRequest(operation('query', 'retrieveAccount', args, data), 'retrieveAccount');
14
+ }
15
+
16
+ async addUserInformation({ email, fingerprint, userInformation, data }) {
17
+ const query = `
18
+ mutation AddUserInformation($email: String!, $fingerprint: String!, $userInformation: UserInformationInput!) {
19
+ addUserInformation(email: $email, fingerprint: $fingerprint, userInformation: $userInformation) {
20
+ ${data.trim()}
21
+ }
22
+ }
23
+ `;
24
+ const variables = {
25
+ email,
26
+ fingerprint,
27
+ userInformation, // Pass as JS object with snake_case keys
28
+ };
29
+ return this.client.sendRequest({ query, variables }, 'addUserInformation');
30
+ }
31
+
32
+ async updateUserInformation({ email, fingerprint, userInformation, data }) {
33
+ const query = `
34
+ mutation UpdateUserInformation($email: String!, $fingerprint: String!, $userInformation: UserInformationInput!) {
35
+ updateUserInformation(email: $email, fingerprint: $fingerprint, userInformation: $userInformation) {
36
+ ${data.trim()}
37
+ }
38
+ }
39
+ `;
40
+ const variables = {
41
+ email,
42
+ fingerprint,
43
+ userInformation // This should be a JS object with the correct schema fields
44
+ };
45
+ return this.client.sendRequest({ query, variables }, 'updateUserInformation');
46
+ }
47
+
48
+ async addProductToWishlist({ email, productID, data }) {
49
+ const args = { productID: ['String!', text(productID)], email: ['String!', text(email)] };
50
+ return this.client.sendRequest(operation('mutation', 'addProductToWishlist', args, data), 'addProductToWishlist');
51
+ }
52
+
53
+ async getWishlist({ email, data }) {
54
+ const args = { email: ['String!', text(email)] };
55
+ return this.client.sendRequest(operation('query', 'getUserWishlist', args, data), 'getUserWishlist');
56
+ }
57
+
58
+ async removeProductFromWishlist({ email, productID, data }) {
59
+ const args = { productID: ['String!', text(productID)], email: ['String!', text(email)] };
60
+ return this.client.sendRequest(operation('mutation', 'deleteProductFromWishlist', args, data), 'deleteProductFromWishlist');
61
+ }
62
+
63
+ async sendAuthCode({ email }) {
64
+ // sendAuthCode returns a String, not an object, so no field selection needed
65
+ const args = { email: ['String!', text(email)] };
66
+ return this.client.sendRequest(operation('mutation', 'sendAuthCode', args), 'sendAuthCode');
67
+ }
68
+
69
+ async resendAuthCode({ email }) {
70
+ // resendAuthCode returns a String, not an object, so no field selection needed
71
+ const args = { email: ['String!', text(email)] };
72
+ return this.client.sendRequest(operation('mutation', 'resendAuthCode', args), 'resendAuthCode');
73
+ }
74
+
75
+ async verifyAuthCode({ email, code, cartID }) {
76
+ const args = {
77
+ email: ['String!', text(email)],
78
+ code: ['String!', text(code)],
79
+ cartID: ['String', text(cartID || undefined)],
80
+ };
81
+
82
+ // Pass the shopper's current (guest) cartID so the server can merge it
83
+ // into their saved cart, or give it to them. If response.cartID comes
84
+ // back, the storefront saves it as the shopper's cart from now on
85
+ // (guides/cart-persistence) — the SDK keeps no cart state itself.
86
+ return this.client.sendRequest(operation('mutation', 'verifyAuthCode', args, 'message cartID'), 'verifyAuthCode');
87
+ }
88
+
89
+ async signOutUser({ email, fingerprint }) {
90
+ // signOutUser returns a String, not an object, so no field selection needed
91
+ const args = { fingerprint: ['String!', text(fingerprint)], email: ['String!', text(email)] };
92
+ return this.client.sendRequest(operation('mutation', 'signOutUser', args), 'signOutUser');
93
+ }
94
+
95
+ async checkIfUserIsSignedIn({ email, fingerprint }) {
96
+ const args = { fingerprint: ['String!', text(fingerprint)], email: ['String!', text(email)] };
97
+ return this.client.sendRequest(operation('query', 'checkIfUserIsSignedIn', args), 'checkIfUserIsSignedIn');
98
+ }
99
+
100
+ async addEmailToList({ email }) {
101
+ // addEmailToList returns a String, not an object, so no field selection needed
102
+ const args = { email: ['String!', text(email)] };
103
+ return this.client.sendRequest(operation('mutation', 'addEmailToList', args), 'addEmailToList');
104
+ }
105
+
106
+ async removeEmailFromList({ unsubscribeToken }) {
107
+ // removeEmailFromList returns a String, not an object, so no field selection needed
108
+ const args = { unsubscribeToken: ['String!', text(unsubscribeToken)] };
109
+ return this.client.sendRequest(operation('mutation', 'removeEmailFromList', args), 'removeEmailFromList');
110
+ }
111
+ }
package/alerts.js ADDED
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Product alerts — a shopper asks to be emailed when a product is back in
3
+ * stock, or when its price drops. Server-side only, like every SDK call.
4
+ *
5
+ * The store's Smart Notifications send the emails: back in stock when a sold
6
+ * out variant gets stock again, price drop when a variant's price falls to
7
+ * the shopper's target (or at all, with no target). Each only sends once the
8
+ * merchant has switched that trigger on.
9
+ *
10
+ * One active alert per shopper, product, variant and type: signing up again
11
+ * returns the existing alert (`created: false`) rather than adding another,
12
+ * and for a price drop saves the new target price.
13
+ *
14
+ * An alert emails once. After its email is sent it's done; signing up again
15
+ * then creates a new one.
16
+ */
17
+ export class Alerts {
18
+ constructor(client) {
19
+ this.client = client;
20
+ }
21
+
22
+ /** Tracking-server replies carry { error, code } on failure; surface them like GraphQL errors. */
23
+ async #send(endpoint, body) {
24
+ const { error, response } = await this.client.trackEvent(endpoint, body);
25
+ if (error) return { error, response: null };
26
+ if (response?.error) {
27
+ const failure = new Error(response.error);
28
+ failure.code = response.code ?? null;
29
+ return { error: failure, response: null };
30
+ }
31
+ return { error: null, response };
32
+ }
33
+
34
+ /**
35
+ * Email the shopper when this product (or this one variant) is back in stock.
36
+ * Leave variantID out to hear about any variant of the product.
37
+ */
38
+ async backInStock({ email, productID, variantID, fingerprint }) {
39
+ return this.#send('subscribe_notification', {
40
+ type: 'back_in_stock',
41
+ email,
42
+ productID,
43
+ variantID: variantID || null,
44
+ prezanseFP: fingerprint || null,
45
+ });
46
+ }
47
+
48
+ /**
49
+ * Email the shopper when this product's (or variant's) price drops.
50
+ * With targetPrice, only once it falls to that price or below; without it,
51
+ * on any drop. targetPrice is in the store's base currency.
52
+ */
53
+ async priceDrop({ email, productID, variantID, targetPrice, fingerprint }) {
54
+ return this.#send('subscribe_notification', {
55
+ type: 'price_drop',
56
+ email,
57
+ productID,
58
+ variantID: variantID || null,
59
+ targetPrice: targetPrice ?? null,
60
+ prezanseFP: fingerprint || null,
61
+ });
62
+ }
63
+
64
+ /**
65
+ * Cancel one of the shopper's alerts. type is 'back_in_stock' or
66
+ * 'price_drop'; productID and variantID must match how it was created.
67
+ * Resolves to { cancelled } — 0 when there was no such alert.
68
+ */
69
+ async cancel({ type, email, productID, variantID }) {
70
+ return this.#send('unsubscribe_notification', {
71
+ type,
72
+ email,
73
+ productID,
74
+ variantID: variantID || null,
75
+ });
76
+ }
77
+ }
package/analytics.js ADDED
@@ -0,0 +1,241 @@
1
+ import { UAParser } from 'ua-parser-js';
2
+
3
+ /**
4
+ * Analytics — server-side only.
5
+ *
6
+ * The SDK holds secret keys, so it runs on the storefront's SERVER, never in
7
+ * the shopper's browser. It can't read the browser, so everything that
8
+ * describes the shopper comes from the storefront:
9
+ *
10
+ * - IP address and User-Agent: the `shopper` option on the client
11
+ * (`new Prezense({ shopper: { ip, userAgent } })`), read from the
12
+ * incoming request. Device type is worked out from the User-Agent.
13
+ * - cartID: the shopper's cart, from the storefront's own cart cookie.
14
+ * - fingerprint: a long-lived random ID the storefront keeps in its own
15
+ * cookie to recognise a returning browser.
16
+ * - pageURL (page views): the full URL of the page, for UTM and affiliate
17
+ * tracking.
18
+ *
19
+ * See the docs guide guides/shopper-context.
20
+ */
21
+ export class Analytics {
22
+ constructor(client) {
23
+ this.client = client;
24
+ }
25
+
26
+ #getUTMParams(url) {
27
+ try {
28
+ const utmParams = {};
29
+ const searchParams = new URL(url).searchParams;
30
+ for (const field of ['source', 'medium', 'campaign', 'content', 'term']) {
31
+ const value = searchParams.get(`utm_${field}`);
32
+ if (value) utmParams[field] = value;
33
+ }
34
+ return Object.keys(utmParams).length > 0 ? utmParams : null;
35
+ } catch {
36
+ return null;
37
+ }
38
+ }
39
+
40
+ #getAffiliateID(url) {
41
+ try {
42
+ return new URL(url).searchParams.get('affiliate') || null;
43
+ } catch {
44
+ return null;
45
+ }
46
+ }
47
+
48
+ /** A friendly device label from the shopper's User-Agent. */
49
+ #getDeviceType() {
50
+ const userAgent = this.client.shopper?.userAgent;
51
+ if (!userAgent) return 'Unknown';
52
+ try {
53
+ const { device, os } = new UAParser(userAgent).getResult();
54
+ const model = (device.model || '').toLowerCase();
55
+ const type = device.type; // e.g. 'mobile', 'tablet', 'console', 'smarttv'
56
+ const vendor = (device.vendor || '').toLowerCase();
57
+ const osName = (os.name || '').toLowerCase();
58
+
59
+ // Apple devices
60
+ if (vendor === 'apple') {
61
+ if (model.includes('iphone')) return 'iPhone';
62
+ if (model.includes('ipad')) return 'iPad';
63
+ if (model.includes('apple tv')) return 'Apple TV';
64
+ if (osName.includes('mac os')) return 'Mac';
65
+ }
66
+
67
+ // Android devices
68
+ if (osName.includes('android')) {
69
+ if (type === 'mobile') return 'Android Phone';
70
+ if (type === 'tablet') return 'Android Tablet';
71
+ }
72
+
73
+ // Smart TVs
74
+ if (type === 'smarttv') {
75
+ if (vendor.includes('samsung')) return 'Samsung TV';
76
+ if (vendor.includes('sony')) return 'Sony TV';
77
+ if (vendor.includes('lg')) return 'LG TV';
78
+ if (vendor.includes('apple')) return 'Apple TV';
79
+ return 'Other';
80
+ }
81
+
82
+ // Consoles
83
+ if (type === 'console') {
84
+ if (vendor.includes('playstation') || model.includes('playstation')) return 'Playstation';
85
+ if (vendor.includes('xbox') || model.includes('xbox')) return 'Xbox';
86
+ return 'Other';
87
+ }
88
+
89
+ // Desktops and laptops (type undefined or 'desktop')
90
+ if (!type || type === 'desktop') {
91
+ if (osName.includes('windows')) return 'Windows PC';
92
+ if (osName.includes('mac')) return 'Mac';
93
+ if (osName.includes('linux')) return 'Linux PC';
94
+ if (osName.includes('chrome os')) return 'Chromebook';
95
+ return 'Other Desktop';
96
+ }
97
+
98
+ if (type === 'mobile') return 'Other Mobile';
99
+ if (type === 'tablet') return 'Other Tablet';
100
+ return 'Other';
101
+ } catch {
102
+ return 'Unknown';
103
+ }
104
+ }
105
+
106
+ /** The fields every shopper event carries. */
107
+ #shopperFields({ cartID, fingerprint }) {
108
+ return {
109
+ prezanseFP: fingerprint || null,
110
+ ipAddress: this.client.shopper?.ip || null,
111
+ deviceType: this.#getDeviceType(),
112
+ cartID: cartID || null,
113
+ };
114
+ }
115
+
116
+ async trackPageView({ cartID, fingerprint, pageURL, pageName, productID, featureSource }) {
117
+ const body = {
118
+ pageName,
119
+ ...this.#shopperFields({ cartID, fingerprint }),
120
+ UTMParams: pageURL ? this.#getUTMParams(pageURL) : null,
121
+ affiliateName: pageURL ? this.#getAffiliateID(pageURL) : null,
122
+ };
123
+ if (productID) body.productID = productID;
124
+ if (featureSource) body.featureSource = featureSource;
125
+ return this.client.trackEvent('pageview', body);
126
+ }
127
+
128
+ async trackSignIn({ cartID, fingerprint } = {}) {
129
+ return this.client.trackEvent('signin', this.#shopperFields({ cartID, fingerprint }));
130
+ }
131
+
132
+ async trackSignOut({ cartID, fingerprint } = {}) {
133
+ return this.client.trackEvent('signout', this.#shopperFields({ cartID, fingerprint }));
134
+ }
135
+
136
+ async trackLoyaltyInteraction({ cartID, fingerprint, interactionType }) {
137
+ // Valid interactionType: 'viewed_rewards' | 'applied_redemption' | 'removed_redemption'
138
+ return this.client.trackEvent('loyaltyinteraction', {
139
+ interactionType,
140
+ ...this.#shopperFields({ cartID, fingerprint }),
141
+ });
142
+ }
143
+
144
+ async trackAddedToCart({ cartID, fingerprint, productID, featureSource }) {
145
+ const body = { ...this.#shopperFields({ cartID, fingerprint }), productID };
146
+ if (featureSource) body.featureSource = featureSource;
147
+ return this.client.trackEvent('addtocart', body);
148
+ }
149
+
150
+ async trackRemovedFromCart({ cartID, fingerprint, productID, featureSource }) {
151
+ const body = { ...this.#shopperFields({ cartID, fingerprint }), productID };
152
+ if (featureSource) body.featureSource = featureSource;
153
+ return this.client.trackEvent('removedfromcart', body);
154
+ }
155
+
156
+ async trackAddedToWishlist({ cartID, fingerprint, email, productID, featureSource }) {
157
+ const body = { ...this.#shopperFields({ cartID, fingerprint }), email, productID };
158
+ if (featureSource) body.featureSource = featureSource;
159
+ return this.client.trackEvent('addtowishlist', body);
160
+ }
161
+
162
+ async trackRemovedFromWishlist({ cartID, fingerprint, email, productID, featureSource }) {
163
+ const body = { ...this.#shopperFields({ cartID, fingerprint }), email, productID };
164
+ if (featureSource) body.featureSource = featureSource;
165
+ return this.client.trackEvent('removedfromwishlist', body);
166
+ }
167
+
168
+ async trackSearch({ cartID, fingerprint } = {}) {
169
+ return this.client.trackEvent('search', this.#shopperFields({ cartID, fingerprint }));
170
+ }
171
+
172
+ async trackInitiatedCheckout({ cartID, fingerprint } = {}) {
173
+ return this.client.trackEvent('initiatedcheckout', this.#shopperFields({ cartID, fingerprint }));
174
+ }
175
+
176
+ /**
177
+ * Optional. The purchase itself is recorded automatically when the order
178
+ * is created (createOrder), with its revenue and products; this call only
179
+ * counts as shopper activity, so calling it never double-counts a sale.
180
+ */
181
+ async trackCompletedCheckout({ cartID, fingerprint, featureSource } = {}) {
182
+ const body = this.#shopperFields({ cartID, fingerprint });
183
+ if (featureSource) body.featureSource = featureSource;
184
+ return this.client.trackEvent('completedcheckout', body);
185
+ }
186
+
187
+ async trackAccountInteraction({ cartID, fingerprint, email }) {
188
+ return this.client.trackEvent('useraccountinteraction', {
189
+ ...this.#shopperFields({ cartID, fingerprint }),
190
+ email,
191
+ });
192
+ }
193
+
194
+ async trackSiteInteraction({ cartID, fingerprint } = {}) {
195
+ return this.client.trackEvent('siteinteraction', this.#shopperFields({ cartID, fingerprint }));
196
+ }
197
+
198
+ // ================================================================
199
+ // EXCHANGE MANAGEMENT FUNNEL
200
+ // ================================================================
201
+
202
+ async trackExchangeSuggested({ returnID, reasonCategory, suggestionSource, suggestionCount, email }) {
203
+ const body = {
204
+ returnID,
205
+ reasonCategory,
206
+ suggestionSource,
207
+ suggestionCount,
208
+ email,
209
+ };
210
+ return this.client.trackEvent('exchange_suggested', body);
211
+ }
212
+
213
+ async trackExchangeAccepted({ returnID, exchangeID, suggestedVariantID, reasonCategory, suggestionSource, email }) {
214
+ const body = {
215
+ returnID,
216
+ exchangeID,
217
+ suggestedVariantID,
218
+ reasonCategory,
219
+ suggestionSource,
220
+ email,
221
+ };
222
+ return this.client.trackEvent('exchange_accepted', body);
223
+ }
224
+
225
+ async trackExchangeDeclined({ returnID, reasonCategory, suggestionSource, email }) {
226
+ const body = { returnID, reasonCategory, suggestionSource, email };
227
+ return this.client.trackEvent('exchange_declined', body);
228
+ }
229
+
230
+ async trackExchangeCompleted({ returnID, exchangeID, reasonCategory, suggestionSource, retainedRevenue, email }) {
231
+ const body = {
232
+ returnID,
233
+ exchangeID,
234
+ reasonCategory,
235
+ suggestionSource,
236
+ retainedRevenue: retainedRevenue != null ? String(retainedRevenue) : undefined,
237
+ email,
238
+ };
239
+ return this.client.trackEvent('exchange_completed', body);
240
+ }
241
+ }
package/bundles.js ADDED
@@ -0,0 +1,14 @@
1
+ export class Bundles {
2
+ constructor(client) {
3
+ this.client = client;
4
+ }
5
+
6
+ async purchaseBundles({ productId, data, limit, minConfidence }) {
7
+ // Values go in as GraphQL variables, never spliced into the query text.
8
+ const query = `query ($productId: String!, $limit: Int, $minConfidence: Float) {
9
+ getProductPurchaseBundles(productId: $productId, limit: $limit, minConfidence: $minConfidence) { ${data.trim()} }
10
+ }`;
11
+ const variables = { productId, limit, minConfidence };
12
+ return this.client.sendRequest({ query, variables }, 'getProductPurchaseBundles');
13
+ }
14
+ }
package/cart.js ADDED
@@ -0,0 +1,214 @@
1
+ /**
2
+ * Cart — server-side only.
3
+ *
4
+ * The SDK holds secret keys, so it runs on the storefront's SERVER, never in
5
+ * the shopper's browser. It therefore keeps no cart state of its own: there
6
+ * is no browser cookie to read. The storefront remembers the shopper's cart
7
+ * ID (its own httpOnly cookie) and passes it as `cartID` to every call that
8
+ * acts on a cart. A call without one returns an error rather than quietly
9
+ * starting a new, empty cart.
10
+ *
11
+ * Login/logout, merging and recency are handled by the server — see the
12
+ * docs guide guides/cart-persistence.
13
+ */
14
+
15
+ import { operation, text, number, textInput } from './graphql.js';
16
+
17
+ function missingCartID() {
18
+ return {
19
+ error: new Error('cartID is required — pass the shopper\'s cart ID (see guides/cart-persistence).'),
20
+ response: null,
21
+ };
22
+ }
23
+
24
+ export class Cart {
25
+ constructor(client) {
26
+ this.client = client;
27
+ }
28
+
29
+ /**
30
+ * Forward fraud signals onto a cart: device fingerprint, session id and
31
+ * device type. The SDK runs on the server, so it can't read these from
32
+ * the browser — the storefront passes whatever it has. Safe to call
33
+ * repeatedly; only values sent overwrite existing ones.
34
+ */
35
+ async updateCartContext({ cartID, deviceFingerprint, sessionID, deviceType } = {}) {
36
+ if (!cartID) return missingCartID();
37
+
38
+ // Values come from the shopper's browser, so they go as GraphQL
39
+ // variables, never spliced into the query text. Unset ones are left
40
+ // out (null) so they don't overwrite what the cart already has.
41
+ const query = `mutation ($cartID: String!, $deviceFingerprint: String, $sessionID: String, $deviceType: String) {
42
+ updateCartContext(cartID: $cartID, deviceFingerprint: $deviceFingerprint, sessionID: $sessionID, deviceType: $deviceType)
43
+ }`;
44
+ const variables = {
45
+ cartID,
46
+ deviceFingerprint: deviceFingerprint || null,
47
+ sessionID: sessionID || null,
48
+ deviceType: deviceType || null,
49
+ };
50
+ return this.client.sendRequest({ query, variables }, 'updateCartContext');
51
+ }
52
+
53
+ #toCamelCase(str) {
54
+ return str.replace(/_([a-z0-9])/g, (_, char) => char.toUpperCase());
55
+ }
56
+
57
+ /**
58
+ * @deprecated The SDK no longer builds query text from values (see
59
+ * graphql.js); kept only so existing callers of this method still work.
60
+ */
61
+ toGraphQLInput(obj) {
62
+ return Object.entries(obj)
63
+ .map(([rawKey, value]) => {
64
+ const key = this.#toCamelCase(rawKey);
65
+ if (typeof value === 'string') {
66
+ return `${key}: "${value}"`;
67
+ } else if (typeof value === 'number' || typeof value === 'boolean') {
68
+ return `${key}: ${value}`;
69
+ } else if (Array.isArray(value)) {
70
+ // Recursively handle arrays
71
+ return `${key}: [${value.map(v => (typeof v === 'string' ? `"${v}"` : v)).join(', ')}]`;
72
+ } else if (typeof value === 'object' && value !== null) {
73
+ // Recursively handle nested objects
74
+ return `${key}: { ${this.toGraphQLInput(value)} }`;
75
+ } else {
76
+ return '';
77
+ }
78
+ })
79
+ .join(', ');
80
+ }
81
+
82
+ // Every value below goes in as a GraphQL variable (graphql.js), never
83
+ // spliced into the query text — names, addresses and codes are typed by
84
+ // the shopper.
85
+
86
+ /** Starts a new cart. Save the returned cartID and pass it to every later call. */
87
+ async createCart({ data, fingerprint } = {}) {
88
+ const args = { fingerprint: ['String', text(fingerprint || undefined)] };
89
+ return this.client.sendRequest(operation('mutation', 'createCart', args, data), 'createCart');
90
+ }
91
+
92
+ async retrieveCart({ cartID, data }) {
93
+ if (!cartID) return missingCartID();
94
+
95
+ const args = { cartID: ['String!', text(cartID)] };
96
+ return this.client.sendRequest(operation('query', 'retrieveCart', args, data), 'retrieveCart');
97
+ }
98
+
99
+ async addCustomerInformation({ cartID, customerInformation, data }) {
100
+ if (!cartID) return missingCartID();
101
+
102
+ const args = {
103
+ cartID: ['String!', text(cartID)],
104
+ customerInformation: ['CustomerInformationInput!', textInput(customerInformation)],
105
+ };
106
+ return this.client.sendRequest(operation('mutation', 'addCustomerInformation', args, data), 'addCustomerInformation');
107
+ }
108
+
109
+ async updateCustomerInformation({ cartID, customerInformation, data }) {
110
+ if (!cartID) return missingCartID();
111
+
112
+ const args = {
113
+ cartID: ['String!', text(cartID)],
114
+ customerInformation: ['CustomerInformationInput!', textInput(customerInformation)],
115
+ };
116
+ return this.client.sendRequest(operation('mutation', 'updateCustomerInformation', args, data), 'updateCustomerInformation');
117
+ }
118
+
119
+ async variantInStock({variantID}) {
120
+ const args = { variantID: ['String!', text(variantID)] };
121
+ return this.client.sendRequest(operation('query', 'checkVariantStock', args), 'checkVariantStock');
122
+ }
123
+
124
+ async addVariantToCart({ cartID, productID, quantity, variantProperties, personalization, data }) {
125
+ if (!cartID) return missingCartID();
126
+
127
+ // variantProperties is required; personalization is optional
128
+ const args = {
129
+ cartID: ['String!', text(cartID)],
130
+ productID: ['String!', text(productID)],
131
+ quantity: ['Int!', number(quantity)],
132
+ variantProperties: ['VariantPropertiesInput!', textInput(variantProperties)],
133
+ personalization: ['VariantPersonalizationInput', personalization ? textInput(personalization) : undefined],
134
+ };
135
+ return this.client.sendRequest(operation('mutation', 'addVariantToCart', args, data), 'addVariantToCart');
136
+ }
137
+
138
+ async addVariantQuantityInCart({ cartID, variantID, quantity, data }) {
139
+ if (!cartID) return missingCartID();
140
+
141
+ const args = {
142
+ cartID: ['String!', text(cartID)],
143
+ variantID: ['String!', text(variantID)],
144
+ quantity: ['Int!', number(quantity)],
145
+ };
146
+ return this.client.sendRequest(operation('mutation', 'addVariantQuantityInCart', args, data), 'addVariantQuantityInCart');
147
+ }
148
+
149
+ async subtractVariantQuantityInCart({ cartID, variantID, quantity, data }) {
150
+ if (!cartID) return missingCartID();
151
+
152
+ const args = {
153
+ cartID: ['String!', text(cartID)],
154
+ variantID: ['String!', text(variantID)],
155
+ quantity: ['Int!', number(quantity)],
156
+ };
157
+ return this.client.sendRequest(operation('mutation', 'subtractVariantQuantityInCart', args, data), 'subtractVariantQuantityInCart');
158
+ }
159
+
160
+ async removeVariantFromCart({ cartID, variantID, data }) {
161
+ if (!cartID) return missingCartID();
162
+
163
+ const args = {
164
+ cartID: ['String!', text(cartID)],
165
+ variantID: ['String!', text(variantID)],
166
+ };
167
+ return this.client.sendRequest(operation('mutation', 'removeVariantFromCart', args, data), 'removeVariantFromCart');
168
+ }
169
+
170
+ async updatePersonalization({ cartID, variantID, personalization, data }) {
171
+ if (!cartID) return missingCartID();
172
+
173
+ const args = {
174
+ cartID: ['String!', text(cartID)],
175
+ variantID: ['String!', text(variantID)],
176
+ personalization: ['VariantPersonalizationInput!', textInput(personalization)],
177
+ };
178
+ return this.client.sendRequest(operation('mutation', 'updatePersonalization', args, data), 'updatePersonalization');
179
+ }
180
+
181
+ async emptyCart({ cartID, data }) {
182
+ if (!cartID) return missingCartID();
183
+
184
+ const args = { cartID: ['String!', text(cartID)] };
185
+ return this.client.sendRequest(operation('mutation', 'emptyCart', args, data), 'emptyCart');
186
+ }
187
+
188
+ async applyPromoCode({ cartID, promoCode, data }) {
189
+ if (!cartID) return missingCartID();
190
+
191
+ const args = {
192
+ cartID: ['String!', text(cartID)],
193
+ promoCode: ['String!', text(promoCode)],
194
+ };
195
+ return this.client.sendRequest(operation('mutation', 'applyPromoCode', args, data), 'applyPromoCode');
196
+ }
197
+
198
+ async setTaxAmount({ cartID, taxAmount, data }) {
199
+ if (!cartID) return missingCartID();
200
+
201
+ const args = {
202
+ cartID: ['String!', text(cartID)],
203
+ taxAmount: ['Float!', Number(taxAmount) || 0],
204
+ };
205
+ return this.client.sendRequest(operation('mutation', 'setCartTaxAmount', args, data), 'setCartTaxAmount');
206
+ }
207
+
208
+ async removePromoCode({ cartID, data }) {
209
+ if (!cartID) return missingCartID();
210
+
211
+ const args = { cartID: ['String!', text(cartID)] };
212
+ return this.client.sendRequest(operation('mutation', 'removePromoCode', args, data), 'removePromoCode');
213
+ }
214
+ }