- useApi.ts: composable centralizado com Authorization header automático - usuarios.vue: GET /api/v1/users — lista usuários reais do tenant - configuracoes.vue: GET/PUT /api/v1/tenant/me — carrega e salva dados reais Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
18 lines
568 B
TypeScript
18 lines
568 B
TypeScript
// Wrapper sobre $fetch que injeta o Authorization header automaticamente.
|
|
export function useApi() {
|
|
const { public: { apiBase } } = useRuntimeConfig()
|
|
const token = useCookie<string | null>('auth_token')
|
|
|
|
function apiFetch<T>(path: string, options: Parameters<typeof $fetch>[1] = {}): Promise<T> {
|
|
return $fetch<T>(`${apiBase}${path}`, {
|
|
...options,
|
|
headers: {
|
|
...(options.headers as Record<string, string> || {}),
|
|
...(token.value ? { Authorization: `Bearer ${token.value}` } : {}),
|
|
},
|
|
})
|
|
}
|
|
|
|
return { apiFetch }
|
|
}
|