初始化发布包 v0.11.1

This commit is contained in:
2026-04-15 15:19:04 +08:00
commit 9d283dad99
4 changed files with 915 additions and 0 deletions

202
types/index.d.ts vendored Normal file
View File

@@ -0,0 +1,202 @@
declare type HttpMethod = "GET" | "POST" | "PUT" | "DELETE";
/**
* @typedef {'GET' | 'POST' | 'PUT' | 'DELETE'} HttpMethod
*/
/**
* @typedef {Object} RequestOptions@typedef {Object} RequestOptions
* @property {string} [url]
* @property {HttpMethod} [method]
* @property {Record<string, unknown>} [params]
* @property {unknown} [data]
*/
/**
* 底层请求函数类型(兼容 axios 等)
* @typedef {(config: RequestOptions & Record<string, unknown>) => Promise<{ code: number, msg: string, data: unknown, res?: string }>} RequestFn
*/
/**
* 缓存条目:[code, msg, data, url, res]
* @typedef {[number, string, unknown, string, string]} CacheEntry
*/
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 {Record<string, unknown>} [base62param]
*/
list: (code: string, base62param?: Record<string, unknown>) => 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>} [json]
* @returns {string}
*/
encode: (json?: Record<string, 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;
/**
* 底层请求函数类型(兼容 axios 等)
*/
declare type RequestFn = (config: RequestOptions & Record<string, unknown>) => Promise<{
code: number;
msg: string;
data: unknown;
res?: string;
}>;
declare type RequestOptions = {
url?: string | undefined;
method?: HttpMethod | undefined;
params?: Record<string, unknown> | undefined;
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 { }