UNPKG

resty-client

Version:

Simple HTTP and REST client library for Browser and Node

99 lines (95 loc) 3.38 kB
import { AxiosRequestConfig, AxiosPromise, AxiosResponse, AxiosInstance } from 'axios'; interface ApisMap { [key: string]: { server?: string; url: string; method: 'get' | 'GET' | 'post' | 'POST' | 'put' | 'PUT' | 'delete' | 'DELETE' | 'options' | 'OPTIONS' | 'patch' | 'PATCH' | 'head' | 'HEAD'; }; } interface ServerMap { [key: string]: Config; } interface BaseMap { prod?: string; stage?: string; test?: string; dev?: string; local?: string; [key: string]: any; } interface Config { default?: boolean; baseURL?: string; baseMap: BaseMap; } interface RequestOptions extends AxiosRequestConfig { rest?: Record<string, any>; } declare type ResolvedMiddleWare<T = any> = (val: T) => T | Promise<T>; declare type RejectedMiddleWare = (err: any) => any; interface Middleware { onFulfilled?: ResolvedMiddleWare; onRejected?: RejectedMiddleWare; } interface ApisConfig extends AxiosRequestConfig { rest?: Record<string, any>; } interface Apis { [key: string]: (config?: ApisConfig) => AxiosPromise; } declare type ApisInstance = Apis; declare type RestyResponse<T> = AxiosResponse<T>; declare class Client { static reqMiddleware: Middleware[]; static resMiddleware: Middleware[]; static useReq(onFulfilled?: ResolvedMiddleWare<RequestOptions>, onRejected?: RejectedMiddleWare): void; static useRes(onFulfilled?: ResolvedMiddleWare<AxiosResponse>, onRejected?: RejectedMiddleWare): void; static create(common?: RequestOptions, serverMap?: ServerMap, apiMap?: ApisMap): Client; common: RequestOptions; base: string; serverMap: ServerMap; apiMap: ApisMap; apis: ApisInstance; instance: AxiosInstance; constructor(common?: RequestOptions, serverMap?: ServerMap, apiMap?: ApisMap); /** * 获取默认的配置 * @returns */ getDefault(): string; /** * 配置正确的baseURL,如果没有baseURL就读默认的 * @returns */ formatConfigUrl(): void; get<T extends Record<string, any> = any>(url: string, request: RequestOptions): Promise<RestyResponse<T>>; delete<T extends Record<string, any> = any>(url: string, request: RequestOptions): Promise<RestyResponse<T>>; post<T extends Record<string, any> = any>(url: string, request: RequestOptions): Promise<RestyResponse<T>>; put<T extends Record<string, any> = any>(url: string, request: RequestOptions): Promise<RestyResponse<T>>; patch<T extends Record<string, any> = any>(url: string, request: RequestOptions): Promise<RestyResponse<T>>; request<T extends Record<string, any> = any>(url: string, request: RequestOptions): Promise<RestyResponse<T>>; /** * 将url转换为restful风格 * @param url * @param rest * @returns */ private restful; /** * 将restful config配置合并 * @param config1 apis文件的基本配置信息 * @param config2 用户传入的配置信息 * @returns */ private rest2Combine; /** * 合并配置,转化为请求方法 * @returns */ private combine2Request; /** * client拦截器处理 */ private middleware; } export { Apis, ApisConfig, ApisInstance, ApisMap, BaseMap, Config, Middleware, RejectedMiddleWare, RequestOptions, ResolvedMiddleWare, RestyResponse, ServerMap, Client as default };