@moonbase.sh/storefront-api 0.4.24 → 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.js CHANGED
@@ -164,7 +164,7 @@ var storefrontProductSchema = z3.object({
164
164
  downloads: downloadSchema.array().optional(),
165
165
  defaultVariation: pricingVariationSchema.optional(),
166
166
  variations: pricingVariationSchema.array().optional(),
167
- type: z3.void().transform(() => "product").pipe(z3.literal("product"))
167
+ type: z3.string().optional().default("Product").pipe(z3.literal("Product"))
168
168
  });
169
169
  var storefrontBundleSchema = z3.object({
170
170
  id: z3.string(),
@@ -179,12 +179,27 @@ var storefrontBundleSchema = z3.object({
179
179
  })).array(),
180
180
  defaultVariation: pricingVariationSchema.optional(),
181
181
  variations: pricingVariationSchema.array().optional(),
182
- type: z3.void().transform(() => "bundle").pipe(z3.literal("bundle"))
182
+ type: z3.string().optional().default("Bundle").pipe(z3.literal("Bundle"))
183
+ });
184
+ var cartContainsItemsConditionSchema = z3.object({
185
+ type: z3.literal("CartContainsItems"),
186
+ minimumItems: z3.number(),
187
+ relevantItemVariations: z3.record(z3.string(), z3.string().array())
188
+ });
189
+ var offerConditionSchema = z3.discriminatedUnion("type", [cartContainsItemsConditionSchema]);
190
+ var storefrontOfferSchema = z3.object({
191
+ id: z3.string(),
192
+ target: z3.union([storefrontProductSchema, storefrontBundleSchema]),
193
+ targetVariations: z3.string().array(),
194
+ condition: offerConditionSchema,
195
+ discount: discountSchema
183
196
  });
184
197
  var storefrontSchema = z3.object({
185
198
  suggestedCurrency: z3.string(),
186
199
  products: storefrontProductSchema.array(),
187
- bundles: storefrontBundleSchema.array()
200
+ bundles: storefrontBundleSchema.array(),
201
+ // Offers need to be optional since we may still have old, cached representations in browsers
202
+ offers: storefrontOfferSchema.array().optional()
188
203
  });
189
204
 
190
205
  // src/activationRequests/models.ts
@@ -669,6 +684,7 @@ var openProductLineItem = z11.object({
669
684
  productId: z11.string(),
670
685
  quantity: z11.number(),
671
686
  variationId: z11.string(),
687
+ offerId: z11.string().optional(),
672
688
  isDefaultVariation: z11.boolean().nullish(),
673
689
  isSubcriptionPayment: z11.boolean().nullish(),
674
690
  price: priceCollectionSchema.optional(),
@@ -692,7 +708,9 @@ var openBundleLineItem = z11.object({
692
708
  bundleId: z11.string(),
693
709
  quantity: z11.number(),
694
710
  variationId: z11.string(),
711
+ offerId: z11.string().optional(),
695
712
  partial: z11.boolean().optional(),
713
+ replaced: z11.string().array().optional(),
696
714
  isDefaultVariation: z11.boolean().nullish(),
697
715
  isSubcriptionPayment: z11.boolean().nullish(),
698
716
  price: priceCollectionSchema.optional(),
@@ -1252,6 +1270,55 @@ __export(schemas_exports3, {
1252
1270
  orders: () => schemas_exports2
1253
1271
  });
1254
1272
 
1273
+ // src/utils/discount.ts
1274
+ var DiscountUtils = class {
1275
+ static apply(discount, price) {
1276
+ switch (discount.type) {
1277
+ case "PercentageOffDiscount":
1278
+ return Object.fromEntries(Object.entries(price).map(([currency, amount]) => [currency, amount * discount.percentage]));
1279
+ case "FlatAmountOffDiscount":
1280
+ return Object.fromEntries(Object.entries(price).map(([currency, amount]) => {
1281
+ var _a, _b;
1282
+ return [currency, Math.min(amount, (_b = (_a = discount.total) == null ? void 0 : _a[currency]) != null ? _b : 0)];
1283
+ }));
1284
+ }
1285
+ }
1286
+ };
1287
+
1288
+ // src/utils/money.ts
1289
+ var MoneyCollectionUtils = class {
1290
+ static sum(a, b) {
1291
+ return Object.fromEntries(Object.entries(a).map(([currency, amount]) => [currency, amount + b[currency]]));
1292
+ }
1293
+ static subtract(a, b) {
1294
+ return Object.fromEntries(Object.entries(a).map(([currency, amount]) => [currency, amount - b[currency]]));
1295
+ }
1296
+ };
1297
+
1298
+ // src/utils/offer.ts
1299
+ var OfferUtils = class {
1300
+ static eligible(offer, order) {
1301
+ switch (offer.condition.type) {
1302
+ case "CartContainsItems":
1303
+ const relevantItems = order.items.filter(
1304
+ (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))
1305
+ );
1306
+ console.log("Relevant items for offer:", { offer, relevantItems });
1307
+ return relevantItems.length >= offer.condition.minimumItems;
1308
+ }
1309
+ console.warn("Unsupported offer condition found:", offer.condition);
1310
+ return false;
1311
+ }
1312
+ static applyToVariation(offer, variation) {
1313
+ const discount = DiscountUtils.apply(offer.discount, variation.price);
1314
+ return {
1315
+ ...variation,
1316
+ price: MoneyCollectionUtils.subtract(variation.price, discount),
1317
+ hasDiscount: Object.values(discount).some((v) => v > 0) || variation.hasDiscount
1318
+ };
1319
+ }
1320
+ };
1321
+
1255
1322
  // src/index.ts
1256
1323
  var MoonbaseClient = class {
1257
1324
  constructor(configuration) {
@@ -1277,16 +1344,19 @@ export {
1277
1344
  ActivationStatus,
1278
1345
  ConsoleLogger,
1279
1346
  CycleLength,
1347
+ DiscountUtils,
1280
1348
  InMemoryStore,
1281
1349
  LicenseStatus,
1282
1350
  LocalStorageStore,
1283
1351
  LogLevel,
1352
+ MoneyCollectionUtils,
1284
1353
  MoonbaseApi,
1285
1354
  MoonbaseClient,
1286
1355
  MoonbaseError,
1287
1356
  NotAuthenticatedError,
1288
1357
  NotAuthorizedError,
1289
1358
  NotFoundError,
1359
+ OfferUtils,
1290
1360
  OrderStatus,
1291
1361
  Platform,
1292
1362
  SubscriptionStatus,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@moonbase.sh/storefront-api",
3
3
  "type": "module",
4
- "version": "0.4.24",
4
+ "version": "0.4.26",
5
5
  "description": "Package to let you build storefronts with Moonbase.sh as payment and delivery provider",
6
6
  "author": "Tobias Lønnerød Madsen <m@dsen.tv>",
7
7
  "license": "MIT",
@@ -19,8 +19,8 @@
19
19
  "devDependencies": {
20
20
  "@types/node": "^22.5.4",
21
21
  "rimraf": "^6.0.1",
22
- "tsup": "^8.2.4",
23
- "typescript": "~5.5.4",
22
+ "tsup": "^8.5.0",
23
+ "typescript": "~5.8.3",
24
24
  "vitest": "^2.1.4"
25
25
  },
26
26
  "scripts": {