Files
ff-request-dist/types/index.d.ts

194 lines
5.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
declare interface Base62Param {
/** 当前选中的 tab 标识 */
tab?: string
/** 当前页码 */
page?: number
/** 每页条数 */
pageSize?: number
/** 筛选条件 */
condition?: Record<string, unknown>
/** 侧边栏选中项标识 */
sider?: string
/** 搜索关键词 */
keyword?: string
}
declare type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE'
declare class HttpRequest {
/**
* @param {string} [appKey]
* @param {string} [appSecret]
* @param {RequestFn} [reqFn]
*/
constructor(appKey?: string, appSecret?: string, reqFn?: RequestFn);
/**
* 初始化请求实例
* @param {string} [appKey]
* @param {string} [appSecret]
* @param {RequestFn} [reqFn]
*/
init: (appKey?: string, appSecret?: string, reqFn?: RequestFn) => void;
/**
* 发起请求,支持缓存与并发合并
* @param {RequestOptions} options
* @param {boolean} [isCacheRequest]
* @returns {any} 返回 Proxy 包裹的 Promise支持链式调用 .then / .catch / .msg / .resp
*/
request: ({ url, method, ...params }: RequestOptions, isCacheRequest?: boolean) => any;
/**
* GET 请求
* @param {string} url
* @param {Record<string, unknown>} [params]
*/
get: (url: string, params?: Record<string, unknown>) => any;
/**
* POST 请求
* @param {string} url
* @param {unknown} [data]
* @param {Record<string, unknown>} [params]
*/
post: (url: string, data?: unknown, params?: Record<string, unknown>) => any;
/**
* PUT 请求
* @param {string} url
* @param {unknown} [data]
* @param {Record<string, unknown>} [params]
*/
put: (url: string, data?: unknown, params?: Record<string, unknown>) => any;
/**
* DELETE 请求
* @param {string} url
* @param {unknown} [data]
* @param {Record<string, unknown>} [params]
*/
del: (url: string, data?: unknown, params?: Record<string, unknown>) => any;
/**
* 下载(待实现)
* @param {string} url
* @param {Record<string, unknown>} [params]
*/
download: (url: string, params?: Record<string, unknown>) => void;
/**
* 永久缓存 GET 请求
* @param {string} url
* @param {Record<string, unknown>} [params]
*/
cache: (url: string, params?: Record<string, unknown>) => any;
/**
* 通过 Base62 编码参数请求列表接口
* @param {string} code
* @param {Base62Param} [base62param]
*/
list: (code: string, base62param?: Base62Param) => any;
getAppInfo: () => void;
/**
* 清除缓存
* @param {boolean} [isSystem] 是否同时清除系统缓存(`/_/` 路由)
*/
refreshCache: (isSystem?: boolean) => void;
/**
* 解码 Base62 字符串为 JSON 对象
* @param {string} [str]
* @param {object} [defaultValue]
* @returns {unknown}
*/
decode: (str?: string, defaultValue?: object) => unknown;
/**
* 将 JSON 对象编码为 Base62 字符串
* @param {Record<string, unknown> | unknown[]} [json]
* @returns {string}
*/
encode: (json?: Record<string, unknown> | unknown[]) => string;
#private;
}
declare namespace HttpRequest {
/**
* 全局未处理 rejection 处理器,适配浏览器和 Node 环境
* @param {any} e
*/
function onUnhandledRejection(e: any): void;
/**
* 全局消息回调,默认打印到控制台
* @param {number} code
* @param {string} msg
*/
function onMsg(code: number, msg: string): void;
}
export default HttpRequest;
/**
* @typedef {Object} HttpResponseData@typedef {Object} HttpResponseData
* @property {number} code
* @property {string} message
* @property {unknown} data
* @property {string} url
* @property {string} res
*/
export declare class HttpResponse {
/**
* @param {number} code
* @param {string} message
* @param {unknown} data
* @param {string} url
* @param {string} res
*/
constructor(code: number, message: string, data: unknown, url: string, res: string);
/** @type {number} */
code: number;
/** @type {string} */
message: string;
/** @type {unknown} */
data: unknown;
/** @type {string} */
url: string;
/** @type {string} */
res: string;
#private;
}
export declare function NewHttpResponse(code: number, message: string, data: unknown, url?: string, res?: string): HttpResponse;
declare type RequestFn = (
config: RequestOptions & Record<string, unknown>
) => Promise<{ code: number; msg: string; data: unknown; res?: string }>
declare interface RequestOptions {
url?: string
method?: HttpMethod
params?: Record<string, unknown>
data?: unknown
}
/**
* DJB2 算法 —— 将字符串散列为十六进制字符串
* @param {string} input
* @returns {string}
*/
export declare function signature(input: string): string;
/**
* 将字符串编码为 Uint8Array
* @param {string} str
* @returns {Uint8Array}
*/
export declare function str2uint8array(str: string): Uint8Array;
/**
* 去除字符串首尾指定字符
* @param {string} str
* @param {string} char
* @returns {string}
*/
export declare function trim(str: string, char: string): string;
/**
* 将 ArrayBuffer 解码为字符串
* @param {ArrayBuffer} buffer
* @returns {string}
*/
export declare function uint8array2str(buffer: ArrayBuffer): string;
export { }