@hlw-uni-mp/request 1.0.2 → 1.0.3
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 +45 -27
- package/dist/index.d.ts +45 -27
- package/dist/index.js +39 -36
- package/dist/index.mjs +38 -36
- package/package.json +1 -1
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:
|
|
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?:
|
|
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?:
|
|
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 =
|
|
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:
|
|
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 =
|
|
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,
|
|
176
|
+
getDevice?: () => DeviceInfo | Record<string, unknown>
|
|
172
177
|
/**
|
|
173
178
|
* 鉴权失败回调
|
|
174
179
|
*/
|
|
@@ -201,17 +206,14 @@ 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
|
|
212
|
-
oSSAccessKeyId?: string
|
|
213
|
-
signature?: string
|
|
214
|
-
successActionStatus?: string
|
|
216
|
+
success_action_status?: string
|
|
215
217
|
}
|
|
216
218
|
|
|
217
219
|
/**
|
|
@@ -290,7 +292,10 @@ declare function encryptRsa(plaintext: string, publicKey: string): string;
|
|
|
290
292
|
* @param source 源文件路径或文件对象
|
|
291
293
|
* @returns 资源访问地址
|
|
292
294
|
*/
|
|
293
|
-
declare function uploadOss(oss: OssConfig, source: string |
|
|
295
|
+
declare function uploadOss(oss: OssConfig, source: string | {
|
|
296
|
+
path?: string;
|
|
297
|
+
tempFilePath?: string;
|
|
298
|
+
}): Promise<string>;
|
|
294
299
|
|
|
295
300
|
/**
|
|
296
301
|
* 请求上传凭证
|
|
@@ -304,7 +309,7 @@ declare function postUploadSign(data: {
|
|
|
304
309
|
size?: number;
|
|
305
310
|
}, signUrl?: string): Promise<ApiRes<UploadSign>>;
|
|
306
311
|
/**
|
|
307
|
-
*
|
|
312
|
+
* 执行文件直传(原生解析,杜绝 escape/decode 废弃 API)
|
|
308
313
|
* @param cred 上传签名凭据
|
|
309
314
|
* @param filePath 本地文件路径
|
|
310
315
|
* @param customHeader 额外请求头
|
|
@@ -318,12 +323,25 @@ declare function doUpload(cred: UploadSign, filePath: string, customHeader?: Rec
|
|
|
318
323
|
*/
|
|
319
324
|
declare function uploadFile(params: UploadFileParams): Promise<string>;
|
|
320
325
|
|
|
326
|
+
/**
|
|
327
|
+
* 统一网络请求异常类
|
|
328
|
+
*/
|
|
329
|
+
declare class HttpError<T = unknown> extends Error {
|
|
330
|
+
response?: AxiosResponse<T>;
|
|
331
|
+
status?: number;
|
|
332
|
+
config?: RequestConfig;
|
|
333
|
+
constructor(message: string, options?: {
|
|
334
|
+
response?: AxiosResponse<T>;
|
|
335
|
+
status?: number;
|
|
336
|
+
config?: RequestConfig;
|
|
337
|
+
});
|
|
338
|
+
}
|
|
321
339
|
/**
|
|
322
340
|
* 拦截管理器
|
|
323
341
|
*/
|
|
324
342
|
declare class InterceptorManager<T> {
|
|
325
343
|
private handlers;
|
|
326
|
-
use(fulfilled?: (value: T) => T | Promise<T>, rejected?: (error:
|
|
344
|
+
use(fulfilled?: (value: T) => T | Promise<T>, rejected?: (error: unknown) => unknown): number;
|
|
327
345
|
eject(id: number): void;
|
|
328
346
|
forEach(fn: (handler: InterceptorHandler<T>) => void): void;
|
|
329
347
|
}
|
|
@@ -338,18 +356,18 @@ declare class UniHttpClient {
|
|
|
338
356
|
};
|
|
339
357
|
interceptors: {
|
|
340
358
|
request: InterceptorManager<RequestConfig>;
|
|
341
|
-
response: InterceptorManager<AxiosResponse<
|
|
359
|
+
response: InterceptorManager<AxiosResponse<unknown>>;
|
|
342
360
|
};
|
|
343
361
|
constructor(options?: {
|
|
344
362
|
baseURL?: string;
|
|
345
363
|
timeout?: number;
|
|
346
364
|
headers?: Record<string, string>;
|
|
347
365
|
});
|
|
348
|
-
request<T =
|
|
349
|
-
get<T =
|
|
350
|
-
post<T =
|
|
351
|
-
put<T =
|
|
352
|
-
delete<T =
|
|
366
|
+
request<T = unknown>(config: RequestConfig): Promise<AxiosResponse<T>>;
|
|
367
|
+
get<T = unknown>(url: string, config?: Partial<RequestConfig>): Promise<AxiosResponse<T>>;
|
|
368
|
+
post<T = unknown>(url: string, data?: unknown, config?: Partial<RequestConfig>): Promise<AxiosResponse<T>>;
|
|
369
|
+
put<T = unknown>(url: string, data?: unknown, config?: Partial<RequestConfig>): Promise<AxiosResponse<T>>;
|
|
370
|
+
delete<T = unknown>(url: string, config?: Partial<RequestConfig>): Promise<AxiosResponse<T>>;
|
|
353
371
|
private resolveUrl;
|
|
354
372
|
private send;
|
|
355
373
|
}
|
|
@@ -364,23 +382,23 @@ declare function setupHttp(options?: Partial<HttpOptions>): UniHttpClient;
|
|
|
364
382
|
/**
|
|
365
383
|
* 发送通用请求
|
|
366
384
|
*/
|
|
367
|
-
declare function request<T =
|
|
385
|
+
declare function request<T = unknown>(config: RequestConfig): Promise<ApiRes<T>>;
|
|
368
386
|
/**
|
|
369
387
|
* 发送 GET 读取请求
|
|
370
388
|
*/
|
|
371
|
-
declare function get<T =
|
|
389
|
+
declare function get<T = unknown>(url: string, params?: unknown): Promise<ApiRes<T>>;
|
|
372
390
|
/**
|
|
373
391
|
* 发送 POST 提交请求
|
|
374
392
|
*/
|
|
375
|
-
declare function post<T =
|
|
393
|
+
declare function post<T = unknown>(url: string, data?: unknown): Promise<ApiRes<T>>;
|
|
376
394
|
/**
|
|
377
395
|
* 发送 PUT 修改请求
|
|
378
396
|
*/
|
|
379
|
-
declare function put<T =
|
|
397
|
+
declare function put<T = unknown>(url: string, data?: unknown): Promise<ApiRes<T>>;
|
|
380
398
|
/**
|
|
381
399
|
* 发送 DELETE 删除请求
|
|
382
400
|
*/
|
|
383
|
-
declare function del<T =
|
|
401
|
+
declare function del<T = unknown>(url: string, params?: unknown): Promise<ApiRes<T>>;
|
|
384
402
|
/**
|
|
385
403
|
* 获取全局网络配置
|
|
386
404
|
*/
|
|
@@ -388,4 +406,4 @@ declare function getHttpOptions(): HttpOptions;
|
|
|
388
406
|
declare const client: UniHttpClient;
|
|
389
407
|
declare const http: UniHttpClient;
|
|
390
408
|
|
|
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 };
|
|
409
|
+
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:
|
|
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?:
|
|
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?:
|
|
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 =
|
|
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:
|
|
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 =
|
|
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,
|
|
176
|
+
getDevice?: () => DeviceInfo | Record<string, unknown>
|
|
172
177
|
/**
|
|
173
178
|
* 鉴权失败回调
|
|
174
179
|
*/
|
|
@@ -201,17 +206,14 @@ 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
|
|
212
|
-
oSSAccessKeyId?: string
|
|
213
|
-
signature?: string
|
|
214
|
-
successActionStatus?: string
|
|
216
|
+
success_action_status?: string
|
|
215
217
|
}
|
|
216
218
|
|
|
217
219
|
/**
|
|
@@ -290,7 +292,10 @@ declare function encryptRsa(plaintext: string, publicKey: string): string;
|
|
|
290
292
|
* @param source 源文件路径或文件对象
|
|
291
293
|
* @returns 资源访问地址
|
|
292
294
|
*/
|
|
293
|
-
declare function uploadOss(oss: OssConfig, source: string |
|
|
295
|
+
declare function uploadOss(oss: OssConfig, source: string | {
|
|
296
|
+
path?: string;
|
|
297
|
+
tempFilePath?: string;
|
|
298
|
+
}): Promise<string>;
|
|
294
299
|
|
|
295
300
|
/**
|
|
296
301
|
* 请求上传凭证
|
|
@@ -304,7 +309,7 @@ declare function postUploadSign(data: {
|
|
|
304
309
|
size?: number;
|
|
305
310
|
}, signUrl?: string): Promise<ApiRes<UploadSign>>;
|
|
306
311
|
/**
|
|
307
|
-
*
|
|
312
|
+
* 执行文件直传(原生解析,杜绝 escape/decode 废弃 API)
|
|
308
313
|
* @param cred 上传签名凭据
|
|
309
314
|
* @param filePath 本地文件路径
|
|
310
315
|
* @param customHeader 额外请求头
|
|
@@ -318,12 +323,25 @@ declare function doUpload(cred: UploadSign, filePath: string, customHeader?: Rec
|
|
|
318
323
|
*/
|
|
319
324
|
declare function uploadFile(params: UploadFileParams): Promise<string>;
|
|
320
325
|
|
|
326
|
+
/**
|
|
327
|
+
* 统一网络请求异常类
|
|
328
|
+
*/
|
|
329
|
+
declare class HttpError<T = unknown> extends Error {
|
|
330
|
+
response?: AxiosResponse<T>;
|
|
331
|
+
status?: number;
|
|
332
|
+
config?: RequestConfig;
|
|
333
|
+
constructor(message: string, options?: {
|
|
334
|
+
response?: AxiosResponse<T>;
|
|
335
|
+
status?: number;
|
|
336
|
+
config?: RequestConfig;
|
|
337
|
+
});
|
|
338
|
+
}
|
|
321
339
|
/**
|
|
322
340
|
* 拦截管理器
|
|
323
341
|
*/
|
|
324
342
|
declare class InterceptorManager<T> {
|
|
325
343
|
private handlers;
|
|
326
|
-
use(fulfilled?: (value: T) => T | Promise<T>, rejected?: (error:
|
|
344
|
+
use(fulfilled?: (value: T) => T | Promise<T>, rejected?: (error: unknown) => unknown): number;
|
|
327
345
|
eject(id: number): void;
|
|
328
346
|
forEach(fn: (handler: InterceptorHandler<T>) => void): void;
|
|
329
347
|
}
|
|
@@ -338,18 +356,18 @@ declare class UniHttpClient {
|
|
|
338
356
|
};
|
|
339
357
|
interceptors: {
|
|
340
358
|
request: InterceptorManager<RequestConfig>;
|
|
341
|
-
response: InterceptorManager<AxiosResponse<
|
|
359
|
+
response: InterceptorManager<AxiosResponse<unknown>>;
|
|
342
360
|
};
|
|
343
361
|
constructor(options?: {
|
|
344
362
|
baseURL?: string;
|
|
345
363
|
timeout?: number;
|
|
346
364
|
headers?: Record<string, string>;
|
|
347
365
|
});
|
|
348
|
-
request<T =
|
|
349
|
-
get<T =
|
|
350
|
-
post<T =
|
|
351
|
-
put<T =
|
|
352
|
-
delete<T =
|
|
366
|
+
request<T = unknown>(config: RequestConfig): Promise<AxiosResponse<T>>;
|
|
367
|
+
get<T = unknown>(url: string, config?: Partial<RequestConfig>): Promise<AxiosResponse<T>>;
|
|
368
|
+
post<T = unknown>(url: string, data?: unknown, config?: Partial<RequestConfig>): Promise<AxiosResponse<T>>;
|
|
369
|
+
put<T = unknown>(url: string, data?: unknown, config?: Partial<RequestConfig>): Promise<AxiosResponse<T>>;
|
|
370
|
+
delete<T = unknown>(url: string, config?: Partial<RequestConfig>): Promise<AxiosResponse<T>>;
|
|
353
371
|
private resolveUrl;
|
|
354
372
|
private send;
|
|
355
373
|
}
|
|
@@ -364,23 +382,23 @@ declare function setupHttp(options?: Partial<HttpOptions>): UniHttpClient;
|
|
|
364
382
|
/**
|
|
365
383
|
* 发送通用请求
|
|
366
384
|
*/
|
|
367
|
-
declare function request<T =
|
|
385
|
+
declare function request<T = unknown>(config: RequestConfig): Promise<ApiRes<T>>;
|
|
368
386
|
/**
|
|
369
387
|
* 发送 GET 读取请求
|
|
370
388
|
*/
|
|
371
|
-
declare function get<T =
|
|
389
|
+
declare function get<T = unknown>(url: string, params?: unknown): Promise<ApiRes<T>>;
|
|
372
390
|
/**
|
|
373
391
|
* 发送 POST 提交请求
|
|
374
392
|
*/
|
|
375
|
-
declare function post<T =
|
|
393
|
+
declare function post<T = unknown>(url: string, data?: unknown): Promise<ApiRes<T>>;
|
|
376
394
|
/**
|
|
377
395
|
* 发送 PUT 修改请求
|
|
378
396
|
*/
|
|
379
|
-
declare function put<T =
|
|
397
|
+
declare function put<T = unknown>(url: string, data?: unknown): Promise<ApiRes<T>>;
|
|
380
398
|
/**
|
|
381
399
|
* 发送 DELETE 删除请求
|
|
382
400
|
*/
|
|
383
|
-
declare function del<T =
|
|
401
|
+
declare function del<T = unknown>(url: string, params?: unknown): Promise<ApiRes<T>>;
|
|
384
402
|
/**
|
|
385
403
|
* 获取全局网络配置
|
|
386
404
|
*/
|
|
@@ -388,4 +406,4 @@ declare function getHttpOptions(): HttpOptions;
|
|
|
388
406
|
declare const client: UniHttpClient;
|
|
389
407
|
declare const http: UniHttpClient;
|
|
390
408
|
|
|
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 };
|
|
409
|
+
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
|
|
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 =
|
|
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
|
|
324
|
-
const name = oss.filename || filePath
|
|
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:
|
|
328
|
+
key: oss.key || "",
|
|
327
329
|
success_action_status: "200"
|
|
328
330
|
};
|
|
329
|
-
if (oss.policy) formData.policy =
|
|
330
|
-
if (oss.OSSAccessKeyId) formData.OSSAccessKeyId =
|
|
331
|
-
if (oss.signature) formData.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 =
|
|
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(
|
|
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
|
|
369
|
-
Signature: cred.Signature
|
|
370
|
-
success_action_status: cred.success_action_status ||
|
|
370
|
+
OSSAccessKeyId: cred.OSSAccessKeyId,
|
|
371
|
+
Signature: cred.Signature,
|
|
372
|
+
success_action_status: cred.success_action_status || "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(
|
|
423
|
-
body
|
|
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
|
|
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
|
|
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
|
|
552
|
-
|
|
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
|
-
|
|
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
|
|
641
|
-
const
|
|
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
|
|
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
|
|
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 =
|
|
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
|
|
267
|
-
const name = oss.filename || filePath
|
|
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:
|
|
270
|
+
key: oss.key || "",
|
|
270
271
|
success_action_status: "200"
|
|
271
272
|
};
|
|
272
|
-
if (oss.policy) formData.policy =
|
|
273
|
-
if (oss.OSSAccessKeyId) formData.OSSAccessKeyId =
|
|
274
|
-
if (oss.signature) formData.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 =
|
|
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(
|
|
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
|
|
312
|
-
Signature: cred.Signature
|
|
313
|
-
success_action_status: cred.success_action_status ||
|
|
312
|
+
OSSAccessKeyId: cred.OSSAccessKeyId,
|
|
313
|
+
Signature: cred.Signature,
|
|
314
|
+
success_action_status: cred.success_action_status || "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(
|
|
366
|
-
body
|
|
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
|
|
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
|
|
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
|
|
495
|
-
|
|
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
|
-
|
|
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
|
|
584
|
-
const
|
|
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
|
|
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