@mitumba/sdk 1.18.0 → 1.19.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 +20 -9
- package/dist/index.d.ts +20 -9
- package/dist/index.js +64 -25
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +64 -25
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -108,15 +108,25 @@ var APIClient = class {
|
|
|
108
108
|
this.config = config;
|
|
109
109
|
this.tokenStore = config.tokenStore ?? createTokenStore();
|
|
110
110
|
if (config.token) {
|
|
111
|
-
this.tokenStore.setTokens(config.token, config.refreshToken
|
|
111
|
+
this.tokenStore.setTokens(config.token, config.refreshToken || "");
|
|
112
112
|
}
|
|
113
113
|
}
|
|
114
114
|
async setToken(token, refreshToken) {
|
|
115
115
|
this.config.token = token;
|
|
116
|
-
if (refreshToken) {
|
|
116
|
+
if (refreshToken && refreshToken.length > 0) {
|
|
117
117
|
this.config.refreshToken = refreshToken;
|
|
118
118
|
}
|
|
119
|
-
|
|
119
|
+
const effectiveRefresh = refreshToken && refreshToken.length > 0 ? refreshToken : this.config.refreshToken && this.config.refreshToken.length > 0 ? this.config.refreshToken : "";
|
|
120
|
+
await this.tokenStore.setTokens(token, effectiveRefresh);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Set both tokens explicitly. Requires a valid refresh token.
|
|
124
|
+
* Preferred over setToken() to avoid dropping the refresh token.
|
|
125
|
+
*/
|
|
126
|
+
async setSession(tokens) {
|
|
127
|
+
this.config.token = tokens.access_token;
|
|
128
|
+
this.config.refreshToken = tokens.refresh_token;
|
|
129
|
+
await this.tokenStore.setTokens(tokens.access_token, tokens.refresh_token);
|
|
120
130
|
}
|
|
121
131
|
getToken() {
|
|
122
132
|
return this.config.token;
|
|
@@ -154,7 +164,7 @@ var APIClient = class {
|
|
|
154
164
|
const refresh = await this.tokenStore.getRefreshToken();
|
|
155
165
|
if (refresh) this.config.refreshToken = refresh;
|
|
156
166
|
}
|
|
157
|
-
if (this.config.token && isExpiringSoon(this.config.token, 60) && this.config.refreshToken) {
|
|
167
|
+
if (this.config.token && isExpiringSoon(this.config.token, 60) && this.config.refreshToken && this.config.refreshToken.length > 0) {
|
|
158
168
|
await this.handleTokenRefresh();
|
|
159
169
|
}
|
|
160
170
|
}
|
|
@@ -220,12 +230,20 @@ var APIClient = class {
|
|
|
220
230
|
}
|
|
221
231
|
break;
|
|
222
232
|
}
|
|
223
|
-
if (response.status === 401 &&
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
233
|
+
if (response.status === 401 && !path.includes("/auth/refresh") && !path.includes("/auth/login") && !path.includes("/auth/register")) {
|
|
234
|
+
const hasRefreshToken = this.config.refreshToken && this.config.refreshToken.length > 0;
|
|
235
|
+
if (hasRefreshToken) {
|
|
236
|
+
try {
|
|
237
|
+
await this.handleTokenRefresh();
|
|
238
|
+
headers.set("Authorization", `Bearer ${this.config.token}`);
|
|
239
|
+
response = await fetch(url.toString(), { ...init, headers });
|
|
240
|
+
} catch {
|
|
241
|
+
if (this.config.onAuthExpired) {
|
|
242
|
+
this.config.onAuthExpired();
|
|
243
|
+
}
|
|
244
|
+
throw new APIError(401, { error: "session_expired", message: "Session expired. Please log in again." });
|
|
245
|
+
}
|
|
246
|
+
} else {
|
|
229
247
|
if (this.config.onAuthExpired) {
|
|
230
248
|
this.config.onAuthExpired();
|
|
231
249
|
}
|
|
@@ -297,6 +315,9 @@ var APIClient = class {
|
|
|
297
315
|
};
|
|
298
316
|
|
|
299
317
|
// src/modules/auth.ts
|
|
318
|
+
function isAuthTokens(result) {
|
|
319
|
+
return typeof result === "object" && result !== null && "access_token" in result && "refresh_token" in result;
|
|
320
|
+
}
|
|
300
321
|
var AuthModule = class {
|
|
301
322
|
constructor(client) {
|
|
302
323
|
this.client = client;
|
|
@@ -304,19 +325,27 @@ var AuthModule = class {
|
|
|
304
325
|
client;
|
|
305
326
|
/**
|
|
306
327
|
* Register a new account.
|
|
307
|
-
* If using EmailRegisterInput, returns AuthTokens.
|
|
328
|
+
* If using EmailRegisterInput, returns AuthTokens (auto-persisted).
|
|
308
329
|
* If using PhoneRegisterInput, returns MessageResponse (OTP sent).
|
|
309
330
|
*/
|
|
310
331
|
async register(input, options) {
|
|
311
|
-
|
|
332
|
+
const result = await this.client.post("/auth/register", input, options);
|
|
333
|
+
if (isAuthTokens(result)) {
|
|
334
|
+
await this.client.setSession(result);
|
|
335
|
+
}
|
|
336
|
+
return result;
|
|
312
337
|
}
|
|
313
338
|
/**
|
|
314
339
|
* Log in to an existing account.
|
|
315
|
-
* If using EmailLoginInput, returns AuthTokens (or TwoFactorRequired
|
|
340
|
+
* If using EmailLoginInput, returns AuthTokens (auto-persisted) or TwoFactorRequired.
|
|
316
341
|
* If using PhoneLoginInput, returns MessageResponse (OTP sent).
|
|
317
342
|
*/
|
|
318
343
|
async login(input, options) {
|
|
319
|
-
|
|
344
|
+
const result = await this.client.post("/auth/login", input, options);
|
|
345
|
+
if (isAuthTokens(result)) {
|
|
346
|
+
await this.client.setSession(result);
|
|
347
|
+
}
|
|
348
|
+
return result;
|
|
320
349
|
}
|
|
321
350
|
/**
|
|
322
351
|
* Send an OTP code to a phone number.
|
|
@@ -325,26 +354,31 @@ var AuthModule = class {
|
|
|
325
354
|
return this.client.post("/auth/otp/send", input, options);
|
|
326
355
|
}
|
|
327
356
|
/**
|
|
328
|
-
* Verify an OTP code.
|
|
357
|
+
* Verify an OTP code. Tokens are auto-persisted.
|
|
329
358
|
*/
|
|
330
359
|
async verifyOtp(input, options) {
|
|
331
|
-
|
|
360
|
+
const result = await this.client.post("/auth/otp/verify", input, options);
|
|
361
|
+
await this.client.setSession(result);
|
|
362
|
+
return result;
|
|
332
363
|
}
|
|
333
364
|
/**
|
|
334
365
|
* Refresh the access token using a refresh token.
|
|
335
366
|
*/
|
|
336
367
|
async refresh(input, options) {
|
|
337
|
-
|
|
368
|
+
const result = await this.client.post("/auth/refresh", input, options);
|
|
369
|
+
await this.client.setSession(result);
|
|
370
|
+
return result;
|
|
338
371
|
}
|
|
339
372
|
/**
|
|
340
|
-
* Revoke the refresh token and log out.
|
|
373
|
+
* Revoke the refresh token and log out. Clears stored session.
|
|
341
374
|
*/
|
|
342
375
|
async logout(input, options) {
|
|
343
|
-
|
|
376
|
+
const result = await this.client.post("/auth/logout", input, options);
|
|
377
|
+
await this.client.clearToken();
|
|
378
|
+
return result;
|
|
344
379
|
}
|
|
345
380
|
/**
|
|
346
381
|
* Request a password reset email.
|
|
347
|
-
* Sends a reset link to the provided email address.
|
|
348
382
|
*/
|
|
349
383
|
async forgotPassword(input, options) {
|
|
350
384
|
return this.client.post("/auth/forgot-password", input, options);
|
|
@@ -368,28 +402,27 @@ var AuthModule = class {
|
|
|
368
402
|
return this.client.post("/auth/onboarding/complete", input, options);
|
|
369
403
|
}
|
|
370
404
|
/**
|
|
371
|
-
* Verify 2FA code during login
|
|
405
|
+
* Verify 2FA code during login. Tokens are auto-persisted.
|
|
372
406
|
*/
|
|
373
407
|
async verify2FA(input, options) {
|
|
374
|
-
|
|
408
|
+
const result = await this.client.post("/auth/2fa/login", input, options);
|
|
409
|
+
await this.client.setSession(result);
|
|
410
|
+
return result;
|
|
375
411
|
}
|
|
376
412
|
/**
|
|
377
413
|
* Send a verification code to the user's email.
|
|
378
|
-
* If unauthenticated, pass the email address.
|
|
379
414
|
*/
|
|
380
415
|
async sendVerificationCode(email, options) {
|
|
381
416
|
return this.client.post("/auth/verify-email/send", email ? { email } : void 0, options);
|
|
382
417
|
}
|
|
383
418
|
/**
|
|
384
419
|
* Verify the email with the 6-digit code.
|
|
385
|
-
* If unauthenticated, pass the email address.
|
|
386
420
|
*/
|
|
387
421
|
async verifyEmail(code, email, options) {
|
|
388
422
|
return this.client.post("/auth/verify-email/confirm", email ? { code, email } : { code }, options);
|
|
389
423
|
}
|
|
390
424
|
/**
|
|
391
425
|
* Upgrade the current user to a seller role.
|
|
392
|
-
* Idempotent — returns success if already a seller.
|
|
393
426
|
*/
|
|
394
427
|
async becomeSeller(input, options) {
|
|
395
428
|
return this.client.post("/auth/become-seller", input, options);
|
|
@@ -1362,6 +1395,12 @@ var MitumbaClient = class {
|
|
|
1362
1395
|
setToken(token, refreshToken) {
|
|
1363
1396
|
this.api.setToken(token, refreshToken);
|
|
1364
1397
|
}
|
|
1398
|
+
/**
|
|
1399
|
+
* Set both tokens explicitly. Preferred — prevents dropping the refresh token.
|
|
1400
|
+
*/
|
|
1401
|
+
setSession(tokens) {
|
|
1402
|
+
return this.api.setSession(tokens);
|
|
1403
|
+
}
|
|
1365
1404
|
/**
|
|
1366
1405
|
* Get the current access token.
|
|
1367
1406
|
*/
|