@gaiot/apis
Version:
aiot apis
42 lines (41 loc) • 2.17 kB
TypeScript
import { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import { AppResponse, Paged } from './model';
export declare class BaseApi {
private axios;
private errorHandler?;
constructor(axios: AxiosInstance, errorHandler?: ApiErrorHandler | undefined);
get<T>(path: string, params?: any, errInvisable?: boolean): Promise<T>;
listPaged<T>(path: string, params?: any): Promise<Paged<T>>;
list<T>(path: string, params?: any): Promise<T>;
delete<T>(path: string, params?: any): Promise<T>;
post<T>(path: string, data: any, params?: any, config?: AxiosRequestConfig): Promise<T>;
postForm<T>(path: string, formData: FormData, params?: any): Promise<T>;
patch<T>(path: string, data: any, params?: any): Promise<T>;
put<T>(path: string, data: any): Promise<T>;
upload<T>(path: string, data: any, config?: AxiosRequestConfig): Promise<T>;
getNonStandard<T>(path: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T, any> | undefined>;
postNonStandard(path: string, data: any, params?: any, config?: AxiosRequestConfig): Promise<any>;
handleResponse<T>(response: Promise<AxiosResponse<AppResponse<T>>>, errInvisable?: boolean): Promise<T>;
handleAppError<T>(response: AppResponse<T>, errInvisable?: boolean): AppResponse<T>;
handleError(error: AxiosError): Error;
}
export interface ApiErrorHandler {
onNetworkError?(): void;
onHttpStatusError?(status: number, message: string, response: AxiosResponse): void;
onAppStatusError?(status: number, message: string, errInvisable?: boolean): void;
}
/***
* 判断是否需要修改message的错误
* 不应该让用户看到的报错信息应包含在 10002 - 10012 区间。
* @param {number} status api error code
* @returns {boolean} 是否为需要修改的 error
*/
export declare const needPrettifyMsg: (status: number) => boolean;
/**
* 优化message可读性
* @param {number} status api error code
* @param {string} message 原始的 api error message
* @returns {string} 优化后的 message
*/
export declare const prettifyMsg: (status: number, message?: string) => string;
export * from './model';