komga/next-ui/src/api/komga-client.ts
2025-11-28 16:05:22 +08:00

49 lines
1.2 KiB
TypeScript

import type { Middleware } from 'openapi-fetch'
import createClient from 'openapi-fetch'
import type { paths } from '@/generated/openapi/komga'
import { API_BASE_URL } from '@/api/base'
// Middleware that throws on error, so it works with Pinia Colada
const coladaMiddleware: Middleware = {
async onResponse({ response }: { response: Response }) {
if (!response.ok) {
let body: SpringError = {}
try {
body = await response.json()
} catch (ignoreErr) {}
throw new Error(`${response.url}: ${response.status} ${response.statusText}`, {
cause: {
body: body,
status: response.status,
message: body?.message,
},
})
}
// return response untouched
return undefined
},
onError() {
throw new Error('error', { cause: {} })
},
}
const client = createClient<paths>({
baseUrl: API_BASE_URL,
// required to pass the session cookie on all requests
credentials: 'include',
// required to avoid browser basic-auth popups
headers: { 'X-Requested-With': 'XMLHttpRequest' },
})
client.use(coladaMiddleware)
type SpringError = {
message?: string
}
export type ErrorCause = {
body?: unknown
status?: number
message?: string
}
export const komgaClient = client