@hlw-uni-mp/request 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,101 @@
1
+ # @hlw-uni-mp/request
2
+
3
+ 基于 uni-app 的极简轻量级 HTTP 请求与文件直传客户端,内置 RSA-2048 非对称加密与请求防篡改验签能力。
4
+
5
+ ## 特性
6
+
7
+ - **轻量易用**:类似 Axios 的 API 设计,开箱即用,支持 Promise
8
+ - **防篡改与安全**:内置 RSA 公钥加密与 SHA-256 数据签名机制,无缝对接后端接口验签规范
9
+ - **全平台设备采集**:自动采集平台、系统、版本与网络状态并注入请求头
10
+ - **拦截器支持**:支持请求前拦截与响应后处理,方便统一注入 Token 或错误兜底
11
+ - **文件直传支持**:内置本地直传、阿里云 OSS 与七牛云直传能力
12
+ - **强类型约束**:全 TypeScript 编写,完备的类型推导与接口响应声明
13
+
14
+ ---
15
+
16
+ ## 安装
17
+
18
+ ```bash
19
+ # pnpm
20
+ pnpm add @hlw-uni-mp/request
21
+
22
+ # npm
23
+ npm install @hlw-uni-mp/request
24
+ ```
25
+
26
+ ---
27
+
28
+ ## 快速上手
29
+
30
+ ### 1. 初始化网络配置
31
+
32
+ ```ts
33
+ import { setupHttp } from "@hlw-uni-mp/request";
34
+
35
+ setupHttp({
36
+ baseURL: "https://api.example.com",
37
+ timeout: 15000,
38
+ publicKey: "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA...", // RSA 公钥
39
+ secret: "your-sign-secret", // 验签密钥
40
+ getToken: () => uni.getStorageSync("token") || "",
41
+ onUnauthorized: (msg) => {
42
+ // 登录失效处理
43
+ },
44
+ });
45
+ ```
46
+
47
+ ---
48
+
49
+ ### 2. 接口请求调用
50
+
51
+ #### 快捷方式(get / post / put / del)
52
+
53
+ ```ts
54
+ import { get, post, request } from "@hlw-uni-mp/request";
55
+
56
+ // GET 请求
57
+ export function fetchUserInfo() {
58
+ return get("/api/v1.user/info");
59
+ }
60
+
61
+ // POST 请求
62
+ export function submitOrder(data: { goodsId: number; num: number }) {
63
+ return post("/api/v1.order/create", data);
64
+ }
65
+
66
+ // 通用 request
67
+ export function customRequest() {
68
+ return request({
69
+ url: "/api/v1.data/list",
70
+ method: "POST",
71
+ data: { page: 1 },
72
+ });
73
+ }
74
+ ```
75
+
76
+ #### 客户端实例方式(http / client)
77
+
78
+ ```ts
79
+ import { http } from "@hlw-uni-mp/request";
80
+
81
+ const response = await http.get("/api/v1.config");
82
+ ```
83
+
84
+ ---
85
+
86
+ ### 3. 文件上传
87
+
88
+ ```ts
89
+ import { uploadFile } from "@hlw-uni-mp/request";
90
+
91
+ const fileUrl = await uploadFile({
92
+ biz: "avatar",
93
+ filePath: tempFilePath,
94
+ });
95
+ ```
96
+
97
+ ---
98
+
99
+ ## License
100
+
101
+ MIT
@@ -0,0 +1,375 @@
1
+ /**
2
+ * 设备信息定义
3
+ */
4
+ interface DeviceInfo {
5
+ appid: string
6
+ appName: string
7
+ version: string
8
+ versionCode: string
9
+ channel: string
10
+ deviceId: string
11
+ deviceType: string
12
+ deviceOrientation: 'portrait' | 'landscape'
13
+ brand: string
14
+ model: string
15
+ system: string
16
+ os: string
17
+ pixelRatio: number
18
+ screenWidth: number
19
+ screenHeight: number
20
+ windowWidth: number
21
+ windowHeight: number
22
+ statusBarHeight: number
23
+ sdkVersion: string
24
+ hostName: string
25
+ hostVersion: string
26
+ hostLanguage: string
27
+ hostTheme: string
28
+ platform: string
29
+ language: string
30
+ networkType: string
31
+ benchmarkLevel: number
32
+ theme: string
33
+ fontSizeSetting: number
34
+ }
35
+
36
+ /**
37
+ * SHA-256 哈希计算 (基于 crypto-js)
38
+ */
39
+ declare function sha256(rawInput: string): string;
40
+ /**
41
+ * 递归规范化参数排序串(与后端 1:1 对齐)
42
+ */
43
+ declare function sortString(data: any): string;
44
+ interface SignOptions {
45
+ timestamp: string | number;
46
+ nonce: string;
47
+ context: string;
48
+ secret?: string;
49
+ data?: any;
50
+ }
51
+ /**
52
+ * 计算请求防篡改签名
53
+ */
54
+ declare function calculateSign(options: SignOptions): string;
55
+
56
+ /**
57
+ * 请求配置项
58
+ */
59
+ interface RequestConfig {
60
+ url: string
61
+ method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | string
62
+ data?: unknown
63
+ params?: unknown
64
+ baseURL?: string
65
+ headers?: Record<string, string>
66
+ timeout?: number
67
+ }
68
+
69
+ /**
70
+ * 响应数据包
71
+ */
72
+ interface AxiosResponse<T = any> {
73
+ data: T
74
+ status: number
75
+ statusText: string
76
+ headers: Record<string, string>
77
+ config: RequestConfig
78
+ }
79
+
80
+ /**
81
+ * 拦截处理器
82
+ */
83
+ interface InterceptorHandler<T> {
84
+ fulfilled?: (value: T) => T | Promise<T>
85
+ rejected?: (error: any) => any
86
+ }
87
+
88
+ /**
89
+ * 模态弹窗配置
90
+ */
91
+ interface ModalOptions {
92
+ title?: string
93
+ content?: string
94
+ confirmText?: string
95
+ cancelText?: string
96
+ showCancel?: boolean
97
+ [key: string]: unknown
98
+ }
99
+
100
+ /**
101
+ * 接口业务数据扩展标记(供各业务项目通过声明合并自由扩展)
102
+ */
103
+ interface ApiResData {
104
+ /**
105
+ * 模态弹窗配置
106
+ */
107
+ modal?: ModalOptions
108
+ }
109
+
110
+ /**
111
+ * 接口响应表
112
+ */
113
+ interface ApiRes<T = any> {
114
+ /**
115
+ * 状态码数值 (1 为成功)
116
+ */
117
+ code: number
118
+ /**
119
+ * 提示文本串
120
+ */
121
+ msg: string
122
+ /**
123
+ * 数据业务体
124
+ */
125
+ data: T & ApiResData
126
+ /**
127
+ * 是否弹窗提示
128
+ */
129
+ modal?: boolean
130
+ /**
131
+ * 模态弹窗配置
132
+ */
133
+ modalOptions?: ModalOptions
134
+ }
135
+
136
+ /**
137
+ * 请求配置项
138
+ */
139
+ interface HttpOptions {
140
+ /**
141
+ * 基础接口路由
142
+ */
143
+ baseURL?: string
144
+ /**
145
+ * 超时时间数值 (ms)
146
+ */
147
+ timeout?: number
148
+ /**
149
+ * 获取凭证函数
150
+ */
151
+ getToken?: () => string
152
+ /**
153
+ * 获取设备函数
154
+ */
155
+ getDevice?: () => DeviceInfo | Record<string, any>
156
+ /**
157
+ * 鉴权失败回调
158
+ */
159
+ onUnauthorized?: (message: string) => void
160
+ /**
161
+ * RSA 公钥(用于非对称加密 X-Client-Context)
162
+ */
163
+ publicKey?: string
164
+ /**
165
+ * 签名密钥(用于计算 X-Client-Sign)
166
+ */
167
+ secret?: string
168
+ }
169
+
170
+ /**
171
+ * 基础上传凭据
172
+ */
173
+ interface UploadSignBase {
174
+ type: 'local' | 'alioss' | 'qiniu'
175
+ key: string
176
+ url: string
177
+ server: string
178
+ }
179
+
180
+ /**
181
+ * 本地上传凭据
182
+ */
183
+ interface LocalUploadSign extends UploadSignBase {
184
+ type: 'local'
185
+ }
186
+
187
+ /**
188
+ * 阿里存储凭据
189
+ */
190
+ interface AliossUploadSign extends UploadSignBase {
191
+ type: 'alioss'
192
+ OSSAccessKeyId: string
193
+ policy: string
194
+ Signature: string
195
+ success_action_status: string
196
+ oSSAccessKeyId?: string
197
+ signature?: string
198
+ successActionStatus?: string
199
+ }
200
+
201
+ /**
202
+ * 七牛存储凭据
203
+ */
204
+ interface QiniuUploadSign extends UploadSignBase {
205
+ type: 'qiniu'
206
+ token: string
207
+ }
208
+
209
+ /**
210
+ * 联合存储凭据
211
+ */
212
+ type UploadSign = LocalUploadSign | AliossUploadSign | QiniuUploadSign
213
+
214
+ /**
215
+ * 文件上传参数
216
+ */
217
+ interface UploadFileParams {
218
+ /** 业务场景标识 */
219
+ biz: string
220
+ /** 本地文件路径 */
221
+ filePath: string
222
+ /** 文件格式后缀 */
223
+ ext?: string
224
+ /** 文件字节大小 */
225
+ size?: number
226
+ /** 签名接口路径 */
227
+ signUrl?: string
228
+ /** 自定义请求头 */
229
+ header?: Record<string, string>
230
+ }
231
+
232
+ /**
233
+ * 阿里配置项
234
+ */
235
+ interface OssConfig {
236
+ /** 对象存储键 */
237
+ key?: string
238
+ /** 上传策略串 */
239
+ policy?: string
240
+ /** 身份标识符 */
241
+ OSSAccessKeyId?: string
242
+ /** 签名密文字 */
243
+ signature?: string
244
+ /** 主机服务址 */
245
+ host?: string
246
+ /** 文件访问址 */
247
+ url?: string
248
+ /** 目标文件名 */
249
+ filename?: string
250
+ [key: string]: unknown
251
+ }
252
+
253
+ /**
254
+ * 采集设备信息
255
+ * @returns 设备参数集
256
+ */
257
+ declare function getDevice(): DeviceInfo;
258
+
259
+ /**
260
+ * 纯 TypeScript 实现的 RSA-2048 公钥分段加密(PKCS#1 v1.5 填充)
261
+ * 零第三方外部依赖,原生兼容小程序、浏览器与 Node 环境
262
+ */
263
+ /**
264
+ * RSA-2048 公钥分段加密
265
+ * @param plaintext 明文字符串(支持 UTF-8 多字节)
266
+ * @param publicKey PEM 格式或纯 Base64 公钥
267
+ * @returns 密文 Base64 编码
268
+ */
269
+ declare function encryptRsa(plaintext: string, publicKey: string): string;
270
+
271
+ /**
272
+ * 直传阿里云 OSS
273
+ * @param oss 阿里配置项
274
+ * @param source 源文件路径或文件对象
275
+ * @returns 资源访问地址
276
+ */
277
+ declare function uploadOss(oss: OssConfig, source: string | any): Promise<string>;
278
+
279
+ /**
280
+ * 请求上传凭证
281
+ * @param data 签名请求参数
282
+ * @param signUrl 签名接口地址
283
+ * @returns 上传签名凭据
284
+ */
285
+ declare function postUploadSign(data: {
286
+ biz: string;
287
+ ext: string;
288
+ size?: number;
289
+ }, signUrl?: string): Promise<ApiRes<UploadSign>>;
290
+ /**
291
+ * 执行文件直传
292
+ * @param cred 上传签名凭据
293
+ * @param filePath 本地文件路径
294
+ * @param customHeader 额外请求头
295
+ * @returns 直传异步项
296
+ */
297
+ declare function doUpload(cred: UploadSign, filePath: string, customHeader?: Record<string, string>): Promise<void>;
298
+ /**
299
+ * 统一直传文件
300
+ * @param params 文件上传参数
301
+ * @returns 文件网络地址
302
+ */
303
+ declare function uploadFile(params: UploadFileParams): Promise<string>;
304
+
305
+ /**
306
+ * 拦截管理器
307
+ */
308
+ declare class InterceptorManager<T> {
309
+ private handlers;
310
+ use(fulfilled?: (value: T) => T | Promise<T>, rejected?: (error: any) => any): number;
311
+ eject(id: number): void;
312
+ forEach(fn: (handler: InterceptorHandler<T>) => void): void;
313
+ }
314
+ /**
315
+ * 原生请求客户端
316
+ */
317
+ declare class UniHttpClient {
318
+ defaults: {
319
+ baseURL: string;
320
+ timeout: number;
321
+ headers: Record<string, string>;
322
+ };
323
+ interceptors: {
324
+ request: InterceptorManager<RequestConfig>;
325
+ response: InterceptorManager<AxiosResponse<any>>;
326
+ };
327
+ constructor(options?: {
328
+ baseURL?: string;
329
+ timeout?: number;
330
+ headers?: Record<string, string>;
331
+ });
332
+ request<T = any>(config: RequestConfig): Promise<AxiosResponse<T>>;
333
+ get<T = any>(url: string, config?: Partial<RequestConfig>): Promise<AxiosResponse<T>>;
334
+ post<T = any>(url: string, data?: unknown, config?: Partial<RequestConfig>): Promise<AxiosResponse<T>>;
335
+ put<T = any>(url: string, data?: unknown, config?: Partial<RequestConfig>): Promise<AxiosResponse<T>>;
336
+ delete<T = any>(url: string, config?: Partial<RequestConfig>): Promise<AxiosResponse<T>>;
337
+ private resolveUrl;
338
+ private send;
339
+ }
340
+ /**
341
+ * 获取请求实例
342
+ */
343
+ declare function getClient(): UniHttpClient;
344
+ /**
345
+ * 初始化配置网络请求
346
+ */
347
+ declare function setupHttp(options?: Partial<HttpOptions>): UniHttpClient;
348
+ /**
349
+ * 发送通用请求
350
+ */
351
+ declare function request<T = any>(config: RequestConfig): Promise<ApiRes<T>>;
352
+ /**
353
+ * 发送 GET 读取请求
354
+ */
355
+ declare function get<T = any>(url: string, params?: unknown): Promise<ApiRes<T>>;
356
+ /**
357
+ * 发送 POST 提交请求
358
+ */
359
+ declare function post<T = any>(url: string, data?: unknown): Promise<ApiRes<T>>;
360
+ /**
361
+ * 发送 PUT 修改请求
362
+ */
363
+ declare function put<T = any>(url: string, data?: unknown): Promise<ApiRes<T>>;
364
+ /**
365
+ * 发送 DELETE 删除请求
366
+ */
367
+ declare function del<T = any>(url: string, params?: unknown): Promise<ApiRes<T>>;
368
+ /**
369
+ * 获取全局网络配置
370
+ */
371
+ declare function getHttpOptions(): HttpOptions;
372
+ declare const client: UniHttpClient;
373
+ declare const http: UniHttpClient;
374
+
375
+ export { type AliossUploadSign, type ApiRes, type ApiResData, type AxiosResponse, type DeviceInfo, type HttpOptions, type InterceptorHandler, InterceptorManager, type LocalUploadSign, type ModalOptions, type OssConfig, type QiniuUploadSign, type RequestConfig, type SignOptions, UniHttpClient, type UploadFileParams, type UploadSign, type UploadSignBase, calculateSign, client, request as default, del, doUpload, encryptRsa, get, getClient, getDevice, getHttpOptions, http, post, postUploadSign, put, request, setupHttp, sha256, sortString, uploadFile, uploadOss };