UNPKG

@uozi-admin/request

Version:

Easy to create curd request functions.

74 lines (67 loc) 3.77 kB
import * as axios from 'axios'; import { AxiosRequestConfig, Method, InternalAxiosRequestConfig, AxiosInterceptorOptions } from 'axios'; export * from 'axios'; declare const service: axios.AxiosInstance; declare function createRequestConfig(userConfig: AxiosRequestConfig): void; type RequestConfig = AxiosRequestConfig & { curd?: { methods?: { getList?: Method; getItem?: Method; createItem?: Method; updateItem?: Method; deleteItem?: Method; restoreItem?: Method; }; }; }; /** * @private * @description internal config */ declare const config: RequestConfig; /** * @description set request and curd api config */ declare function setRequestConfig(userConfig: RequestConfig): RequestConfig; declare const http: { get<T = any>(url: string, config?: RequestConfig): Promise<T>; post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T>; put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T>; delete<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>; patch<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T>; }; declare function useAxios(): { service: axios.AxiosInstance; setRequestInterceptor(onFulfilled: (value: InternalAxiosRequestConfig<any>) => InternalAxiosRequestConfig<any> | Promise<InternalAxiosRequestConfig<any>>, onRejected?: (error: any) => any | null, options?: AxiosInterceptorOptions): void; setResponseInterceptor(onFulfilled: (value: any) => any | Promise<any>, onRejected?: (error: any) => any | null): void; }; interface BaseCurdApi<T = any, P = any> { getList: (params?: Record<string, any>, config?: AxiosRequestConfig) => Promise<{ data: T[]; pagination: P; }>; getItem: (id: string | number, params?: Record<string, any>, config?: AxiosRequestConfig) => Promise<T>; createItem: (data: Record<string, any>, config?: AxiosRequestConfig) => Promise<T>; updateItem: (id: string | number, data: Record<string, any>, config?: AxiosRequestConfig) => Promise<T>; deleteItem: (id: string | number, params?: Record<string, any>, config?: AxiosRequestConfig) => Promise<any>; restoreItem: (id: string | number, params?: Record<string, any>, config?: AxiosRequestConfig) => Promise<any>; batchSave: (ids: (number | string)[], data: Record<string, any>, config?: AxiosRequestConfig) => Promise<any>; getUrl: () => string; } type CurdApi<T = any, P = any> = BaseCurdApi<T, P>; declare function useCurdApi<T = any, P = any, M = object>(url: string | (() => string), moreApis?: M): { getList: (params?: Record<string, any>, config?: AxiosRequestConfig) => Promise<{ data: T[]; pagination: P; }>; getItem: (id: string | number, params?: Record<string, any>, config?: AxiosRequestConfig) => Promise<T>; createItem: (data: Record<string, any>, config?: AxiosRequestConfig) => Promise<T>; updateItem: (id: string | number, data: Record<string, any>, config?: AxiosRequestConfig) => Promise<T>; deleteItem: (id: string | number, params?: Record<string, any>, config?: AxiosRequestConfig) => Promise<any>; restoreItem: (id: string | number, params?: Record<string, any>, config?: AxiosRequestConfig) => Promise<T>; batchSave: (ids: (number | string)[], data: Record<string, any>, config?: AxiosRequestConfig) => Promise<any>; getUrl: () => string; } & M; declare function extendCurdApi<T, M extends object = object>(baseCurd: CurdApi<T, any>, newApis: M): CurdApi<T, any> & M; export { type BaseCurdApi, type CurdApi, type RequestConfig, config, createRequestConfig, extendCurdApi, http, service, setRequestConfig, useAxios, useCurdApi };