mirror of
https://github.com/gotson/komga.git
synced 2025-12-21 16:03:03 +01:00
49 lines
1.2 KiB
TypeScript
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
|