UNPKG

@ace-fetch/vue

Version:

vue adapter for @ace-fetch/core.

90 lines (89 loc) 2.35 kB
import type { App } from 'vue-demi'; import type { Prefix, MethodUrl, RegistApi, FetchClient, PluginDefinition } from '@ace-fetch/core'; type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never; }; type XOR<T, U> = T | U extends object ? (Without<T, U> & U) | (Without<U, T> & T) : T | U; export interface Fetch { /** * Install fetch plugin */ install: (app: App) => void; /** * Current fetch client with `Vue.createFetch()` */ client: FetchClient; /** * Add a plugin to use every regist api */ use: (plugin: RegistApiPlugin) => Fetch; } /** * 'defineRegistApi' options */ export type DefineRegistApiOptions<C extends Record<string, MethodUrl>> = { /** * Base URL */ prefix?: Prefix; /** * Plugins apply current registApis */ plugins?: RegistApiPlugin[]; } & XOR<{ /** * Api definition * @example { * getUsers: typedUrl<User[]>`get /users`, * getUser: typedUrl<User, { id: string | number }>`/user/${'id'}`, * addUser: typedUrl<User, any, Partial<Omit<User, 'id'>>>`post /user`, * updateFile: typedUrl<string>({headers:{'content-type':'form-data'}})`post /upload/image` * } */ definition: C; }, { /** * Register api object * @deprecated use `definition` instead */ apis: C; }>; /** * Use regist api definition */ export interface UseRegistApiDefinition<C extends Record<string, MethodUrl>> { (fetch?: Fetch): RegistApi<C>; } /** * Plugin to extend every store. */ export type RegistApiPlugin = ReturnType<PluginDefinition<any>>; /** * Plugin extended options */ export interface DefineRegistApiOptionsInPlugin<C extends Record<string, MethodUrl>> extends Omit<DefineRegistApiOptions<C>, 'id'> { } declare module '@ace-fetch/core' { /** * Context argument passed to RegistApiPlugins. */ interface RegisterPluginContext<C extends Record<string, MethodUrl> = Record<string, MethodUrl>> { /** * Register id */ id: string | Symbol; /** * Fetch */ fetch: Fetch; /** * Current app created with `Vue.createApp()`. */ app: App; /** * regist api options */ options: DefineRegistApiOptionsInPlugin<C>; } } export {};