@seayoo-web/request 3.5.2 → 4.0.1

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/wx.js CHANGED
@@ -1,61 +1,70 @@
1
- import { h as i, r as p, c as f, H as l, E as h, a as d, i as y, N as g } from "./retry-Cxkf8ke1.js";
2
- import { g as E } from "./retry-Cxkf8ke1.js";
3
- const m = async function(t, n, r) {
4
- return i(await p(x, t, n, r), t, n, r);
5
- }, x = async function(t, n, r) {
6
- const e = await f(t, n, r), a = e.method === "PATCH" ? "POST" : e.method, u = l(e.url, e.params);
7
- return h() ? new Promise((c) => {
1
+ import { h as l, r as p, c as y, E as R, R as u, f as x, a as h, t as c, N as w } from "./retry-b0Etywu_.js";
2
+ import { g as O } from "./retry-b0Etywu_.js";
3
+ const m = async function(s, o, n) {
4
+ return l(await p(q, s, o, n), s, o, n);
5
+ }, q = async function(s, o, n) {
6
+ const t = await y(s, o, n), r = t.method;
7
+ return !R() || r === "PATCH" ? {
8
+ url: t.url,
9
+ method: r,
10
+ status: -1,
11
+ error: u.NotSupport,
12
+ statusText: u.NotSupport,
13
+ body: ""
14
+ } : new Promise((f) => {
8
15
  wx.request({
9
- url: u,
10
- data: e.body,
11
- header: e.headers,
12
- method: a,
13
- dataType: "string",
14
- responseType: "text",
15
- fail() {
16
- c({
17
- url: u,
18
- method: a,
16
+ url: t.url,
17
+ data: t.body,
18
+ header: t.headers,
19
+ method: r,
20
+ dataType: "raw",
21
+ timeout: t.timeout,
22
+ responseType: t.asBuffer ? "arraybuffer" : "text",
23
+ fail({ errMsg: e, errno: d, exception: i }) {
24
+ f({
25
+ url: t.url,
26
+ method: r,
19
27
  status: -1,
20
- statusText: d.Unknown,
21
- body: ""
28
+ error: u.Unknown,
29
+ statusText: e,
30
+ body: JSON.stringify({ errMsg: e, errno: d, exception: i })
22
31
  });
23
32
  },
24
- success(o) {
25
- c({
26
- url: u,
27
- method: a,
28
- status: o.statusCode,
29
- statusText: o.statusCode + "",
30
- headers: { ...o.header },
31
- body: e.method === "HEAD" || o.statusCode === 204 ? "" : R(o.data)
32
- });
33
+ async success(e) {
34
+ f(
35
+ await x(
36
+ {
37
+ url: t.url,
38
+ method: r,
39
+ status: e.statusCode,
40
+ statusText: `WxRequest${e.statusCode}`,
41
+ headers: { ...e.header },
42
+ body: null
43
+ },
44
+ {
45
+ asBuffer: t.asBuffer,
46
+ text: () => c(e.data),
47
+ buffer: async () => e.data instanceof ArrayBuffer ? e.data : await h(c(e.data))
48
+ }
49
+ )
50
+ );
33
51
  }
34
52
  });
35
- }) : {
36
- url: u,
37
- method: a,
38
- status: -1,
39
- statusText: d.NotSupport,
40
- body: ""
41
- };
53
+ });
42
54
  };
43
- function R(t) {
44
- return typeof t == "string" ? t : t instanceof ArrayBuffer && y() ? new TextDecoder().decode(t) : JSON.stringify(t);
45
- }
46
55
  // @__NO_SIDE_EFFECTS__
47
- function q(t) {
48
- return new g(m, t);
56
+ function b(s) {
57
+ return new w(m, s);
49
58
  }
50
- const s = /* @__PURE__ */ q(), T = s.setConfig, b = s.head, C = s.get, H = s.post, N = s.del, S = s.put;
59
+ const a = /* @__PURE__ */ b(), T = a.setConfig, N = a.head, B = a.get, C = a.post, S = a.del, A = a.put;
51
60
  export {
52
- q as NetRequest,
53
- d as RequestInternalError,
54
- N as del,
55
- C as get,
56
- E as getResponseRulesDescription,
57
- b as head,
58
- H as post,
59
- S as put,
61
+ b as NetRequest,
62
+ u as RequestInternalError,
63
+ S as del,
64
+ B as get,
65
+ O as getResponseRulesDescription,
66
+ N as head,
67
+ C as post,
68
+ A as put,
60
69
  T as setGlobalConfig
61
70
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seayoo-web/request",
3
- "version": "3.5.2",
3
+ "version": "4.0.1",
4
4
  "description": "request tools for seayoo web",
5
5
  "type": "module",
6
6
  "source": "./src/index.ts",
@@ -1,4 +1,4 @@
1
- import { RequestCacheResolve } from "./type";
1
+ import { RequestCacheResolve, RequestQueryParams } from "./type";
2
2
  /** 为 get 请求并发而设计的缓冲工具 */
3
3
  export declare class Cache<T = unknown> {
4
4
  private ttl;
@@ -6,7 +6,7 @@ export declare class Cache<T = unknown> {
6
6
  private timer;
7
7
  private loopCheck;
8
8
  constructor(ttl?: number);
9
- getKey(method: string, url: string, param?: Record<string, unknown>, resolve?: RequestCacheResolve): string;
9
+ getKey(method: string, url: string, param?: RequestQueryParams, resolve?: RequestCacheResolve): string;
10
10
  updateTTL(ttl: number): void;
11
11
  get(key: string, ttl?: number): Promise<T> | null;
12
12
  set(key: string, action: Promise<T>, ttl?: number): void;
@@ -1,10 +1,10 @@
1
- import type { IRequestGlobalConfig } from "./type";
1
+ import type { RequestGlobalConfig } from "./type";
2
2
  import type { SomeRequired } from "@seayoo-web/utils";
3
- export declare class RequestGlobalConfig {
3
+ export declare class GlobalConfigHelper {
4
4
  private config;
5
- constructor(config?: IRequestGlobalConfig);
6
- set(config: Partial<IRequestGlobalConfig>): void;
7
- get<T extends keyof IRequestGlobalConfig>(key: T): SomeRequired<IRequestGlobalConfig, "timeout" | "credentials" | "baseURL" | "maxRetry" | "cacheTTL" | "responseRule" | "retryInterval" | "retryResolve" | "defaultTypeGuardMessage">[T];
5
+ constructor(config?: RequestGlobalConfig);
6
+ set(config: Partial<RequestGlobalConfig>): void;
7
+ get<T extends keyof RequestGlobalConfig>(key: T): SomeRequired<RequestGlobalConfig, "timeout" | "credentials" | "baseURL" | "maxRetry" | "cacheTTL" | "responseRule" | "retryInterval" | "retryResolve" | "defaultTypeGuardMessage">[T];
8
8
  /** 基于 baseURL 返回完整的 url 地址 */
9
9
  getFullUrl(url: string): string;
10
10
  /** 提示消息 */
@@ -1,3 +1,3 @@
1
- import type { RequestGlobalConfig } from "./config";
2
- import type { IResponseResult, TypeGuardParam } from "./type";
3
- export declare function checkTypedDataResult<T>(url: string, result: IResponseResult, config: RequestGlobalConfig, typeGard: TypeGuardParam<T> | null): IResponseResult<T | null | unknown>;
1
+ import type { GlobalConfigHelper } from "./config";
2
+ import type { ResponseResult, DataGuard } from "./type";
3
+ export declare function withGuard<T>(url: string, result: ResponseResult, config: GlobalConfigHelper, dataGuard: DataGuard<T> | null): ResponseResult<T | null | unknown>;
@@ -1,9 +1,10 @@
1
+ import { RequestQueryParams } from "./type";
1
2
  import type { TypeGuardFn } from "@seayoo-web/utils";
2
3
  /**
3
4
  * 以 script 方式加载远程资源,资源通过回调函数接收,强制进行类型校验
4
5
  */
5
- export declare function jsonp<T>(url: string, typeGuard: TypeGuardFn<T>, params?: Record<string, string | number | boolean>): Promise<T | null>;
6
+ export declare function jsonp<T>(url: string, typeGuard: TypeGuardFn<T>, params?: RequestQueryParams): Promise<T | null>;
6
7
  /**
7
8
  * 以 script 方式加载远程资源,资源通过全局变量接收,强制进行类型校验
8
9
  */
9
- export declare function jsonx<T>(url: string, typeGuard: TypeGuardFn<T>, params?: Record<string, string | number | boolean>): Promise<T | null>;
10
+ export declare function jsonx<T>(url: string, typeGuard: TypeGuardFn<T>, params?: RequestQueryParams): Promise<T | null>;
@@ -1,63 +1,80 @@
1
- import type { TypeGuardParam, IRequestOptions, IRequestGlobalConfig, IResponseResult, NetRequestAgent, ResponseWithType, ResponseWithoutType } from "./type";
2
- type RequestBody = NonNullable<IRequestOptions["body"]>;
1
+ import type { DataGuard, RequestOptions, RequestGlobalConfig, ResponseResult, NetRequestAgent, ResponseWithType, ResponseWithoutType, RequestQueryParams } from "./type";
2
+ type RequestBody = NonNullable<RequestOptions["body"]>;
3
+ export type URLPlus = string | {
4
+ url: string;
5
+ params: RequestQueryParams;
6
+ };
7
+ export type BufferRequestOptions = RequestOptions & {
8
+ asBuffer: true;
9
+ };
3
10
  /** 工具函数主类 */
4
11
  export declare class NetRequestHandler {
5
12
  private agent;
6
13
  private config;
7
14
  private cache;
8
- constructor(agent: NetRequestAgent, config?: IRequestGlobalConfig);
15
+ constructor(agent: NetRequestAgent, config?: RequestGlobalConfig);
16
+ /** 发送网络请求,含有缓存控制逻辑,不含有 guard 逻辑 */
17
+ private send;
9
18
  /**
10
- * 执行网络请求
19
+ * 发送一个网络请求,method 默认是 `GET`。推荐使用包装函数,如 `get` / `post` 等
11
20
  */
12
- request(url: string, options?: IRequestOptions): Promise<IResponseResult<unknown>>;
13
- /**
14
- * 检查响应的数据类型
15
- */
16
- private guard;
21
+ request(op: {
22
+ url: string;
23
+ options?: RequestOptions;
24
+ }): Promise<ResponseResult<unknown>>;
25
+ request<T>(op: {
26
+ url: string;
27
+ options?: RequestOptions;
28
+ guard: DataGuard<T>;
29
+ }): Promise<ResponseResult<T>>;
30
+ /** 整理合并通用参数 */
31
+ private gen;
17
32
  /**
18
33
  * 修改默认请求配置: baseURL / timeout / credentials / errorHandler / messageHandler / responseHandler / logHandler / responseRule
19
34
  */
20
- setConfig(config: IRequestGlobalConfig): void;
35
+ setConfig(config: RequestGlobalConfig): void;
21
36
  /**
22
37
  * 读取默认的请求配置
23
38
  */
24
- getConfig<T extends keyof IRequestGlobalConfig>(key: T): import("@seayoo-web/utils").SomeRequired<IRequestGlobalConfig, "timeout" | "credentials" | "baseURL" | "maxRetry" | "cacheTTL" | "responseRule" | "retryInterval" | "retryResolve" | "defaultTypeGuardMessage">[T];
39
+ getConfig<T extends keyof RequestGlobalConfig>(key: T): import("@seayoo-web/utils").SomeRequired<RequestGlobalConfig, "timeout" | "credentials" | "baseURL" | "maxRetry" | "cacheTTL" | "responseRule" | "retryInterval" | "retryResolve" | "defaultTypeGuardMessage">[T];
25
40
  /**
26
41
  * 发送一个 HEAD 请求,并且不处理响应 body
27
42
  */
28
- head(url: string, options?: IRequestOptions): ResponseWithoutType;
43
+ head(url: string, params?: RequestQueryParams | null, options?: Omit<RequestOptions, "asBuffer">): ResponseWithoutType;
29
44
  /**
30
45
  * 发送一个 GET 请求,请求自带 500ms 缓冲控制以应对并发场景
31
46
  */
32
- get(url: string): ResponseWithoutType;
33
- get(url: string, typeGard: null, options?: IRequestOptions): ResponseWithoutType;
34
- get<T>(url: string, typeGard: TypeGuardParam<T>, options?: IRequestOptions): ResponseWithType<T>;
47
+ get(url: string, params?: RequestQueryParams): ResponseWithoutType;
48
+ get(url: string, params: RequestQueryParams | null, guard: null, options: BufferRequestOptions): ResponseWithType<ArrayBuffer>;
49
+ get(url: string, params: RequestQueryParams | null, guard: null, options?: RequestOptions): ResponseWithoutType;
50
+ get<T>(url: string, params: RequestQueryParams | null, guard: DataGuard<T>, options?: RequestOptions): ResponseWithType<T>;
35
51
  /**
36
- * 发送一个 POST 请求,可选 typeGuard 用于检查数据类型
52
+ * 发送一个 POST 请求
37
53
  */
38
- post(url: string): ResponseWithoutType;
39
- post(url: string, data: RequestBody): ResponseWithoutType;
40
- post(url: string, data: RequestBody, typeGard: null, options?: IRequestOptions): ResponseWithoutType;
41
- post<T>(url: string, data: RequestBody, typeGard: TypeGuardParam<T>, options?: IRequestOptions): ResponseWithType<T>;
54
+ post(url: URLPlus, data?: RequestBody): ResponseWithoutType;
55
+ post(url: URLPlus, body: RequestBody, guard: null, options: BufferRequestOptions): ResponseWithType<ArrayBuffer>;
56
+ post(url: URLPlus, body: RequestBody, guard: null, options?: RequestOptions): ResponseWithoutType;
57
+ post<T>(url: URLPlus, body: RequestBody, guard: DataGuard<T>, options?: RequestOptions): ResponseWithType<T>;
42
58
  /**
43
- * 发送一个 DELETE 请求,可选 typeGuard 用于检查数据类型
59
+ * 发送一个 DELETE 请求
44
60
  */
45
- del(url: string): ResponseWithoutType;
46
- del(url: string, typeGard: null, options?: IRequestOptions): ResponseWithoutType;
47
- del<T>(url: string, typeGard: TypeGuardParam<T>, options?: IRequestOptions): ResponseWithType<T>;
61
+ del(url: string, params?: RequestQueryParams): ResponseWithoutType;
62
+ del(url: string, params: RequestQueryParams | null, guard: null, options: BufferRequestOptions): ResponseWithType<ArrayBuffer>;
63
+ del(url: string, params: RequestQueryParams | null, guard: null, options?: RequestOptions): ResponseWithoutType;
64
+ del<T>(url: string, params: RequestQueryParams | null, guard: DataGuard<T>, options?: RequestOptions): ResponseWithType<T>;
48
65
  /**
49
- * 发送一个 PUT 请求,可选 typeGuard 用于检查数据类型
66
+ * 发送一个 PUT 请求
50
67
  */
51
- put(url: string): ResponseWithoutType;
52
- put(url: string, data: RequestBody): ResponseWithoutType;
53
- put(url: string, data: RequestBody, typeGard: null, options?: IRequestOptions): ResponseWithoutType;
54
- put<T>(url: string, data: RequestBody, typeGard: TypeGuardParam<T>, options?: IRequestOptions): ResponseWithType<T>;
68
+ put(url: URLPlus, body?: RequestBody): ResponseWithoutType;
69
+ put(url: URLPlus, body: RequestBody, guard: null, options: BufferRequestOptions): ResponseWithType<ArrayBuffer>;
70
+ put(url: URLPlus, body: RequestBody, guard: null, options?: RequestOptions): ResponseWithoutType;
71
+ put<T>(url: URLPlus, body: RequestBody, guard: DataGuard<T>, options?: RequestOptions): ResponseWithType<T>;
55
72
  /**
56
- * 发送一个 PATCH 请求,可选 typeGuard 用于检查数据类型
73
+ * 发送一个 PATCH 请求
57
74
  */
58
- patch(url: string): ResponseWithoutType;
59
- patch(url: string, data: RequestBody): ResponseWithoutType;
60
- patch(url: string, data: RequestBody, typeGard: null, options?: IRequestOptions): ResponseWithoutType;
61
- patch<T>(url: string, data: RequestBody, typeGard: TypeGuardParam<T>, options?: IRequestOptions): ResponseWithType<T>;
75
+ patch(url: URLPlus, data?: RequestBody): ResponseWithoutType;
76
+ patch(url: URLPlus, body: RequestBody, guard: null, options: BufferRequestOptions): ResponseWithType<ArrayBuffer>;
77
+ patch(url: URLPlus, body: RequestBody, guard: null, options?: RequestOptions): ResponseWithoutType;
78
+ patch<T>(url: URLPlus, body: RequestBody, guard: DataGuard<T>, options?: RequestOptions): ResponseWithType<T>;
62
79
  }
63
80
  export {};
@@ -1,11 +1,10 @@
1
- import type { RequestGlobalConfig } from "./config";
2
- import type { IBaseRequestOptions, IBaseRequestBody } from "./type";
3
- type FixedRequestOptions = Omit<Required<IBaseRequestOptions>, "params" | "body" | "abort"> & {
1
+ import type { GlobalConfigHelper } from "./config";
2
+ import type { BaseRequestOptions, BaseRequestBody } from "./type";
3
+ type FixedRequestOptions = Omit<Required<BaseRequestOptions>, "params" | "body" | "abort"> & {
4
4
  url: string;
5
- params: Record<string, string>;
6
- abort?: IBaseRequestOptions["abort"];
5
+ abort?: BaseRequestOptions["abort"];
7
6
  body: ReturnType<typeof convertBody>;
8
7
  };
9
- export declare function convertOptions(url: string, config: RequestGlobalConfig, options?: IBaseRequestOptions): Promise<FixedRequestOptions>;
10
- declare function convertBody(body: IBaseRequestOptions["body"]): IBaseRequestBody | undefined;
8
+ export declare function convertOptions(url: string, config: GlobalConfigHelper, options?: BaseRequestOptions): Promise<FixedRequestOptions>;
9
+ declare function convertBody(body: BaseRequestOptions["body"]): BaseRequestBody | undefined;
11
10
  export {};
@@ -1,6 +1,6 @@
1
- import type { NetRequestAgent, IRequestGlobalConfig, IRequestOptions, IResponseResult, IRetryRequestOptions, IBaseRequestOptions } from "./type";
1
+ import type { NetRequestAgent, RequestGlobalConfig, RequestOptions, ResponseResult, RetryRequestOptions, BaseRequestOptions } from "./type";
2
2
  /** 进度回调函数配置 */
3
- interface XHRequestOptions extends IBaseRequestOptions {
3
+ interface XHRequestOptions extends BaseRequestOptions {
4
4
  onUploadProgress?: (progress: {
5
5
  total: number;
6
6
  loaded: number;
@@ -15,10 +15,10 @@ export declare const xhRequest: NetRequestAgent;
15
15
  /**
16
16
  * 上传文件,支持进度信息和多文件上传,不抛错,限制在浏览器中使用,不支持重试
17
17
  */
18
- export declare function xhrUpload(url: string, files: Record<string, Blob> | Blob, options?: Omit<IRequestOptions, keyof XHRequestOptions | keyof IRetryRequestOptions> & Omit<XHRequestOptions, "body" | "method"> & {
18
+ export declare function xhrUpload(url: string, files: Record<string, Blob> | Blob, options?: Omit<RequestOptions, keyof XHRequestOptions | keyof RetryRequestOptions> & Omit<XHRequestOptions, "body" | "method"> & {
19
19
  body?: Record<string, unknown>;
20
20
  method?: "POST" | "PUT";
21
21
  },
22
22
  /** 全局配置中独有的配置,以受全局配置控制 */
23
- config?: Omit<IRequestGlobalConfig, keyof IRequestOptions>): Promise<IResponseResult>;
23
+ config?: Omit<RequestGlobalConfig, keyof RequestOptions>): Promise<ResponseResult>;
24
24
  export {};
@@ -1,3 +1,8 @@
1
- import type { RequestGlobalConfig } from "./config";
2
- import type { IRequestOptions, IResponseResult, IRequestBaseResponse } from "./type";
3
- export declare function handleResponse(res: IRequestBaseResponse, url: string, config: RequestGlobalConfig, options?: IRequestOptions): IResponseResult;
1
+ import type { GlobalConfigHelper } from "./config";
2
+ import type { RequestOptions, ResponseResult, RequestBaseResponse } from "./type";
3
+ export declare function formatResponse(baseResult: RequestBaseResponse, options: {
4
+ asBuffer?: boolean;
5
+ text: () => Promise<string> | string;
6
+ buffer: () => Promise<ArrayBuffer> | ArrayBuffer;
7
+ }): Promise<RequestBaseResponse>;
8
+ export declare function handleResponse(res: RequestBaseResponse, url: string, config: GlobalConfigHelper, options?: RequestOptions): ResponseResult;
@@ -1,3 +1,3 @@
1
- import type { RequestGlobalConfig } from "./config";
2
- import type { NetRequestCoreFn, IRequestOptions } from "./type";
3
- export declare function retryRequest(agent: NetRequestCoreFn, url: string, config: RequestGlobalConfig, options?: IRequestOptions, _try?: number): ReturnType<NetRequestCoreFn>;
1
+ import type { GlobalConfigHelper } from "./config";
2
+ import type { NetRequestCoreFn, RequestOptions } from "./type";
3
+ export declare function retryRequest(agent: NetRequestCoreFn, url: string, config: GlobalConfigHelper, options?: RequestOptions, _try?: number): ReturnType<NetRequestCoreFn>;
@@ -1,12 +1,12 @@
1
- import type { IResponseResult, IResponseRule } from "./type";
2
- type ResponseInfo = Pick<IResponseResult, "ok" | "code" | "data" | "message">;
1
+ import type { ResponseResult, ResponseRule, RequestBaseResponse } from "./type";
2
+ type ResponseInfo = Pick<ResponseResult, "ok" | "code" | "data" | "message">;
3
3
  /**
4
4
  * 分析响应内容中的错误信息和数据
5
5
  */
6
- export declare function analysesResponse(status: number, statusText: string, responseText: string, globalRule: IResponseRule, rule?: IResponseRule): ResponseInfo;
6
+ export declare function analysesResponse(res: RequestBaseResponse, globalRule: ResponseRule, rule?: ResponseRule): ResponseInfo;
7
+ export declare function statusOK(status: number): boolean;
7
8
  /**
8
9
  * 将 Response Rules 转化成为自然描述语言
9
10
  */
10
- export declare const getResponseRulesDescription: (rules: IResponseRule) => string;
11
- export declare function statusOK(status: number): boolean;
11
+ export declare const getResponseRulesDescription: (rules: ResponseRule) => string;
12
12
  export {};
@@ -1,12 +1,12 @@
1
- import type { ERequestRawError, IRequestGlobalConfig } from "./type";
1
+ import type { ERequestRawError, RequestGlobalConfig, ResponseBody } from "./type";
2
2
  /** 根据响应组织整理 sentry 上报信息 */
3
3
  export declare function getSentryExceptionData(data: {
4
4
  url: string;
5
5
  status: number;
6
- code: string;
7
6
  method: string;
7
+ code: string;
8
8
  message: string;
9
- body: string;
9
+ body: ResponseBody;
10
10
  headers: Record<string, string>;
11
11
  error?: ERequestRawError;
12
- }): Pick<Parameters<NonNullable<IRequestGlobalConfig["errorHandler"]>>[0], "sentryError" | "sentryTags" | "sentryExtra">;
12
+ }): Pick<Parameters<NonNullable<RequestGlobalConfig["errorHandler"]>>[0], "sentryError" | "sentryTags" | "sentryExtra">;