@nauth-toolkit/client 0.1.65 → 0.1.67

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 CHANGED
@@ -680,6 +680,44 @@ interface NAuthClientConfig {
680
680
  * base URL that points to the JSON-based mobile auth endpoints.
681
681
  */
682
682
  baseUrl: string;
683
+ /**
684
+ * Optional path prefix automatically prepended to all endpoint paths.
685
+ * Simplifies configuration when all auth endpoints share a common prefix.
686
+ *
687
+ * Benefits:
688
+ * - Avoids repeating `/auth` in every endpoint path or having to give it if using default endpoints in backend.
689
+ * - Cleaner endpoint configuration
690
+ * - Easier to change the auth path globally
691
+ *
692
+ * Note: Authorization tokens are still sent to ALL requests matching `baseUrl`,
693
+ * regardless of this prefix. This is a convenience feature, not a security boundary.
694
+ *
695
+ * @example Without authPathPrefix (verbose)
696
+ * ```typescript
697
+ * {
698
+ * baseUrl: 'https://api.example.com',
699
+ * endpoints: {
700
+ * profile: '/auth/profile',
701
+ * login: '/auth/login/mobile',
702
+ * mfaStatus: '/auth/mfa/status',
703
+ * }
704
+ * }
705
+ * ```
706
+ *
707
+ * @example With authPathPrefix (clean)
708
+ * ```typescript
709
+ * {
710
+ * baseUrl: 'https://api.example.com',
711
+ * authPathPrefix: '/auth',
712
+ * endpoints: {
713
+ * profile: '/profile', // SDK builds: /auth/profile
714
+ * login: '/login/mobile', // SDK builds: /auth/login/mobile
715
+ * mfaStatus: '/mfa/status', // SDK builds: /auth/mfa/status
716
+ * }
717
+ * }
718
+ * ```
719
+ */
720
+ authPathPrefix?: string;
683
721
  /**
684
722
  * How tokens are delivered between client and server.
685
723
  *
@@ -1651,6 +1689,7 @@ declare class NAuthClient {
1651
1689
  private getTokenDeliveryMode;
1652
1690
  /**
1653
1691
  * Build request URL by combining baseUrl with path.
1692
+ * Automatically prepends authPathPrefix if configured and not already in path or baseUrl.
1654
1693
  * @private
1655
1694
  */
1656
1695
  private buildUrl;
package/dist/index.d.ts CHANGED
@@ -680,6 +680,44 @@ interface NAuthClientConfig {
680
680
  * base URL that points to the JSON-based mobile auth endpoints.
681
681
  */
682
682
  baseUrl: string;
683
+ /**
684
+ * Optional path prefix automatically prepended to all endpoint paths.
685
+ * Simplifies configuration when all auth endpoints share a common prefix.
686
+ *
687
+ * Benefits:
688
+ * - Avoids repeating `/auth` in every endpoint path or having to give it if using default endpoints in backend.
689
+ * - Cleaner endpoint configuration
690
+ * - Easier to change the auth path globally
691
+ *
692
+ * Note: Authorization tokens are still sent to ALL requests matching `baseUrl`,
693
+ * regardless of this prefix. This is a convenience feature, not a security boundary.
694
+ *
695
+ * @example Without authPathPrefix (verbose)
696
+ * ```typescript
697
+ * {
698
+ * baseUrl: 'https://api.example.com',
699
+ * endpoints: {
700
+ * profile: '/auth/profile',
701
+ * login: '/auth/login/mobile',
702
+ * mfaStatus: '/auth/mfa/status',
703
+ * }
704
+ * }
705
+ * ```
706
+ *
707
+ * @example With authPathPrefix (clean)
708
+ * ```typescript
709
+ * {
710
+ * baseUrl: 'https://api.example.com',
711
+ * authPathPrefix: '/auth',
712
+ * endpoints: {
713
+ * profile: '/profile', // SDK builds: /auth/profile
714
+ * login: '/login/mobile', // SDK builds: /auth/login/mobile
715
+ * mfaStatus: '/mfa/status', // SDK builds: /auth/mfa/status
716
+ * }
717
+ * }
718
+ * ```
719
+ */
720
+ authPathPrefix?: string;
683
721
  /**
684
722
  * How tokens are delivered between client and server.
685
723
  *
@@ -1651,6 +1689,7 @@ declare class NAuthClient {
1651
1689
  private getTokenDeliveryMode;
1652
1690
  /**
1653
1691
  * Build request URL by combining baseUrl with path.
1692
+ * Automatically prepends authPathPrefix if configured and not already in path or baseUrl.
1654
1693
  * @private
1655
1694
  */
1656
1695
  private buildUrl;
package/dist/index.mjs CHANGED
@@ -314,10 +314,13 @@ var TokenManager = class {
314
314
  *
315
315
  * @param refreshFn - function performing refresh request
316
316
  */
317
- async refreshOnce(refreshFn) {
317
+ async refreshOnce(refreshFn, options) {
318
+ const shouldPersist = options?.persist !== false;
318
319
  if (!this.refreshPromise) {
319
320
  this.refreshPromise = refreshFn().then(async (tokens) => {
320
- await this.setTokens(tokens);
321
+ if (shouldPersist) {
322
+ await this.setTokens(tokens);
323
+ }
321
324
  return tokens;
322
325
  }).catch((error) => {
323
326
  throw error;
@@ -784,7 +787,7 @@ var NAuthClient = class {
784
787
  const refreshFn = async () => {
785
788
  return this.post(this.config.endpoints.refresh, body, false);
786
789
  };
787
- const tokens = await this.tokenManager.refreshOnce(refreshFn);
790
+ const tokens = await this.tokenManager.refreshOnce(refreshFn, { persist: tokenDelivery === "json" });
788
791
  this.config.onTokenRefresh?.();
789
792
  this.eventEmitter.emit({ type: "auth:refresh", data: { success: true }, timestamp: Date.now() });
790
793
  return tokens;
@@ -1425,10 +1428,15 @@ var NAuthClient = class {
1425
1428
  }
1426
1429
  /**
1427
1430
  * Build request URL by combining baseUrl with path.
1431
+ * Automatically prepends authPathPrefix if configured and not already in path or baseUrl.
1428
1432
  * @private
1429
1433
  */
1430
1434
  buildUrl(path) {
1431
- return `${this.config.baseUrl}${path}`;
1435
+ const normalizedPath = path.startsWith("/") ? path : `/${path}`;
1436
+ const baseUrlEndsWithPrefix = this.config.authPathPrefix && this.config.baseUrl.endsWith(this.config.authPathPrefix);
1437
+ const effectivePath = this.config.authPathPrefix && !baseUrlEndsWithPrefix && !normalizedPath.startsWith(this.config.authPathPrefix) ? `${this.config.authPathPrefix}${normalizedPath}` : normalizedPath;
1438
+ const normalizedBaseUrl = this.config.baseUrl.endsWith("/") ? this.config.baseUrl.slice(0, -1) : this.config.baseUrl;
1439
+ return `${normalizedBaseUrl}${effectivePath}`;
1432
1440
  }
1433
1441
  /**
1434
1442
  * Build request headers for authentication.