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