@hlw-uni-mp/request 1.0.2 → 1.0.4

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/dist/index.d.mts CHANGED
@@ -56,25 +56,30 @@ declare function sha256(rawInput: string): string;
56
56
  /**
57
57
  * 递归规范化参数排序串(与后端 1:1 对齐)
58
58
  */
59
- declare function sortString(data: any): string;
59
+ declare function sortString(data: unknown): string;
60
60
  interface SignOptions {
61
61
  timestamp: string | number;
62
62
  nonce: string;
63
63
  context: string;
64
64
  secret?: string;
65
- data?: any;
65
+ data?: unknown;
66
66
  }
67
67
  /**
68
68
  * 计算请求防篡改签名
69
69
  */
70
70
  declare function calculateSign(options: SignOptions): string;
71
71
 
72
+ /**
73
+ * 支持的 HTTP 请求动词
74
+ */
75
+ type HttpMethod = 'OPTIONS' | 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'TRACE' | 'CONNECT'
76
+
72
77
  /**
73
78
  * 请求配置项
74
79
  */
75
80
  interface RequestConfig {
76
81
  url: string
77
- method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | string
82
+ method?: HttpMethod | string
78
83
  data?: unknown
79
84
  params?: unknown
80
85
  baseURL?: string
@@ -85,7 +90,7 @@ interface RequestConfig {
85
90
  /**
86
91
  * 响应数据包
87
92
  */
88
- interface AxiosResponse<T = any> {
93
+ interface AxiosResponse<T = unknown> {
89
94
  data: T
90
95
  status: number
91
96
  statusText: string
@@ -98,7 +103,7 @@ interface AxiosResponse<T = any> {
98
103
  */
99
104
  interface InterceptorHandler<T> {
100
105
  fulfilled?: (value: T) => T | Promise<T>
101
- rejected?: (error: any) => any
106
+ rejected?: (error: unknown) => unknown
102
107
  }
103
108
 
104
109
  /**
@@ -126,7 +131,7 @@ interface ApiResData {
126
131
  /**
127
132
  * 接口响应表
128
133
  */
129
- interface ApiRes<T = any> {
134
+ interface ApiRes<T = unknown> {
130
135
  /**
131
136
  * 状态码数值 (1 为成功)
132
137
  */
@@ -168,7 +173,7 @@ interface HttpOptions {
168
173
  /**
169
174
  * 获取设备函数
170
175
  */
171
- getDevice?: () => DeviceInfo | Record<string, any>
176
+ getDevice?: () => DeviceInfo | Record<string, unknown>
172
177
  /**
173
178
  * 鉴权失败回调
174
179
  */
@@ -201,16 +206,13 @@ interface LocalUploadSign extends UploadSignBase {
201
206
  }
202
207
 
203
208
  /**
204
- * 阿里存储凭据
209
+ * 阿里存储凭据(严格遵循阿里云 PostObject 规范单一属性名)
205
210
  */
206
211
  interface AliossUploadSign extends UploadSignBase {
207
212
  type: 'alioss'
208
213
  OSSAccessKeyId: string
209
214
  policy: string
210
215
  Signature: string
211
- success_action_status: string
212
- oSSAccessKeyId?: string
213
- signature?: string
214
216
  successActionStatus?: string
215
217
  }
216
218
 
@@ -263,6 +265,8 @@ interface OssConfig {
263
265
  url?: string
264
266
  /** 目标文件名 */
265
267
  filename?: string
268
+ /** 成功响应状态码 */
269
+ successActionStatus?: string
266
270
  [key: string]: unknown
267
271
  }
268
272
 
@@ -290,7 +294,10 @@ declare function encryptRsa(plaintext: string, publicKey: string): string;
290
294
  * @param source 源文件路径或文件对象
291
295
  * @returns 资源访问地址
292
296
  */
293
- declare function uploadOss(oss: OssConfig, source: string | any): Promise<string>;
297
+ declare function uploadOss(oss: OssConfig, source: string | {
298
+ path?: string;
299
+ tempFilePath?: string;
300
+ }): Promise<string>;
294
301
 
295
302
  /**
296
303
  * 请求上传凭证
@@ -304,7 +311,7 @@ declare function postUploadSign(data: {
304
311
  size?: number;
305
312
  }, signUrl?: string): Promise<ApiRes<UploadSign>>;
306
313
  /**
307
- * 执行文件直传
314
+ * 执行文件直传(原生解析,杜绝 escape/decode 废弃 API)
308
315
  * @param cred 上传签名凭据
309
316
  * @param filePath 本地文件路径
310
317
  * @param customHeader 额外请求头
@@ -318,12 +325,25 @@ declare function doUpload(cred: UploadSign, filePath: string, customHeader?: Rec
318
325
  */
319
326
  declare function uploadFile(params: UploadFileParams): Promise<string>;
320
327
 
328
+ /**
329
+ * 统一网络请求异常类
330
+ */
331
+ declare class HttpError<T = unknown> extends Error {
332
+ response?: AxiosResponse<T>;
333
+ status?: number;
334
+ config?: RequestConfig;
335
+ constructor(message: string, options?: {
336
+ response?: AxiosResponse<T>;
337
+ status?: number;
338
+ config?: RequestConfig;
339
+ });
340
+ }
321
341
  /**
322
342
  * 拦截管理器
323
343
  */
324
344
  declare class InterceptorManager<T> {
325
345
  private handlers;
326
- use(fulfilled?: (value: T) => T | Promise<T>, rejected?: (error: any) => any): number;
346
+ use(fulfilled?: (value: T) => T | Promise<T>, rejected?: (error: unknown) => unknown): number;
327
347
  eject(id: number): void;
328
348
  forEach(fn: (handler: InterceptorHandler<T>) => void): void;
329
349
  }
@@ -338,18 +358,18 @@ declare class UniHttpClient {
338
358
  };
339
359
  interceptors: {
340
360
  request: InterceptorManager<RequestConfig>;
341
- response: InterceptorManager<AxiosResponse<any>>;
361
+ response: InterceptorManager<AxiosResponse<unknown>>;
342
362
  };
343
363
  constructor(options?: {
344
364
  baseURL?: string;
345
365
  timeout?: number;
346
366
  headers?: Record<string, string>;
347
367
  });
348
- request<T = any>(config: RequestConfig): Promise<AxiosResponse<T>>;
349
- get<T = any>(url: string, config?: Partial<RequestConfig>): Promise<AxiosResponse<T>>;
350
- post<T = any>(url: string, data?: unknown, config?: Partial<RequestConfig>): Promise<AxiosResponse<T>>;
351
- put<T = any>(url: string, data?: unknown, config?: Partial<RequestConfig>): Promise<AxiosResponse<T>>;
352
- delete<T = any>(url: string, config?: Partial<RequestConfig>): Promise<AxiosResponse<T>>;
368
+ request<T = unknown>(config: RequestConfig): Promise<AxiosResponse<T>>;
369
+ get<T = unknown>(url: string, config?: Partial<RequestConfig>): Promise<AxiosResponse<T>>;
370
+ post<T = unknown>(url: string, data?: unknown, config?: Partial<RequestConfig>): Promise<AxiosResponse<T>>;
371
+ put<T = unknown>(url: string, data?: unknown, config?: Partial<RequestConfig>): Promise<AxiosResponse<T>>;
372
+ delete<T = unknown>(url: string, config?: Partial<RequestConfig>): Promise<AxiosResponse<T>>;
353
373
  private resolveUrl;
354
374
  private send;
355
375
  }
@@ -364,23 +384,23 @@ declare function setupHttp(options?: Partial<HttpOptions>): UniHttpClient;
364
384
  /**
365
385
  * 发送通用请求
366
386
  */
367
- declare function request<T = any>(config: RequestConfig): Promise<ApiRes<T>>;
387
+ declare function request<T = unknown>(config: RequestConfig): Promise<ApiRes<T>>;
368
388
  /**
369
389
  * 发送 GET 读取请求
370
390
  */
371
- declare function get<T = any>(url: string, params?: unknown): Promise<ApiRes<T>>;
391
+ declare function get<T = unknown>(url: string, params?: unknown): Promise<ApiRes<T>>;
372
392
  /**
373
393
  * 发送 POST 提交请求
374
394
  */
375
- declare function post<T = any>(url: string, data?: unknown): Promise<ApiRes<T>>;
395
+ declare function post<T = unknown>(url: string, data?: unknown): Promise<ApiRes<T>>;
376
396
  /**
377
397
  * 发送 PUT 修改请求
378
398
  */
379
- declare function put<T = any>(url: string, data?: unknown): Promise<ApiRes<T>>;
399
+ declare function put<T = unknown>(url: string, data?: unknown): Promise<ApiRes<T>>;
380
400
  /**
381
401
  * 发送 DELETE 删除请求
382
402
  */
383
- declare function del<T = any>(url: string, params?: unknown): Promise<ApiRes<T>>;
403
+ declare function del<T = unknown>(url: string, params?: unknown): Promise<ApiRes<T>>;
384
404
  /**
385
405
  * 获取全局网络配置
386
406
  */
@@ -388,4 +408,4 @@ declare function getHttpOptions(): HttpOptions;
388
408
  declare const client: UniHttpClient;
389
409
  declare const http: UniHttpClient;
390
410
 
391
- 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 };
411
+ export { type AliossUploadSign, type ApiRes, type ApiResData, type AxiosResponse, type DeviceInfo, HttpError, type HttpMethod, 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 };
package/dist/index.d.ts CHANGED
@@ -56,25 +56,30 @@ declare function sha256(rawInput: string): string;
56
56
  /**
57
57
  * 递归规范化参数排序串(与后端 1:1 对齐)
58
58
  */
59
- declare function sortString(data: any): string;
59
+ declare function sortString(data: unknown): string;
60
60
  interface SignOptions {
61
61
  timestamp: string | number;
62
62
  nonce: string;
63
63
  context: string;
64
64
  secret?: string;
65
- data?: any;
65
+ data?: unknown;
66
66
  }
67
67
  /**
68
68
  * 计算请求防篡改签名
69
69
  */
70
70
  declare function calculateSign(options: SignOptions): string;
71
71
 
72
+ /**
73
+ * 支持的 HTTP 请求动词
74
+ */
75
+ type HttpMethod = 'OPTIONS' | 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'TRACE' | 'CONNECT'
76
+
72
77
  /**
73
78
  * 请求配置项
74
79
  */
75
80
  interface RequestConfig {
76
81
  url: string
77
- method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | string
82
+ method?: HttpMethod | string
78
83
  data?: unknown
79
84
  params?: unknown
80
85
  baseURL?: string
@@ -85,7 +90,7 @@ interface RequestConfig {
85
90
  /**
86
91
  * 响应数据包
87
92
  */
88
- interface AxiosResponse<T = any> {
93
+ interface AxiosResponse<T = unknown> {
89
94
  data: T
90
95
  status: number
91
96
  statusText: string
@@ -98,7 +103,7 @@ interface AxiosResponse<T = any> {
98
103
  */
99
104
  interface InterceptorHandler<T> {
100
105
  fulfilled?: (value: T) => T | Promise<T>
101
- rejected?: (error: any) => any
106
+ rejected?: (error: unknown) => unknown
102
107
  }
103
108
 
104
109
  /**
@@ -126,7 +131,7 @@ interface ApiResData {
126
131
  /**
127
132
  * 接口响应表
128
133
  */
129
- interface ApiRes<T = any> {
134
+ interface ApiRes<T = unknown> {
130
135
  /**
131
136
  * 状态码数值 (1 为成功)
132
137
  */
@@ -168,7 +173,7 @@ interface HttpOptions {
168
173
  /**
169
174
  * 获取设备函数
170
175
  */
171
- getDevice?: () => DeviceInfo | Record<string, any>
176
+ getDevice?: () => DeviceInfo | Record<string, unknown>
172
177
  /**
173
178
  * 鉴权失败回调
174
179
  */
@@ -201,16 +206,13 @@ interface LocalUploadSign extends UploadSignBase {
201
206
  }
202
207
 
203
208
  /**
204
- * 阿里存储凭据
209
+ * 阿里存储凭据(严格遵循阿里云 PostObject 规范单一属性名)
205
210
  */
206
211
  interface AliossUploadSign extends UploadSignBase {
207
212
  type: 'alioss'
208
213
  OSSAccessKeyId: string
209
214
  policy: string
210
215
  Signature: string
211
- success_action_status: string
212
- oSSAccessKeyId?: string
213
- signature?: string
214
216
  successActionStatus?: string
215
217
  }
216
218
 
@@ -263,6 +265,8 @@ interface OssConfig {
263
265
  url?: string
264
266
  /** 目标文件名 */
265
267
  filename?: string
268
+ /** 成功响应状态码 */
269
+ successActionStatus?: string
266
270
  [key: string]: unknown
267
271
  }
268
272
 
@@ -290,7 +294,10 @@ declare function encryptRsa(plaintext: string, publicKey: string): string;
290
294
  * @param source 源文件路径或文件对象
291
295
  * @returns 资源访问地址
292
296
  */
293
- declare function uploadOss(oss: OssConfig, source: string | any): Promise<string>;
297
+ declare function uploadOss(oss: OssConfig, source: string | {
298
+ path?: string;
299
+ tempFilePath?: string;
300
+ }): Promise<string>;
294
301
 
295
302
  /**
296
303
  * 请求上传凭证
@@ -304,7 +311,7 @@ declare function postUploadSign(data: {
304
311
  size?: number;
305
312
  }, signUrl?: string): Promise<ApiRes<UploadSign>>;
306
313
  /**
307
- * 执行文件直传
314
+ * 执行文件直传(原生解析,杜绝 escape/decode 废弃 API)
308
315
  * @param cred 上传签名凭据
309
316
  * @param filePath 本地文件路径
310
317
  * @param customHeader 额外请求头
@@ -318,12 +325,25 @@ declare function doUpload(cred: UploadSign, filePath: string, customHeader?: Rec
318
325
  */
319
326
  declare function uploadFile(params: UploadFileParams): Promise<string>;
320
327
 
328
+ /**
329
+ * 统一网络请求异常类
330
+ */
331
+ declare class HttpError<T = unknown> extends Error {
332
+ response?: AxiosResponse<T>;
333
+ status?: number;
334
+ config?: RequestConfig;
335
+ constructor(message: string, options?: {
336
+ response?: AxiosResponse<T>;
337
+ status?: number;
338
+ config?: RequestConfig;
339
+ });
340
+ }
321
341
  /**
322
342
  * 拦截管理器
323
343
  */
324
344
  declare class InterceptorManager<T> {
325
345
  private handlers;
326
- use(fulfilled?: (value: T) => T | Promise<T>, rejected?: (error: any) => any): number;
346
+ use(fulfilled?: (value: T) => T | Promise<T>, rejected?: (error: unknown) => unknown): number;
327
347
  eject(id: number): void;
328
348
  forEach(fn: (handler: InterceptorHandler<T>) => void): void;
329
349
  }
@@ -338,18 +358,18 @@ declare class UniHttpClient {
338
358
  };
339
359
  interceptors: {
340
360
  request: InterceptorManager<RequestConfig>;
341
- response: InterceptorManager<AxiosResponse<any>>;
361
+ response: InterceptorManager<AxiosResponse<unknown>>;
342
362
  };
343
363
  constructor(options?: {
344
364
  baseURL?: string;
345
365
  timeout?: number;
346
366
  headers?: Record<string, string>;
347
367
  });
348
- request<T = any>(config: RequestConfig): Promise<AxiosResponse<T>>;
349
- get<T = any>(url: string, config?: Partial<RequestConfig>): Promise<AxiosResponse<T>>;
350
- post<T = any>(url: string, data?: unknown, config?: Partial<RequestConfig>): Promise<AxiosResponse<T>>;
351
- put<T = any>(url: string, data?: unknown, config?: Partial<RequestConfig>): Promise<AxiosResponse<T>>;
352
- delete<T = any>(url: string, config?: Partial<RequestConfig>): Promise<AxiosResponse<T>>;
368
+ request<T = unknown>(config: RequestConfig): Promise<AxiosResponse<T>>;
369
+ get<T = unknown>(url: string, config?: Partial<RequestConfig>): Promise<AxiosResponse<T>>;
370
+ post<T = unknown>(url: string, data?: unknown, config?: Partial<RequestConfig>): Promise<AxiosResponse<T>>;
371
+ put<T = unknown>(url: string, data?: unknown, config?: Partial<RequestConfig>): Promise<AxiosResponse<T>>;
372
+ delete<T = unknown>(url: string, config?: Partial<RequestConfig>): Promise<AxiosResponse<T>>;
353
373
  private resolveUrl;
354
374
  private send;
355
375
  }
@@ -364,23 +384,23 @@ declare function setupHttp(options?: Partial<HttpOptions>): UniHttpClient;
364
384
  /**
365
385
  * 发送通用请求
366
386
  */
367
- declare function request<T = any>(config: RequestConfig): Promise<ApiRes<T>>;
387
+ declare function request<T = unknown>(config: RequestConfig): Promise<ApiRes<T>>;
368
388
  /**
369
389
  * 发送 GET 读取请求
370
390
  */
371
- declare function get<T = any>(url: string, params?: unknown): Promise<ApiRes<T>>;
391
+ declare function get<T = unknown>(url: string, params?: unknown): Promise<ApiRes<T>>;
372
392
  /**
373
393
  * 发送 POST 提交请求
374
394
  */
375
- declare function post<T = any>(url: string, data?: unknown): Promise<ApiRes<T>>;
395
+ declare function post<T = unknown>(url: string, data?: unknown): Promise<ApiRes<T>>;
376
396
  /**
377
397
  * 发送 PUT 修改请求
378
398
  */
379
- declare function put<T = any>(url: string, data?: unknown): Promise<ApiRes<T>>;
399
+ declare function put<T = unknown>(url: string, data?: unknown): Promise<ApiRes<T>>;
380
400
  /**
381
401
  * 发送 DELETE 删除请求
382
402
  */
383
- declare function del<T = any>(url: string, params?: unknown): Promise<ApiRes<T>>;
403
+ declare function del<T = unknown>(url: string, params?: unknown): Promise<ApiRes<T>>;
384
404
  /**
385
405
  * 获取全局网络配置
386
406
  */
@@ -388,4 +408,4 @@ declare function getHttpOptions(): HttpOptions;
388
408
  declare const client: UniHttpClient;
389
409
  declare const http: UniHttpClient;
390
410
 
391
- 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 };
411
+ export { type AliossUploadSign, type ApiRes, type ApiResData, type AxiosResponse, type DeviceInfo, HttpError, type HttpMethod, 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 };
package/dist/index.js CHANGED
@@ -30,6 +30,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
+ HttpError: () => HttpError,
33
34
  InterceptorManager: () => InterceptorManager,
34
35
  UniHttpClient: () => UniHttpClient,
35
36
  calculateSign: () => calculateSign,
@@ -298,10 +299,11 @@ function sortString(data) {
298
299
  if (Array.isArray(data)) {
299
300
  return "[" + data.map((item) => sortString(item)).join(",") + "]";
300
301
  }
301
- const keys = Object.keys(data).sort();
302
+ const obj = data;
303
+ const keys = Object.keys(obj).sort();
302
304
  const parts = [];
303
305
  for (const key of keys) {
304
- const val = data[key];
306
+ const val = obj[key];
305
307
  if (val !== null && val !== void 0) {
306
308
  parts.push(`${key}=${sortString(val)}`);
307
309
  }
@@ -320,17 +322,17 @@ function calculateSign(options) {
320
322
 
321
323
  // src/oss.ts
322
324
  async function uploadOss(oss, source) {
323
- const filePath = typeof source === "string" ? source : source?.path || source?.tempFilePath || "";
324
- const name = oss.filename || filePath?.split("/").pop() || "file";
325
+ const filePath = typeof source === "string" ? source : source.path || source.tempFilePath || "";
326
+ const name = oss.filename || filePath.split("/").pop() || "file";
325
327
  const formData = {
326
- key: String(oss.key || ""),
327
- success_action_status: "200"
328
+ key: oss.key || "",
329
+ success_action_status: oss.successActionStatus || "200"
328
330
  };
329
- if (oss.policy) formData.policy = String(oss.policy);
330
- if (oss.OSSAccessKeyId) formData.OSSAccessKeyId = String(oss.OSSAccessKeyId);
331
- if (oss.signature) formData.signature = String(oss.signature);
331
+ if (oss.policy) formData.policy = oss.policy;
332
+ if (oss.OSSAccessKeyId) formData.OSSAccessKeyId = oss.OSSAccessKeyId;
333
+ if (oss.signature) formData.signature = oss.signature;
332
334
  if (oss["x-oss-security-token"]) formData["x-oss-security-token"] = String(oss["x-oss-security-token"]);
333
- const target2 = String(oss.host || "");
335
+ const target2 = oss.host || "";
334
336
  return new Promise((resolve, reject) => {
335
337
  uni.uploadFile({
336
338
  url: target2,
@@ -342,7 +344,7 @@ async function uploadOss(oss, source) {
342
344
  },
343
345
  success: (res) => {
344
346
  if (res.statusCode === 200 || res.statusCode === 204) {
345
- resolve(String(oss.url || ""));
347
+ resolve(oss.url || "");
346
348
  } else {
347
349
  reject(new Error(`\u4E0A\u4F20\u963F\u91CC\u4E91\u5931\u8D25 (${res.statusCode}): ${res.data || ""}`));
348
350
  }
@@ -365,9 +367,9 @@ function buildFormData(cred) {
365
367
  return {
366
368
  key: cred.key,
367
369
  policy: cred.policy,
368
- OSSAccessKeyId: cred.OSSAccessKeyId || cred.oSSAccessKeyId || "",
369
- Signature: cred.Signature || cred.signature || "",
370
- success_action_status: cred.success_action_status || cred.successActionStatus || "200"
370
+ OSSAccessKeyId: cred.OSSAccessKeyId,
371
+ Signature: cred.Signature,
372
+ success_action_status: cred.successActionStatus || "200"
371
373
  };
372
374
  }
373
375
  return {
@@ -375,14 +377,6 @@ function buildFormData(cred) {
375
377
  token: cred.token
376
378
  };
377
379
  }
378
- function decodeUtf8(value) {
379
- if (!value) return value;
380
- try {
381
- return decodeURIComponent(escape(value));
382
- } catch {
383
- return value;
384
- }
385
- }
386
380
  function buildHeader(cred, customHeader) {
387
381
  if (cred.type !== "local" && !customHeader) {
388
382
  return void 0;
@@ -419,13 +413,13 @@ function doUpload(cred, filePath, customHeader) {
419
413
  return;
420
414
  }
421
415
  try {
422
- const body = JSON.parse(decodeUtf8(res.data));
423
- body.code === 1 ? resolve() : reject(new Error(body.msg || "\u4E0A\u4F20\u5931\u8D25"));
416
+ const body = typeof res.data === "string" ? JSON.parse(res.data) : res.data;
417
+ body?.code === 1 ? resolve() : reject(new Error(body?.msg || "\u4E0A\u4F20\u5931\u8D25"));
424
418
  } catch {
425
419
  reject(new Error("\u54CD\u5E94\u89E3\u6790\u5931\u8D25"));
426
420
  }
427
421
  },
428
- fail: (error) => reject(new Error(error?.errMsg || "\u4E0A\u4F20\u5931\u8D25"))
422
+ fail: (error) => reject(new Error(error.errMsg || "\u4E0A\u4F20\u5931\u8D25"))
429
423
  });
430
424
  });
431
425
  }
@@ -442,6 +436,15 @@ async function uploadFile(params) {
442
436
  }
443
437
 
444
438
  // src/index.ts
439
+ var HttpError = class extends Error {
440
+ constructor(message, options) {
441
+ super(message);
442
+ this.name = "HttpError";
443
+ this.response = options?.response;
444
+ this.status = options?.status;
445
+ this.config = options?.config;
446
+ }
447
+ };
445
448
  var InterceptorManager = class {
446
449
  constructor() {
447
450
  this.handlers = [];
@@ -530,9 +533,10 @@ var UniHttpClient = class {
530
533
  return new Promise((resolve, reject) => {
531
534
  const isGet = (cfg.method || "GET").toUpperCase() === "GET";
532
535
  const requestData = isGet ? cfg.params ?? cfg.data : cfg.data ?? cfg.params;
536
+ const method = cfg.method?.toUpperCase() || "GET";
533
537
  uni.request({
534
538
  url,
535
- method: cfg.method?.toUpperCase() || "GET",
539
+ method,
536
540
  data: requestData,
537
541
  header: cfg.headers,
538
542
  timeout: cfg.timeout,
@@ -548,16 +552,12 @@ var UniHttpClient = class {
548
552
  resolve(response);
549
553
  } else {
550
554
  const body = res.data;
551
- const error = new Error(String(body?.msg || `\u8BF7\u6C42\u5931\u8D25: ${res.statusCode}`));
552
- error.response = response;
553
- error.status = res.statusCode;
554
- reject(error);
555
+ const msg = typeof body?.msg === "string" ? body.msg : `\u8BF7\u6C42\u5931\u8D25: ${res.statusCode}`;
556
+ reject(new HttpError(msg, { response, status: res.statusCode, config: cfg }));
555
557
  }
556
558
  },
557
559
  fail: (errorResult) => {
558
- const error = new Error(errorResult.errMsg || "\u7F51\u7EDC\u8BF7\u6C42\u5931\u8D25");
559
- error.config = cfg;
560
- reject(error);
560
+ reject(new HttpError(errorResult.errMsg || "\u7F51\u7EDC\u8BF7\u6C42\u5931\u8D25", { config: cfg }));
561
561
  }
562
562
  });
563
563
  });
@@ -620,7 +620,7 @@ function getClient() {
620
620
  config.headers["X-Client-Nonce"] = nonce;
621
621
  config.headers["X-Client-Sign"] = sign;
622
622
  config.headers["X-Client-Context"] = cipher;
623
- if (devInfo.appid) {
623
+ if ("appid" in devInfo && devInfo.appid) {
624
624
  config.headers["x-appid"] = String(devInfo.appid);
625
625
  }
626
626
  return config;
@@ -637,8 +637,10 @@ function getClient() {
637
637
  return response;
638
638
  },
639
639
  (error) => {
640
- const status = error.response?.status;
641
- const message = error.response?.data?.msg || error.message || "\u7F51\u7EDC\u8FDE\u63A5\u5F02\u5E38";
640
+ const httpErr = error instanceof HttpError ? error : void 0;
641
+ const status = httpErr?.status ?? httpErr?.response?.status;
642
+ const body = httpErr?.response?.data;
643
+ const message = body?.msg || (error instanceof Error ? error.message : "\u7F51\u7EDC\u8FDE\u63A5\u5F02\u5E38");
642
644
  if (status === 401 || status === 403) {
643
645
  handleUnauthorized(message, currentOptions.onUnauthorized);
644
646
  }
@@ -676,7 +678,7 @@ function getHttpOptions() {
676
678
  var client = new Proxy({}, {
677
679
  get(_, prop) {
678
680
  const inst = getClient();
679
- const val = inst[prop];
681
+ const val = Reflect.get(inst, prop);
680
682
  return typeof val === "function" ? val.bind(inst) : val;
681
683
  }
682
684
  });
@@ -684,6 +686,7 @@ var http = client;
684
686
  var index_default = request;
685
687
  // Annotate the CommonJS export names for ESM import in node:
686
688
  0 && (module.exports = {
689
+ HttpError,
687
690
  InterceptorManager,
688
691
  UniHttpClient,
689
692
  calculateSign,
package/dist/index.mjs CHANGED
@@ -241,10 +241,11 @@ function sortString(data) {
241
241
  if (Array.isArray(data)) {
242
242
  return "[" + data.map((item) => sortString(item)).join(",") + "]";
243
243
  }
244
- const keys = Object.keys(data).sort();
244
+ const obj = data;
245
+ const keys = Object.keys(obj).sort();
245
246
  const parts = [];
246
247
  for (const key of keys) {
247
- const val = data[key];
248
+ const val = obj[key];
248
249
  if (val !== null && val !== void 0) {
249
250
  parts.push(`${key}=${sortString(val)}`);
250
251
  }
@@ -263,17 +264,17 @@ function calculateSign(options) {
263
264
 
264
265
  // src/oss.ts
265
266
  async function uploadOss(oss, source) {
266
- const filePath = typeof source === "string" ? source : source?.path || source?.tempFilePath || "";
267
- const name = oss.filename || filePath?.split("/").pop() || "file";
267
+ const filePath = typeof source === "string" ? source : source.path || source.tempFilePath || "";
268
+ const name = oss.filename || filePath.split("/").pop() || "file";
268
269
  const formData = {
269
- key: String(oss.key || ""),
270
- success_action_status: "200"
270
+ key: oss.key || "",
271
+ success_action_status: oss.successActionStatus || "200"
271
272
  };
272
- if (oss.policy) formData.policy = String(oss.policy);
273
- if (oss.OSSAccessKeyId) formData.OSSAccessKeyId = String(oss.OSSAccessKeyId);
274
- if (oss.signature) formData.signature = String(oss.signature);
273
+ if (oss.policy) formData.policy = oss.policy;
274
+ if (oss.OSSAccessKeyId) formData.OSSAccessKeyId = oss.OSSAccessKeyId;
275
+ if (oss.signature) formData.signature = oss.signature;
275
276
  if (oss["x-oss-security-token"]) formData["x-oss-security-token"] = String(oss["x-oss-security-token"]);
276
- const target2 = String(oss.host || "");
277
+ const target2 = oss.host || "";
277
278
  return new Promise((resolve, reject) => {
278
279
  uni.uploadFile({
279
280
  url: target2,
@@ -285,7 +286,7 @@ async function uploadOss(oss, source) {
285
286
  },
286
287
  success: (res) => {
287
288
  if (res.statusCode === 200 || res.statusCode === 204) {
288
- resolve(String(oss.url || ""));
289
+ resolve(oss.url || "");
289
290
  } else {
290
291
  reject(new Error(`\u4E0A\u4F20\u963F\u91CC\u4E91\u5931\u8D25 (${res.statusCode}): ${res.data || ""}`));
291
292
  }
@@ -308,9 +309,9 @@ function buildFormData(cred) {
308
309
  return {
309
310
  key: cred.key,
310
311
  policy: cred.policy,
311
- OSSAccessKeyId: cred.OSSAccessKeyId || cred.oSSAccessKeyId || "",
312
- Signature: cred.Signature || cred.signature || "",
313
- success_action_status: cred.success_action_status || cred.successActionStatus || "200"
312
+ OSSAccessKeyId: cred.OSSAccessKeyId,
313
+ Signature: cred.Signature,
314
+ success_action_status: cred.successActionStatus || "200"
314
315
  };
315
316
  }
316
317
  return {
@@ -318,14 +319,6 @@ function buildFormData(cred) {
318
319
  token: cred.token
319
320
  };
320
321
  }
321
- function decodeUtf8(value) {
322
- if (!value) return value;
323
- try {
324
- return decodeURIComponent(escape(value));
325
- } catch {
326
- return value;
327
- }
328
- }
329
322
  function buildHeader(cred, customHeader) {
330
323
  if (cred.type !== "local" && !customHeader) {
331
324
  return void 0;
@@ -362,13 +355,13 @@ function doUpload(cred, filePath, customHeader) {
362
355
  return;
363
356
  }
364
357
  try {
365
- const body = JSON.parse(decodeUtf8(res.data));
366
- body.code === 1 ? resolve() : reject(new Error(body.msg || "\u4E0A\u4F20\u5931\u8D25"));
358
+ const body = typeof res.data === "string" ? JSON.parse(res.data) : res.data;
359
+ body?.code === 1 ? resolve() : reject(new Error(body?.msg || "\u4E0A\u4F20\u5931\u8D25"));
367
360
  } catch {
368
361
  reject(new Error("\u54CD\u5E94\u89E3\u6790\u5931\u8D25"));
369
362
  }
370
363
  },
371
- fail: (error) => reject(new Error(error?.errMsg || "\u4E0A\u4F20\u5931\u8D25"))
364
+ fail: (error) => reject(new Error(error.errMsg || "\u4E0A\u4F20\u5931\u8D25"))
372
365
  });
373
366
  });
374
367
  }
@@ -385,6 +378,15 @@ async function uploadFile(params) {
385
378
  }
386
379
 
387
380
  // src/index.ts
381
+ var HttpError = class extends Error {
382
+ constructor(message, options) {
383
+ super(message);
384
+ this.name = "HttpError";
385
+ this.response = options?.response;
386
+ this.status = options?.status;
387
+ this.config = options?.config;
388
+ }
389
+ };
388
390
  var InterceptorManager = class {
389
391
  constructor() {
390
392
  this.handlers = [];
@@ -473,9 +475,10 @@ var UniHttpClient = class {
473
475
  return new Promise((resolve, reject) => {
474
476
  const isGet = (cfg.method || "GET").toUpperCase() === "GET";
475
477
  const requestData = isGet ? cfg.params ?? cfg.data : cfg.data ?? cfg.params;
478
+ const method = cfg.method?.toUpperCase() || "GET";
476
479
  uni.request({
477
480
  url,
478
- method: cfg.method?.toUpperCase() || "GET",
481
+ method,
479
482
  data: requestData,
480
483
  header: cfg.headers,
481
484
  timeout: cfg.timeout,
@@ -491,16 +494,12 @@ var UniHttpClient = class {
491
494
  resolve(response);
492
495
  } else {
493
496
  const body = res.data;
494
- const error = new Error(String(body?.msg || `\u8BF7\u6C42\u5931\u8D25: ${res.statusCode}`));
495
- error.response = response;
496
- error.status = res.statusCode;
497
- reject(error);
497
+ const msg = typeof body?.msg === "string" ? body.msg : `\u8BF7\u6C42\u5931\u8D25: ${res.statusCode}`;
498
+ reject(new HttpError(msg, { response, status: res.statusCode, config: cfg }));
498
499
  }
499
500
  },
500
501
  fail: (errorResult) => {
501
- const error = new Error(errorResult.errMsg || "\u7F51\u7EDC\u8BF7\u6C42\u5931\u8D25");
502
- error.config = cfg;
503
- reject(error);
502
+ reject(new HttpError(errorResult.errMsg || "\u7F51\u7EDC\u8BF7\u6C42\u5931\u8D25", { config: cfg }));
504
503
  }
505
504
  });
506
505
  });
@@ -563,7 +562,7 @@ function getClient() {
563
562
  config.headers["X-Client-Nonce"] = nonce;
564
563
  config.headers["X-Client-Sign"] = sign;
565
564
  config.headers["X-Client-Context"] = cipher;
566
- if (devInfo.appid) {
565
+ if ("appid" in devInfo && devInfo.appid) {
567
566
  config.headers["x-appid"] = String(devInfo.appid);
568
567
  }
569
568
  return config;
@@ -580,8 +579,10 @@ function getClient() {
580
579
  return response;
581
580
  },
582
581
  (error) => {
583
- const status = error.response?.status;
584
- const message = error.response?.data?.msg || error.message || "\u7F51\u7EDC\u8FDE\u63A5\u5F02\u5E38";
582
+ const httpErr = error instanceof HttpError ? error : void 0;
583
+ const status = httpErr?.status ?? httpErr?.response?.status;
584
+ const body = httpErr?.response?.data;
585
+ const message = body?.msg || (error instanceof Error ? error.message : "\u7F51\u7EDC\u8FDE\u63A5\u5F02\u5E38");
585
586
  if (status === 401 || status === 403) {
586
587
  handleUnauthorized(message, currentOptions.onUnauthorized);
587
588
  }
@@ -619,13 +620,14 @@ function getHttpOptions() {
619
620
  var client = new Proxy({}, {
620
621
  get(_, prop) {
621
622
  const inst = getClient();
622
- const val = inst[prop];
623
+ const val = Reflect.get(inst, prop);
623
624
  return typeof val === "function" ? val.bind(inst) : val;
624
625
  }
625
626
  });
626
627
  var http = client;
627
628
  var index_default = request;
628
629
  export {
630
+ HttpError,
629
631
  InterceptorManager,
630
632
  UniHttpClient,
631
633
  calculateSign,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hlw-uni-mp/request",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Universal Uni-App HTTP and upload client with RSA encryption & sign verification",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",