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 +111 -0
- package/alerts.js +77 -0
- package/analytics.js +241 -0
- package/bundles.js +14 -0
- package/cart.js +214 -0
- package/cli.js +156 -0
- package/collections.js +30 -0
- package/currency.js +92 -0
- package/fraud.js +59 -0
- package/gift_cards.js +35 -0
- package/graphql.js +68 -0
- package/index.js +2 -0
- package/locations.js +33 -0
- package/loyalty.js +78 -0
- package/media.js +87 -0
- package/orders.js +166 -0
- package/package.json +15 -5
- package/pages.js +41 -0
- package/personalization.js +13 -0
- package/post_purchase.js +37 -0
- package/prezanse.js +156 -0
- package/products.js +107 -0
- package/promotions.js +42 -0
- package/reviews.js +53 -0
- package/search.js +18 -0
- package/shipping.js +32 -0
- package/trending.js +26 -0
- package/README.md +0 -3
package/orders.js
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { operation, text, number, textList } from './graphql.js';
|
|
2
|
+
|
|
3
|
+
// Every value goes in as a GraphQL variable, never spliced into the query
|
|
4
|
+
// text — emails, IDs and return reasons are typed by the customer.
|
|
5
|
+
|
|
6
|
+
export class Orders {
|
|
7
|
+
constructor(client) {
|
|
8
|
+
this.client = client;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async create({ cartID, stripePaymentIntentID, data }) {
|
|
12
|
+
// The Stripe PaymentIntent that paid for this cart. Optional for backwards
|
|
13
|
+
// compatibility, but always pass it: without it the order has no link to the
|
|
14
|
+
// payment and can never be refunded through Prezense.
|
|
15
|
+
const args = {
|
|
16
|
+
cartID: ['String!', text(cartID)],
|
|
17
|
+
stripePaymentIntentID: ['String', text(stripePaymentIntentID || undefined)],
|
|
18
|
+
};
|
|
19
|
+
return this.client.sendRequest(operation('mutation', 'createOrder', args, data), 'createOrder');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Look up one purchase by the customer's email + purchase ID — what an
|
|
24
|
+
* order-status / tracking page calls. Select `orders { shipments { … } }`
|
|
25
|
+
* for live per-box tracking (status, location, ETA, scan history).
|
|
26
|
+
*
|
|
27
|
+
* email and purchase_id are customer-typed, so they go as GraphQL
|
|
28
|
+
* variables, never spliced into the query text.
|
|
29
|
+
*/
|
|
30
|
+
async getOrder({ email, purchase_id, data }) {
|
|
31
|
+
const query = `query RetrieveOrder($email: String!, $purchaseId: String!) {
|
|
32
|
+
retrieveOrder(email: $email, purchaseId: $purchaseId) {
|
|
33
|
+
${data.trim()}
|
|
34
|
+
}
|
|
35
|
+
}`;
|
|
36
|
+
|
|
37
|
+
return this.client.sendRequest({ query, variables: { email, purchaseId: purchase_id } }, 'retrieveOrder');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async getAllOrders({ email, data }) {
|
|
41
|
+
const query = `query RetrieveAllOrders($email: String!) {
|
|
42
|
+
retrieveAllOrders(email: $email) {
|
|
43
|
+
${data.trim()}
|
|
44
|
+
}
|
|
45
|
+
}`;
|
|
46
|
+
|
|
47
|
+
return this.client.sendRequest({ query, variables: { email } }, 'retrieveAllOrders');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async getReturns({ customer_email, data, limit, offset }) {
|
|
51
|
+
const args = {
|
|
52
|
+
customerEmail: ['String!', text(customer_email)],
|
|
53
|
+
limit: ['Int', number(limit)],
|
|
54
|
+
offset: ['Int', number(offset)],
|
|
55
|
+
};
|
|
56
|
+
return this.client.sendRequest(operation('query', 'getCustomerReturns', args, data), 'getCustomerReturns');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* One return by its ID — what a store's returns page shows. The return
|
|
61
|
+
* emails link to that page as `?return=<id>`. Resolves `response` to null
|
|
62
|
+
* when no return with that ID belongs to the store.
|
|
63
|
+
*
|
|
64
|
+
* `label.labelURL` is normally on the store's own domain
|
|
65
|
+
* (`/_prezense/return-label/<id>`, served by the platform, not the app);
|
|
66
|
+
* it can be the shipping provider's link while the platform's copy isn't
|
|
67
|
+
* there yet. Link to it as given.
|
|
68
|
+
*
|
|
69
|
+
* @param {Object} args
|
|
70
|
+
* @param {string} args.return_id
|
|
71
|
+
* @param {string} args.data GraphQL selection set
|
|
72
|
+
*/
|
|
73
|
+
async getReturn({ return_id, data }) {
|
|
74
|
+
const query = `query GetReturn($returnID: String!) {
|
|
75
|
+
getReturn(returnID: $returnID) {
|
|
76
|
+
${data.trim()}
|
|
77
|
+
}
|
|
78
|
+
}`;
|
|
79
|
+
|
|
80
|
+
return this.client.sendRequest({ query, variables: { returnID: return_id } }, 'getReturn');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async createReturn({ order_id, customer_email, products_to_return, reason_for_return, data }) {
|
|
84
|
+
// reasonForReturn is optional; the reason is sent as typed
|
|
85
|
+
const args = {
|
|
86
|
+
orderId: ['String!', text(order_id)],
|
|
87
|
+
customerEmail: ['String!', text(customer_email)],
|
|
88
|
+
productsToReturn: ['[String!]!', textList(products_to_return)],
|
|
89
|
+
reasonForReturn: ['String', text(reason_for_return)],
|
|
90
|
+
};
|
|
91
|
+
return this.client.sendRequest(operation('mutation', 'createReturnRequest', args, data), 'createReturnRequest');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// ================================================================
|
|
95
|
+
// EXCHANGE MANAGEMENT
|
|
96
|
+
// ================================================================
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Submit a return request and synchronously receive exchange suggestions.
|
|
100
|
+
*
|
|
101
|
+
* The backend routes to deterministic sibling-variant lookups, personalized
|
|
102
|
+
* recommendations, or a Claude Haiku agentic loop based on reasonCategory
|
|
103
|
+
* and the free-text detail. Suggestions come back in the same response —
|
|
104
|
+
* no follow-up call required.
|
|
105
|
+
*
|
|
106
|
+
* @param {Object} args
|
|
107
|
+
* @param {string} args.order_id
|
|
108
|
+
* @param {string} args.customer_email
|
|
109
|
+
* @param {string[]} args.products_to_return variantIDs
|
|
110
|
+
* @param {string} [args.reason_category] one of: size, fit, color, quality,
|
|
111
|
+
* damaged, wrong_item, changed_mind,
|
|
112
|
+
* late_arrival, other
|
|
113
|
+
* @param {string} [args.reason_for_return] free-text detail
|
|
114
|
+
* @param {number} [args.max_suggestions] 1..10, defaults to 5
|
|
115
|
+
* @param {string} args.data GraphQL selection set
|
|
116
|
+
*/
|
|
117
|
+
async submitReturnRequest({
|
|
118
|
+
order_id,
|
|
119
|
+
customer_email,
|
|
120
|
+
products_to_return,
|
|
121
|
+
reason_category,
|
|
122
|
+
reason_for_return,
|
|
123
|
+
max_suggestions,
|
|
124
|
+
data,
|
|
125
|
+
}) {
|
|
126
|
+
const args = {
|
|
127
|
+
orderId: ['String!', text(order_id)],
|
|
128
|
+
customerEmail: ['String!', text(customer_email)],
|
|
129
|
+
productsToReturn: ['[String!]!', textList(products_to_return)],
|
|
130
|
+
reasonCategory: ['ReturnReasonCategory', reason_category ? text(reason_category).toUpperCase() : undefined],
|
|
131
|
+
reasonForReturn: ['String', text(reason_for_return)],
|
|
132
|
+
maxSuggestions: ['Int', max_suggestions ? Number(max_suggestions) : undefined],
|
|
133
|
+
};
|
|
134
|
+
return this.client.sendRequest(operation('mutation', 'submitReturnRequest', args, data), 'submitReturnRequest');
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async acceptExchangeSuggestion({ suggestion_id, data }) {
|
|
138
|
+
const args = { suggestionID: ['String!', text(suggestion_id)] };
|
|
139
|
+
return this.client.sendRequest(operation('mutation', 'acceptExchangeSuggestion', args, data), 'acceptExchangeSuggestion');
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async cancelExchange({ exchange_id, data }) {
|
|
143
|
+
const args = { exchangeID: ['String!', text(exchange_id)] };
|
|
144
|
+
return this.client.sendRequest(operation('mutation', 'cancelExchange', args, data), 'cancelExchange');
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async declineExchangeSuggestions({ return_id, data }) {
|
|
148
|
+
const args = { returnID: ['String!', text(return_id)] };
|
|
149
|
+
return this.client.sendRequest(operation('mutation', 'declineExchangeSuggestions', args, data), 'declineExchangeSuggestions');
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async getExchanges({ limit, offset, status, data }) {
|
|
153
|
+
const args = {
|
|
154
|
+
limit: ['Int', number(limit)],
|
|
155
|
+
offset: ['Int', number(offset)],
|
|
156
|
+
status: ['ExchangeStatus', status ? text(status).toUpperCase() : undefined],
|
|
157
|
+
};
|
|
158
|
+
return this.client.sendRequest(operation('query', 'getExchanges', args, data), 'getExchanges');
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async getExchangeByID({ exchange_id, data }) {
|
|
162
|
+
// The server's field is getExchangeById; the response is the same.
|
|
163
|
+
const args = { exchangeID: ['String!', text(exchange_id)] };
|
|
164
|
+
return this.client.sendRequest(operation('query', 'getExchangeById', args, data), 'getExchangeById');
|
|
165
|
+
}
|
|
166
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "prezense",
|
|
3
|
+
"version": "1.3.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"prezense": "./cli.js"
|
|
8
|
+
},
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": ">=18"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"node-fetch": "^3.3.2",
|
|
14
|
+
"ua-parser-js": "^2.0.3"
|
|
15
|
+
}
|
|
16
|
+
}
|
package/pages.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { operation, text, number } from './graphql.js';
|
|
2
|
+
|
|
3
|
+
// A store's pages (About, Shipping, …) and blog posts. Storefront routes:
|
|
4
|
+
// /pages/<handle> and /blog/<handle>. Only published ones are returned; `body`
|
|
5
|
+
// is HTML.
|
|
6
|
+
|
|
7
|
+
export class Pages {
|
|
8
|
+
constructor(client) {
|
|
9
|
+
this.client = client;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Published pages, newest first. `data` selects StorePage fields. */
|
|
13
|
+
async allPages({ data, limit, offset }) {
|
|
14
|
+
return this.#list('page', { data, limit, offset });
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** One page by handle. */
|
|
18
|
+
async page({ handle, data }) {
|
|
19
|
+
return this.#one('page', { handle, data });
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Published blog posts, newest first. */
|
|
23
|
+
async allBlogPosts({ data, limit, offset }) {
|
|
24
|
+
return this.#list('blog_post', { data, limit, offset });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** One blog post by handle. */
|
|
28
|
+
async blogPost({ handle, data }) {
|
|
29
|
+
return this.#one('blog_post', { handle, data });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async #list(kind, { data, limit, offset }) {
|
|
33
|
+
const args = { kind: ['String', kind], limit: ['Int', number(limit)], offset: ['Int', number(offset)] };
|
|
34
|
+
return this.client.sendRequest(operation('query', 'getPages', args, data), 'getPages');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async #one(kind, { handle, data }) {
|
|
38
|
+
const args = { handle: ['String!', text(handle)], kind: ['String', kind] };
|
|
39
|
+
return this.client.sendRequest(operation('query', 'getPage', args, data), 'getPage');
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { operation, text, number } from './graphql.js';
|
|
2
|
+
|
|
3
|
+
export class Personalization {
|
|
4
|
+
constructor(client) {
|
|
5
|
+
this.client = client;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
async recommendations({ userID, data, limit }) {
|
|
9
|
+
// Values go in as GraphQL variables, never spliced into the query text.
|
|
10
|
+
const args = { userID: ['String!', text(userID)], limit: ['Int', number(limit)] };
|
|
11
|
+
return this.client.sendRequest(operation('query', 'getPersonalizedRecommendations', args, data), 'getPersonalizedRecommendations');
|
|
12
|
+
}
|
|
13
|
+
}
|
package/post_purchase.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { operation, text, number } from './graphql.js';
|
|
2
|
+
|
|
3
|
+
// Every value goes in as a GraphQL variable (graphql.js), never spliced into
|
|
4
|
+
// the query text.
|
|
5
|
+
|
|
6
|
+
export class PostPurchase {
|
|
7
|
+
constructor(client) {
|
|
8
|
+
this.client = client;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async recommendations({ orderID, userID, data, limit }) {
|
|
12
|
+
const args = {
|
|
13
|
+
orderID: ['String!', text(orderID)],
|
|
14
|
+
userID: ['String', text(userID)],
|
|
15
|
+
limit: ['Int', number(limit)],
|
|
16
|
+
};
|
|
17
|
+
return this.client.sendRequest(operation('query', 'getPostPurchaseRecommendations', args, data), 'getPostPurchaseRecommendations');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async appendItems({ orderID, items, paymentReference, taxAmount, data }) {
|
|
21
|
+
const args = {
|
|
22
|
+
orderID: ['String!', text(orderID)],
|
|
23
|
+
items: ['[AppendOrderItemInput!]!', items.map(i => ({ variantID: text(i.variantID), quantity: number(i.quantity) }))],
|
|
24
|
+
paymentReference: ['String!', text(paymentReference)],
|
|
25
|
+
taxAmount: ['Float!', Number(taxAmount) || 0],
|
|
26
|
+
};
|
|
27
|
+
return this.client.sendRequest(operation('mutation', 'appendItemsToOrder', args, data), 'appendItemsToOrder');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async rollbackAppend({ orderID, appendID, data }) {
|
|
31
|
+
const args = {
|
|
32
|
+
orderID: ['String!', text(orderID)],
|
|
33
|
+
appendID: ['String!', text(appendID)],
|
|
34
|
+
};
|
|
35
|
+
return this.client.sendRequest(operation('mutation', 'rollbackAppendedItems', args, data), 'rollbackAppendedItems');
|
|
36
|
+
}
|
|
37
|
+
}
|
package/prezanse.js
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import fetch from 'node-fetch';
|
|
2
|
+
import { Products } from './products.js';
|
|
3
|
+
import { Cart } from './cart.js';
|
|
4
|
+
import { Locations } from './locations.js';
|
|
5
|
+
import { Promotions } from './promotions.js';
|
|
6
|
+
import { Media } from './media.js';
|
|
7
|
+
import { Reviews } from './reviews.js';
|
|
8
|
+
import { Accounts } from './accounts.js';
|
|
9
|
+
import { Orders } from './orders.js';
|
|
10
|
+
import { Analytics } from './analytics.js';
|
|
11
|
+
import { Shipping } from './shipping.js';
|
|
12
|
+
import { Personalization } from './personalization.js';
|
|
13
|
+
import { PostPurchase } from './post_purchase.js';
|
|
14
|
+
import { Trending } from './trending.js';
|
|
15
|
+
import { Search } from './search.js';
|
|
16
|
+
import { Bundles } from './bundles.js';
|
|
17
|
+
import { Currency } from './currency.js';
|
|
18
|
+
import { Loyalty } from './loyalty.js';
|
|
19
|
+
import { Fraud } from './fraud.js';
|
|
20
|
+
import { Alerts } from './alerts.js';
|
|
21
|
+
import { Collections } from './collections.js';
|
|
22
|
+
import { Pages } from './pages.js';
|
|
23
|
+
import { GiftCards } from './gift_cards.js';
|
|
24
|
+
|
|
25
|
+
export class Prezense {
|
|
26
|
+
constructor(apiKey, platformKey, options = {}) {
|
|
27
|
+
// `new Prezense({ ...options })` — options alone, keys from env — is
|
|
28
|
+
// the usual storefront form.
|
|
29
|
+
if (apiKey && typeof apiKey === 'object') {
|
|
30
|
+
options = apiKey;
|
|
31
|
+
apiKey = undefined;
|
|
32
|
+
platformKey = undefined;
|
|
33
|
+
}
|
|
34
|
+
// Credentials and endpoints all fall back to env so the SDK works with
|
|
35
|
+
// zero args inside an E2B sandbox (env injected by the harness). Explicit
|
|
36
|
+
// args still win; the localhost defaults keep local dev intact.
|
|
37
|
+
this.apiKey = apiKey || process.env.PREZENSE_API_KEY;
|
|
38
|
+
this.platformKey = platformKey || process.env.PREZENSE_PLATFORM_KEY;
|
|
39
|
+
this.currencyCode = options.currency || null;
|
|
40
|
+
// The shopper this request is for: { ip, userAgent }, read by the
|
|
41
|
+
// storefront from its incoming request. The SDK runs on the server, so
|
|
42
|
+
// without it Prezense would see the storefront server's IP and
|
|
43
|
+
// User-Agent instead of the shopper's — for fraud checks, currency
|
|
44
|
+
// detection and analytics (guides/shopper-context).
|
|
45
|
+
// An x-forwarded-for chain ("shopper, proxy, …") is cut to its first
|
|
46
|
+
// address, the shopper's.
|
|
47
|
+
this.shopper = options.shopper
|
|
48
|
+
? {
|
|
49
|
+
ip: options.shopper.ip ? String(options.shopper.ip).split(',')[0].trim() : null,
|
|
50
|
+
userAgent: options.shopper.userAgent ? String(options.shopper.userAgent).slice(0, 500) : null,
|
|
51
|
+
}
|
|
52
|
+
: null;
|
|
53
|
+
this.baseUrl = options.baseUrl || process.env.PREZENSE_API_URL || 'http://localhost:4000/';
|
|
54
|
+
this.trackingUrl = options.trackingUrl || process.env.PREZENSE_TRACKING_URL || 'http://localhost:4001';
|
|
55
|
+
|
|
56
|
+
// Instantiate each category class
|
|
57
|
+
this.products = new Products(this);
|
|
58
|
+
this.cart = new Cart(this);
|
|
59
|
+
this.locations = new Locations(this);
|
|
60
|
+
this.promotions = new Promotions(this);
|
|
61
|
+
this.media = new Media(this);
|
|
62
|
+
this.reviews = new Reviews(this);
|
|
63
|
+
this.accounts = new Accounts(this);
|
|
64
|
+
this.orders = new Orders(this);
|
|
65
|
+
this.analytics = new Analytics(this);
|
|
66
|
+
this.shipping = new Shipping(this);
|
|
67
|
+
this.personalization = new Personalization(this);
|
|
68
|
+
this.postPurchase = new PostPurchase(this);
|
|
69
|
+
this.trending = new Trending(this);
|
|
70
|
+
this.search = new Search(this);
|
|
71
|
+
this.bundles = new Bundles(this);
|
|
72
|
+
this.currency = new Currency(this);
|
|
73
|
+
this.loyalty = new Loyalty(this);
|
|
74
|
+
this.fraud = new Fraud(this);
|
|
75
|
+
this.alerts = new Alerts(this);
|
|
76
|
+
this.collections = new Collections(this);
|
|
77
|
+
this.pages = new Pages(this);
|
|
78
|
+
this.giftCards = new GiftCards(this);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** The shopper's IP and User-Agent, forwarded so Prezense sees the shopper, not this server. */
|
|
82
|
+
#shopperHeaders() {
|
|
83
|
+
const headers = {};
|
|
84
|
+
const ip = this.shopper?.ip;
|
|
85
|
+
const userAgent = this.shopper?.userAgent;
|
|
86
|
+
if (ip) headers['x-shopper-ip'] = ip;
|
|
87
|
+
if (userAgent) headers['x-shopper-user-agent'] = userAgent;
|
|
88
|
+
return headers;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Method to send a request directly to the GraphQL server
|
|
92
|
+
async sendRequest(query, dataField) {
|
|
93
|
+
try {
|
|
94
|
+
|
|
95
|
+
const headers = {
|
|
96
|
+
'Content-Type': 'application/json',
|
|
97
|
+
'x-api-key': this.apiKey,
|
|
98
|
+
'x-platform-key': this.platformKey,
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
if (this.currencyCode) {
|
|
102
|
+
headers['x-currency'] = this.currencyCode;
|
|
103
|
+
}
|
|
104
|
+
Object.assign(headers, this.#shopperHeaders());
|
|
105
|
+
|
|
106
|
+
const response = await fetch(this.baseUrl, {
|
|
107
|
+
method: 'POST',
|
|
108
|
+
headers,
|
|
109
|
+
body: JSON.stringify(query),
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
const result = await response.json();
|
|
113
|
+
|
|
114
|
+
// Check for errors in the response
|
|
115
|
+
if (result.errors) {
|
|
116
|
+
// Keep the machine-readable code (e.g. FRAUD_RISK_TOO_HIGH) so
|
|
117
|
+
// callers can branch on it instead of matching message text.
|
|
118
|
+
const first = result.errors[0];
|
|
119
|
+
const error = new Error(first.message);
|
|
120
|
+
error.code = first.code ?? first.extensions?.code ?? null;
|
|
121
|
+
return { error, response: null };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// If no errors, return the data
|
|
125
|
+
return { error: null, response: result.data[dataField] };
|
|
126
|
+
} catch (error) {
|
|
127
|
+
return { error, response: null };
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async trackEvent(endpoint, body) {
|
|
132
|
+
try {
|
|
133
|
+
|
|
134
|
+
const response = await fetch(`${this.trackingUrl}/track/${endpoint}`, {
|
|
135
|
+
method: 'POST',
|
|
136
|
+
headers: {
|
|
137
|
+
'Content-Type': 'application/json',
|
|
138
|
+
'apikey': this.apiKey,
|
|
139
|
+
'platformkey': this.platformKey,
|
|
140
|
+
...this.#shopperHeaders(),
|
|
141
|
+
},
|
|
142
|
+
body: JSON.stringify(body),
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
const result = await response.json();
|
|
146
|
+
|
|
147
|
+
return { error: null, response: result };
|
|
148
|
+
|
|
149
|
+
} catch (error) {
|
|
150
|
+
return { error, response: null };
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
package/products.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { operation, text, number, bool, textList } from './graphql.js';
|
|
2
|
+
|
|
3
|
+
// Every value goes in as a GraphQL variable (graphql.js), never spliced into
|
|
4
|
+
// the query text — search boxes and category links carry shopper text.
|
|
5
|
+
|
|
6
|
+
export class Products {
|
|
7
|
+
constructor(client) {
|
|
8
|
+
this.client = client;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async product({ productID, data }) {
|
|
12
|
+
const args = { productID: ['String!', text(productID)] };
|
|
13
|
+
return this.client.sendRequest(operation('query', 'getProductById', args, data), 'getProductById');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async allProducts({ data, limit, offset }) {
|
|
17
|
+
const args = { limit: ['Int', number(limit)], offset: ['Int', number(offset)] };
|
|
18
|
+
return this.client.sendRequest(operation('query', 'getAllProducts', args, data), 'getAllProducts');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async productsByID({ productIDs, data, limit, offset }) {
|
|
22
|
+
const args = {
|
|
23
|
+
productIDs: ['[String!]!', textList(productIDs)],
|
|
24
|
+
limit: ['Int', number(limit)],
|
|
25
|
+
offset: ['Int', number(offset)],
|
|
26
|
+
};
|
|
27
|
+
return this.client.sendRequest(operation('query', 'getProductsById', args, data), 'getProductsById');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async collectionProducts({ collection, data, limit, offset }) {
|
|
31
|
+
const args = {
|
|
32
|
+
collection: ['String!', text(collection)],
|
|
33
|
+
limit: ['Int', number(limit)],
|
|
34
|
+
offset: ['Int', number(offset)],
|
|
35
|
+
};
|
|
36
|
+
return this.client.sendRequest(operation('query', 'getProductsByCollection', args, data), 'getProductsByCollection');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async categoryProducts({ category, data, limit, offset }) {
|
|
40
|
+
const args = {
|
|
41
|
+
category: ['String!', text(category)],
|
|
42
|
+
limit: ['Int', number(limit)],
|
|
43
|
+
offset: ['Int', number(offset)],
|
|
44
|
+
};
|
|
45
|
+
return this.client.sendRequest(operation('query', 'getProductsByCategory', args, data), 'getProductsByCategory');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async typeProducts({ type, data, limit, offset }) {
|
|
49
|
+
const args = {
|
|
50
|
+
type: ['String!', text(type)],
|
|
51
|
+
limit: ['Int', number(limit)],
|
|
52
|
+
offset: ['Int', number(offset)],
|
|
53
|
+
};
|
|
54
|
+
return this.client.sendRequest(operation('query', 'getProductsByType', args, data), 'getProductsByType');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async brandProducts({ brand, data, limit, offset }) {
|
|
58
|
+
const args = {
|
|
59
|
+
brand: ['String!', text(brand)],
|
|
60
|
+
limit: ['Int', number(limit)],
|
|
61
|
+
offset: ['Int', number(offset)],
|
|
62
|
+
};
|
|
63
|
+
return this.client.sendRequest(operation('query', 'getProductsByBrand', args, data), 'getProductsByBrand');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async productVariants({ productID, data }) {
|
|
67
|
+
const args = { productID: ['String!', text(productID)] };
|
|
68
|
+
return this.client.sendRequest(operation('query', 'getVariantsById', args, data), 'getVariantsById');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async personalizedRecommendations({ userID, data, limit }) {
|
|
72
|
+
const args = { userID: ['String!', text(userID)], limit: ['Int', number(limit)] };
|
|
73
|
+
return this.client.sendRequest(operation('query', 'getPersonalizedRecommendations', args, data), 'getPersonalizedRecommendations');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async searchProducts({ query, data, limit, offset }) {
|
|
77
|
+
// The search text is sent as typed — quotes and backslashes included.
|
|
78
|
+
const args = {
|
|
79
|
+
query: ['String!', text(query)],
|
|
80
|
+
limit: ['Int', number(limit)],
|
|
81
|
+
offset: ['Int', number(offset)],
|
|
82
|
+
};
|
|
83
|
+
return this.client.sendRequest(operation('query', 'searchProducts', args, data), 'searchProducts');
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async trendingProducts({ timeWindow, category, data, limit, minScore, includeProductDetails }) {
|
|
87
|
+
// Arguments left out fall back to the server's defaults.
|
|
88
|
+
const args = {
|
|
89
|
+
timeWindow: ['String!', text(timeWindow)],
|
|
90
|
+
category: ['String', text(category)],
|
|
91
|
+
limit: ['Int', number(limit)],
|
|
92
|
+
minScore: ['Float', number(minScore)],
|
|
93
|
+
includeProductDetails: ['Boolean!', bool(includeProductDetails)],
|
|
94
|
+
};
|
|
95
|
+
return this.client.sendRequest(operation('query', 'getTrendingProducts', args, data), 'getTrendingProducts');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async purchaseBundles({ productId, data, limit, minConfidence }) {
|
|
99
|
+
// Values go in as GraphQL variables, never spliced into the query text.
|
|
100
|
+
const query = `query ($productId: String!, $limit: Int, $minConfidence: Float) {
|
|
101
|
+
getProductPurchaseBundles(productId: $productId, limit: $limit, minConfidence: $minConfidence) { ${data.trim()} }
|
|
102
|
+
}`;
|
|
103
|
+
const variables = { productId, limit, minConfidence };
|
|
104
|
+
return this.client.sendRequest({ query, variables }, 'getProductPurchaseBundles');
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
}
|
package/promotions.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { operation, text, number } from './graphql.js';
|
|
2
|
+
|
|
3
|
+
// Every value goes in as a GraphQL variable (graphql.js), never spliced into
|
|
4
|
+
// the query text.
|
|
5
|
+
|
|
6
|
+
export class Promotions {
|
|
7
|
+
constructor(client) {
|
|
8
|
+
this.client = client;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async hasSale({ productID, data, limit, offset }) {
|
|
12
|
+
const args = {
|
|
13
|
+
productID: ['String!', text(productID)],
|
|
14
|
+
limit: ['Int', number(limit)],
|
|
15
|
+
offset: ['Int', number(offset)],
|
|
16
|
+
};
|
|
17
|
+
return this.client.sendRequest(operation('query', 'checkIfProductHasSale', args, data), 'checkIfProductHasSale');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async hasDiscount({ productID, data, limit, offset }) {
|
|
21
|
+
const args = {
|
|
22
|
+
productID: ['String!', text(productID)],
|
|
23
|
+
limit: ['Int', number(limit)],
|
|
24
|
+
offset: ['Int', number(offset)],
|
|
25
|
+
};
|
|
26
|
+
return this.client.sendRequest(operation('query', 'checkIfProductHasDiscount', args, data), 'checkIfProductHasDiscount');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async hasPromoCode({ productID, data, limit, offset }) {
|
|
30
|
+
const args = {
|
|
31
|
+
productID: ['String!', text(productID)],
|
|
32
|
+
limit: ['Int', number(limit)],
|
|
33
|
+
offset: ['Int', number(offset)],
|
|
34
|
+
};
|
|
35
|
+
return this.client.sendRequest(operation('query', 'checkIfProductHasPromoCode', args, data), 'checkIfProductHasPromoCode');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async hasFreeShipping({ productID }) {
|
|
39
|
+
const args = { productID: ['String!', text(productID)] };
|
|
40
|
+
return this.client.sendRequest(operation('query', 'checkIfProductHasFreeShipping', args), 'checkIfProductHasFreeShipping');
|
|
41
|
+
}
|
|
42
|
+
}
|
package/reviews.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export class Reviews {
|
|
2
|
+
constructor(client) {
|
|
3
|
+
this.client = client;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
// Every value goes in as a GraphQL variable, never spliced into the query
|
|
7
|
+
// text, so reviews with newlines, quotes or backslashes are sent intact.
|
|
8
|
+
|
|
9
|
+
async addReview({ productID, rating, review, customerEmail }) {
|
|
10
|
+
const query = `mutation ($productID: String!, $rating: Int!, $review: String!, $customerEmail: String!) {
|
|
11
|
+
addProductReview(productID: $productID, rating: $rating, review: $review, customerEmail: $customerEmail)
|
|
12
|
+
}`;
|
|
13
|
+
const variables = { productID, rating, review, customerEmail };
|
|
14
|
+
return this.client.sendRequest({ query, variables }, 'addProductReview');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async getAllReviewsOfProduct({ productID, data, limit, offset }) {
|
|
18
|
+
const query = `query ($productID: String!, $limit: Int, $offset: Int) {
|
|
19
|
+
getAllReviewsOfProduct(productID: $productID, limit: $limit, offset: $offset) {
|
|
20
|
+
${data.trim()}
|
|
21
|
+
}
|
|
22
|
+
}`;
|
|
23
|
+
const variables = { productID, limit, offset };
|
|
24
|
+
return this.client.sendRequest({ query, variables }, 'getAllReviewsOfProduct');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async getReviewByID({ reviewID, data }) {
|
|
28
|
+
const query = `query ($reviewID: String!) {
|
|
29
|
+
getReviewByID(reviewID: $reviewID) {
|
|
30
|
+
${data.trim()}
|
|
31
|
+
}
|
|
32
|
+
}`;
|
|
33
|
+
return this.client.sendRequest({ query, variables: { reviewID } }, 'getReviewByID');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async getReviewsByID({ reviewIDs, data }) {
|
|
37
|
+
const query = `query ($reviewIDs: [String!]!) {
|
|
38
|
+
getListOfReviewsByIDs(reviewIDs: $reviewIDs) {
|
|
39
|
+
${data.trim()}
|
|
40
|
+
}
|
|
41
|
+
}`;
|
|
42
|
+
return this.client.sendRequest({ query, variables: { reviewIDs } }, 'getListOfReviewsByIDs');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async getProductRatingSummary({ productID, data }) {
|
|
46
|
+
const query = `query ($productID: String!) {
|
|
47
|
+
getProductRatingSummary(productID: $productID) {
|
|
48
|
+
${data.trim()}
|
|
49
|
+
}
|
|
50
|
+
}`;
|
|
51
|
+
return this.client.sendRequest({ query, variables: { productID } }, 'getProductRatingSummary');
|
|
52
|
+
}
|
|
53
|
+
}
|
package/search.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { operation, text, number } from './graphql.js';
|
|
2
|
+
|
|
3
|
+
export class Search {
|
|
4
|
+
constructor(client) {
|
|
5
|
+
this.client = client;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
async products({ query, data, limit, offset }) {
|
|
9
|
+
// The search text goes in as a GraphQL variable, sent as typed —
|
|
10
|
+
// quotes and backslashes included — never spliced into the query text.
|
|
11
|
+
const args = {
|
|
12
|
+
query: ['String!', text(query)],
|
|
13
|
+
limit: ['Int', number(limit)],
|
|
14
|
+
offset: ['Int', number(offset)],
|
|
15
|
+
};
|
|
16
|
+
return this.client.sendRequest(operation('query', 'searchProducts', args, data), 'searchProducts');
|
|
17
|
+
}
|
|
18
|
+
}
|