mulink 1.2.3 → 1.2.4

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.
@@ -1767,6 +1767,42 @@ export class BaseApiClient {
1767
1767
  // Handle HTTP errors
1768
1768
  if (!response.ok) {
1769
1769
  const errorData = await this.parseErrorResponse(response)
1770
+
1771
+ // Handle token refresh for 401 errors (token expired)
1772
+ if (response.status === 401 && !skipAuth && ${this.configuration.auth?.enabled ? "true" : "false"} && attempt === 0) {
1773
+ try {
1774
+ // Try to refresh token using auth client
1775
+ const authModule = await import('${authPath}').catch(() => null)
1776
+ if (authModule?.authClient || authModule?.AuthClient) {
1777
+ const authClient = authModule.authClient || authModule.AuthClient.getInstance()
1778
+ const tokens = authClient.getTokens?.()
1779
+
1780
+ if (tokens?.refresh_token) {
1781
+ try {
1782
+ await authClient.refreshToken(tokens.refresh_token)
1783
+ // Token refreshed successfully - retry request with new token
1784
+ console.log('[API Client] Token refreshed, retrying request')
1785
+ // Update auth headers and retry
1786
+ requestConfig.headers = {
1787
+ ...requestConfig.headers,
1788
+ ...(await this.getAuthHeaders())
1789
+ }
1790
+ // Continue to retry the request
1791
+ continue
1792
+ } catch (refreshError: any) {
1793
+ // Token refresh failed - proceed with error handling
1794
+ console.warn('[API Client] Token refresh failed:', refreshError)
1795
+ }
1796
+ }
1797
+ }
1798
+ } catch (refreshAttemptError) {
1799
+ // Auth client not available - proceed with error handling
1800
+ if (process.env.NODE_ENV === 'development') {
1801
+ console.warn('[API Client] Could not attempt token refresh:', refreshAttemptError)
1802
+ }
1803
+ }
1804
+ }
1805
+
1770
1806
  const apiError = new ApiError(
1771
1807
  errorData.message || \`HTTP \${response.status}: \${response.statusText}\`,
1772
1808
  response.status,
@@ -2001,7 +2037,8 @@ async get<TData>(
2001
2037
  const authErrorHandlerPath = errorHandling?.authErrorHandlerPath || "@/lib/auth-error-handler";
2002
2038
  const authErrorMiddlewareCode = enableAuthErrorHandling ? `
2003
2039
  // Authentication error middleware
2004
- // Handles 401/403 errors and redirects to login when needed
2040
+ // Handles 401/403 errors after token refresh has been attempted by the base client
2041
+ // Note: Token refresh is handled automatically by the base client before this middleware runs
2005
2042
  export const authErrorMiddleware: RequestMiddleware = {
2006
2043
  name: 'auth-error',
2007
2044
  onError: async (error) => {
@@ -2022,7 +2059,7 @@ export const authErrorMiddleware: RequestMiddleware = {
2022
2059
  errorCode.toLowerCase() === 'user_account_inactive'
2023
2060
  )
2024
2061
 
2025
- console.warn('[Auth Middleware] Authentication error detected:', {
2062
+ console.warn('[Auth Middleware] Authentication error detected (token refresh already attempted):', {
2026
2063
  status,
2027
2064
  message: errorMessage,
2028
2065
  errorCode,
@@ -8711,6 +8748,40 @@ export function isAuthError(error: unknown): boolean {
8711
8748
  return false
8712
8749
  }
8713
8750
 
8751
+ /**
8752
+ * Type guard to check if an error is a preventive unauthorized error
8753
+ * Preventive errors occur when no authentication is provided (before API call)
8754
+ * These should be handled silently without showing notifications
8755
+ *
8756
+ * Optimized for performance: early returns and minimal string operations
8757
+ */
8758
+ export function isPreventiveUnauthorizedError(error: unknown): boolean {
8759
+ if (!isAuthError(error)) return false
8760
+
8761
+ const apiError = error as ApiError
8762
+
8763
+ // Early return if response exists (reactive error, not preventive)
8764
+ if (apiError.response) return false
8765
+
8766
+ // Extract error data once to minimize property access
8767
+ const errorData = apiError.data as any
8768
+ const errorCode = errorData?.error_code || errorData?.code
8769
+
8770
+ // Fast path: check error code first (most reliable indicator)
8771
+ if (errorCode && errorCode.toLowerCase() === 'unauthorized') {
8772
+ return true
8773
+ }
8774
+
8775
+ // Fallback: check error message (only if no error code found)
8776
+ const errorMessage = errorData?.detail || apiError.message
8777
+ if (errorMessage) {
8778
+ const msg = errorMessage.toLowerCase().trim()
8779
+ return msg === 'unauthorized'
8780
+ }
8781
+
8782
+ return false
8783
+ }
8784
+
8714
8785
  /**
8715
8786
  * Type guard to check if an error is an inactive account error
8716
8787
  * Auto-detected account status patterns from schema
@@ -8763,19 +8834,30 @@ export async function handleAuthError(error: unknown, redirectTo?: string): Prom
8763
8834
  }
8764
8835
 
8765
8836
  const apiError = error as ApiError
8837
+
8838
+ // Extract error data once to avoid repeated property access
8839
+ const errorData = apiError.data as any
8840
+ const errorCode = errorData?.error_code || errorData?.code || ''
8841
+ const errorMessage = errorData?.detail || apiError.message || ''
8842
+
8843
+ // Check error types efficiently (single function call, no redundant checks)
8844
+ const isPreventive = isPreventiveUnauthorizedError(error)
8766
8845
  const isInactive = isInactiveAccountError(error)
8767
- const errorMessage = apiError.data?.detail || apiError.message || ''
8768
8846
 
8769
8847
  console.warn('[Auth Error Handler] Authentication error detected:', {
8770
8848
  status: apiError.status,
8771
8849
  message: errorMessage,
8850
+ errorCode,
8772
8851
  isInactive,
8852
+ isPreventive,
8773
8853
  })
8774
8854
 
8775
8855
  const toastInstance = await getToast()
8776
8856
 
8777
8857
  // Show appropriate error message based on error type
8778
- if (toastInstance) {
8858
+ // Skip toast for preventive unauthorized errors (no auth provided - silent handling)
8859
+ // Show toast for reactive auth errors (session expired, account issues, etc.)
8860
+ if (toastInstance && !isPreventive) {
8779
8861
  if (isInactive) {
8780
8862
  toastInstance.error('Your account has been deactivated. Please contact support for assistance.', {
8781
8863
  duration: 5000,
@@ -8840,6 +8922,7 @@ export function createAuthErrorHandler(redirectTo?: string) {
8840
8922
  metadata: {
8841
8923
  exports: [
8842
8924
  "isAuthError",
8925
+ "isPreventiveUnauthorizedError",
8843
8926
  "isInactiveAccountError",
8844
8927
  "handleAuthError",
8845
8928
  "createAuthErrorHandler"
@@ -10057,5 +10140,5 @@ ${errorMessages}`,
10057
10140
  };
10058
10141
 
10059
10142
  export { BridgeCore, BridgeError, BridgeLogger, ConfigurationLoader, FileSystemManager, GenerationError, LogLevel, NextJsCodeGenerator, OpenApiSchemaParser, SchemaParseError, ValidationError, VersionChecker, __name, checkAndNotifyUpdates, createBridgeVersionChecker };
10060
- //# sourceMappingURL=chunk-TXE66XFM.js.map
10061
- //# sourceMappingURL=chunk-TXE66XFM.js.map
10143
+ //# sourceMappingURL=chunk-RXLPPBZQ.js.map
10144
+ //# sourceMappingURL=chunk-RXLPPBZQ.js.map