@moonbase.sh/storefront-api 0.1.78

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 ADDED
@@ -0,0 +1,685 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ ActivationMethod: () => ActivationMethod,
34
+ ActivationStatus: () => ActivationStatus,
35
+ LicenseStatus: () => LicenseStatus,
36
+ MoonbaseClient: () => MoonbaseClient,
37
+ NotAuthenticatedError: () => NotAuthenticatedError,
38
+ NotAuthorizedError: () => NotAuthorizedError,
39
+ NotFoundError: () => NotFoundError,
40
+ OrderStatus: () => OrderStatus,
41
+ Platform: () => Platform
42
+ });
43
+ module.exports = __toCommonJS(src_exports);
44
+
45
+ // src/identity/endpoints.ts
46
+ var import_cross_fetch = __toESM(require("cross-fetch"), 1);
47
+
48
+ // src/utils/problemHandler.ts
49
+ var import_zod = require("zod");
50
+
51
+ // src/utils/errors.ts
52
+ var NotAuthorizedError = class extends Error {
53
+ constructor() {
54
+ super();
55
+ this.name = "NotAuthorizedError";
56
+ }
57
+ };
58
+ var NotAuthenticatedError = class extends Error {
59
+ constructor() {
60
+ super();
61
+ this.name = "NotAuthenticatedError";
62
+ }
63
+ };
64
+ var NotFoundError = class extends Error {
65
+ constructor() {
66
+ super();
67
+ this.name = "NotFoundError";
68
+ }
69
+ };
70
+
71
+ // src/utils/problemHandler.ts
72
+ var problemDetailsSchema = import_zod.z.object({
73
+ type: import_zod.z.string(),
74
+ title: import_zod.z.string(),
75
+ detail: import_zod.z.string().optional(),
76
+ status: import_zod.z.number()
77
+ });
78
+ async function handleResponseProblem(response) {
79
+ if (response.status === 404)
80
+ throw new NotFoundError();
81
+ if (response.status === 401)
82
+ throw new NotAuthenticatedError();
83
+ if (response.status === 403)
84
+ throw new NotAuthorizedError();
85
+ let problemDetails;
86
+ try {
87
+ const json = await response.json();
88
+ problemDetails = problemDetailsSchema.parse(json);
89
+ } catch (e) {
90
+ throw new Error("An unknown problem occurred");
91
+ }
92
+ if (problemDetails.detail)
93
+ throw new Error(problemDetails.detail);
94
+ if (problemDetails.title)
95
+ throw new Error(problemDetails.title);
96
+ throw new Error("An unknown problem occurred");
97
+ }
98
+
99
+ // src/identity/schemas.ts
100
+ var import_zod2 = require("zod");
101
+ var addressSchema = import_zod2.z.object({
102
+ countryCode: import_zod2.z.string(),
103
+ streetAddress1: import_zod2.z.string(),
104
+ streetAddress2: import_zod2.z.string().nullable(),
105
+ locality: import_zod2.z.string().nullable(),
106
+ region: import_zod2.z.string().nullable(),
107
+ postCode: import_zod2.z.string()
108
+ });
109
+ var communicationPreferencesSchema = import_zod2.z.object({
110
+ newsletterOptIn: import_zod2.z.boolean()
111
+ // productUpdatesOptIn: z.boolean(), // TODO: Enable when relevant
112
+ });
113
+ var userSchema = import_zod2.z.object({
114
+ id: import_zod2.z.string(),
115
+ email: import_zod2.z.string(),
116
+ name: import_zod2.z.string(),
117
+ tenantId: import_zod2.z.string(),
118
+ address: addressSchema.optional(),
119
+ communicationPreferences: communicationPreferencesSchema
120
+ });
121
+ var identityUserSchema = userSchema.and(import_zod2.z.object({
122
+ accessToken: import_zod2.z.string(),
123
+ refreshToken: import_zod2.z.string()
124
+ }));
125
+
126
+ // src/identity/endpoints.ts
127
+ var IdentityEndpoints = class {
128
+ constructor(api, tokenStore) {
129
+ this.api = api;
130
+ this.tokenStore = tokenStore;
131
+ }
132
+ async get() {
133
+ const response = await this.api.authenticatedFetch("/api/customer/meta/user");
134
+ return userSchema.parse(response);
135
+ }
136
+ async signIn(email, password) {
137
+ const response = await (0, import_cross_fetch.default)(`${this.api.baseUrl}/api/customer/identity/sign-in?email=${email}&scheme=JWT`, {
138
+ method: "POST",
139
+ headers: {
140
+ "Accept": "application/json",
141
+ "Content-Type": "text/plain",
142
+ "x-mb-cors": "1"
143
+ },
144
+ body: password
145
+ });
146
+ if (response.status >= 400)
147
+ await handleResponseProblem(response);
148
+ const data = await response.json();
149
+ const user = identityUserSchema.parse(data);
150
+ this.tokenStore.setUser(user);
151
+ return user;
152
+ }
153
+ async signUp(name, email, password, address, acceptedPrivacyPolicy, acceptedTermsAndConditions, communicationOptIn) {
154
+ const response = await this.api.fetch(`/api/customer/identity/sign-up?scheme=JWT&communicationOptIn=${communicationOptIn ? "true" : "false"}`, "POST", {
155
+ name,
156
+ email,
157
+ password,
158
+ address,
159
+ acceptedPrivacyPolicy,
160
+ acceptedTermsAndConditions
161
+ });
162
+ const user = identityUserSchema.parse(response);
163
+ this.tokenStore.setUser(user);
164
+ return user;
165
+ }
166
+ async update(name, email, emailConfirmationToken, communicationPreferences) {
167
+ const response = await this.api.authenticatedFetch("/api/customer/identity", "PATCH", {
168
+ name,
169
+ email,
170
+ emailConfirmationToken,
171
+ communicationPreferences
172
+ });
173
+ return {
174
+ needsEmailConfirmationToken: response.status === 201
175
+ };
176
+ }
177
+ async setPassword(currentPassword, newPassword) {
178
+ await this.api.authenticatedFetch(`/api/customer/identity/set-password`, "POST", {
179
+ currentPassword,
180
+ newPassword
181
+ });
182
+ }
183
+ async forgotPassword(email) {
184
+ await this.api.fetch(`/api/customer/identity/forgot-password?email=${encodeURIComponent(email)}`, "POST");
185
+ }
186
+ async resetPassword(email, newPassword, code) {
187
+ const response = await (0, import_cross_fetch.default)(`${this.api.baseUrl}/api/customer/identity/reset-password?email=${encodeURIComponent(email)}&code=${code}`, {
188
+ method: "POST",
189
+ headers: {
190
+ "Accept": "application/json",
191
+ "Content-Type": "text/plain",
192
+ "x-mb-cors": "1"
193
+ },
194
+ body: newPassword
195
+ });
196
+ if (response.status >= 400)
197
+ await handleResponseProblem(response);
198
+ }
199
+ };
200
+
201
+ // src/globalSchemas.ts
202
+ var import_zod3 = require("zod");
203
+ var priceCollectionSchema = import_zod3.z.record(import_zod3.z.number());
204
+ var percentageOffDiscountSchema = import_zod3.z.object({
205
+ type: import_zod3.z.literal("PercentageOffDiscount"),
206
+ name: import_zod3.z.string(),
207
+ description: import_zod3.z.string().optional(),
208
+ percentage: import_zod3.z.number(),
209
+ total: priceCollectionSchema
210
+ });
211
+ var flatAmountOffDiscountSchema = import_zod3.z.object({
212
+ type: import_zod3.z.literal("FlatAmountOffDiscount"),
213
+ name: import_zod3.z.string(),
214
+ description: import_zod3.z.string().optional(),
215
+ total: priceCollectionSchema
216
+ });
217
+ var discountSchema = import_zod3.z.discriminatedUnion("type", [
218
+ percentageOffDiscountSchema,
219
+ flatAmountOffDiscountSchema
220
+ ]);
221
+ var pricingVariationSchema = import_zod3.z.object({
222
+ id: import_zod3.z.string(),
223
+ name: import_zod3.z.string(),
224
+ originalPrice: priceCollectionSchema,
225
+ price: priceCollectionSchema,
226
+ hasDiscount: import_zod3.z.boolean(),
227
+ discount: discountSchema.optional()
228
+ });
229
+ function paged(itemSchema) {
230
+ return import_zod3.z.object({
231
+ items: import_zod3.z.array(itemSchema),
232
+ hasMore: import_zod3.z.boolean(),
233
+ next: import_zod3.z.string().nullable()
234
+ });
235
+ }
236
+ function quantifiable(itemSchema) {
237
+ return import_zod3.z.object({
238
+ value: itemSchema,
239
+ quantity: import_zod3.z.number()
240
+ });
241
+ }
242
+
243
+ // src/licenses/schemas.ts
244
+ var import_zod5 = require("zod");
245
+
246
+ // src/products/schemas.ts
247
+ var import_zod4 = require("zod");
248
+
249
+ // src/products/models.ts
250
+ var Platform = /* @__PURE__ */ ((Platform2) => {
251
+ Platform2["Universal"] = "Universal";
252
+ Platform2["Windows"] = "Windows";
253
+ Platform2["Linux"] = "Linux";
254
+ Platform2["Mac"] = "Mac";
255
+ return Platform2;
256
+ })(Platform || {});
257
+
258
+ // src/products/schemas.ts
259
+ var downloadSchema = import_zod4.z.object({
260
+ name: import_zod4.z.string(),
261
+ key: import_zod4.z.string(),
262
+ platform: import_zod4.z.nativeEnum(Platform),
263
+ size: import_zod4.z.number(),
264
+ path: import_zod4.z.string().nullable()
265
+ });
266
+ var productSummarySchema = import_zod4.z.object({
267
+ id: import_zod4.z.string(),
268
+ name: import_zod4.z.string(),
269
+ tagline: import_zod4.z.string(),
270
+ website: import_zod4.z.string().optional(),
271
+ iconUrl: import_zod4.z.string().optional(),
272
+ numberOfLicenses: import_zod4.z.number().optional(),
273
+ numberOfTrials: import_zod4.z.number().optional(),
274
+ currentActivations: import_zod4.z.number().optional(),
275
+ maxActivations: import_zod4.z.number().optional(),
276
+ currentVersion: import_zod4.z.string().nullable(),
277
+ downloadsNeedsUser: import_zod4.z.boolean(),
278
+ downloadsNeedsOwnership: import_zod4.z.boolean(),
279
+ downloads: import_zod4.z.array(downloadSchema).optional()
280
+ });
281
+
282
+ // src/licenses/models.ts
283
+ var LicenseStatus = /* @__PURE__ */ ((LicenseStatus2) => {
284
+ LicenseStatus2["Active"] = "Active";
285
+ LicenseStatus2["Revoked"] = "Revoked";
286
+ return LicenseStatus2;
287
+ })(LicenseStatus || {});
288
+ var ActivationStatus = /* @__PURE__ */ ((ActivationStatus2) => {
289
+ ActivationStatus2["Active"] = "Active";
290
+ ActivationStatus2["Revoked"] = "Revoked";
291
+ return ActivationStatus2;
292
+ })(ActivationStatus || {});
293
+ var ActivationMethod = /* @__PURE__ */ ((ActivationMethod2) => {
294
+ ActivationMethod2["Online"] = "Online";
295
+ ActivationMethod2["Offline"] = "Offline";
296
+ return ActivationMethod2;
297
+ })(ActivationMethod || {});
298
+
299
+ // src/licenses/schemas.ts
300
+ var licenseSchema = import_zod5.z.object({
301
+ id: import_zod5.z.string(),
302
+ status: import_zod5.z.nativeEnum(LicenseStatus),
303
+ product: productSummarySchema.optional(),
304
+ activeNumberOfActivations: import_zod5.z.number(),
305
+ maxNumberOfActivations: import_zod5.z.number(),
306
+ createdAt: import_zod5.z.coerce.date()
307
+ });
308
+ var activationSchema = import_zod5.z.object({
309
+ id: import_zod5.z.string(),
310
+ licenseId: import_zod5.z.string(),
311
+ name: import_zod5.z.string(),
312
+ status: import_zod5.z.nativeEnum(ActivationStatus),
313
+ activationMethod: import_zod5.z.nativeEnum(ActivationMethod),
314
+ lastValidatedAt: import_zod5.z.coerce.date().nullable()
315
+ });
316
+
317
+ // src/licenses/endpoints.ts
318
+ var LicenseEndpoints = class {
319
+ constructor(api) {
320
+ this.api = api;
321
+ }
322
+ async get(nextUrl) {
323
+ const response = await this.api.authenticatedFetch(nextUrl || "/api/customer/licenses");
324
+ return paged(licenseSchema).parse(response);
325
+ }
326
+ async getActivations(licenseId, nextUrl) {
327
+ const response = await this.api.authenticatedFetch(
328
+ nextUrl || `/api/customer/licenses/${licenseId}/activations`
329
+ );
330
+ return paged(activationSchema).parse(response);
331
+ }
332
+ async revokeActivation(licenseId, activationId) {
333
+ await this.api.authenticatedFetch(`/api/customer/licenses/${licenseId}/activations/${activationId}/revoke`);
334
+ }
335
+ };
336
+
337
+ // src/orders/schemas.ts
338
+ var import_zod7 = require("zod");
339
+
340
+ // src/storefront/schemas.ts
341
+ var import_zod6 = require("zod");
342
+ var storefrontProductSchema = import_zod6.z.object({
343
+ id: import_zod6.z.string(),
344
+ name: import_zod6.z.string(),
345
+ tagline: import_zod6.z.string(),
346
+ iconUrl: import_zod6.z.string().nullable(),
347
+ owned: import_zod6.z.boolean(),
348
+ defaultVariation: pricingVariationSchema.optional(),
349
+ variations: pricingVariationSchema.array().optional(),
350
+ type: import_zod6.z.void().transform(() => "product").pipe(import_zod6.z.literal("product"))
351
+ });
352
+ var storefrontBundleSchema = import_zod6.z.object({
353
+ id: import_zod6.z.string(),
354
+ name: import_zod6.z.string(),
355
+ tagline: import_zod6.z.string(),
356
+ iconUrl: import_zod6.z.string().nullable(),
357
+ owned: import_zod6.z.boolean(),
358
+ partial: import_zod6.z.boolean(),
359
+ products: storefrontProductSchema.and(import_zod6.z.object({
360
+ included: import_zod6.z.boolean()
361
+ })).array(),
362
+ defaultVariation: pricingVariationSchema.optional(),
363
+ variations: pricingVariationSchema.array().optional(),
364
+ type: import_zod6.z.void().transform(() => "bundle").pipe(import_zod6.z.literal("bundle"))
365
+ });
366
+ var storefrontSchema = import_zod6.z.object({
367
+ suggestedCurrency: import_zod6.z.string(),
368
+ products: storefrontProductSchema.array(),
369
+ bundles: storefrontBundleSchema.array()
370
+ });
371
+
372
+ // src/orders/models.ts
373
+ var OrderStatus = /* @__PURE__ */ ((OrderStatus2) => {
374
+ OrderStatus2["Open"] = "Open";
375
+ OrderStatus2["PaymentRequested"] = "PaymentRequested";
376
+ OrderStatus2["Completed"] = "Completed";
377
+ OrderStatus2["Fulfilled"] = "Fulfilled";
378
+ OrderStatus2["Returned"] = "Returned";
379
+ OrderStatus2["Failed"] = "Failed";
380
+ return OrderStatus2;
381
+ })(OrderStatus || {});
382
+
383
+ // src/orders/schemas.ts
384
+ var couponSchema = import_zod7.z.object({
385
+ code: import_zod7.z.string(),
386
+ name: import_zod7.z.string(),
387
+ description: import_zod7.z.string()
388
+ });
389
+ var percentageOffDiscountSchema2 = import_zod7.z.object({
390
+ type: import_zod7.z.literal("PercentageOffDiscount"),
391
+ name: import_zod7.z.string(),
392
+ description: import_zod7.z.string().optional(),
393
+ percentage: import_zod7.z.number(),
394
+ total: priceCollectionSchema.optional(),
395
+ isExclusive: import_zod7.z.boolean()
396
+ });
397
+ var flatAmountOffDiscountSchema2 = import_zod7.z.object({
398
+ type: import_zod7.z.literal("FlatAmountOffDiscount"),
399
+ name: import_zod7.z.string(),
400
+ description: import_zod7.z.string().optional(),
401
+ total: priceCollectionSchema.optional(),
402
+ isExclusive: import_zod7.z.boolean()
403
+ });
404
+ var discountSchema2 = import_zod7.z.discriminatedUnion("type", [
405
+ percentageOffDiscountSchema2,
406
+ flatAmountOffDiscountSchema2
407
+ ]);
408
+ var openProductLineItem = import_zod7.z.object({
409
+ id: import_zod7.z.string(),
410
+ type: import_zod7.z.literal("Product"),
411
+ productId: import_zod7.z.string(),
412
+ quantity: import_zod7.z.number(),
413
+ variationId: import_zod7.z.string(),
414
+ price: priceCollectionSchema.optional(),
415
+ variation: pricingVariationSchema.optional(),
416
+ product: storefrontProductSchema.optional(),
417
+ appliedDiscount: discountSchema2.optional()
418
+ });
419
+ var openBundleLineItem = import_zod7.z.object({
420
+ id: import_zod7.z.string(),
421
+ type: import_zod7.z.literal("Bundle"),
422
+ bundleId: import_zod7.z.string(),
423
+ quantity: import_zod7.z.number(),
424
+ variationId: import_zod7.z.string(),
425
+ price: priceCollectionSchema.optional(),
426
+ variation: pricingVariationSchema.optional(),
427
+ bundle: storefrontBundleSchema.optional(),
428
+ appliedDiscount: discountSchema2.optional()
429
+ });
430
+ var openOrderLineItem = import_zod7.z.discriminatedUnion("type", [
431
+ openProductLineItem,
432
+ openBundleLineItem
433
+ ]);
434
+ var openOrderSchema = import_zod7.z.object({
435
+ id: import_zod7.z.string(),
436
+ status: import_zod7.z.literal("Open" /* Open */),
437
+ currency: import_zod7.z.string(),
438
+ items: openOrderLineItem.array(),
439
+ couponsApplied: couponSchema.array(),
440
+ checkoutUrl: import_zod7.z.string().optional()
441
+ });
442
+ var orderSchema = import_zod7.z.discriminatedUnion("status", [
443
+ openOrderSchema
444
+ ]);
445
+
446
+ // src/orders/endpoints.ts
447
+ var OrderEndpoints = class {
448
+ constructor(api) {
449
+ this.api = api;
450
+ }
451
+ async get(orderId) {
452
+ const response = await this.api.fetch(`/api/customer/orders/${orderId}`);
453
+ return orderSchema.parse(response);
454
+ }
455
+ async pushContent(order, checkout) {
456
+ const response = await this.api.fetch(
457
+ `/api/customer/orders/${order.id}${checkout ? `?checkout=true&returnUrl=${encodeURIComponent(checkout.returnUrl)}` : ""}`,
458
+ "PATCH",
459
+ {
460
+ currency: order.currency,
461
+ items: order.items
462
+ }
463
+ );
464
+ return openOrderSchema.parse(response);
465
+ }
466
+ };
467
+
468
+ // src/products/endpoints.ts
469
+ var ProductEndpoints = class {
470
+ constructor(api) {
471
+ this.api = api;
472
+ }
473
+ async getOwned(nextUrl) {
474
+ const response = await this.api.authenticatedFetch(nextUrl || "/api/customer/products");
475
+ return paged(productSummarySchema).parse(response);
476
+ }
477
+ async getLicenses(productId, nextUrl) {
478
+ const response = await this.api.authenticatedFetch(nextUrl || `/api/customer/products/${productId}/licenses`);
479
+ return paged(licenseSchema).parse(response);
480
+ }
481
+ async getActivations(productId, nextUrl) {
482
+ const response = await this.api.authenticatedFetch(nextUrl || `/api/customer/products/${productId}/licenses/activations`);
483
+ return paged(activationSchema).parse(response);
484
+ }
485
+ };
486
+
487
+ // src/storefront/endpoints.ts
488
+ var StorefrontEndpoints = class {
489
+ constructor(api) {
490
+ this.api = api;
491
+ }
492
+ async get() {
493
+ const response = await this.api.fetch("/api/customer/storefront");
494
+ return storefrontSchema.parse(response);
495
+ }
496
+ };
497
+
498
+ // src/utils/api.ts
499
+ var import_cross_fetch2 = __toESM(require("cross-fetch"), 1);
500
+ var MoonbaseApi = class {
501
+ constructor(baseUrl, tokenStore) {
502
+ this.baseUrl = baseUrl;
503
+ this.tokenStore = tokenStore;
504
+ }
505
+ async authenticatedFetch(path, method, body) {
506
+ if (!this.tokenStore.hasAccessToken)
507
+ throw new NotAuthenticatedError();
508
+ return await this.fetch(path, method, body);
509
+ }
510
+ async fetch(path, method, body) {
511
+ const accessToken = await this.tokenStore.getAccessToken();
512
+ const response = await (0, import_cross_fetch2.default)(this.baseUrl + path, {
513
+ method: method || "GET",
514
+ mode: "cors",
515
+ headers: {
516
+ "Accept": "application/json",
517
+ "Content-Type": "application/json",
518
+ // While this fetch can be anonymous, we add the token if we have it
519
+ ...accessToken ? { Authorization: `Bearer ${accessToken}` } : {},
520
+ // Force CORS on all calls
521
+ "x-mb-cors": "1"
522
+ },
523
+ body: body ? JSON.stringify(body) : void 0
524
+ });
525
+ if (response.status >= 400)
526
+ await handleResponseProblem(response);
527
+ return await response.json();
528
+ }
529
+ };
530
+
531
+ // src/utils/tokenStore.ts
532
+ var import_cross_fetch3 = __toESM(require("cross-fetch"), 1);
533
+ var _TokenStore = class _TokenStore {
534
+ constructor(configuration) {
535
+ this.configuration = configuration;
536
+ this.tokens = null;
537
+ this.refreshTimeoutId = null;
538
+ this.refreshPromise = null;
539
+ if (typeof window !== "undefined") {
540
+ window.addEventListener("storage", (event) => this.handleStorageUpdate(event));
541
+ const storedTokens = localStorage.getItem(_TokenStore.storageKey);
542
+ if (storedTokens) {
543
+ this.tokens = JSON.parse(storedTokens);
544
+ this.tokens.expiresAt = new Date(this.tokens.expiresAt);
545
+ }
546
+ }
547
+ }
548
+ get user() {
549
+ if (this.tokens)
550
+ return this.tokens;
551
+ return null;
552
+ }
553
+ get hasAccessToken() {
554
+ return !!this.tokens;
555
+ }
556
+ async getAccessToken() {
557
+ var _a, _b, _c;
558
+ if (this.isExpired) {
559
+ if (this.refreshPromise) {
560
+ const tokens2 = await this.refreshPromise;
561
+ return (_a = tokens2 == null ? void 0 : tokens2.accessToken) != null ? _a : null;
562
+ }
563
+ this.refreshPromise = this.refreshTokens();
564
+ const tokens = await this.refreshPromise;
565
+ return (_b = tokens == null ? void 0 : tokens.accessToken) != null ? _b : null;
566
+ }
567
+ return ((_c = this.tokens) == null ? void 0 : _c.accessToken) || null;
568
+ }
569
+ setUser(user) {
570
+ if (user === null) {
571
+ this.tokens = null;
572
+ if (typeof window !== "undefined" && localStorage)
573
+ localStorage.removeItem(_TokenStore.storageKey);
574
+ if (this.refreshTimeoutId != null)
575
+ window.clearTimeout(this.refreshTimeoutId);
576
+ return null;
577
+ }
578
+ this.tokens = {
579
+ ...user,
580
+ // Hardcoded 15 minutes now, might want to check the JWT tho
581
+ expiresAt: new Date((/* @__PURE__ */ new Date()).getTime() + 15 * 60 * 1e3)
582
+ };
583
+ if (typeof window !== "undefined" && localStorage)
584
+ localStorage.setItem(_TokenStore.storageKey, JSON.stringify(this.tokens));
585
+ if (this.refreshTimeoutId != null)
586
+ window.clearTimeout(this.refreshTimeoutId);
587
+ this.refreshTimeoutId = window.setTimeout(() => {
588
+ this.refreshPromise = this.refreshTokens();
589
+ }, 10 * 60 * 1e3);
590
+ return this.tokens;
591
+ }
592
+ get isExpired() {
593
+ return this.tokens != null && this.tokens.expiresAt < /* @__PURE__ */ new Date();
594
+ }
595
+ async refreshTokens() {
596
+ if (!this.tokens)
597
+ throw new Error("No tokens found to refresh");
598
+ const response = await (0, import_cross_fetch3.default)(`${this.configuration.endpoint}/api/customer/identity/refresh?token=${this.tokens.refreshToken}`, {
599
+ method: "POST",
600
+ headers: {
601
+ "Accept": "application/json",
602
+ "Content-Type": "text/plain",
603
+ "x-mb-cors": "1"
604
+ },
605
+ body: this.tokens.accessToken
606
+ });
607
+ if (response.status !== 200) {
608
+ if (response.status === 403) {
609
+ this.setUser(null);
610
+ return null;
611
+ }
612
+ throw new Error(`Could not refresh access token, status code ${response.status}`);
613
+ }
614
+ const result = identityUserSchema.parse(await response.json());
615
+ return this.setUser(result);
616
+ }
617
+ handleStorageUpdate(event) {
618
+ switch (event.key) {
619
+ case _TokenStore.storageKey:
620
+ this.tokens = JSON.parse(event.newValue);
621
+ this.tokens.expiresAt = new Date(this.tokens.expiresAt);
622
+ break;
623
+ }
624
+ }
625
+ };
626
+ _TokenStore.storageKey = "moonbase_auth";
627
+ var TokenStore = _TokenStore;
628
+
629
+ // src/vouchers/schemas.ts
630
+ var import_zod8 = require("zod");
631
+ var voucherSchema = import_zod8.z.object({
632
+ id: import_zod8.z.string(),
633
+ name: import_zod8.z.string(),
634
+ description: import_zod8.z.string(),
635
+ code: import_zod8.z.string(),
636
+ redeemed: import_zod8.z.boolean(),
637
+ redeemsProducts: quantifiable(storefrontProductSchema).array(),
638
+ redeemsBundles: quantifiable(storefrontBundleSchema).array()
639
+ });
640
+
641
+ // src/vouchers/endpoints.ts
642
+ var VoucherEndpoints = class {
643
+ constructor(api) {
644
+ this.api = api;
645
+ }
646
+ async peek(code) {
647
+ const response = await this.api.fetch(`/api/customer/vouchers?code=${encodeURIComponent(code)}`);
648
+ return voucherSchema.parse(response);
649
+ }
650
+ async redeem(code) {
651
+ const response = await this.api.authenticatedFetch(
652
+ `/api/customer/vouchers/redeem?code=${encodeURIComponent(code)}`,
653
+ "POST"
654
+ );
655
+ return voucherSchema.parse(response);
656
+ }
657
+ };
658
+
659
+ // src/index.ts
660
+ var MoonbaseClient = class {
661
+ constructor(configuration) {
662
+ this.configuration = configuration;
663
+ this.configuration.endpoint = this.configuration.endpoint.replace(/\/$/, "");
664
+ this.tokenStore = new TokenStore(configuration);
665
+ const api = new MoonbaseApi(this.configuration.endpoint, this.tokenStore);
666
+ this.storefront = new StorefrontEndpoints(api);
667
+ this.identity = new IdentityEndpoints(api, this.tokenStore);
668
+ this.vouchers = new VoucherEndpoints(api);
669
+ this.orders = new OrderEndpoints(api);
670
+ this.licenses = new LicenseEndpoints(api);
671
+ this.products = new ProductEndpoints(api);
672
+ }
673
+ };
674
+ // Annotate the CommonJS export names for ESM import in node:
675
+ 0 && (module.exports = {
676
+ ActivationMethod,
677
+ ActivationStatus,
678
+ LicenseStatus,
679
+ MoonbaseClient,
680
+ NotAuthenticatedError,
681
+ NotAuthorizedError,
682
+ NotFoundError,
683
+ OrderStatus,
684
+ Platform
685
+ });