@mitumba/sdk 5.4.0 → 5.6.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.
package/dist/index.js CHANGED
@@ -151,6 +151,21 @@ function isExpiringSoon(token, thresholdSeconds) {
151
151
  return false;
152
152
  }
153
153
  }
154
+ function refreshPathForToken(token) {
155
+ if (!token) return "/auth/refresh";
156
+ try {
157
+ const encoded = token.split(".")[1];
158
+ if (!encoded) return "/auth/refresh";
159
+ const normalized = encoded.replace(/-/g, "+").replace(/_/g, "/");
160
+ const payload = JSON.parse(atob(normalized));
161
+ return payload.ptype === "iam" ? "/auth/iam/refresh" : "/auth/refresh";
162
+ } catch {
163
+ return "/auth/refresh";
164
+ }
165
+ }
166
+ function isSessionEndpoint(path) {
167
+ return path === "/auth/login" || path === "/auth/register" || path === "/auth/refresh" || path === "/auth/iam/login" || path === "/auth/iam/login/2fa" || path === "/auth/iam/login/passkey/start" || path === "/auth/iam/login/passkey/finish" || path === "/auth/iam/activate" || path === "/auth/iam/refresh" || path === "/auth/iam/logout";
168
+ }
154
169
  function isErrorRecord(value) {
155
170
  return Boolean(value) && typeof value === "object" && !Array.isArray(value);
156
171
  }
@@ -266,7 +281,7 @@ var APIClient = class {
266
281
  }
267
282
  }
268
283
  async request(method, path, body, params, options) {
269
- if (!path.includes("/auth/refresh") && !path.includes("/auth/login") && !path.includes("/auth/register")) {
284
+ if (!isSessionEndpoint(path)) {
270
285
  await this.ensureFreshToken(options);
271
286
  }
272
287
  const url = new URL(path, this.config.baseUrl);
@@ -327,7 +342,7 @@ var APIClient = class {
327
342
  }
328
343
  break;
329
344
  }
330
- if (response.status === 401 && !path.includes("/auth/refresh") && !path.includes("/auth/login") && !path.includes("/auth/register")) {
345
+ if (response.status === 401 && !isSessionEndpoint(path)) {
331
346
  const hasRefreshToken = this.config.refreshToken && this.config.refreshToken.length > 0;
332
347
  if (hasRefreshToken) {
333
348
  try {
@@ -367,7 +382,7 @@ var APIClient = class {
367
382
  this.isRefreshing = true;
368
383
  this.refreshPromise = (async () => {
369
384
  try {
370
- const url = new URL("/auth/refresh", this.config.baseUrl);
385
+ const url = new URL(refreshPathForToken(this.config.token), this.config.baseUrl);
371
386
  const response = await fetch(url.toString(), {
372
387
  method: "POST",
373
388
  headers: { "Content-Type": "application/json" },
@@ -2601,6 +2616,151 @@ var WholesaleModule = class {
2601
2616
  }
2602
2617
  };
2603
2618
 
2619
+ // src/modules/iam.ts
2620
+ function isIamTokens(result) {
2621
+ return "access_token" in result;
2622
+ }
2623
+ var IamModule = class {
2624
+ constructor(client) {
2625
+ this.client = client;
2626
+ }
2627
+ client;
2628
+ /** Start IAM sign-in with an organization ID or alias, email and password. */
2629
+ async login(input, options) {
2630
+ const result = await this.client.post("/auth/iam/login", input, options);
2631
+ if (isIamTokens(result)) await this.client.setSession(result);
2632
+ return result;
2633
+ }
2634
+ /** Finish IAM sign-in with a TOTP or one-time backup code. */
2635
+ async verifyTwoFactor(input, options) {
2636
+ const result = await this.client.post("/auth/iam/login/2fa", input, options);
2637
+ await this.client.setSession(result);
2638
+ return result;
2639
+ }
2640
+ /** Get WebAuthn assertion options for a pending IAM sign-in. */
2641
+ async startPasskeyLogin(tempToken, options) {
2642
+ return this.client.post("/auth/iam/login/passkey/start", { temp_token: tempToken }, options);
2643
+ }
2644
+ /** Finish IAM passkey sign-in and persist the returned IAM session. */
2645
+ async finishPasskeyLogin(input, options) {
2646
+ const result = await this.client.post("/auth/iam/login/passkey/finish", input, options);
2647
+ await this.client.setSession(result);
2648
+ return result;
2649
+ }
2650
+ /** Activate an invited IAM user and choose its password. */
2651
+ async activate(input, options) {
2652
+ return this.client.post("/auth/iam/activate", input, options);
2653
+ }
2654
+ /** Rotate an IAM refresh token and persist the returned session. */
2655
+ async refresh(refreshToken, options) {
2656
+ const result = await this.client.post("/auth/iam/refresh", { refresh_token: refreshToken }, options);
2657
+ await this.client.setSession(result);
2658
+ return result;
2659
+ }
2660
+ /** End the current IAM session and clear locally stored credentials. */
2661
+ async logout(options) {
2662
+ const refreshToken = await this.client.getStoredRefreshToken();
2663
+ if (!refreshToken) {
2664
+ await this.client.clearToken();
2665
+ return { ok: true };
2666
+ }
2667
+ const result = await this.client.post("/auth/iam/logout", { refresh_token: refreshToken }, options);
2668
+ await this.client.clearToken();
2669
+ return result;
2670
+ }
2671
+ /** Get the current IAM principal, organization and effective access state. */
2672
+ async me(options) {
2673
+ return this.client.get("/auth/iam/me", void 0, options);
2674
+ }
2675
+ /** List this IAM principal's enrolled second-factor methods. */
2676
+ async listMethods(options) {
2677
+ return this.client.get("/auth/iam/2fa/methods", void 0, options);
2678
+ }
2679
+ /** Begin adding an authenticator app or passkey. */
2680
+ async addMethod(input, options) {
2681
+ return this.client.post("/auth/iam/2fa/methods", input, options);
2682
+ }
2683
+ /** Verify and enable a pending IAM authenticator method. */
2684
+ async verifyMethod(methodId, code, options) {
2685
+ return this.client.post(`/auth/iam/2fa/methods/${encodeURIComponent(methodId)}/verify`, { code }, options);
2686
+ }
2687
+ /** Finish and enable a pending IAM passkey method. */
2688
+ async finishPasskeyEnrollment(methodId, credential, options) {
2689
+ return this.client.post(`/auth/iam/2fa/methods/${encodeURIComponent(methodId)}/passkey/finish`, credential, options);
2690
+ }
2691
+ /** List organizations the current root/global operator may administer. */
2692
+ async listOrganizations(options) {
2693
+ return this.client.get("/auth/orgs", void 0, options);
2694
+ }
2695
+ /** Create an organization. */
2696
+ async createOrganization(input, options) {
2697
+ return this.client.post("/auth/orgs", input, options);
2698
+ }
2699
+ /** Update an organization's name or lifecycle state. */
2700
+ async updateOrganization(orgId, input, options) {
2701
+ return this.client.patch(`/auth/orgs/${encodeURIComponent(orgId)}`, input, options);
2702
+ }
2703
+ /** List IAM users and grantable permissions in one organization. */
2704
+ async listUsers(orgId, options) {
2705
+ return this.client.get(`/auth/orgs/${encodeURIComponent(orgId)}/iam-users`, void 0, options);
2706
+ }
2707
+ /** Provision an IAM user. The activation code is returned once. */
2708
+ async createUser(orgId, input, options) {
2709
+ return this.client.post(`/auth/orgs/${encodeURIComponent(orgId)}/iam-users`, input, options);
2710
+ }
2711
+ /** Grant one organization-scoped permission to an IAM user. */
2712
+ async grantPermission(orgId, iamUserId, permission, options) {
2713
+ return this.client.post(
2714
+ `/auth/orgs/${encodeURIComponent(orgId)}/iam-users/${encodeURIComponent(iamUserId)}/permissions`,
2715
+ { permission },
2716
+ options
2717
+ );
2718
+ }
2719
+ /** Revoke one permission and immediately end the IAM user's sessions. */
2720
+ async revokePermission(orgId, iamUserId, permission, options) {
2721
+ return this.client.delete(
2722
+ `/auth/orgs/${encodeURIComponent(orgId)}/iam-users/${encodeURIComponent(iamUserId)}/permissions/${encodeURIComponent(permission)}`,
2723
+ void 0,
2724
+ options
2725
+ );
2726
+ }
2727
+ /** Enable or disable an IAM user. Disabling immediately ends its sessions. */
2728
+ async setUserStatus(orgId, iamUserId, status, options) {
2729
+ return this.client.post(
2730
+ `/auth/orgs/${encodeURIComponent(orgId)}/iam-users/${encodeURIComponent(iamUserId)}/status`,
2731
+ { status },
2732
+ options
2733
+ );
2734
+ }
2735
+ /** Reissue the one-time activation credential for an unactivated IAM user. */
2736
+ async reissueActivation(orgId, iamUserId, options) {
2737
+ return this.client.post(
2738
+ `/auth/orgs/${encodeURIComponent(orgId)}/iam-users/${encodeURIComponent(iamUserId)}/activation`,
2739
+ void 0,
2740
+ options
2741
+ );
2742
+ }
2743
+ };
2744
+
2745
+ // src/modules/markets.ts
2746
+ var MarketsModule = class {
2747
+ constructor(client) {
2748
+ this.client = client;
2749
+ }
2750
+ client;
2751
+ /**
2752
+ * List the platform's markets: the countries Mitumba operates in or plans to, with each
2753
+ * market's currency, locale, phone rules, region naming and payment rails.
2754
+ *
2755
+ * Public (no sign-in needed) and cacheable for a few minutes. Read this at runtime instead of
2756
+ * hardcoding a country, and pair it with the helpers exported from the package root
2757
+ * (`formatMoney`, `normalizePhone`, `findMarket`, ...).
2758
+ */
2759
+ async list(options) {
2760
+ return this.client.get("/markets", void 0, options);
2761
+ }
2762
+ };
2763
+
2604
2764
  // src/types/auth.ts
2605
2765
  var TWO_FACTOR_METHOD_TYPES = ["totp", "sms", "email", "passkey"];
2606
2766
 
@@ -2632,6 +2792,103 @@ var DISPUTE_STATUSES = ["open", "seller_responded", "under_review", "resolved_re
2632
2792
  // src/types/wholesale.ts
2633
2793
  var BALE_ORDER_STATUSES = ["pending_payment", "paid", "confirmed", "in_transit", "delivered", "completed", "cancelled"];
2634
2794
 
2795
+ // src/markets.ts
2796
+ var GAP = " ";
2797
+ function group(value, locale) {
2798
+ const rounded = Math.round(Math.abs(value));
2799
+ try {
2800
+ return new Intl.NumberFormat(locale, { maximumFractionDigits: 0 }).format(rounded);
2801
+ } catch {
2802
+ return String(rounded).replace(/\B(?=(\d{3})+(?!\d))/g, ",");
2803
+ }
2804
+ }
2805
+ function formatMoney(amount, market) {
2806
+ const { code, locale } = market.currency;
2807
+ if (!Number.isFinite(amount)) return `${code}${GAP}\u2014`;
2808
+ return `${amount < 0 ? "-" : ""}${code}${GAP}${group(amount, locale)}`;
2809
+ }
2810
+ function formatAmount(amount, market) {
2811
+ if (!Number.isFinite(amount)) return "\u2014";
2812
+ return `${amount < 0 ? "-" : ""}${group(amount, market.currency.locale)}`;
2813
+ }
2814
+ function formatMoneyCompact(amount, market) {
2815
+ const absolute = Math.abs(amount);
2816
+ if (!Number.isFinite(amount) || absolute < 1e4) return formatMoney(amount, market);
2817
+ const { code } = market.currency;
2818
+ const sign = amount < 0 ? "-" : "";
2819
+ const trim = (n) => n.toFixed(1).replace(/\.0$/, "");
2820
+ if (absolute < 1e6) {
2821
+ const thousands = absolute / 1e3;
2822
+ if (Math.round(thousands) < 1e3) {
2823
+ return `${sign}${code}${GAP}${thousands < 100 ? trim(thousands) : String(Math.round(thousands))}K`;
2824
+ }
2825
+ }
2826
+ return `${sign}${code}${GAP}${trim(absolute / 1e6)}M`;
2827
+ }
2828
+ function digitsOnly(value) {
2829
+ return value.replace(/\D/g, "");
2830
+ }
2831
+ function normalizePhone(input, market) {
2832
+ const { calling_code, national_length } = market.phone;
2833
+ const callingDigits = digitsOnly(calling_code);
2834
+ const trimmed = input.trim();
2835
+ const digits = digitsOnly(trimmed);
2836
+ if (digits.length === 0) return null;
2837
+ const fits = (national) => national_length.includes(national.length) && !national.startsWith("0");
2838
+ const afterCallingCode = (rest) => {
2839
+ if (fits(rest)) return rest;
2840
+ if (rest.startsWith("0") && fits(rest.slice(1))) return rest.slice(1);
2841
+ return null;
2842
+ };
2843
+ if (trimmed.startsWith("+")) {
2844
+ if (!digits.startsWith(callingDigits)) return null;
2845
+ const national = afterCallingCode(digits.slice(callingDigits.length));
2846
+ return national === null ? null : `+${callingDigits}${national}`;
2847
+ }
2848
+ if (digits.startsWith("0") && fits(digits.slice(1))) return `+${callingDigits}${digits.slice(1)}`;
2849
+ if (digits.startsWith(callingDigits)) {
2850
+ const national = afterCallingCode(digits.slice(callingDigits.length));
2851
+ if (national !== null) return `+${callingDigits}${national}`;
2852
+ }
2853
+ if (fits(digits)) return `+${callingDigits}${digits}`;
2854
+ return null;
2855
+ }
2856
+ function isValidPhone(input, market) {
2857
+ return normalizePhone(input, market) !== null;
2858
+ }
2859
+ function splitNational(e164, market) {
2860
+ const calling = market.phone.calling_code;
2861
+ if (!e164.startsWith(calling)) return null;
2862
+ const national = e164.slice(calling.length);
2863
+ if (!/^\d+$/.test(national) || !market.phone.national_length.includes(national.length)) return null;
2864
+ return { calling, national };
2865
+ }
2866
+ function formatPhone(value, market) {
2867
+ const compact = value.replace(/[^\d+]/g, "");
2868
+ const parts = splitNational(compact, market);
2869
+ if (!parts) return value;
2870
+ const groups = parts.national.match(/\d{1,3}/g) ?? [];
2871
+ if (groups.length > 1 && groups[groups.length - 1].length === 1) {
2872
+ const last = groups.pop();
2873
+ groups[groups.length - 1] += last;
2874
+ }
2875
+ return `${parts.calling}${GAP}${groups.join(GAP)}`;
2876
+ }
2877
+ function maskPhone(value, market) {
2878
+ const compact = value.replace(/[^\d+]/g, "");
2879
+ const parts = splitNational(compact, market);
2880
+ if (!parts || parts.national.length < 7) return value;
2881
+ const head = parts.national.slice(0, 3);
2882
+ const tail = parts.national.slice(-3);
2883
+ return `${parts.calling}${GAP}${head}${GAP}\u2022\u2022\u2022${GAP}${tail}`;
2884
+ }
2885
+ function findMarket(markets, code) {
2886
+ return markets.find((market) => market.code === code);
2887
+ }
2888
+ function activeMarkets(markets) {
2889
+ return markets.filter((market) => market.status === "active");
2890
+ }
2891
+
2635
2892
  // src/share.ts
2636
2893
  var DEFAULT_SHARE_ORIGIN = "https://go.mitumba.africa";
2637
2894
  var ID_PATTERN = /^[0-9a-f]{32}$/;
@@ -2694,6 +2951,8 @@ var MitumbaClient = class {
2694
2951
  uploads;
2695
2952
  reports;
2696
2953
  wholesale;
2954
+ iam;
2955
+ markets;
2697
2956
  constructor(config) {
2698
2957
  this.api = new APIClient(config);
2699
2958
  this.auth = new AuthModule(this.api);
@@ -2717,6 +2976,8 @@ var MitumbaClient = class {
2717
2976
  this.uploads = new UploadsModule(this.api);
2718
2977
  this.reports = new ReportsModule(this.api);
2719
2978
  this.wholesale = new WholesaleModule(this.api);
2979
+ this.iam = new IamModule(this.api);
2980
+ this.markets = new MarketsModule(this.api);
2720
2981
  }
2721
2982
  /**
2722
2983
  * Set the access token for authenticated requests.
@@ -2771,9 +3032,11 @@ exports.DISPUTE_REASONS = DISPUTE_REASONS;
2771
3032
  exports.DISPUTE_STATUSES = DISPUTE_STATUSES;
2772
3033
  exports.DisputesModule = DisputesModule;
2773
3034
  exports.IDBTokenStore = IDBTokenStore;
3035
+ exports.IamModule = IamModule;
2774
3036
  exports.LISTING_STATUSES = LISTING_STATUSES;
2775
3037
  exports.ListingsModule = ListingsModule;
2776
3038
  exports.MailerModule = MailerModule;
3039
+ exports.MarketsModule = MarketsModule;
2777
3040
  exports.MemoryTokenStore = MemoryTokenStore;
2778
3041
  exports.MessagesModule = MessagesModule;
2779
3042
  exports.MitumbaClient = MitumbaClient;
@@ -2795,11 +3058,20 @@ exports.UploadsModule = UploadsModule;
2795
3058
  exports.VaziModule = VaziModule;
2796
3059
  exports.WholesaleModule = WholesaleModule;
2797
3060
  exports.WishlistsModule = WishlistsModule;
3061
+ exports.activeMarkets = activeMarkets;
2798
3062
  exports.baleShareUrl = baleShareUrl;
2799
3063
  exports.createTokenStore = createTokenStore;
3064
+ exports.findMarket = findMarket;
3065
+ exports.formatAmount = formatAmount;
3066
+ exports.formatMoney = formatMoney;
3067
+ exports.formatMoneyCompact = formatMoneyCompact;
3068
+ exports.formatPhone = formatPhone;
2800
3069
  exports.isConsentRequired = isConsentRequired;
2801
3070
  exports.isMessagingAPIError = isMessagingAPIError;
3071
+ exports.isValidPhone = isValidPhone;
2802
3072
  exports.listingShareUrl = listingShareUrl;
3073
+ exports.maskPhone = maskPhone;
3074
+ exports.normalizePhone = normalizePhone;
2803
3075
  exports.storeShareUrl = storeShareUrl;
2804
3076
  //# sourceMappingURL=index.js.map
2805
3077
  //# sourceMappingURL=index.js.map