nuxt-advanced-fetch
Version:
Enhances Nuxt 3 $fetch with lifecycle handlers, dynamic management, and custom fetch instances for modular API requests.
35 lines (29 loc) • 959 B
TypeScript
import type { NitroFetchOptions } from 'nitropack'
import type { FetchContext, FetchResponse } from 'ofetch'
export type IFetchOptions = NitroFetchOptions<'json'>
export interface IApiHandlerTypes {
onRequest: FetchContext
onRequestError: FetchContext & { error: Error }
onResponse: FetchContext
onResponseError: FetchContext & { response: FetchResponse<unknown> }
}
export type IApiHandlers = {
[K in keyof IApiHandlerTypes]: Array<(context: IApiHandlerTypes[K]) => void>
}
export interface IApiPlugin {
<T>(url: string, options?: IFetchOptions): Promise<T>
addHandler<K extends keyof IApiHandlers>(
type: K,
handler: (context: IApiHandlerTypes[K]) => void
): void
removeHandler<K extends keyof IApiHandlers>(
type: K,
handler: (context: IApiHandlerTypes[K]) => void
): void
create(...args: Parameters<typeof $fetch['create']>): IApiPlugin
}
declare module '#app' {
interface NuxtApp {
$api: IApiPlugin
}
}