2 Commits

Author SHA1 Message Date
de07ccac8e chore(apps/app): drop unnecessary async on synchronous error handlers
Both interceptor error handlers in lib/axios.ts were declared
`async` but contain zero `await` calls — the request handler
just rethrows, and the response handler walks a synchronous
status-code branching tree before rethrowing. axios accepts both
sync and async handler signatures, so dropping the keyword is
mechanical and behavior-neutral.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-05-04 22:40:12 +02:00
853939e8b8 refactor(apps/app): decouple axios from impersonation sessionStorage contract
Chose Option A from the follow-up brief: useImpersonationStore
already holds an `ImpersonationState` ref hydrated from
sessionStorage at store-init and exposes the active impersonation
target user as a public `targetUserId` computed. The store is the
canonical source; sessionStorage is just its persistence sidecar.

Adds a fifth callback `getImpersonationTargetUserId: () => string
| null` to AxiosBindingsDeps and replaces the
sessionStorage.getItem('crewli_impersonation') + JSON.parse block
in the request interceptor with a single `deps.getImpersonationTargetUserId()`
call. The bindings plugin wires it to
`useImpersonationStore().targetUserId`.

After this commit lib/axios.ts has zero references to
sessionStorage and zero magic strings about impersonation
persistence — the only persistence-mechanism knowledge left is in
useImpersonationStore (where it belongs) and in
plugins/3.axios-bindings.ts (allowed to know about stores). The
HTTP module is now unambiguously pure infrastructure.

Behavior preserved 1:1: the store hydrates from sessionStorage
synchronously inside the defineStore factory, so the very first
HTTP request after page load sees the same target user id as
before.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-05-04 22:39:04 +02:00
2 changed files with 7 additions and 14 deletions

View File

@@ -9,6 +9,7 @@ import type { AxiosInstance, InternalAxiosRequestConfig } from 'axios'
*/
export interface AxiosBindingsDeps {
getActiveOrgId: () => string | null
getImpersonationTargetUserId: () => string | null
notify: (message: string, level: 'error' | 'warning') => void
onAuthFail: () => void
onImpersonationRevoked: () => void
@@ -31,25 +32,16 @@ export function registerInterceptors(client: AxiosInstance, deps: AxiosBindingsD
if (orgId)
config.headers['X-Organisation-Id'] = orgId
// Read impersonation header directly from sessionStorage — no store dep.
const impersonationData = sessionStorage.getItem('crewli_impersonation')
if (impersonationData) {
try {
const parsed = JSON.parse(impersonationData) as { targetUserId?: string }
if (parsed.targetUserId)
config.headers['X-Impersonate-User'] = parsed.targetUserId
}
catch {
// Invalid data — ignore
}
}
const impersonationTargetUserId = deps.getImpersonationTargetUserId()
if (impersonationTargetUserId)
config.headers['X-Impersonate-User'] = impersonationTargetUserId
if (import.meta.env.DEV)
console.log(`🚀 ${config.method?.toUpperCase()} ${config.url}`, config.data)
return config
},
async error => { throw error },
error => { throw error },
)
client.interceptors.response.use(
@@ -59,7 +51,7 @@ export function registerInterceptors(client: AxiosInstance, deps: AxiosBindingsD
return response
},
async error => {
error => {
if (import.meta.env.DEV)
console.error(`${error.response?.status} ${error.config?.url}`, error.response?.data)

View File

@@ -12,6 +12,7 @@ import { useOrganisationStore } from '@/stores/useOrganisationStore'
export default function (_: App): void {
registerInterceptors(apiClient, {
getActiveOrgId: () => useOrganisationStore().activeOrganisationId,
getImpersonationTargetUserId: () => useImpersonationStore().targetUserId,
notify: (message, level) => useNotificationStore().show(message, level),
onAuthFail: () => {
const authStore = useAuthStore()