@moonbase.sh/storefront-api 0.4.18 → 0.4.20

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 CHANGED
@@ -34,10 +34,12 @@ __export(index_exports, {
34
34
  ActivationRequestFulfillmentType: () => ActivationRequestFulfillmentType,
35
35
  ActivationRequestStatus: () => ActivationRequestStatus,
36
36
  ActivationStatus: () => ActivationStatus,
37
+ ConsoleLogger: () => ConsoleLogger,
37
38
  CycleLength: () => CycleLength,
38
39
  InMemoryStore: () => InMemoryStore,
39
40
  LicenseStatus: () => LicenseStatus,
40
41
  LocalStorageStore: () => LocalStorageStore,
42
+ LogLevel: () => LogLevel,
41
43
  MoonbaseApi: () => MoonbaseApi,
42
44
  MoonbaseClient: () => MoonbaseClient,
43
45
  MoonbaseError: () => MoonbaseError,
@@ -357,21 +359,24 @@ function camelCase(str) {
357
359
  return firstLetter + restOfLetters[0];
358
360
  }).join("");
359
361
  }
360
- async function handleResponseProblem(response) {
362
+ async function handleResponseProblem(response, logger) {
361
363
  if (response.status === 404)
362
364
  throw new NotFoundError();
363
- if (response.status === 401)
365
+ if (response.status === 401) {
364
366
  throw new NotAuthenticatedError();
365
- if (response.status === 403)
367
+ }
368
+ if (response.status === 403) {
366
369
  throw new NotAuthorizedError();
370
+ }
367
371
  let problemDetails;
368
372
  try {
369
373
  const json = await response.json();
370
374
  problemDetails = problemDetailsSchema.parse(json);
371
375
  } catch (err) {
372
- console.warn("Could not handle response", { response, err, content: await response.text() });
376
+ logger.warn("Could not handle response", { response, err, content: await response.text() });
373
377
  throw new Error("An unknown problem occurred");
374
378
  }
379
+ logger.debug("The response indicates a problem", problemDetails);
375
380
  throw new MoonbaseError(
376
381
  problemDetails.title,
377
382
  problemDetails.detail,
@@ -427,9 +432,10 @@ var userAccountConfirmedSchema = import_zod7.z.object({
427
432
 
428
433
  // src/identity/endpoints.ts
429
434
  var IdentityEndpoints = class {
430
- constructor(api, tokenStore) {
435
+ constructor(api, tokenStore, logger) {
431
436
  this.api = api;
432
437
  this.tokenStore = tokenStore;
438
+ this.logger = logger;
433
439
  }
434
440
  async get() {
435
441
  const response = await this.api.authenticatedFetch("/api/customer/meta/user", userSchema);
@@ -448,14 +454,14 @@ var IdentityEndpoints = class {
448
454
  if (response.status === 404)
449
455
  throw new NotFoundError("User not found");
450
456
  if (response.status >= 400)
451
- await handleResponseProblem(response);
457
+ await handleResponseProblem(response, this.logger);
452
458
  try {
453
459
  const data = await response.json();
454
460
  const user = identitySchema.parse(data);
455
461
  this.tokenStore.setUser(user);
456
462
  return userSchema.parse(user);
457
463
  } catch (err) {
458
- console.warn("Could not sign in user", { email, response, err });
464
+ this.logger.warn("Could not sign in user", { email, response, err });
459
465
  throw new MoonbaseError("Bad response", "Could not sign in user", response.status);
460
466
  }
461
467
  }
@@ -516,14 +522,14 @@ var IdentityEndpoints = class {
516
522
  body: newPassword
517
523
  });
518
524
  if (response.status >= 400)
519
- await handleResponseProblem(response);
525
+ await handleResponseProblem(response, this.logger);
520
526
  }
521
527
  async confirmAccount(email, code) {
522
528
  const response = await this.api.fetch(`/api/customer/identity/confirm-account?email=${encodeURIComponent(email)}&code=${encodeURIComponent(code.replaceAll(" ", "+"))}`, userAccountConfirmedSchema, { method: "POST" });
523
529
  try {
524
530
  return userAccountConfirmedSchema.parse(response.data);
525
531
  } catch (err) {
526
- console.warn("Could not confirm user account", { email, code, response, err });
532
+ this.logger.warn("Could not confirm user account", { email, code, response, err });
527
533
  throw new MoonbaseError("Bad response", "Could not confirm user account", response.status);
528
534
  }
529
535
  }
@@ -925,9 +931,10 @@ var StorefrontEndpoints = class {
925
931
  // src/utils/api.ts
926
932
  var import_cross_fetch2 = __toESM(require("cross-fetch"), 1);
927
933
  var MoonbaseApi = class {
928
- constructor(baseUrl, tokenStore) {
934
+ constructor(baseUrl, tokenStore, logger) {
929
935
  this.baseUrl = baseUrl;
930
936
  this.tokenStore = tokenStore;
937
+ this.logger = logger;
931
938
  }
932
939
  async authenticatedFetch(path, schema, options) {
933
940
  if (!this.tokenStore.user)
@@ -938,6 +945,11 @@ var MoonbaseApi = class {
938
945
  var _a;
939
946
  const accessToken = await this.tokenStore.getAccessToken();
940
947
  const contentType = (_a = options == null ? void 0 : options.contentType) != null ? _a : "application/json";
948
+ this.logger.debug("Making request to Moonbase API...", {
949
+ path,
950
+ body: options == null ? void 0 : options.body
951
+ });
952
+ const startedAt = /* @__PURE__ */ new Date();
941
953
  const request = {
942
954
  method: (options == null ? void 0 : options.method) || "GET",
943
955
  mode: "cors",
@@ -954,9 +966,15 @@ var MoonbaseApi = class {
954
966
  redirect: "manual"
955
967
  };
956
968
  const response = await (0, import_cross_fetch2.default)(this.baseUrl + path, request);
969
+ const finishedAt = /* @__PURE__ */ new Date();
970
+ this.logger.debug("Received response from Moonbase", {
971
+ path,
972
+ status: response.status,
973
+ duration: finishedAt.getTime() - startedAt.getTime()
974
+ });
957
975
  if (response.status >= 400) {
958
976
  try {
959
- await handleResponseProblem(response);
977
+ await handleResponseProblem(response, this.logger);
960
978
  } catch (err) {
961
979
  this.reportRequestProblem(path, request, response, err);
962
980
  throw err;
@@ -971,7 +989,7 @@ var MoonbaseApi = class {
971
989
  status: response.status
972
990
  };
973
991
  } catch (err) {
974
- console.warn("Could not parse response", {
992
+ this.logger.warn("Could not parse response", {
975
993
  status: response.status,
976
994
  path,
977
995
  content: json || (response.bodyUsed ? "unknown" : await response.text()),
@@ -1248,6 +1266,32 @@ var VoucherEndpoints = class {
1248
1266
  }
1249
1267
  };
1250
1268
 
1269
+ // src/utils/logger.ts
1270
+ var LogLevel = /* @__PURE__ */ ((LogLevel2) => {
1271
+ LogLevel2[LogLevel2["Debug"] = 0] = "Debug";
1272
+ LogLevel2[LogLevel2["Information"] = 1] = "Information";
1273
+ LogLevel2[LogLevel2["Warn"] = 2] = "Warn";
1274
+ LogLevel2[LogLevel2["Error"] = 3] = "Error";
1275
+ return LogLevel2;
1276
+ })(LogLevel || {});
1277
+ var ConsoleLogger = class {
1278
+ constructor(level = 2 /* Warn */) {
1279
+ this.level = level;
1280
+ }
1281
+ debug(message, ...optionalParams) {
1282
+ if (this.level <= 0 /* Debug */) console.debug(message, ...optionalParams);
1283
+ }
1284
+ log(message, ...optionalParams) {
1285
+ if (this.level <= 1 /* Information */) console.log(message, ...optionalParams);
1286
+ }
1287
+ warn(message, ...optionalParams) {
1288
+ if (this.level <= 2 /* Warn */) console.warn(message, ...optionalParams);
1289
+ }
1290
+ error(message, ...optionalParams) {
1291
+ if (this.level <= 3 /* Error */) console.error(message, ...optionalParams);
1292
+ }
1293
+ };
1294
+
1251
1295
  // src/schemas.ts
1252
1296
  var schemas_exports3 = {};
1253
1297
  __export(schemas_exports3, {
@@ -1258,12 +1302,14 @@ __export(schemas_exports3, {
1258
1302
  // src/index.ts
1259
1303
  var MoonbaseClient = class {
1260
1304
  constructor(configuration) {
1305
+ var _a;
1261
1306
  this.configuration = configuration;
1262
1307
  this.configuration.endpoint = this.configuration.endpoint.replace(/\/$/, "");
1308
+ this.logger = (_a = this.configuration.logger) != null ? _a : new ConsoleLogger();
1263
1309
  this.tokenStore = new TokenStore(configuration);
1264
- this.api = new MoonbaseApi(this.configuration.endpoint, this.tokenStore);
1310
+ this.api = new MoonbaseApi(this.configuration.endpoint, this.tokenStore, this.logger);
1265
1311
  this.storefront = new StorefrontEndpoints(this.api, this.configuration);
1266
- this.identity = new IdentityEndpoints(this.api, this.tokenStore);
1312
+ this.identity = new IdentityEndpoints(this.api, this.tokenStore, this.logger);
1267
1313
  this.vouchers = new VoucherEndpoints(this.api);
1268
1314
  this.orders = new OrderEndpoints(this.api);
1269
1315
  this.inventory = new InventoryEndpoints(this.api, this.configuration);
@@ -1277,10 +1323,12 @@ var MoonbaseClient = class {
1277
1323
  ActivationRequestFulfillmentType,
1278
1324
  ActivationRequestStatus,
1279
1325
  ActivationStatus,
1326
+ ConsoleLogger,
1280
1327
  CycleLength,
1281
1328
  InMemoryStore,
1282
1329
  LicenseStatus,
1283
1330
  LocalStorageStore,
1331
+ LogLevel,
1284
1332
  MoonbaseApi,
1285
1333
  MoonbaseClient,
1286
1334
  MoonbaseError,
package/dist/index.d.cts CHANGED
@@ -26,6 +26,27 @@ declare class TokenStore implements ITokenStore {
26
26
  private handleStorageUpdate;
27
27
  }
28
28
 
29
+ interface ILogger {
30
+ debug: (message?: any, ...optionalParams: any[]) => void;
31
+ log: (message?: any, ...optionalParams: any[]) => void;
32
+ warn: (message?: any, ...optionalParams: any[]) => void;
33
+ error: (message?: any, ...optionalParams: any[]) => void;
34
+ }
35
+ declare enum LogLevel {
36
+ Debug = 0,
37
+ Information = 1,
38
+ Warn = 2,
39
+ Error = 3
40
+ }
41
+ declare class ConsoleLogger implements ILogger {
42
+ private readonly level;
43
+ constructor(level?: LogLevel);
44
+ debug(message?: any, ...optionalParams: any[]): void;
45
+ log(message?: any, ...optionalParams: any[]): void;
46
+ warn(message?: any, ...optionalParams: any[]): void;
47
+ error(message?: any, ...optionalParams: any[]): void;
48
+ }
49
+
29
50
  type HttpMethods = 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE';
30
51
  interface Response<T> {
31
52
  data: T;
@@ -41,7 +62,8 @@ interface FetchOptions {
41
62
  declare class MoonbaseApi {
42
63
  baseUrl: string;
43
64
  private tokenStore;
44
- constructor(baseUrl: string, tokenStore: ITokenStore);
65
+ private logger;
66
+ constructor(baseUrl: string, tokenStore: ITokenStore, logger: ILogger);
45
67
  authenticatedFetch<T extends ZodTypeAny>(path: string, schema: T | null, options?: FetchOptions): Promise<Response<z.infer<T>>>;
46
68
  fetch<T extends ZodTypeAny>(path: string, schema: T | null, options?: FetchOptions): Promise<Response<z.infer<T>>>;
47
69
  private reportParsingProblem;
@@ -1096,7 +1118,8 @@ type UserAccountConfirmed = z.infer<typeof userAccountConfirmedSchema>;
1096
1118
  declare class IdentityEndpoints {
1097
1119
  private api;
1098
1120
  private tokenStore;
1099
- constructor(api: MoonbaseApi, tokenStore: ITokenStore);
1121
+ private logger;
1122
+ constructor(api: MoonbaseApi, tokenStore: ITokenStore, logger: ILogger);
1100
1123
  get(): Promise<User>;
1101
1124
  signIn(email: string, password: string): Promise<User>;
1102
1125
  signUp(name: string, email: string, password: string, address: Address | null | undefined, acceptedPrivacyPolicy: boolean, acceptedTermsAndConditions: boolean, communicationOptIn?: boolean): Promise<User>;
@@ -26178,10 +26201,12 @@ interface MoonbaseConfiguration {
26178
26201
  persistUtm?: boolean;
26179
26202
  includeManifests?: boolean;
26180
26203
  store?: IStore;
26204
+ logger?: ILogger;
26181
26205
  }
26182
26206
  declare class MoonbaseClient {
26183
26207
  private readonly configuration;
26184
26208
  readonly tokenStore: ITokenStore;
26209
+ private readonly logger;
26185
26210
  constructor(configuration: MoonbaseConfiguration);
26186
26211
  api: MoonbaseApi;
26187
26212
  storefront: StorefrontEndpoints;
@@ -26193,4 +26218,4 @@ declare class MoonbaseClient {
26193
26218
  orders: OrderEndpoints;
26194
26219
  }
26195
26220
 
26196
- export { type Activation, ActivationMethod, type ActivationRequest, ActivationRequestFulfillmentType, ActivationRequestStatus, ActivationStatus, type Address, type BundleLineItem, type CommunicationPreferences, type CompletedOrder, CycleLength, type Discount, type Download, type DownloadManifest, type IRecurrence, type IStore, type ITokenStore, type Identity, InMemoryStore, type License, LicenseStatus, type LineItem, LocalStorageStore, type Money, MoonbaseApi, MoonbaseClient, type MoonbaseConfiguration, MoonbaseError, NotAuthenticatedError, NotAuthorizedError, NotFoundError, type OpenOrder, type Order, OrderStatus, type OwnedProduct, type Page, Platform, type PricingTier, type PricingVariation, type ProblemDetails, type ProductLineItem, type Quantifiable, type Storefront, type StorefrontBundle, type StorefrontProduct, type Subscription, SubscriptionStatus, TokenStore, type UrchinTrackingModule, type User, type UserAccountConfirmed, type Vendor, type Voucher, problemDetailsSchema, schemas, utmToObject };
26221
+ export { type Activation, ActivationMethod, type ActivationRequest, ActivationRequestFulfillmentType, ActivationRequestStatus, ActivationStatus, type Address, type BundleLineItem, type CommunicationPreferences, type CompletedOrder, ConsoleLogger, CycleLength, type Discount, type Download, type DownloadManifest, type ILogger, type IRecurrence, type IStore, type ITokenStore, type Identity, InMemoryStore, type License, LicenseStatus, type LineItem, LocalStorageStore, LogLevel, type Money, MoonbaseApi, MoonbaseClient, type MoonbaseConfiguration, MoonbaseError, NotAuthenticatedError, NotAuthorizedError, NotFoundError, type OpenOrder, type Order, OrderStatus, type OwnedProduct, type Page, Platform, type PricingTier, type PricingVariation, type ProblemDetails, type ProductLineItem, type Quantifiable, type Storefront, type StorefrontBundle, type StorefrontProduct, type Subscription, SubscriptionStatus, TokenStore, type UrchinTrackingModule, type User, type UserAccountConfirmed, type Vendor, type Voucher, problemDetailsSchema, schemas, utmToObject };
package/dist/index.d.ts CHANGED
@@ -26,6 +26,27 @@ declare class TokenStore implements ITokenStore {
26
26
  private handleStorageUpdate;
27
27
  }
28
28
 
29
+ interface ILogger {
30
+ debug: (message?: any, ...optionalParams: any[]) => void;
31
+ log: (message?: any, ...optionalParams: any[]) => void;
32
+ warn: (message?: any, ...optionalParams: any[]) => void;
33
+ error: (message?: any, ...optionalParams: any[]) => void;
34
+ }
35
+ declare enum LogLevel {
36
+ Debug = 0,
37
+ Information = 1,
38
+ Warn = 2,
39
+ Error = 3
40
+ }
41
+ declare class ConsoleLogger implements ILogger {
42
+ private readonly level;
43
+ constructor(level?: LogLevel);
44
+ debug(message?: any, ...optionalParams: any[]): void;
45
+ log(message?: any, ...optionalParams: any[]): void;
46
+ warn(message?: any, ...optionalParams: any[]): void;
47
+ error(message?: any, ...optionalParams: any[]): void;
48
+ }
49
+
29
50
  type HttpMethods = 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE';
30
51
  interface Response<T> {
31
52
  data: T;
@@ -41,7 +62,8 @@ interface FetchOptions {
41
62
  declare class MoonbaseApi {
42
63
  baseUrl: string;
43
64
  private tokenStore;
44
- constructor(baseUrl: string, tokenStore: ITokenStore);
65
+ private logger;
66
+ constructor(baseUrl: string, tokenStore: ITokenStore, logger: ILogger);
45
67
  authenticatedFetch<T extends ZodTypeAny>(path: string, schema: T | null, options?: FetchOptions): Promise<Response<z.infer<T>>>;
46
68
  fetch<T extends ZodTypeAny>(path: string, schema: T | null, options?: FetchOptions): Promise<Response<z.infer<T>>>;
47
69
  private reportParsingProblem;
@@ -1096,7 +1118,8 @@ type UserAccountConfirmed = z.infer<typeof userAccountConfirmedSchema>;
1096
1118
  declare class IdentityEndpoints {
1097
1119
  private api;
1098
1120
  private tokenStore;
1099
- constructor(api: MoonbaseApi, tokenStore: ITokenStore);
1121
+ private logger;
1122
+ constructor(api: MoonbaseApi, tokenStore: ITokenStore, logger: ILogger);
1100
1123
  get(): Promise<User>;
1101
1124
  signIn(email: string, password: string): Promise<User>;
1102
1125
  signUp(name: string, email: string, password: string, address: Address | null | undefined, acceptedPrivacyPolicy: boolean, acceptedTermsAndConditions: boolean, communicationOptIn?: boolean): Promise<User>;
@@ -26178,10 +26201,12 @@ interface MoonbaseConfiguration {
26178
26201
  persistUtm?: boolean;
26179
26202
  includeManifests?: boolean;
26180
26203
  store?: IStore;
26204
+ logger?: ILogger;
26181
26205
  }
26182
26206
  declare class MoonbaseClient {
26183
26207
  private readonly configuration;
26184
26208
  readonly tokenStore: ITokenStore;
26209
+ private readonly logger;
26185
26210
  constructor(configuration: MoonbaseConfiguration);
26186
26211
  api: MoonbaseApi;
26187
26212
  storefront: StorefrontEndpoints;
@@ -26193,4 +26218,4 @@ declare class MoonbaseClient {
26193
26218
  orders: OrderEndpoints;
26194
26219
  }
26195
26220
 
26196
- export { type Activation, ActivationMethod, type ActivationRequest, ActivationRequestFulfillmentType, ActivationRequestStatus, ActivationStatus, type Address, type BundleLineItem, type CommunicationPreferences, type CompletedOrder, CycleLength, type Discount, type Download, type DownloadManifest, type IRecurrence, type IStore, type ITokenStore, type Identity, InMemoryStore, type License, LicenseStatus, type LineItem, LocalStorageStore, type Money, MoonbaseApi, MoonbaseClient, type MoonbaseConfiguration, MoonbaseError, NotAuthenticatedError, NotAuthorizedError, NotFoundError, type OpenOrder, type Order, OrderStatus, type OwnedProduct, type Page, Platform, type PricingTier, type PricingVariation, type ProblemDetails, type ProductLineItem, type Quantifiable, type Storefront, type StorefrontBundle, type StorefrontProduct, type Subscription, SubscriptionStatus, TokenStore, type UrchinTrackingModule, type User, type UserAccountConfirmed, type Vendor, type Voucher, problemDetailsSchema, schemas, utmToObject };
26221
+ export { type Activation, ActivationMethod, type ActivationRequest, ActivationRequestFulfillmentType, ActivationRequestStatus, ActivationStatus, type Address, type BundleLineItem, type CommunicationPreferences, type CompletedOrder, ConsoleLogger, CycleLength, type Discount, type Download, type DownloadManifest, type ILogger, type IRecurrence, type IStore, type ITokenStore, type Identity, InMemoryStore, type License, LicenseStatus, type LineItem, LocalStorageStore, LogLevel, type Money, MoonbaseApi, MoonbaseClient, type MoonbaseConfiguration, MoonbaseError, NotAuthenticatedError, NotAuthorizedError, NotFoundError, type OpenOrder, type Order, OrderStatus, type OwnedProduct, type Page, Platform, type PricingTier, type PricingVariation, type ProblemDetails, type ProductLineItem, type Quantifiable, type Storefront, type StorefrontBundle, type StorefrontProduct, type Subscription, SubscriptionStatus, TokenStore, type UrchinTrackingModule, type User, type UserAccountConfirmed, type Vendor, type Voucher, problemDetailsSchema, schemas, utmToObject };
package/dist/index.js CHANGED
@@ -307,21 +307,24 @@ function camelCase(str) {
307
307
  return firstLetter + restOfLetters[0];
308
308
  }).join("");
309
309
  }
310
- async function handleResponseProblem(response) {
310
+ async function handleResponseProblem(response, logger) {
311
311
  if (response.status === 404)
312
312
  throw new NotFoundError();
313
- if (response.status === 401)
313
+ if (response.status === 401) {
314
314
  throw new NotAuthenticatedError();
315
- if (response.status === 403)
315
+ }
316
+ if (response.status === 403) {
316
317
  throw new NotAuthorizedError();
318
+ }
317
319
  let problemDetails;
318
320
  try {
319
321
  const json = await response.json();
320
322
  problemDetails = problemDetailsSchema.parse(json);
321
323
  } catch (err) {
322
- console.warn("Could not handle response", { response, err, content: await response.text() });
324
+ logger.warn("Could not handle response", { response, err, content: await response.text() });
323
325
  throw new Error("An unknown problem occurred");
324
326
  }
327
+ logger.debug("The response indicates a problem", problemDetails);
325
328
  throw new MoonbaseError(
326
329
  problemDetails.title,
327
330
  problemDetails.detail,
@@ -377,9 +380,10 @@ var userAccountConfirmedSchema = z7.object({
377
380
 
378
381
  // src/identity/endpoints.ts
379
382
  var IdentityEndpoints = class {
380
- constructor(api, tokenStore) {
383
+ constructor(api, tokenStore, logger) {
381
384
  this.api = api;
382
385
  this.tokenStore = tokenStore;
386
+ this.logger = logger;
383
387
  }
384
388
  async get() {
385
389
  const response = await this.api.authenticatedFetch("/api/customer/meta/user", userSchema);
@@ -398,14 +402,14 @@ var IdentityEndpoints = class {
398
402
  if (response.status === 404)
399
403
  throw new NotFoundError("User not found");
400
404
  if (response.status >= 400)
401
- await handleResponseProblem(response);
405
+ await handleResponseProblem(response, this.logger);
402
406
  try {
403
407
  const data = await response.json();
404
408
  const user = identitySchema.parse(data);
405
409
  this.tokenStore.setUser(user);
406
410
  return userSchema.parse(user);
407
411
  } catch (err) {
408
- console.warn("Could not sign in user", { email, response, err });
412
+ this.logger.warn("Could not sign in user", { email, response, err });
409
413
  throw new MoonbaseError("Bad response", "Could not sign in user", response.status);
410
414
  }
411
415
  }
@@ -466,14 +470,14 @@ var IdentityEndpoints = class {
466
470
  body: newPassword
467
471
  });
468
472
  if (response.status >= 400)
469
- await handleResponseProblem(response);
473
+ await handleResponseProblem(response, this.logger);
470
474
  }
471
475
  async confirmAccount(email, code) {
472
476
  const response = await this.api.fetch(`/api/customer/identity/confirm-account?email=${encodeURIComponent(email)}&code=${encodeURIComponent(code.replaceAll(" ", "+"))}`, userAccountConfirmedSchema, { method: "POST" });
473
477
  try {
474
478
  return userAccountConfirmedSchema.parse(response.data);
475
479
  } catch (err) {
476
- console.warn("Could not confirm user account", { email, code, response, err });
480
+ this.logger.warn("Could not confirm user account", { email, code, response, err });
477
481
  throw new MoonbaseError("Bad response", "Could not confirm user account", response.status);
478
482
  }
479
483
  }
@@ -875,9 +879,10 @@ var StorefrontEndpoints = class {
875
879
  // src/utils/api.ts
876
880
  import fetch2 from "cross-fetch";
877
881
  var MoonbaseApi = class {
878
- constructor(baseUrl, tokenStore) {
882
+ constructor(baseUrl, tokenStore, logger) {
879
883
  this.baseUrl = baseUrl;
880
884
  this.tokenStore = tokenStore;
885
+ this.logger = logger;
881
886
  }
882
887
  async authenticatedFetch(path, schema, options) {
883
888
  if (!this.tokenStore.user)
@@ -888,6 +893,11 @@ var MoonbaseApi = class {
888
893
  var _a;
889
894
  const accessToken = await this.tokenStore.getAccessToken();
890
895
  const contentType = (_a = options == null ? void 0 : options.contentType) != null ? _a : "application/json";
896
+ this.logger.debug("Making request to Moonbase API...", {
897
+ path,
898
+ body: options == null ? void 0 : options.body
899
+ });
900
+ const startedAt = /* @__PURE__ */ new Date();
891
901
  const request = {
892
902
  method: (options == null ? void 0 : options.method) || "GET",
893
903
  mode: "cors",
@@ -904,9 +914,15 @@ var MoonbaseApi = class {
904
914
  redirect: "manual"
905
915
  };
906
916
  const response = await fetch2(this.baseUrl + path, request);
917
+ const finishedAt = /* @__PURE__ */ new Date();
918
+ this.logger.debug("Received response from Moonbase", {
919
+ path,
920
+ status: response.status,
921
+ duration: finishedAt.getTime() - startedAt.getTime()
922
+ });
907
923
  if (response.status >= 400) {
908
924
  try {
909
- await handleResponseProblem(response);
925
+ await handleResponseProblem(response, this.logger);
910
926
  } catch (err) {
911
927
  this.reportRequestProblem(path, request, response, err);
912
928
  throw err;
@@ -921,7 +937,7 @@ var MoonbaseApi = class {
921
937
  status: response.status
922
938
  };
923
939
  } catch (err) {
924
- console.warn("Could not parse response", {
940
+ this.logger.warn("Could not parse response", {
925
941
  status: response.status,
926
942
  path,
927
943
  content: json || (response.bodyUsed ? "unknown" : await response.text()),
@@ -1198,6 +1214,32 @@ var VoucherEndpoints = class {
1198
1214
  }
1199
1215
  };
1200
1216
 
1217
+ // src/utils/logger.ts
1218
+ var LogLevel = /* @__PURE__ */ ((LogLevel2) => {
1219
+ LogLevel2[LogLevel2["Debug"] = 0] = "Debug";
1220
+ LogLevel2[LogLevel2["Information"] = 1] = "Information";
1221
+ LogLevel2[LogLevel2["Warn"] = 2] = "Warn";
1222
+ LogLevel2[LogLevel2["Error"] = 3] = "Error";
1223
+ return LogLevel2;
1224
+ })(LogLevel || {});
1225
+ var ConsoleLogger = class {
1226
+ constructor(level = 2 /* Warn */) {
1227
+ this.level = level;
1228
+ }
1229
+ debug(message, ...optionalParams) {
1230
+ if (this.level <= 0 /* Debug */) console.debug(message, ...optionalParams);
1231
+ }
1232
+ log(message, ...optionalParams) {
1233
+ if (this.level <= 1 /* Information */) console.log(message, ...optionalParams);
1234
+ }
1235
+ warn(message, ...optionalParams) {
1236
+ if (this.level <= 2 /* Warn */) console.warn(message, ...optionalParams);
1237
+ }
1238
+ error(message, ...optionalParams) {
1239
+ if (this.level <= 3 /* Error */) console.error(message, ...optionalParams);
1240
+ }
1241
+ };
1242
+
1201
1243
  // src/schemas.ts
1202
1244
  var schemas_exports3 = {};
1203
1245
  __export(schemas_exports3, {
@@ -1208,12 +1250,14 @@ __export(schemas_exports3, {
1208
1250
  // src/index.ts
1209
1251
  var MoonbaseClient = class {
1210
1252
  constructor(configuration) {
1253
+ var _a;
1211
1254
  this.configuration = configuration;
1212
1255
  this.configuration.endpoint = this.configuration.endpoint.replace(/\/$/, "");
1256
+ this.logger = (_a = this.configuration.logger) != null ? _a : new ConsoleLogger();
1213
1257
  this.tokenStore = new TokenStore(configuration);
1214
- this.api = new MoonbaseApi(this.configuration.endpoint, this.tokenStore);
1258
+ this.api = new MoonbaseApi(this.configuration.endpoint, this.tokenStore, this.logger);
1215
1259
  this.storefront = new StorefrontEndpoints(this.api, this.configuration);
1216
- this.identity = new IdentityEndpoints(this.api, this.tokenStore);
1260
+ this.identity = new IdentityEndpoints(this.api, this.tokenStore, this.logger);
1217
1261
  this.vouchers = new VoucherEndpoints(this.api);
1218
1262
  this.orders = new OrderEndpoints(this.api);
1219
1263
  this.inventory = new InventoryEndpoints(this.api, this.configuration);
@@ -1226,10 +1270,12 @@ export {
1226
1270
  ActivationRequestFulfillmentType,
1227
1271
  ActivationRequestStatus,
1228
1272
  ActivationStatus,
1273
+ ConsoleLogger,
1229
1274
  CycleLength,
1230
1275
  InMemoryStore,
1231
1276
  LicenseStatus,
1232
1277
  LocalStorageStore,
1278
+ LogLevel,
1233
1279
  MoonbaseApi,
1234
1280
  MoonbaseClient,
1235
1281
  MoonbaseError,
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.18",
4
+ "version": "0.4.20",
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",