femto-webapp/src/app/api/client.ts
2025-06-11 23:12:03 +02:00

19 lines
586 B
TypeScript

import { paths } from './schema.ts'
import createClient, { Middleware } from 'openapi-fetch'
import { dispatchMessage } from '../messageBus/messageBus.ts'
export const initClient = () => {
const client = createClient<paths>({ baseUrl: import.meta.env.VITE_API_URL })
const UnauthorizedHandlerMiddleware: Middleware = {
async onResponse({ response }) {
if (response.status === 401) {
dispatchMessage('auth:unauthorized', null)
}
},
}
client.use(UnauthorizedHandlerMiddleware)
return client
}
export type ApiClient = ReturnType<typeof initClient>