@moonbase.sh/storefront-api 0.4.25 → 0.4.26
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/dist/index.cjs +79 -6
- package/dist/index.d.cts +6610 -768
- package/dist/index.d.ts +6610 -768
- package/dist/index.js +73 -3
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -28,24 +28,27 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
28
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
29
|
|
|
30
30
|
// src/index.ts
|
|
31
|
-
var
|
|
32
|
-
__export(
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
33
|
ActivationMethod: () => ActivationMethod,
|
|
34
34
|
ActivationRequestFulfillmentType: () => ActivationRequestFulfillmentType,
|
|
35
35
|
ActivationRequestStatus: () => ActivationRequestStatus,
|
|
36
36
|
ActivationStatus: () => ActivationStatus,
|
|
37
37
|
ConsoleLogger: () => ConsoleLogger,
|
|
38
38
|
CycleLength: () => CycleLength,
|
|
39
|
+
DiscountUtils: () => DiscountUtils,
|
|
39
40
|
InMemoryStore: () => InMemoryStore,
|
|
40
41
|
LicenseStatus: () => LicenseStatus,
|
|
41
42
|
LocalStorageStore: () => LocalStorageStore,
|
|
42
43
|
LogLevel: () => LogLevel,
|
|
44
|
+
MoneyCollectionUtils: () => MoneyCollectionUtils,
|
|
43
45
|
MoonbaseApi: () => MoonbaseApi,
|
|
44
46
|
MoonbaseClient: () => MoonbaseClient,
|
|
45
47
|
MoonbaseError: () => MoonbaseError,
|
|
46
48
|
NotAuthenticatedError: () => NotAuthenticatedError,
|
|
47
49
|
NotAuthorizedError: () => NotAuthorizedError,
|
|
48
50
|
NotFoundError: () => NotFoundError,
|
|
51
|
+
OfferUtils: () => OfferUtils,
|
|
49
52
|
OrderStatus: () => OrderStatus,
|
|
50
53
|
Platform: () => Platform,
|
|
51
54
|
SubscriptionStatus: () => SubscriptionStatus,
|
|
@@ -54,7 +57,7 @@ __export(src_exports, {
|
|
|
54
57
|
schemas: () => schemas_exports3,
|
|
55
58
|
utmToObject: () => utmToObject
|
|
56
59
|
});
|
|
57
|
-
module.exports = __toCommonJS(
|
|
60
|
+
module.exports = __toCommonJS(index_exports);
|
|
58
61
|
|
|
59
62
|
// src/activationRequests/endpoints.ts
|
|
60
63
|
var import_zod5 = require("zod");
|
|
@@ -216,7 +219,7 @@ var storefrontProductSchema = import_zod3.z.object({
|
|
|
216
219
|
downloads: downloadSchema.array().optional(),
|
|
217
220
|
defaultVariation: pricingVariationSchema.optional(),
|
|
218
221
|
variations: pricingVariationSchema.array().optional(),
|
|
219
|
-
type: import_zod3.z.
|
|
222
|
+
type: import_zod3.z.string().optional().default("Product").pipe(import_zod3.z.literal("Product"))
|
|
220
223
|
});
|
|
221
224
|
var storefrontBundleSchema = import_zod3.z.object({
|
|
222
225
|
id: import_zod3.z.string(),
|
|
@@ -231,12 +234,27 @@ var storefrontBundleSchema = import_zod3.z.object({
|
|
|
231
234
|
})).array(),
|
|
232
235
|
defaultVariation: pricingVariationSchema.optional(),
|
|
233
236
|
variations: pricingVariationSchema.array().optional(),
|
|
234
|
-
type: import_zod3.z.
|
|
237
|
+
type: import_zod3.z.string().optional().default("Bundle").pipe(import_zod3.z.literal("Bundle"))
|
|
238
|
+
});
|
|
239
|
+
var cartContainsItemsConditionSchema = import_zod3.z.object({
|
|
240
|
+
type: import_zod3.z.literal("CartContainsItems"),
|
|
241
|
+
minimumItems: import_zod3.z.number(),
|
|
242
|
+
relevantItemVariations: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.string().array())
|
|
243
|
+
});
|
|
244
|
+
var offerConditionSchema = import_zod3.z.discriminatedUnion("type", [cartContainsItemsConditionSchema]);
|
|
245
|
+
var storefrontOfferSchema = import_zod3.z.object({
|
|
246
|
+
id: import_zod3.z.string(),
|
|
247
|
+
target: import_zod3.z.union([storefrontProductSchema, storefrontBundleSchema]),
|
|
248
|
+
targetVariations: import_zod3.z.string().array(),
|
|
249
|
+
condition: offerConditionSchema,
|
|
250
|
+
discount: discountSchema
|
|
235
251
|
});
|
|
236
252
|
var storefrontSchema = import_zod3.z.object({
|
|
237
253
|
suggestedCurrency: import_zod3.z.string(),
|
|
238
254
|
products: storefrontProductSchema.array(),
|
|
239
|
-
bundles: storefrontBundleSchema.array()
|
|
255
|
+
bundles: storefrontBundleSchema.array(),
|
|
256
|
+
// Offers need to be optional since we may still have old, cached representations in browsers
|
|
257
|
+
offers: storefrontOfferSchema.array().optional()
|
|
240
258
|
});
|
|
241
259
|
|
|
242
260
|
// src/activationRequests/models.ts
|
|
@@ -721,6 +739,7 @@ var openProductLineItem = import_zod11.z.object({
|
|
|
721
739
|
productId: import_zod11.z.string(),
|
|
722
740
|
quantity: import_zod11.z.number(),
|
|
723
741
|
variationId: import_zod11.z.string(),
|
|
742
|
+
offerId: import_zod11.z.string().optional(),
|
|
724
743
|
isDefaultVariation: import_zod11.z.boolean().nullish(),
|
|
725
744
|
isSubcriptionPayment: import_zod11.z.boolean().nullish(),
|
|
726
745
|
price: priceCollectionSchema.optional(),
|
|
@@ -744,7 +763,9 @@ var openBundleLineItem = import_zod11.z.object({
|
|
|
744
763
|
bundleId: import_zod11.z.string(),
|
|
745
764
|
quantity: import_zod11.z.number(),
|
|
746
765
|
variationId: import_zod11.z.string(),
|
|
766
|
+
offerId: import_zod11.z.string().optional(),
|
|
747
767
|
partial: import_zod11.z.boolean().optional(),
|
|
768
|
+
replaced: import_zod11.z.string().array().optional(),
|
|
748
769
|
isDefaultVariation: import_zod11.z.boolean().nullish(),
|
|
749
770
|
isSubcriptionPayment: import_zod11.z.boolean().nullish(),
|
|
750
771
|
price: priceCollectionSchema.optional(),
|
|
@@ -1304,6 +1325,55 @@ __export(schemas_exports3, {
|
|
|
1304
1325
|
orders: () => schemas_exports2
|
|
1305
1326
|
});
|
|
1306
1327
|
|
|
1328
|
+
// src/utils/discount.ts
|
|
1329
|
+
var DiscountUtils = class {
|
|
1330
|
+
static apply(discount, price) {
|
|
1331
|
+
switch (discount.type) {
|
|
1332
|
+
case "PercentageOffDiscount":
|
|
1333
|
+
return Object.fromEntries(Object.entries(price).map(([currency, amount]) => [currency, amount * discount.percentage]));
|
|
1334
|
+
case "FlatAmountOffDiscount":
|
|
1335
|
+
return Object.fromEntries(Object.entries(price).map(([currency, amount]) => {
|
|
1336
|
+
var _a, _b;
|
|
1337
|
+
return [currency, Math.min(amount, (_b = (_a = discount.total) == null ? void 0 : _a[currency]) != null ? _b : 0)];
|
|
1338
|
+
}));
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
};
|
|
1342
|
+
|
|
1343
|
+
// src/utils/money.ts
|
|
1344
|
+
var MoneyCollectionUtils = class {
|
|
1345
|
+
static sum(a, b) {
|
|
1346
|
+
return Object.fromEntries(Object.entries(a).map(([currency, amount]) => [currency, amount + b[currency]]));
|
|
1347
|
+
}
|
|
1348
|
+
static subtract(a, b) {
|
|
1349
|
+
return Object.fromEntries(Object.entries(a).map(([currency, amount]) => [currency, amount - b[currency]]));
|
|
1350
|
+
}
|
|
1351
|
+
};
|
|
1352
|
+
|
|
1353
|
+
// src/utils/offer.ts
|
|
1354
|
+
var OfferUtils = class {
|
|
1355
|
+
static eligible(offer, order) {
|
|
1356
|
+
switch (offer.condition.type) {
|
|
1357
|
+
case "CartContainsItems":
|
|
1358
|
+
const relevantItems = order.items.filter(
|
|
1359
|
+
(i) => i.type === "Product" && offer.condition.relevantItemVariations[`Product/${i.productId}`] && (offer.condition.relevantItemVariations[`Product/${i.productId}`].length === 0 || offer.condition.relevantItemVariations[`Product/${i.productId}`].includes(i.variationId)) || i.type === "Bundle" && offer.condition.relevantItemVariations[`Bundle/${i.bundleId}`] && (offer.condition.relevantItemVariations[`Bundle/${i.bundleId}`].length === 0 || offer.condition.relevantItemVariations[`Bundle/${i.bundleId}`].includes(i.variationId))
|
|
1360
|
+
);
|
|
1361
|
+
console.log("Relevant items for offer:", { offer, relevantItems });
|
|
1362
|
+
return relevantItems.length >= offer.condition.minimumItems;
|
|
1363
|
+
}
|
|
1364
|
+
console.warn("Unsupported offer condition found:", offer.condition);
|
|
1365
|
+
return false;
|
|
1366
|
+
}
|
|
1367
|
+
static applyToVariation(offer, variation) {
|
|
1368
|
+
const discount = DiscountUtils.apply(offer.discount, variation.price);
|
|
1369
|
+
return {
|
|
1370
|
+
...variation,
|
|
1371
|
+
price: MoneyCollectionUtils.subtract(variation.price, discount),
|
|
1372
|
+
hasDiscount: Object.values(discount).some((v) => v > 0) || variation.hasDiscount
|
|
1373
|
+
};
|
|
1374
|
+
}
|
|
1375
|
+
};
|
|
1376
|
+
|
|
1307
1377
|
// src/index.ts
|
|
1308
1378
|
var MoonbaseClient = class {
|
|
1309
1379
|
constructor(configuration) {
|
|
@@ -1330,16 +1400,19 @@ var MoonbaseClient = class {
|
|
|
1330
1400
|
ActivationStatus,
|
|
1331
1401
|
ConsoleLogger,
|
|
1332
1402
|
CycleLength,
|
|
1403
|
+
DiscountUtils,
|
|
1333
1404
|
InMemoryStore,
|
|
1334
1405
|
LicenseStatus,
|
|
1335
1406
|
LocalStorageStore,
|
|
1336
1407
|
LogLevel,
|
|
1408
|
+
MoneyCollectionUtils,
|
|
1337
1409
|
MoonbaseApi,
|
|
1338
1410
|
MoonbaseClient,
|
|
1339
1411
|
MoonbaseError,
|
|
1340
1412
|
NotAuthenticatedError,
|
|
1341
1413
|
NotAuthorizedError,
|
|
1342
1414
|
NotFoundError,
|
|
1415
|
+
OfferUtils,
|
|
1343
1416
|
OrderStatus,
|
|
1344
1417
|
Platform,
|
|
1345
1418
|
SubscriptionStatus,
|