injext client

This commit is contained in:
john 2025-05-20 11:47:16 +02:00
parent bc97d009ae
commit 0cbcab6597
5 changed files with 38 additions and 34 deletions

View file

@ -1,20 +1,21 @@
import { paths } from './schema.ts'
import createClient, { Middleware } from 'openapi-fetch'
import createClient, { Client, Middleware } from 'openapi-fetch'
import { dispatchMessage } from '../messageBus/messageBus.ts'
const client = createClient<paths>({ baseUrl: import.meta.env.VITE_API_URL })
export type ApiClient = Client<paths>
const UnauthorizedHandlerMiddleware: Middleware = {
async onResponse({ response }) {
console.debug(response.headers.getSetCookie())
console.debug(response.headers.get('set-cookie'))
if (response.status === 401) {
dispatchMessage('auth:unauthorized', null)
}
},
export function initClient(): ApiClient {
const client = createClient<paths>({ baseUrl: import.meta.env.VITE_API_URL })
const UnauthorizedHandlerMiddleware: Middleware = {
async onResponse({ response }) {
console.debug(response.headers.getSetCookie())
console.debug(response.headers.get('set-cookie'))
if (response.status === 401) {
dispatchMessage('auth:unauthorized', null)
}
},
}
client.use(UnauthorizedHandlerMiddleware)
return client
}
client.use(UnauthorizedHandlerMiddleware)
// todo inject this if necessary
export default client