prezense 0.0.0-stage → 1.4.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.
Potentially problematic release.
This version of prezense might be problematic. Click here for more details.
- package/accounts.js +98 -0
- package/alerts.js +77 -0
- package/analytics.js +241 -0
- package/bundles.js +19 -0
- package/cart.js +244 -0
- package/cli.js +483 -0
- package/collections.js +30 -0
- package/currency.js +95 -0
- package/fraud.js +47 -0
- package/gift_cards.js +35 -0
- package/graphql.js +107 -0
- package/index.js +2 -0
- package/locations.js +33 -0
- package/loyalty.js +72 -0
- package/media.js +87 -0
- package/orders.js +173 -0
- package/package.json +18 -5
- package/pages.js +41 -0
- package/personalization.js +13 -0
- package/post_purchase.js +80 -0
- package/prezanse.js +262 -0
- package/products.js +108 -0
- package/promotions.js +42 -0
- package/reviews.js +51 -0
- package/search.js +18 -0
- package/shipping.js +42 -0
- package/test/checkout-payment.test.js +44 -0
- package/test/offline.test.js +261 -0
- package/test/post-purchase-charge.test.js +56 -0
- package/trending.js +26 -0
- package/README.md +0 -3
package/gift_cards.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { operation, text } from './graphql.js';
|
|
2
|
+
|
|
3
|
+
// Gift cards at checkout. Applying a card takes what it covers off the cart's
|
|
4
|
+
// total; charge the shopper only the returned `total` (0 = nothing to charge,
|
|
5
|
+
// create the order without a payment). The card's balance is drawn down when
|
|
6
|
+
// the order is created.
|
|
7
|
+
//
|
|
8
|
+
// GiftCardApplication fields: cartID, giftCardAmount, total, last4.
|
|
9
|
+
// GiftCardBalance fields: last4, balance, currency, expiresAt.
|
|
10
|
+
|
|
11
|
+
const APPLICATION = 'cartID giftCardAmount total last4';
|
|
12
|
+
|
|
13
|
+
export class GiftCards {
|
|
14
|
+
constructor(client) {
|
|
15
|
+
this.client = client;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** A card's balance by its code. */
|
|
19
|
+
async balance({ code, data = 'last4 balance currency expiresAt' }) {
|
|
20
|
+
const args = { code: ['String!', text(code)] };
|
|
21
|
+
return this.client.sendRequest(operation('query', 'getGiftCardBalance', args, data), 'getGiftCardBalance');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Pay part or all of a cart with a card (replaces one already applied). */
|
|
25
|
+
async apply({ cartID, code, data = APPLICATION }) {
|
|
26
|
+
const args = { cartID: ['String!', text(cartID)], code: ['String!', text(code)] };
|
|
27
|
+
return this.client.sendRequest(operation('mutation', 'applyGiftCard', args, data), 'applyGiftCard');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Take the card off a cart. */
|
|
31
|
+
async remove({ cartID, data = APPLICATION }) {
|
|
32
|
+
const args = { cartID: ['String!', text(cartID)] };
|
|
33
|
+
return this.client.sendRequest(operation('mutation', 'removeGiftCard', args, data), 'removeGiftCard');
|
|
34
|
+
}
|
|
35
|
+
}
|
package/graphql.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builds the SDK's GraphQL operations.
|
|
3
|
+
*
|
|
4
|
+
* Every value a caller passes goes to the server as a GraphQL variable,
|
|
5
|
+
* never spliced into the query text, so a shopper's name, address or search
|
|
6
|
+
* containing a quote or backslash can't change what the query does.
|
|
7
|
+
*
|
|
8
|
+
* The query text used to be built with template strings, which quietly
|
|
9
|
+
* accepted loose types: a quantity read from a form ("2") became the number
|
|
10
|
+
* 2, a numeric product ID (4) became the text "4". Variables are strictly
|
|
11
|
+
* typed, so the converters below turn each value into what the old query
|
|
12
|
+
* text sent, and existing storefront code keeps working.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* An SDK error: a real Error (so stack traces and `instanceof Error` still
|
|
17
|
+
* work) carrying `code` and any extra fields (`details`, `status`). The
|
|
18
|
+
* message is made enumerable so logging or JSON-encoding the error shows it.
|
|
19
|
+
*/
|
|
20
|
+
export function sdkError(message, code, extra = {}) {
|
|
21
|
+
const error = new Error(message);
|
|
22
|
+
Object.defineProperty(error, 'message', { value: message, enumerable: true, writable: true, configurable: true });
|
|
23
|
+
error.code = code ?? null;
|
|
24
|
+
Object.assign(error, extra);
|
|
25
|
+
return error;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** What a method that needs a field selection resolves to without one. */
|
|
29
|
+
export function missingData() {
|
|
30
|
+
return { error: sdkError('data is required: the fields to return', 'MISSING_DATA'), response: null };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** What a method resolves to when an argument can't be used. */
|
|
34
|
+
export function invalidInput(message) {
|
|
35
|
+
return { error: sdkError(message, 'INVALID_INPUT'), response: null };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** True when `data` is a usable field selection: a non-empty string. */
|
|
39
|
+
export function hasSelection(selection) {
|
|
40
|
+
return typeof selection === 'string' && selection.trim() !== '';
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Text for a String argument. null and undefined pass through. */
|
|
44
|
+
export function text(value) {
|
|
45
|
+
return value == null ? value : String(value);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** A number for an Int or Float argument. null and undefined pass through. */
|
|
49
|
+
export function number(value) {
|
|
50
|
+
return value == null ? value : Number(value);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** A Boolean argument: true, or the text "true". null and undefined pass through. */
|
|
54
|
+
export function bool(value) {
|
|
55
|
+
return value == null ? value : String(value) === 'true';
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** A list of Strings. */
|
|
59
|
+
export function textList(values) {
|
|
60
|
+
return Array.isArray(values) ? values.map(text) : values;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* An input object whose fields are all text. snake_case keys become
|
|
65
|
+
* camelCase, and fields set to null or undefined are left out, as before.
|
|
66
|
+
*/
|
|
67
|
+
export function textInput(fields) {
|
|
68
|
+
return Object.fromEntries(
|
|
69
|
+
Object.entries(fields)
|
|
70
|
+
.filter(([, value]) => value != null)
|
|
71
|
+
.map(([key, value]) => [
|
|
72
|
+
key.replace(/_([a-z0-9])/g, (_, char) => char.toUpperCase()),
|
|
73
|
+
typeof value === 'object' ? value : text(value),
|
|
74
|
+
]),
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* One operation on one root field, as `{ query, variables }` for
|
|
80
|
+
* `sendRequest`. `args` maps each argument name to [GraphQL type, value]. An
|
|
81
|
+
* argument whose value is undefined is left out of the query, as the old
|
|
82
|
+
* queries did, so the server's own default for it applies. `selection` is the
|
|
83
|
+
* caller's field selection (the `data` string), for fields that return an
|
|
84
|
+
* object.
|
|
85
|
+
*
|
|
86
|
+
* Passing a fourth argument at all means the field returns an object and
|
|
87
|
+
* needs a selection: if it's missing or empty, the result carries the
|
|
88
|
+
* MISSING_DATA error instead of a query, and `sendRequest` resolves to that
|
|
89
|
+
* error without calling the API. Fields that return a plain value (a String,
|
|
90
|
+
* a Boolean) are called with three arguments.
|
|
91
|
+
*/
|
|
92
|
+
export function operation(kind, field, args, ...rest) {
|
|
93
|
+
const needsSelection = rest.length > 0;
|
|
94
|
+
const selection = rest[0];
|
|
95
|
+
if (needsSelection && !hasSelection(selection)) {
|
|
96
|
+
return { error: missingData().error };
|
|
97
|
+
}
|
|
98
|
+
const given = Object.entries(args).filter(([, [, value]]) => value !== undefined);
|
|
99
|
+
const declarations = given.map(([name, [type]]) => `$${name}: ${type}`).join(', ');
|
|
100
|
+
const argumentList = given.map(([name]) => `${name}: $${name}`).join(', ');
|
|
101
|
+
const variables = Object.fromEntries(given.map(([name, [, value]]) => [name, value]));
|
|
102
|
+
|
|
103
|
+
const query = `${kind}${given.length ? ` (${declarations})` : ''} {
|
|
104
|
+
${field}${given.length ? `(${argumentList})` : ''}${needsSelection ? ` { ${selection.trim()} }` : ''}
|
|
105
|
+
}`;
|
|
106
|
+
return { query, variables };
|
|
107
|
+
}
|
package/index.js
ADDED
package/locations.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
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 Locations {
|
|
7
|
+
constructor(client) {
|
|
8
|
+
this.client = client;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async storeLocations({ data, limit, offset }) {
|
|
12
|
+
const args = { limit: ['Int', number(limit)], offset: ['Int', number(offset)] };
|
|
13
|
+
return this.client.sendRequest(operation('query', 'getAllStoreLocations', args, data), 'getAllStoreLocations');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async storeLocationsByCountry({ country, data, limit, offset }) {
|
|
17
|
+
const args = {
|
|
18
|
+
country: ['String!', text(country)],
|
|
19
|
+
limit: ['Int', number(limit)],
|
|
20
|
+
offset: ['Int', number(offset)],
|
|
21
|
+
};
|
|
22
|
+
return this.client.sendRequest(operation('query', 'getStoreLocationsByCountry', args, data), 'getStoreLocationsByCountry');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async storeLocationsByCity({ city, data, limit, offset }) {
|
|
26
|
+
const args = {
|
|
27
|
+
city: ['String!', text(city)],
|
|
28
|
+
limit: ['Int', number(limit)],
|
|
29
|
+
offset: ['Int', number(offset)],
|
|
30
|
+
};
|
|
31
|
+
return this.client.sendRequest(operation('query', 'getStoreLocationsByCity', args, data), 'getStoreLocationsByCity');
|
|
32
|
+
}
|
|
33
|
+
}
|
package/loyalty.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { operation, text, number } from './graphql.js';
|
|
2
|
+
|
|
3
|
+
// Every value goes in as a GraphQL variable, never spliced into the query
|
|
4
|
+
// text.
|
|
5
|
+
|
|
6
|
+
export class Loyalty {
|
|
7
|
+
constructor(client) {
|
|
8
|
+
this.client = client;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async getStatus({ email, fingerprint, data }) {
|
|
12
|
+
const args = { email: ['String!', text(email)], fingerprint: ['String!', text(fingerprint)] };
|
|
13
|
+
return this.client.sendRequest(operation('query', 'getCustomerLoyaltyStatus', args, data), 'getCustomerLoyaltyStatus');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// The store's program rules and tiers — no customer, so no sign-in needed.
|
|
17
|
+
async getProgram({ data }) {
|
|
18
|
+
return this.client.sendRequest(operation('query', 'getLoyaltyProgram', {}, data), 'getLoyaltyProgram');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// A signed-in customer's own points history, newest first. limit
|
|
22
|
+
// defaults to 20 and offset to 0; numbers or numeric strings both work.
|
|
23
|
+
async getHistory({ email, fingerprint, limit, offset, data }) {
|
|
24
|
+
const args = {
|
|
25
|
+
email: ['String!', text(email)],
|
|
26
|
+
fingerprint: ['String!', text(fingerprint)],
|
|
27
|
+
limit: ['Int!', number(limit ?? 20)],
|
|
28
|
+
offset: ['Int!', number(offset ?? 0)],
|
|
29
|
+
};
|
|
30
|
+
return this.client.sendRequest(operation('query', 'getCustomerLoyaltyHistory', args, data), 'getCustomerLoyaltyHistory');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async getAvailableRewards({ email, fingerprint, cartID, data }) {
|
|
34
|
+
const args = {
|
|
35
|
+
email: ['String!', text(email)],
|
|
36
|
+
fingerprint: ['String!', text(fingerprint)],
|
|
37
|
+
cartID: ['String!', text(cartID)],
|
|
38
|
+
};
|
|
39
|
+
return this.client.sendRequest(operation('query', 'getAvailableRewards', args, data), 'getAvailableRewards');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async previewRedemption({ email, fingerprint, cartID, points, data }) {
|
|
43
|
+
const args = {
|
|
44
|
+
email: ['String!', text(email)],
|
|
45
|
+
fingerprint: ['String!', text(fingerprint)],
|
|
46
|
+
cartID: ['String!', text(cartID)],
|
|
47
|
+
points: ['Int!', number(points)],
|
|
48
|
+
};
|
|
49
|
+
return this.client.sendRequest(operation('query', 'previewLoyaltyRedemption', args, data), 'previewLoyaltyRedemption');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async applyRedemption({ email, fingerprint, cartID, points, data }) {
|
|
53
|
+
const input = {
|
|
54
|
+
email: text(email),
|
|
55
|
+
fingerprint: text(fingerprint),
|
|
56
|
+
cartID: text(cartID),
|
|
57
|
+
points: number(points),
|
|
58
|
+
};
|
|
59
|
+
const args = { input: ['ApplyLoyaltyRedemptionInput!', input] };
|
|
60
|
+
return this.client.sendRequest(operation('mutation', 'applyLoyaltyRedemption', args, data), 'applyLoyaltyRedemption');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async removeRedemption({ email, fingerprint, cartID, data }) {
|
|
64
|
+
const input = {
|
|
65
|
+
email: text(email),
|
|
66
|
+
fingerprint: text(fingerprint),
|
|
67
|
+
cartID: text(cartID),
|
|
68
|
+
};
|
|
69
|
+
const args = { input: ['RemoveLoyaltyRedemptionInput!', input] };
|
|
70
|
+
return this.client.sendRequest(operation('mutation', 'removeLoyaltyRedemption', args, data), 'removeLoyaltyRedemption');
|
|
71
|
+
}
|
|
72
|
+
}
|
package/media.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { operation, text, number, textList } 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 Media {
|
|
7
|
+
constructor(client) {
|
|
8
|
+
this.client = client;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async image({ imageID }) {
|
|
12
|
+
const args = { imageID: ['String!', text(imageID)] };
|
|
13
|
+
return this.client.sendRequest(operation('query', 'getImageByID', args), 'getImageByID');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async video({ videoID }) {
|
|
17
|
+
const args = { videoID: ['String!', text(videoID)] };
|
|
18
|
+
return this.client.sendRequest(operation('query', 'getVideoByID', args), 'getVideoByID');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async audio({ audioID }) {
|
|
22
|
+
const args = { audioID: ['String!', text(audioID)] };
|
|
23
|
+
return this.client.sendRequest(operation('query', 'getAudioByID', args), 'getAudioByID');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async productImages({ productID, limit, offset }) {
|
|
27
|
+
const args = {
|
|
28
|
+
productID: ['String!', text(productID)],
|
|
29
|
+
limit: ['Int', number(limit)],
|
|
30
|
+
offset: ['Int', number(offset)],
|
|
31
|
+
};
|
|
32
|
+
return this.client.sendRequest(operation('query', 'getAllImagesOfProduct', args), 'getAllImagesOfProduct');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async productVideos({ productID, limit, offset }) {
|
|
36
|
+
const args = {
|
|
37
|
+
productID: ['String!', text(productID)],
|
|
38
|
+
limit: ['Int', number(limit)],
|
|
39
|
+
offset: ['Int', number(offset)],
|
|
40
|
+
};
|
|
41
|
+
return this.client.sendRequest(operation('query', 'getAllVideosOfProduct', args), 'getAllVideosOfProduct');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async productAudio({ productID, limit, offset }) {
|
|
45
|
+
const args = {
|
|
46
|
+
productID: ['String!', text(productID)],
|
|
47
|
+
limit: ['Int', number(limit)],
|
|
48
|
+
offset: ['Int', number(offset)],
|
|
49
|
+
};
|
|
50
|
+
return this.client.sendRequest(operation('query', 'getAllAudioOfProduct', args), 'getAllAudioOfProduct');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async imagesByID({ imageIDs }) {
|
|
54
|
+
const args = { imageIDs: ['[String!]!', textList(imageIDs)] };
|
|
55
|
+
return this.client.sendRequest(operation('query', 'getListOfImagesByIDs', args), 'getListOfImagesByIDs');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async videosByID({ videoIDs }) {
|
|
59
|
+
const args = { videoIDs: ['[String!]!', textList(videoIDs)] };
|
|
60
|
+
return this.client.sendRequest(operation('query', 'getListOfVideosByIDs', args), 'getListOfVideosByIDs');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async audioByID({ audioIDs }) {
|
|
64
|
+
const args = { audioIDs: ['[String!]!', textList(audioIDs)] };
|
|
65
|
+
return this.client.sendRequest(operation('query', 'getListOfAudioByIDs', args), 'getListOfAudioByIDs');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async globalMediaByID({ mediaIDs }) {
|
|
69
|
+
const args = { mediaIDs: ['[String!]!', textList(mediaIDs)] };
|
|
70
|
+
const selection = `
|
|
71
|
+
mediaID
|
|
72
|
+
mediaType
|
|
73
|
+
mediaURL
|
|
74
|
+
`;
|
|
75
|
+
return this.client.sendRequest(operation('query', 'getListOfGlobalMediaByID', args, selection), 'getListOfGlobalMediaByID');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async allGlobalMedia({ limit, offset }) {
|
|
79
|
+
const args = { limit: ['Int', number(limit)], offset: ['Int', number(offset)] };
|
|
80
|
+
const selection = `
|
|
81
|
+
mediaID
|
|
82
|
+
mediaType
|
|
83
|
+
mediaURL
|
|
84
|
+
`;
|
|
85
|
+
return this.client.sendRequest(operation('query', 'getAllGlobalMediaOfStore', args, selection), 'getAllGlobalMediaOfStore');
|
|
86
|
+
}
|
|
87
|
+
}
|
package/orders.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
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, paymentAmount, paymentCurrency, 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
|
+
//
|
|
16
|
+
// paymentAmount / paymentCurrency: what the payment charged, as Stripe
|
|
17
|
+
// reports it on the PaymentIntent (`amount` in minor units, `currency`).
|
|
18
|
+
// Optional. When given, the order is refused unless they match the cart's
|
|
19
|
+
// charge (cart.prepareCheckoutPayment), and the currency is recorded as
|
|
20
|
+
// the one the shopper paid in.
|
|
21
|
+
const args = {
|
|
22
|
+
cartID: ['String!', text(cartID)],
|
|
23
|
+
stripePaymentIntentID: ['String', text(stripePaymentIntentID || undefined)],
|
|
24
|
+
paymentAmount: ['Int', paymentAmount == null ? undefined : Math.round(Number(paymentAmount))],
|
|
25
|
+
paymentCurrency: ['String', paymentCurrency ? text(paymentCurrency).toUpperCase() : undefined],
|
|
26
|
+
};
|
|
27
|
+
return this.client.sendRequest(operation('mutation', 'createOrder', args, data), 'createOrder');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Look up one purchase by the customer's email + purchase ID — what an
|
|
32
|
+
* order-status / tracking page calls. Select `orders { shipments { … } }`
|
|
33
|
+
* for live per-box tracking (status, location, ETA, scan history).
|
|
34
|
+
*
|
|
35
|
+
* email and purchase_id are customer-typed, so they go as GraphQL
|
|
36
|
+
* variables, never spliced into the query text.
|
|
37
|
+
*/
|
|
38
|
+
async getOrder({ email, purchase_id, data }) {
|
|
39
|
+
const args = {
|
|
40
|
+
email: ['String!', text(email)],
|
|
41
|
+
purchaseId: ['String!', text(purchase_id)],
|
|
42
|
+
};
|
|
43
|
+
return this.client.sendRequest(operation('query', 'retrieveOrder', args, data), 'retrieveOrder');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async getAllOrders({ email, data }) {
|
|
47
|
+
const args = { email: ['String!', text(email)] };
|
|
48
|
+
return this.client.sendRequest(operation('query', 'retrieveAllOrders', args, data), 'retrieveAllOrders');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The store's exchange settings (whether exchanges are on, the
|
|
53
|
+
* acceptance window, how many suggestions are offered, …). Readable with
|
|
54
|
+
* storefront keys. Fields: enabled, autoApproveReturnOnAccept,
|
|
55
|
+
* underPaymentMode, requireReturnReceipt, acceptanceWindowHours,
|
|
56
|
+
* maxSuggestions.
|
|
57
|
+
*/
|
|
58
|
+
async exchangeSettings({ data }) {
|
|
59
|
+
return this.client.sendRequest(operation('query', 'getExchangeSettings', {}, data), 'getExchangeSettings');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async getReturns({ customer_email, data, limit, offset }) {
|
|
63
|
+
const args = {
|
|
64
|
+
customerEmail: ['String!', text(customer_email)],
|
|
65
|
+
limit: ['Int', number(limit)],
|
|
66
|
+
offset: ['Int', number(offset)],
|
|
67
|
+
};
|
|
68
|
+
return this.client.sendRequest(operation('query', 'getCustomerReturns', args, data), 'getCustomerReturns');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* One return by its ID — what a store's returns page shows. The return
|
|
73
|
+
* emails link to that page as `?return=<id>`. Resolves `response` to null
|
|
74
|
+
* when no return with that ID belongs to the store.
|
|
75
|
+
*
|
|
76
|
+
* `label.labelURL` is normally on the store's own domain
|
|
77
|
+
* (`/_prezense/return-label/<id>`, served by the platform, not the app);
|
|
78
|
+
* it can be the shipping provider's link while the platform's copy isn't
|
|
79
|
+
* there yet. Link to it as given.
|
|
80
|
+
*
|
|
81
|
+
* @param {Object} args
|
|
82
|
+
* @param {string} args.return_id
|
|
83
|
+
* @param {string} args.data GraphQL selection set
|
|
84
|
+
*/
|
|
85
|
+
async getReturn({ return_id, data }) {
|
|
86
|
+
const args = { returnID: ['String!', text(return_id)] };
|
|
87
|
+
return this.client.sendRequest(operation('query', 'getReturn', args, data), 'getReturn');
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async createReturn({ order_id, customer_email, products_to_return, reason_for_return, data }) {
|
|
91
|
+
// reasonForReturn is optional; the reason is sent as typed
|
|
92
|
+
const args = {
|
|
93
|
+
orderId: ['String!', text(order_id)],
|
|
94
|
+
customerEmail: ['String!', text(customer_email)],
|
|
95
|
+
productsToReturn: ['[String!]!', textList(products_to_return)],
|
|
96
|
+
reasonForReturn: ['String', text(reason_for_return)],
|
|
97
|
+
};
|
|
98
|
+
return this.client.sendRequest(operation('mutation', 'createReturnRequest', args, data), 'createReturnRequest');
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// ================================================================
|
|
102
|
+
// EXCHANGE MANAGEMENT
|
|
103
|
+
// ================================================================
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Submit a return request and synchronously receive exchange suggestions.
|
|
107
|
+
*
|
|
108
|
+
* The backend routes to deterministic sibling-variant lookups, personalized
|
|
109
|
+
* recommendations, or a Claude Haiku agentic loop based on reasonCategory
|
|
110
|
+
* and the free-text detail. Suggestions come back in the same response —
|
|
111
|
+
* no follow-up call required.
|
|
112
|
+
*
|
|
113
|
+
* @param {Object} args
|
|
114
|
+
* @param {string} args.order_id
|
|
115
|
+
* @param {string} args.customer_email
|
|
116
|
+
* @param {string[]} args.products_to_return variantIDs
|
|
117
|
+
* @param {string} [args.reason_category] one of: size, fit, color, quality,
|
|
118
|
+
* damaged, wrong_item, changed_mind,
|
|
119
|
+
* late_arrival, other
|
|
120
|
+
* @param {string} [args.reason_for_return] free-text detail
|
|
121
|
+
* @param {number} [args.max_suggestions] 1..10, defaults to 5
|
|
122
|
+
* @param {string} args.data GraphQL selection set
|
|
123
|
+
*/
|
|
124
|
+
async submitReturnRequest({
|
|
125
|
+
order_id,
|
|
126
|
+
customer_email,
|
|
127
|
+
products_to_return,
|
|
128
|
+
reason_category,
|
|
129
|
+
reason_for_return,
|
|
130
|
+
max_suggestions,
|
|
131
|
+
data,
|
|
132
|
+
}) {
|
|
133
|
+
const args = {
|
|
134
|
+
orderId: ['String!', text(order_id)],
|
|
135
|
+
customerEmail: ['String!', text(customer_email)],
|
|
136
|
+
productsToReturn: ['[String!]!', textList(products_to_return)],
|
|
137
|
+
reasonCategory: ['ReturnReasonCategory', reason_category ? text(reason_category).toUpperCase() : undefined],
|
|
138
|
+
reasonForReturn: ['String', text(reason_for_return)],
|
|
139
|
+
maxSuggestions: ['Int', max_suggestions ? Number(max_suggestions) : undefined],
|
|
140
|
+
};
|
|
141
|
+
return this.client.sendRequest(operation('mutation', 'submitReturnRequest', args, data), 'submitReturnRequest');
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async acceptExchangeSuggestion({ suggestion_id, data }) {
|
|
145
|
+
const args = { suggestionID: ['String!', text(suggestion_id)] };
|
|
146
|
+
return this.client.sendRequest(operation('mutation', 'acceptExchangeSuggestion', args, data), 'acceptExchangeSuggestion');
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async cancelExchange({ exchange_id, data }) {
|
|
150
|
+
const args = { exchangeID: ['String!', text(exchange_id)] };
|
|
151
|
+
return this.client.sendRequest(operation('mutation', 'cancelExchange', args, data), 'cancelExchange');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async declineExchangeSuggestions({ return_id, data }) {
|
|
155
|
+
const args = { returnID: ['String!', text(return_id)] };
|
|
156
|
+
return this.client.sendRequest(operation('mutation', 'declineExchangeSuggestions', args, data), 'declineExchangeSuggestions');
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async getExchanges({ limit, offset, status, data }) {
|
|
160
|
+
const args = {
|
|
161
|
+
limit: ['Int', number(limit)],
|
|
162
|
+
offset: ['Int', number(offset)],
|
|
163
|
+
status: ['ExchangeStatus', status ? text(status).toUpperCase() : undefined],
|
|
164
|
+
};
|
|
165
|
+
return this.client.sendRequest(operation('query', 'getExchanges', args, data), 'getExchanges');
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
async getExchangeByID({ exchange_id, data }) {
|
|
169
|
+
// The server's field is getExchangeById; the response is the same.
|
|
170
|
+
const args = { exchangeID: ['String!', text(exchange_id)] };
|
|
171
|
+
return this.client.sendRequest(operation('query', 'getExchangeById', args, data), 'getExchangeById');
|
|
172
|
+
}
|
|
173
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "prezense",
|
|
3
|
+
"version": "1.4.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"prezense": "./cli.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "node --test test/offline.test.js test/checkout-payment.test.js test/post-purchase-charge.test.js"
|
|
11
|
+
},
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">=18"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"node-fetch": "^3.3.2",
|
|
17
|
+
"ua-parser-js": "^2.0.3"
|
|
18
|
+
}
|
|
19
|
+
}
|
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
|
+
}
|