@seayoo-web/request 3.3.3 → 3.4.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seayoo-web/request",
3
- "version": "3.3.3",
3
+ "version": "3.4.2",
4
4
  "description": "request tools for seayoo web",
5
5
  "type": "module",
6
6
  "source": "./src/index.ts",
@@ -12,27 +12,23 @@
12
12
  ".": {
13
13
  "types": "./types/index.d.ts",
14
14
  "import": "./dist/index.js",
15
- "require": "./dist/index.cjs",
16
15
  "default": "./dist/index.js"
17
16
  },
18
17
  "./node": {
19
18
  "browser": null,
20
19
  "types": "./types/node.d.ts",
21
20
  "import": "./dist/node.js",
22
- "require": "./dist/node.cjs",
23
21
  "default": "./dist/node.cjs"
24
22
  },
25
23
  "./dist/node": {
26
24
  "browser": null,
27
25
  "types": "./types/node.d.ts",
28
26
  "import": "./dist/node.js",
29
- "require": "./dist/node.cjs",
30
27
  "default": "./dist/node.cjs"
31
28
  },
32
29
  "./wx": {
33
30
  "types": "./types/wx.d.ts",
34
31
  "import": "./dist/wx.js",
35
- "require": "./dist/wx.cjs",
36
32
  "default": "./dist/wx.js"
37
33
  }
38
34
  },
@@ -55,12 +51,12 @@
55
51
  "author": "web@seayoo.com",
56
52
  "license": "ISC",
57
53
  "devDependencies": {
58
- "@vitest/browser": "^3.0.5",
59
- "@vitest/coverage-istanbul": "^3.0.5",
54
+ "@vitest/browser": "^3.2.4",
55
+ "@vitest/coverage-istanbul": "^3.2.4",
60
56
  "happy-dom": "^12.10.3",
61
- "msw": "^2.7.0",
62
- "vitest": "^3.0.5",
63
- "@seayoo-web/utils": "^3.8.0"
57
+ "msw": "^2.10.5",
58
+ "vitest": "^3.2.4",
59
+ "@seayoo-web/utils": "^4.1.3"
64
60
  },
65
61
  "scripts": {
66
62
  "prebuild": "pnpm --F utils build",
@@ -1,5 +1,5 @@
1
- import { type SomeRequired } from "@seayoo-web/utils";
2
1
  import type { IRequestGlobalConfig } from "./type";
2
+ import type { SomeRequired } from "@seayoo-web/utils";
3
3
  export declare class RequestGlobalConfig {
4
4
  private config;
5
5
  constructor(config?: IRequestGlobalConfig);
@@ -1,3 +1,3 @@
1
- import { type IResponseResult, type TypeGuardParam } from "./type";
2
1
  import type { RequestGlobalConfig } from "./config";
2
+ import type { IResponseResult, TypeGuardParam } from "./type";
3
3
  export declare function checkTypedDataResult<T>(url: string, result: IResponseResult, config: RequestGlobalConfig, typeGard: TypeGuardParam<T> | null): IResponseResult<T | null | unknown>;
@@ -1,4 +1,4 @@
1
- import { type TypeGuardFn } from "@seayoo-web/utils";
1
+ import type { TypeGuardFn } from "@seayoo-web/utils";
2
2
  /**
3
3
  * 以 script 方式加载远程资源,资源通过回调函数接收,强制进行类型校验
4
4
  */
@@ -1,4 +1,4 @@
1
- import { type TypeGuardParam, type IRequestOptions, type IRequestGlobalConfig, type IResponseResult, type NetRequestAgent, type ResponseWithType, type ResponseWithoutType } from "./type";
1
+ import type { TypeGuardParam, IRequestOptions, IRequestGlobalConfig, IResponseResult, NetRequestAgent, ResponseWithType, ResponseWithoutType } from "./type";
2
2
  type RequestBody = NonNullable<IRequestOptions["body"]>;
3
3
  /** 工具函数主类 */
4
4
  export declare class NetRequestHandler {
@@ -1,4 +1,4 @@
1
- import { type NetRequestAgent } from "./type";
1
+ import type { NetRequestAgent } from "./type";
2
2
  /**
3
3
  * 基于 fetch 的网络请求包装函数,文件上传不支持进度,需要进度请使用 upload 方法
4
4
  *
@@ -1,4 +1,4 @@
1
- import { type NetRequestAgent } from "./type";
1
+ import type { NetRequestAgent } from "./type";
2
2
  /**
3
3
  * 基于 nodejs 的 http/https 包的网络请求包装函数,url 必须是一个完整 url
4
4
  *
@@ -1,4 +1,4 @@
1
- import { type NetRequestAgent } from "./type";
1
+ import type { NetRequestAgent } from "./type";
2
2
  /**
3
3
  * 基于 wx.request 的网络请求包装函数,该方法必定 resolve,限制在微信小程序环境中使用
4
4
  */
@@ -1,4 +1,4 @@
1
- import { type NetRequestAgent, type IRequestGlobalConfig, type IRequestOptions, type IResponseResult, type IRetryRequestOptions, type IBaseRequestOptions } from "./type";
1
+ import type { NetRequestAgent, IRequestGlobalConfig, IRequestOptions, IResponseResult, IRetryRequestOptions, IBaseRequestOptions } from "./type";
2
2
  /** 进度回调函数配置 */
3
3
  interface XHRequestOptions extends IBaseRequestOptions {
4
4
  onUploadProgress?: (progress: {
@@ -2,3 +2,8 @@
2
2
  * 简版 Object.fromEntries 以处理某些低版本浏览器和 webview 的问题
3
3
  */
4
4
  export declare function fromEntries(kv: [string, string | undefined][]): Record<string, string>;
5
+ /**
6
+ * 安全解析 JSON
7
+ * 防止原型污染和原型链攻击
8
+ */
9
+ export declare function safeJSONParse<T = unknown>(text: string): T | null;
package/dist/index.cjs DELETED
@@ -1,2 +0,0 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("./retry-D_7OkloZ.cjs"),m=require("./request.fetch-Ce3_4Yuj.cjs"),y=async function(t,s,e){return r.handleResponse(await r.retryRequest(R,t,s,e),t,s,e)};async function E(t,s,e,n){const o=e?.body,u=e?.method==="PUT"?"PUT":"POST";if(s instanceof Blob){const l=new r.RequestGlobalConfig(n),f=await R(t,l,{...e,method:u,body:s});return r.handleResponse(f,t,l,e)}const c=new FormData,d={...s};o instanceof Object&&Object.entries(o).forEach(([l,f])=>{f instanceof Blob?d[l]=f:Array.isArray(f)?f.forEach((a,b)=>{c.append(`${l}[${b}]`,String(a))}):c.append(l,String(f))});for(const l in d)c.append(l,d[l]);const g=new r.RequestGlobalConfig(n),w=await R(t,g,{...e,method:u,body:c});return r.handleResponse(w,t,g,e)}const R=async function(t,s,e){const n=await r.convertOptions(t,s,e),o=n.method,u=e?.onUploadProgress,c=r.pe(n.url,n.params);return await new Promise(d=>{let g=null,w=!1;const l=function(){w||(a.abort(),w=!0)};function f(){g!==null&&clearTimeout(g),n.abort&&n.abort.removeEventListener("abort",l)}const a=new XMLHttpRequest;let b=!1;if(a.open(o,c,!0),u){let p=1;a.upload.addEventListener("progress",h=>{p=h.total,u({total:h.total,loaded:h.loaded})}),a.addEventListener("load",()=>{u({loaded:p,total:p})})}a.addEventListener("load",()=>{const p=a.status;f(),d({url:c,method:o,status:p,statusText:a.statusText,headers:T(a),body:o==="HEAD"||p===204?"":a.responseText})}),a.addEventListener("error",p=>{f(),d({url:c,method:o,status:a.status||-1,statusText:a.statusText||r.RequestInternalError.NetworkError,body:"",rawError:p})},!0),a.addEventListener("abort",()=>{f(),d({url:c,method:o,status:b?-1:0,statusText:b?r.RequestInternalError.Timeout:r.RequestInternalError.Aborted,body:""})}),Object.entries(n.headers).forEach(([p,h])=>{a.setRequestHeader(p,h)}),n.credentials==="include"&&(a.withCredentials=!0),a.send(n.body||void 0),n.abort&&n.abort.addEventListener("abort",l),n.timeout>0&&(g=setTimeout(function(){b=!0,l()},n.timeout))})};function T(t){const s={};if(!t)return s;const e=t.getAllResponseHeaders();return e&&e!=="null"&&e.replace(/\r/g,"").split(`
2
- `).forEach(n=>{const o=n.trim();if(!o)return;const u=o.split(":"),c=u[0].trim();c&&(s[c]=(u[1]||"").trim())}),s}async function H(t,s,e={}){const n=window;"callback"in e||(e.callback="jsonxData"+Math.random().toString(16).slice(2));const o=e.callback+"";if(!t)return null;const u=r.pe(t,e,!0);return new Promise(c=>{n[o]=function(d){if(o in window&&delete n[o],s(d))return d;console.warn("response type check failed",t,d),c(null)},r.ce(u).catch(function(){c(null),delete n[o]})})}async function x(t,s,e={}){const n=window;return"var"in e||(e.var="jsonxData"+Math.random().toString(16).slice(2)),t?await r.ce(r.pe(t,e,!0)).then(()=>{const o=n[e.var+""];return s(o)?o:(console.warn("response type check failed",t,o),null)}).catch(()=>null):null}const C=async function(t,s,e){return await E(t,s,e,{baseURL:i.getConfig("baseURL"),logHandler:i.getConfig("logHandler"),errorHandler:i.getConfig("errorHandler"),requestTransformer:i.getConfig("requestTransformer"),messageHandler:i.getConfig("messageHandler"),responseHandler:i.getConfig("responseHandler")})};function q(t){if(!r.St.window)throw new Error("Default Module Only Support In Browser");return r.St.fetch?new r.NetRequestHandler(m.fetchRequest,t):new r.NetRequestHandler(y,t)}const i=q(),S=i.setConfig,j=i.request,L=i.head,k=i.get,v=i.post,D=i.del,U=i.put,O=i.patch;exports.RequestInternalError=r.RequestInternalError;exports.getResponseRulesDescription=r.getResponseRulesDescription;exports.NetRequest=q;exports.del=D;exports.get=k;exports.head=L;exports.jsonp=H;exports.jsonx=x;exports.patch=O;exports.post=v;exports.put=U;exports.request=j;exports.setGlobalConfig=S;exports.upload=C;
package/dist/node.cjs DELETED
@@ -1 +0,0 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./retry-D_7OkloZ.cjs"),y=require("./request.fetch-Ce3_4Yuj.cjs"),b=require("node:http"),g=require("node:https"),E=async function(r,a,c){return e.handleResponse(await e.retryRequest(w,r,a,c),r,a,c)},w=async function(r,a,c){const t=await e.convertOptions(r,a,c);if(!e.At(t.url))return{url:t.url,method:t.method,status:-1,statusText:e.RequestInternalError.URLFormatError,headers:{},body:""};const R=/^https:\/\//i.test(t.url)?g:b,u=new URL(t.url),i=t.params;i instanceof Object&&Object.keys(i).forEach(n=>u.searchParams.set(n,i[n]));const q=t.method==="HEAD";return new Promise(n=>{const d=R.request(u,{headers:t.headers,method:t.method,timeout:t.timeout>0?t.timeout:void 0},function(o){const m=[];o.on("data",h=>m.push(h)),o.on("end",()=>{const h=e.fromEntries(Object.entries(o.headers).map(([f,l])=>[f.toLowerCase(),Array.isArray(l)?l.join(","):l]));n({url:u.toString(),method:t.method,status:o.statusCode||-1,statusText:o.statusMessage||e.RequestInternalError.Unknown,headers:h,body:q||o.statusCode===204?"":Buffer.concat(m).toString("utf-8")})})});d.on("error",o=>{n({url:u.toString(),method:t.method,status:-1,statusText:e.RequestInternalError.NetworkError,body:"",rawError:o})}),d.on("timeout",()=>{n({url:u.toString(),method:t.method,status:-1,statusText:e.RequestInternalError.Timeout,body:""})}),t.body&&d.write(t.body),d.end()})};function p(r){return e.St.fetch?new e.NetRequestHandler(y.fetchRequest,r):new e.NetRequestHandler(E,r)}const s=p(),S=s.setConfig,C=s.head,I=s.get,N=s.post,T=s.del,j=s.put,O=s.patch;exports.RequestInternalError=e.RequestInternalError;exports.getResponseRulesDescription=e.getResponseRulesDescription;exports.NetRequest=p;exports.del=T;exports.get=I;exports.head=C;exports.patch=O;exports.post=N;exports.put=j;exports.setGlobalConfig=S;
@@ -1 +0,0 @@
1
- "use strict";const r=require("./retry-D_7OkloZ.cjs"),E=async function(s,a,i){return r.handleResponse(await r.retryRequest(w,s,a,i),s,a,i)},w=async function(s,a,i){const t=await r.convertOptions(s,a,i),c=new URL(t.url),u=t.params;u instanceof Object&&Object.keys(u).forEach(e=>c.searchParams.set(e,u[e]));const o=r.St.AbortController?new AbortController:null;function l(){o&&!o.signal.aborted&&o.abort()}t.abort&&t.abort.addEventListener("abort",l);let d=!1;const h=t.timeout>0?setTimeout(function(){d=!0,l()},t.timeout):null,f=t.method==="HEAD";return await fetch(c,{method:t.method,headers:Object.keys(t.headers).length>0?new Headers(t.headers):void 0,body:t.body,credentials:t.credentials,signal:o?.signal,redirect:"follow"}).then(async e=>{const n={url:c.toString(),method:t.method,status:e.status,statusText:e.statusText,headers:r.fromEntries([...e.headers.entries()])},b=f||e.status===204?"":await e.text().catch(m=>m);return b instanceof Error?{...n,body:"",statusText:r.RequestInternalError.Unknown,rawError:b}:{...n,body:b}}).catch(e=>{const n=o?.signal.aborted;return{url:c.toString(),method:t.method,status:n&&!d?0:-1,statusText:d?r.RequestInternalError.Timeout:n?r.RequestInternalError.Aborted:r.RequestInternalError.NetworkError,body:"",rawError:e}}).finally(()=>{h!==null&&clearTimeout(h),t.abort&&t.abort.removeEventListener("abort",l)})};exports.fetchRequest=E;