@sycsq/common 0.0.3 → 0.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.
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const x=require("axios"),I=require("./axiosCancel.cjs"),u=require("../utils.cjs"),h=require("./types.cjs"),a=c=>typeof c=="function";function E(c){const t=[],e=(s,n)=>{if(n!==void 0){if(n===null){t.push(`${encodeURIComponent(s)}=`);return}if(Array.isArray(n)){n.forEach(r=>e(`${s}[]`,r));return}if(typeof n=="object"){Object.entries(n).forEach(([r,i])=>{e(`${s}[${r}]`,i)});return}t.push(`${encodeURIComponent(s)}=${encodeURIComponent(String(n))}`)}};return c&&typeof c=="object"&&Object.entries(c).forEach(([s,n])=>e(s,n)),t.join("&")}class T{axiosInstance;options;constructor(t){this.options=t,this.axiosInstance=x.create(t),this.setupInterceptors()}createAxios(t){this.axiosInstance=x.create(t)}getTransform(){const{transform:t}=this.options;return t}getAxios(){return this.axiosInstance}configAxios(t){this.axiosInstance&&(this.options={...this.options,...t,requestOptions:{...this.options.requestOptions,...t.requestOptions},transform:t.transform||this.options.transform},this.createAxios(this.options),this.setupInterceptors())}setHeader(t){this.axiosInstance&&Object.assign(this.axiosInstance.defaults.headers,t)}supportFormData(t){const e=t.headers||this.options.headers,s=typeof e?.get=="function"?e.get("Content-Type"):e?.["Content-Type"]||e?.["content-type"];return!String(s||"").includes(h.ContentTypeEnum.FORM_URLENCODED)||!Reflect.has(t,"data")||t.method?.toUpperCase()===h.RequestEnum.GET?t:{...t,data:E(t.data)}}request(t,e){let s=t;const n=this.getTransform(),{requestOptions:r}=this.options,i={...r,...e},{beforeRequestHook:o,requestCatchHook:p,transformResponseHook:m}=n||{};return o&&a(o)&&(s=o(s,i)),s.requestOptions=i,s=this.supportFormData(s),new Promise((C,d)=>{this.axiosInstance.request(s).then(q=>{if(m&&a(m)){try{const f=m(q,i);C(f)}catch(f){d(f||new Error("request error!"))}return}C(q)}).catch(q=>{if(p&&a(p)){d(p(q,i));return}d(q)})})}get(t,e,s){const n=u.isString(t)?{...e,url:t}:t,r=u.isString(t)?s:e;return this.request({...n,method:h.RequestEnum.GET},r)}post(t,e,s,n){const r=u.isString(t)?{...s,url:t,data:e}:t,i=u.isString(t)?n:e;return this.request({...r,method:h.RequestEnum.POST},i)}put(t,e,s,n){const r=u.isString(t)?{...s,url:t,data:e}:t,i=u.isString(t)?n:e;return this.request({...r,method:h.RequestEnum.PUT},i)}delete(t,e,s){const n=u.isString(t)?{...e,url:t}:t,r=u.isString(t)?s:e;return this.request({...n,method:h.RequestEnum.DELETE},r)}patch(t,e,s,n){const r=u.isString(t)?{...s,url:t,data:e}:t,i=u.isString(t)?n:e;return this.request({...r,method:"PATCH"},i)}setupInterceptors(){const t=this.getTransform();if(!t)return;const{requestInterceptors:e,requestInterceptorsCatch:s,responseInterceptors:n,responseInterceptorsCatch:r}=t,i=new I.AxiosCanceler;this.axiosInstance.interceptors.request.use(o=>{const p=o.headers?.ignoreCancelToken;return(p!==void 0?p:this.options.requestOptions?.ignoreCancelToken)||i.addPending(o),e&&a(e)&&(o=e(o,this.options)),o},o=>s&&a(s)?s(o):Promise.reject(o),{synchronous:!1,runWhen:()=>!0}),this.axiosInstance.interceptors.response.use(o=>(o?.config&&i.removePending(o.config),n&&a(n)&&(o=n(o)),o),o=>(o?.config&&i.removePending(o.config),r&&a(r)?r(o):Promise.reject(o)))}}exports.Axios=T;
@@ -0,0 +1,149 @@
1
+ import I from "axios";
2
+ import { AxiosCanceler as x } from "./axiosCancel.js";
3
+ import { isString as u } from "../utils.js";
4
+ import { ContentTypeEnum as T, RequestEnum as m } from "./types.js";
5
+ const a = (c) => typeof c == "function";
6
+ function E(c) {
7
+ const t = [], e = (s, n) => {
8
+ if (n !== void 0) {
9
+ if (n === null) {
10
+ t.push(`${encodeURIComponent(s)}=`);
11
+ return;
12
+ }
13
+ if (Array.isArray(n)) {
14
+ n.forEach((r) => e(`${s}[]`, r));
15
+ return;
16
+ }
17
+ if (typeof n == "object") {
18
+ Object.entries(n).forEach(([r, i]) => {
19
+ e(`${s}[${r}]`, i);
20
+ });
21
+ return;
22
+ }
23
+ t.push(`${encodeURIComponent(s)}=${encodeURIComponent(String(n))}`);
24
+ }
25
+ };
26
+ return c && typeof c == "object" && Object.entries(c).forEach(([s, n]) => e(s, n)), t.join("&");
27
+ }
28
+ class b {
29
+ axiosInstance;
30
+ options;
31
+ constructor(t) {
32
+ this.options = t, this.axiosInstance = I.create(t), this.setupInterceptors();
33
+ }
34
+ /**
35
+ * @description create axios instance
36
+ */
37
+ createAxios(t) {
38
+ this.axiosInstance = I.create(t);
39
+ }
40
+ /**
41
+ * get transform options
42
+ */
43
+ getTransform() {
44
+ const { transform: t } = this.options;
45
+ return t;
46
+ }
47
+ /**
48
+ * get instance
49
+ */
50
+ getAxios() {
51
+ return this.axiosInstance;
52
+ }
53
+ /**
54
+ * @description Reconfigure axios
55
+ */
56
+ configAxios(t) {
57
+ this.axiosInstance && (this.options = {
58
+ ...this.options,
59
+ ...t,
60
+ requestOptions: {
61
+ ...this.options.requestOptions,
62
+ ...t.requestOptions
63
+ },
64
+ transform: t.transform || this.options.transform
65
+ }, this.createAxios(this.options), this.setupInterceptors());
66
+ }
67
+ /**
68
+ * @description set general header
69
+ */
70
+ setHeader(t) {
71
+ this.axiosInstance && Object.assign(this.axiosInstance.defaults.headers, t);
72
+ }
73
+ supportFormData(t) {
74
+ const e = t.headers || this.options.headers, s = typeof e?.get == "function" ? e.get("Content-Type") : e?.["Content-Type"] || e?.["content-type"];
75
+ return !String(s || "").includes(T.FORM_URLENCODED) || !Reflect.has(t, "data") || t.method?.toUpperCase() === m.GET ? t : {
76
+ ...t,
77
+ data: E(t.data)
78
+ };
79
+ }
80
+ request(t, e) {
81
+ let s = t;
82
+ const n = this.getTransform(), { requestOptions: r } = this.options, i = { ...r, ...e }, { beforeRequestHook: o, requestCatchHook: p, transformResponseHook: f } = n || {};
83
+ return o && a(o) && (s = o(s, i)), s.requestOptions = i, s = this.supportFormData(s), new Promise((C, d) => {
84
+ this.axiosInstance.request(s).then((h) => {
85
+ if (f && a(f)) {
86
+ try {
87
+ const q = f(h, i);
88
+ C(q);
89
+ } catch (q) {
90
+ d(q || new Error("request error!"));
91
+ }
92
+ return;
93
+ }
94
+ C(h);
95
+ }).catch((h) => {
96
+ if (p && a(p)) {
97
+ d(p(h, i));
98
+ return;
99
+ }
100
+ d(h);
101
+ });
102
+ });
103
+ }
104
+ get(t, e, s) {
105
+ const n = u(t) ? { ...e, url: t } : t, r = u(t) ? s : e;
106
+ return this.request({ ...n, method: m.GET }, r);
107
+ }
108
+ post(t, e, s, n) {
109
+ const r = u(t) ? { ...s, url: t, data: e } : t, i = u(t) ? n : e;
110
+ return this.request({ ...r, method: m.POST }, i);
111
+ }
112
+ put(t, e, s, n) {
113
+ const r = u(t) ? { ...s, url: t, data: e } : t, i = u(t) ? n : e;
114
+ return this.request({ ...r, method: m.PUT }, i);
115
+ }
116
+ delete(t, e, s) {
117
+ const n = u(t) ? { ...e, url: t } : t, r = u(t) ? s : e;
118
+ return this.request({ ...n, method: m.DELETE }, r);
119
+ }
120
+ patch(t, e, s, n) {
121
+ const r = u(t) ? { ...s, url: t, data: e } : t, i = u(t) ? n : e;
122
+ return this.request({ ...r, method: "PATCH" }, i);
123
+ }
124
+ /**
125
+ * @description Interceptor configuration
126
+ */
127
+ setupInterceptors() {
128
+ const t = this.getTransform();
129
+ if (!t) return;
130
+ const { requestInterceptors: e, requestInterceptorsCatch: s, responseInterceptors: n, responseInterceptorsCatch: r } = t, i = new x();
131
+ this.axiosInstance.interceptors.request.use(
132
+ (o) => {
133
+ const p = o.headers?.ignoreCancelToken;
134
+ return (p !== void 0 ? p : this.options.requestOptions?.ignoreCancelToken) || i.addPending(o), e && a(e) && (o = e(o, this.options)), o;
135
+ },
136
+ (o) => s && a(s) ? s(o) : Promise.reject(o),
137
+ {
138
+ synchronous: !1,
139
+ runWhen: () => !0
140
+ }
141
+ ), this.axiosInstance.interceptors.response.use(
142
+ (o) => (o?.config && i.removePending(o.config), n && a(n) && (o = n(o)), o),
143
+ (o) => (o?.config && i.removePending(o.config), r && a(r) ? r(o) : Promise.reject(o))
144
+ );
145
+ }
146
+ }
147
+ export {
148
+ b as Axios
149
+ };
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("axios"),s=require("../utils.cjs");let t=new Map;const r=i=>[i.method,i.url].join("&");class a{addPending(n){this.removePending(n);let e=r(n);e=e.split("?")[0],n.cancelToken=n.cancelToken||new o.CancelToken(l=>{t.has(e)||t.set(e,l)})}removePending(n){let e=r(n);if(e=e?.split("?")[0],t.has(e)){const l=t.get(e);l&&s.isFunction(l)&&l(e),t.delete(e)}}removeAllPending(){t.forEach(n=>{n&&s.isFunction(n)&&n()}),t.clear()}reset(){t=new Map}}exports.AxiosCanceler=a;exports.getPendingUrl=r;
@@ -0,0 +1,32 @@
1
+ import s from "axios";
2
+ import { isFunction as o } from "../utils.js";
3
+ let t = /* @__PURE__ */ new Map();
4
+ const i = (r) => [r.method, r.url].join("&");
5
+ class p {
6
+ addPending(n) {
7
+ this.removePending(n);
8
+ let e = i(n);
9
+ e = e.split("?")[0], n.cancelToken = n.cancelToken || new s.CancelToken((l) => {
10
+ t.has(e) || t.set(e, l);
11
+ });
12
+ }
13
+ removePending(n) {
14
+ let e = i(n);
15
+ if (e = e?.split("?")[0], t.has(e)) {
16
+ const l = t.get(e);
17
+ l && o(l) && l(e), t.delete(e);
18
+ }
19
+ }
20
+ removeAllPending() {
21
+ t.forEach((n) => {
22
+ n && o(n) && n();
23
+ }), t.clear();
24
+ }
25
+ reset() {
26
+ t = /* @__PURE__ */ new Map();
27
+ }
28
+ }
29
+ export {
30
+ p as AxiosCanceler,
31
+ i as getPendingUrl
32
+ };
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const h=require("../utils.cjs"),l=require("./helper.cjs"),i=require("./types.cjs"),R={transformResponseHook(e,t){const{isTransformResponse:s,isReturnNativeResponse:p}=t;if(p)return e;if(!s)return e.data;const{data:a}=e;if(!a)return"[HTTP] Request has no return value";const{code:u,body:d,data:o,message:r,msg:m}=a,T=[i.ResultEnum.ASYNC_SUCCESS,i.ResultEnum.SUCCESS];if(a&&Reflect.has(a,"code")&&T.includes(u))return o!==void 0?o:d;let n="";switch(u){case i.ResultEnum.TIMEOUT:n="请求超时";break;default:n=r||m}throw t.errorHandler?t.errorHandler(n):console.error(n),new Error(n||"请求失败")},beforeRequestHook(e,t){const{apiUrl:s,joinPrefix:p,joinParamsToUrl:a,formatDate:u,joinTime:d=!0,urlPrefix:o}=t;p&&o&&(e.url=`${o}${e.url}`),s&&h.isString(s)&&(e.url=`${s}${e.url}`);const r=e.params||{},m=e.data||null;return u&&m&&l.formatRequestDate(m),e.method?.toUpperCase()===i.RequestEnum.GET?h.isString(r)?(e.url=`${e.url}${r}${l.joinTimestamp(d,!0)}`,e.params=void 0):(u&&l.formatRequestDate(r),e.params=Object.assign(r||{},l.joinTimestamp(d,!1))):h.isString(r)?(e.url+=r,e.params=void 0):(u&&l.formatRequestDate(r),Reflect.has(e,"data")&&e.data&&(e.data=m),Reflect.has(e,"params")&&e.params&&(e.params=r),a&&(e.url=l.setObjToUrlParams(e.url,{...e.params,...e.data}))),e},requestInterceptors(e,t){const s=typeof globalThis<"u"&&"localStorage"in globalThis?globalThis.localStorage?.getItem("token"):null;return s&&e?.requestOptions?.withToken!==!1&&(e.headers=e.headers||{},"set"in e.headers&&typeof e.headers.set=="function"?e.headers.set("Authtoken",s):e.headers.Authtoken=s),e}};exports.axiosTransform=R;
@@ -0,0 +1,48 @@
1
+ import { isString as n } from "../utils.js";
2
+ import { formatRequestDate as h, joinTimestamp as T, setObjToUrlParams as S } from "./helper.js";
3
+ import { RequestEnum as b, ResultEnum as i } from "./types.js";
4
+ const $ = {
5
+ transformResponseHook(e, a) {
6
+ const { isTransformResponse: s, isReturnNativeResponse: p } = a;
7
+ if (p) return e;
8
+ if (!s) return e.data;
9
+ const { data: t } = e;
10
+ if (!t)
11
+ return "[HTTP] Request has no return value";
12
+ const { code: l, body: d, data: o, message: r, msg: u } = t, R = [i.ASYNC_SUCCESS, i.SUCCESS];
13
+ if (t && Reflect.has(t, "code") && R.includes(l)) return o !== void 0 ? o : d;
14
+ let m = "";
15
+ switch (l) {
16
+ case i.TIMEOUT:
17
+ m = "请求超时";
18
+ break;
19
+ default:
20
+ m = r || u;
21
+ }
22
+ throw a.errorHandler ? a.errorHandler(m) : console.error(m), new Error(m || "请求失败");
23
+ },
24
+ beforeRequestHook(e, a) {
25
+ const { apiUrl: s, joinPrefix: p, joinParamsToUrl: t, formatDate: l, joinTime: d = !0, urlPrefix: o } = a;
26
+ p && o && (e.url = `${o}${e.url}`), s && n(s) && (e.url = `${s}${e.url}`);
27
+ const r = e.params || {}, u = e.data || null;
28
+ return l && u && h(u), e.method?.toUpperCase() === b.GET ? n(r) ? (e.url = `${e.url}${r}${T(d, !0)}`, e.params = void 0) : (l && h(r), e.params = Object.assign(r || {}, T(d, !1))) : n(r) ? (e.url += r, e.params = void 0) : (l && h(r), Reflect.has(e, "data") && e.data && (e.data = u), Reflect.has(e, "params") && e.params && (e.params = r), t && (e.url = S(e.url, { ...e.params, ...e.data }))), e;
29
+ },
30
+ /**
31
+ * @description 请求拦截
32
+ */
33
+ requestInterceptors(e, a) {
34
+ const s = typeof globalThis < "u" && "localStorage" in globalThis ? globalThis.localStorage?.getItem("token") : null;
35
+ return s && e?.requestOptions?.withToken !== !1 && (e.headers = e.headers || {}, "set" in e.headers && typeof e.headers.set == "function" ? e.headers.set("Authtoken", s) : e.headers.Authtoken = s), e;
36
+ }
37
+ // requestInterceptorCatch(err: Error) {},
38
+ /**
39
+ * @description 响应拦截处理
40
+ */
41
+ // responseInterceptors(res) {},
42
+ // responseInterceptorsCatch(err: Error) {
43
+ // // 处理错误请求
44
+ // }
45
+ };
46
+ export {
47
+ $ as axiosTransform
48
+ };
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("../utils.cjs"),f="YYYY-MM-DD HH:mm";let u=0;function s(t,e=!1){if(!t)return e?"":{};const n=new Date().getTime()+ ++u;return e?`?_t=${n}`:{_t:n}}function c(t){if(Object.prototype.toString.call(t)==="[object Object]")for(const e in t){const n=t[e]?.format??null;if(n&&typeof n=="function"&&(t[e]=t[e].format(f)),r.isString(e)){const o=t[e];if(o)try{t[e]=r.isString(o)?o.trim():o}catch(i){throw new Error(i)}}r.isObject(t[e])&&c(t[e])}}function m(t,e){const n=Object.entries(e||{}).filter(([,o])=>o!=null).map(([o,i])=>`${encodeURIComponent(o)}=${encodeURIComponent(String(i))}`).join("&");return n?/[?&]$/.test(t)?t+n:`${t}${t.includes("?")?"&":"?"}${n}`:t}exports.formatRequestDate=c;exports.joinTimestamp=s;exports.setObjToUrlParams=m;
@@ -0,0 +1,33 @@
1
+ import { isString as r, isObject as c } from "../utils.js";
2
+ const f = "YYYY-MM-DD HH:mm";
3
+ let u = 0;
4
+ function $(t, n = !1) {
5
+ if (!t) return n ? "" : {};
6
+ const e = (/* @__PURE__ */ new Date()).getTime() + ++u;
7
+ return n ? `?_t=${e}` : { _t: e };
8
+ }
9
+ function m(t) {
10
+ if (Object.prototype.toString.call(t) === "[object Object]")
11
+ for (const n in t) {
12
+ const e = t[n]?.format ?? null;
13
+ if (e && typeof e == "function" && (t[n] = t[n].format(f)), r(n)) {
14
+ const o = t[n];
15
+ if (o)
16
+ try {
17
+ t[n] = r(o) ? o.trim() : o;
18
+ } catch (i) {
19
+ throw new Error(i);
20
+ }
21
+ }
22
+ c(t[n]) && m(t[n]);
23
+ }
24
+ }
25
+ function l(t, n) {
26
+ const e = Object.entries(n || {}).filter(([, o]) => o != null).map(([o, i]) => `${encodeURIComponent(o)}=${encodeURIComponent(String(i))}`).join("&");
27
+ return e ? /[?&]$/.test(t) ? t + e : `${t}${t.includes("?") ? "&" : "?"}${e}` : t;
28
+ }
29
+ export {
30
+ m as formatRequestDate,
31
+ $ as joinTimestamp,
32
+ l as setObjToUrlParams
33
+ };
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var e=(r=>(r.SUCCESS="200",r.ASYNC_SUCCESS="202",r.ERROR="-1",r.TIMEOUT="10042",r.TYPE="success",r.CERT_VALID="409-iam-cert-valid",r))(e||{}),o=(r=>(r.JSON="application/json;charset=UTF-8",r.TEXT="text/plain;charset=UTF-8",r.FORM_URLENCODED="application/x-www-form-urlencoded;charset=UTF-8",r.FORM_DATA="multipart/form-data;charset=UTF-8",r))(o||{}),t=(r=>(r.GET="GET",r.POST="POST",r.PUT="PUT",r.DELETE="DELETE",r))(t||{});class a{beforeRequestHook;requestCatchHook;transformResponseHook;requestInterceptors;responseInterceptors;requestInterceptorsCatch;responseInterceptorsCatch}exports.AxiosTransform=a;exports.ContentTypeEnum=o;exports.RequestEnum=t;exports.ResultEnum=e;
@@ -0,0 +1,16 @@
1
+ var a = /* @__PURE__ */ ((r) => (r.SUCCESS = "200", r.ASYNC_SUCCESS = "202", r.ERROR = "-1", r.TIMEOUT = "10042", r.TYPE = "success", r.CERT_VALID = "409-iam-cert-valid", r))(a || {}), o = /* @__PURE__ */ ((r) => (r.JSON = "application/json;charset=UTF-8", r.TEXT = "text/plain;charset=UTF-8", r.FORM_URLENCODED = "application/x-www-form-urlencoded;charset=UTF-8", r.FORM_DATA = "multipart/form-data;charset=UTF-8", r))(o || {}), e = /* @__PURE__ */ ((r) => (r.GET = "GET", r.POST = "POST", r.PUT = "PUT", r.DELETE = "DELETE", r))(e || {});
2
+ class s {
3
+ beforeRequestHook;
4
+ requestCatchHook;
5
+ transformResponseHook;
6
+ requestInterceptors;
7
+ responseInterceptors;
8
+ requestInterceptorsCatch;
9
+ responseInterceptorsCatch;
10
+ }
11
+ export {
12
+ s as AxiosTransform,
13
+ o as ContentTypeEnum,
14
+ e as RequestEnum,
15
+ a as ResultEnum
16
+ };
package/lib/http.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("./http/Axios.cjs"),n=require("./http/axiosTransform.cjs"),e=require("./http/types.cjs"),o=require("./http/axiosCancel.cjs"),r=require("./http/helper.cjs");require("./utils.cjs");function i(){return new s.Axios({timeout:10*1e3,headers:{"Content-Type":e.ContentTypeEnum.JSON},transform:n.axiosTransform,requestOptions:{joinPrefix:!0,isReturnNativeResponse:!1,isTransformResponse:!0,joinParamsToUrl:!1,formatDate:!0,apiUrl:"",urlPrefix:"",joinTime:!0,ignoreCancelToken:!0,withToken:!0}})}const t=i(),a=t.request.bind(t);exports.Axios=s.Axios;exports.axiosTransform=n.axiosTransform;exports.AxiosTransform=e.AxiosTransform;exports.ContentTypeEnum=e.ContentTypeEnum;exports.RequestEnum=e.RequestEnum;exports.ResultEnum=e.ResultEnum;exports.AxiosCanceler=o.AxiosCanceler;exports.getPendingUrl=o.getPendingUrl;exports.formatRequestDate=r.formatRequestDate;exports.joinTimestamp=r.joinTimestamp;exports.setObjToUrlParams=r.setObjToUrlParams;exports.http=t;exports.request=a;
package/lib/http.js ADDED
@@ -0,0 +1,54 @@
1
+ import { Axios as r } from "./http/Axios.js";
2
+ import { axiosTransform as t } from "./http/axiosTransform.js";
3
+ import { ContentTypeEnum as o } from "./http/types.js";
4
+ import { AxiosTransform as x, RequestEnum as T, ResultEnum as l } from "./http/types.js";
5
+ import { AxiosCanceler as R, getPendingUrl as j } from "./http/axiosCancel.js";
6
+ import { formatRequestDate as P, joinTimestamp as A, setObjToUrlParams as C } from "./http/helper.js";
7
+ import "./utils.js";
8
+ function n() {
9
+ return new r({
10
+ // baseURL: '',
11
+ timeout: 10 * 1e3,
12
+ headers: { "Content-Type": o.JSON },
13
+ // 数据处理方式
14
+ transform: t,
15
+ requestOptions: {
16
+ // 默认将prefix 添加到url
17
+ joinPrefix: !0,
18
+ // 是否返回原生响应头 比如:需要获取响应头时使用该属性
19
+ isReturnNativeResponse: !1,
20
+ // 需要对返回数据进行处理
21
+ isTransformResponse: !0,
22
+ // post请求的时候添加参数到url
23
+ joinParamsToUrl: !1,
24
+ // 格式化提交参数时间
25
+ formatDate: !0,
26
+ // 接口地址
27
+ apiUrl: "",
28
+ // 接口拼接地址
29
+ urlPrefix: "",
30
+ // 是否加入时间戳
31
+ joinTime: !0,
32
+ // 忽略重复请求
33
+ ignoreCancelToken: !0,
34
+ // 是否携带token
35
+ withToken: !0
36
+ }
37
+ });
38
+ }
39
+ const e = n(), u = e.request.bind(e);
40
+ export {
41
+ r as Axios,
42
+ R as AxiosCanceler,
43
+ x as AxiosTransform,
44
+ o as ContentTypeEnum,
45
+ T as RequestEnum,
46
+ l as ResultEnum,
47
+ t as axiosTransform,
48
+ P as formatRequestDate,
49
+ j as getPendingUrl,
50
+ e as http,
51
+ A as joinTimestamp,
52
+ u as request,
53
+ C as setObjToUrlParams
54
+ };
package/lib/index.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("./http.cjs"),e=require("./utils.cjs"),n=require("./http/Axios.cjs"),o=require("./http/axiosTransform.cjs"),r=require("./http/axiosCancel.cjs"),s=require("./http/helper.cjs"),i=require("./http/types.cjs");exports.http=t.http;exports.request=t.request;exports.is=e.is;exports.isArray=e.isArray;exports.isBoolean=e.isBoolean;exports.isClient=e.isClient;exports.isDate=e.isDate;exports.isDef=e.isDef;exports.isElement=e.isElement;exports.isEmpty=e.isEmpty;exports.isFunction=e.isFunction;exports.isNull=e.isNull;exports.isNullAndUnDef=e.isNullAndUnDef;exports.isNullOrUnDef=e.isNullOrUnDef;exports.isNumber=e.isNumber;exports.isObject=e.isObject;exports.isPromise=e.isPromise;exports.isRegExp=e.isRegExp;exports.isServer=e.isServer;exports.isString=e.isString;exports.isSymbol=e.isSymbol;exports.isUnDef=e.isUnDef;exports.isUrl=e.isUrl;exports.isWindow=e.isWindow;exports.Axios=n.Axios;exports.axiosTransform=o.axiosTransform;exports.AxiosCanceler=r.AxiosCanceler;exports.getPendingUrl=r.getPendingUrl;exports.formatRequestDate=s.formatRequestDate;exports.joinTimestamp=s.joinTimestamp;exports.setObjToUrlParams=s.setObjToUrlParams;exports.AxiosTransform=i.AxiosTransform;exports.ContentTypeEnum=i.ContentTypeEnum;exports.RequestEnum=i.RequestEnum;exports.ResultEnum=i.ResultEnum;
package/lib/index.js ADDED
@@ -0,0 +1,44 @@
1
+ import { http as i, request as r } from "./http.js";
2
+ import { is as t, isArray as n, isBoolean as m, isClient as l, isDate as f, isDef as a, isElement as p, isEmpty as u, isFunction as x, isNull as D, isNullAndUnDef as E, isNullOrUnDef as U, isNumber as A, isObject as T, isPromise as b, isRegExp as g, isServer as y, isString as N, isSymbol as R, isUnDef as c, isUrl as d, isWindow as j } from "./utils.js";
3
+ import { Axios as C } from "./http/Axios.js";
4
+ import { axiosTransform as P } from "./http/axiosTransform.js";
5
+ import { AxiosCanceler as h, getPendingUrl as v } from "./http/axiosCancel.js";
6
+ import { formatRequestDate as B, joinTimestamp as F, setObjToUrlParams as W } from "./http/helper.js";
7
+ import { AxiosTransform as z, ContentTypeEnum as G, RequestEnum as H, ResultEnum as I } from "./http/types.js";
8
+ export {
9
+ C as Axios,
10
+ h as AxiosCanceler,
11
+ z as AxiosTransform,
12
+ G as ContentTypeEnum,
13
+ H as RequestEnum,
14
+ I as ResultEnum,
15
+ P as axiosTransform,
16
+ B as formatRequestDate,
17
+ v as getPendingUrl,
18
+ i as http,
19
+ t as is,
20
+ n as isArray,
21
+ m as isBoolean,
22
+ l as isClient,
23
+ f as isDate,
24
+ a as isDef,
25
+ p as isElement,
26
+ u as isEmpty,
27
+ x as isFunction,
28
+ D as isNull,
29
+ E as isNullAndUnDef,
30
+ U as isNullOrUnDef,
31
+ A as isNumber,
32
+ T as isObject,
33
+ b as isPromise,
34
+ g as isRegExp,
35
+ y as isServer,
36
+ N as isString,
37
+ R as isSymbol,
38
+ c as isUnDef,
39
+ d as isUrl,
40
+ j as isWindow,
41
+ F as joinTimestamp,
42
+ r as request,
43
+ W as setObjToUrlParams
44
+ };
package/lib/utils.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const{toString:y}=Object.prototype;function t(n,u){return y.call(n)===`[object ${u}]`}function o(n){return typeof n<"u"}function i(n){return!o(n)}function r(n){return n!==null&&t(n,"Object")}function d(n){return typeof n=="symbol"}function g(n){return c(n)||f(n)?n.length===0:n instanceof Map||n instanceof Set?n.size===0:r(n)?Object.keys(n).length===0:!1}function m(n){return t(n,"Date")}function s(n){return n===null}function p(n){return i(n)||s(n)}function w(n){return i(n)||s(n)}function b(n){return t(n,"Number")}function D(n){return!!n&&(t(n,"Promise")||r(n))&&e(n.then)&&e(n.catch)}function f(n){return t(n,"String")}function e(n){return typeof n=="function"}function S(n){return t(n,"Boolean")}function N(n){return t(n,"RegExp")}function c(n){return Array.isArray(n)}function A(n){return typeof window<"u"&&t(n,"Window")}function O(n){return!!n&&typeof n.tagName=="string"}const l=typeof window>"u",U=!l;function j(n){return!!n&&/(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/.test(n)}exports.is=t;exports.isArray=c;exports.isBoolean=S;exports.isClient=U;exports.isDate=m;exports.isDef=o;exports.isElement=O;exports.isEmpty=g;exports.isFunction=e;exports.isNull=s;exports.isNullAndUnDef=p;exports.isNullOrUnDef=w;exports.isNumber=b;exports.isObject=r;exports.isPromise=D;exports.isRegExp=N;exports.isServer=l;exports.isString=f;exports.isSymbol=d;exports.isUnDef=i;exports.isUrl=j;exports.isWindow=A;
package/lib/utils.js ADDED
@@ -0,0 +1,86 @@
1
+ const { toString: f } = Object.prototype;
2
+ function t(n, e) {
3
+ return f.call(n) === `[object ${e}]`;
4
+ }
5
+ function s(n) {
6
+ return typeof n < "u";
7
+ }
8
+ function i(n) {
9
+ return !s(n);
10
+ }
11
+ function u(n) {
12
+ return n !== null && t(n, "Object");
13
+ }
14
+ function p(n) {
15
+ return typeof n == "symbol";
16
+ }
17
+ function d(n) {
18
+ return w(n) || c(n) ? n.length === 0 : n instanceof Map || n instanceof Set ? n.size === 0 : u(n) ? Object.keys(n).length === 0 : !1;
19
+ }
20
+ function g(n) {
21
+ return t(n, "Date");
22
+ }
23
+ function o(n) {
24
+ return n === null;
25
+ }
26
+ function b(n) {
27
+ return i(n) || o(n);
28
+ }
29
+ function m(n) {
30
+ return i(n) || o(n);
31
+ }
32
+ function l(n) {
33
+ return t(n, "Number");
34
+ }
35
+ function A(n) {
36
+ return !!n && (t(n, "Promise") || u(n)) && r(n.then) && r(n.catch);
37
+ }
38
+ function c(n) {
39
+ return t(n, "String");
40
+ }
41
+ function r(n) {
42
+ return typeof n == "function";
43
+ }
44
+ function D(n) {
45
+ return t(n, "Boolean");
46
+ }
47
+ function N(n) {
48
+ return t(n, "RegExp");
49
+ }
50
+ function w(n) {
51
+ return Array.isArray(n);
52
+ }
53
+ function S(n) {
54
+ return typeof window < "u" && t(n, "Window");
55
+ }
56
+ function j(n) {
57
+ return !!n && typeof n.tagName == "string";
58
+ }
59
+ const y = typeof window > "u", O = !y;
60
+ function h(n) {
61
+ return !!n && /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/.test(n);
62
+ }
63
+ export {
64
+ t as is,
65
+ w as isArray,
66
+ D as isBoolean,
67
+ O as isClient,
68
+ g as isDate,
69
+ s as isDef,
70
+ j as isElement,
71
+ d as isEmpty,
72
+ r as isFunction,
73
+ o as isNull,
74
+ b as isNullAndUnDef,
75
+ m as isNullOrUnDef,
76
+ l as isNumber,
77
+ u as isObject,
78
+ A as isPromise,
79
+ N as isRegExp,
80
+ y as isServer,
81
+ c as isString,
82
+ p as isSymbol,
83
+ i as isUnDef,
84
+ h as isUrl,
85
+ S as isWindow
86
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sycsq/common",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "一个基于 Vite 构建的 TypeScript 工具库,提供 HTTP 请求封装和常用工具函数,支持完整的类型声明",
5
5
  "keywords": [
6
6
  "typescript",
@@ -28,14 +28,53 @@
28
28
  "homepage": "https://github.com/SSQLQIANBB/common/blob/main/README.md",
29
29
  "private": false,
30
30
  "type": "module",
31
- "main": "./lib/index.umd.js",
32
- "module": "./lib/index.esm.js",
33
- "types": "./lib/index.d.ts",
31
+ "main": "./lib/index.cjs",
32
+ "module": "./lib/index.js",
33
+ "types": "./types/index.d.ts",
34
+ "sideEffects": false,
34
35
  "exports": {
35
36
  ".": {
36
37
  "types": "./types/index.d.ts",
37
- "import": "./lib/index.esm.js",
38
- "require": "./lib/index.umd.js"
38
+ "import": "./lib/index.js",
39
+ "require": "./lib/index.cjs"
40
+ },
41
+ "./http": {
42
+ "types": "./types/http/index.d.ts",
43
+ "import": "./lib/http.js",
44
+ "require": "./lib/http.cjs"
45
+ },
46
+ "./http/Axios": {
47
+ "types": "./types/http/Axios.d.ts",
48
+ "import": "./lib/http/Axios.js",
49
+ "require": "./lib/http/Axios.cjs"
50
+ },
51
+ "./http/axiosCancel": {
52
+ "types": "./types/http/axiosCancel.d.ts",
53
+ "import": "./lib/http/axiosCancel.js",
54
+ "require": "./lib/http/axiosCancel.cjs"
55
+ },
56
+ "./http/axiosTransform": {
57
+ "types": "./types/http/axiosTransform.d.ts",
58
+ "import": "./lib/http/axiosTransform.js",
59
+ "require": "./lib/http/axiosTransform.cjs"
60
+ },
61
+ "./http/helper": {
62
+ "types": "./types/http/helper.d.ts",
63
+ "import": "./lib/http/helper.js",
64
+ "require": "./lib/http/helper.cjs"
65
+ },
66
+ "./http/types": {
67
+ "types": "./types/http/types.d.ts",
68
+ "import": "./lib/http/types.js",
69
+ "require": "./lib/http/types.cjs"
70
+ },
71
+ "./utils": {
72
+ "types": "./types/utils/index.d.ts",
73
+ "import": "./lib/utils.js",
74
+ "require": "./lib/utils.cjs"
75
+ },
76
+ "./package.json": {
77
+ "default": "./package.json"
39
78
  }
40
79
  },
41
80
  "files": [
@@ -45,26 +84,19 @@
45
84
  "LICENSE"
46
85
  ],
47
86
  "engines": {
48
- "node": ">=16.0.0"
87
+ "node": "^20.19.0 || >=22.12.0"
49
88
  },
50
89
  "publishConfig": {
51
90
  "access": "public",
52
91
  "registry": "https://registry.npmjs.org/"
53
92
  },
54
- "devDependencies": {
55
- "@types/qs": "^6.14.0",
56
- "qs": "^6.14.0",
57
- "typescript": "~5.9.2",
58
- "vite": "^7.0.6"
59
- },
60
- "dependencies": {
61
- "axios": "^1.11.0"
62
- },
63
- "peerDependenciesMeta": {},
64
93
  "scripts": {
65
94
  "dev": "vite",
95
+ "test": "vitest run",
96
+ "test:watch": "vitest",
66
97
  "build": "tsc && vite build",
67
98
  "build:types": "tsc --emitDeclarationOnly",
99
+ "prepublishOnly": "npm run build",
68
100
  "version:patch": "node scripts/version.js patch",
69
101
  "version:minor": "node scripts/version.js minor",
70
102
  "version:major": "node scripts/version.js major",
@@ -76,5 +108,14 @@
76
108
  "example:build": "pnpm build --filter personal-utils-example",
77
109
  "example:preview": "pnpm preview --filter personal-utils-example",
78
110
  "example:setup": "pnpm install && pnpm build && pnpm example:dev"
79
- }
80
- }
111
+ },
112
+ "devDependencies": {
113
+ "typescript": "~5.9.2",
114
+ "vite": "^7.0.6",
115
+ "vitest": "^4.1.6"
116
+ },
117
+ "dependencies": {
118
+ "axios": "^1.11.0"
119
+ },
120
+ "peerDependenciesMeta": {}
121
+ }
@@ -1,21 +1,8 @@
1
1
  import type { AxiosInstance, AxiosRequestConfig } from 'axios';
2
- type CreateAxiosOptions = any;
3
- type RequestOptions = any;
4
- export declare enum ContentTypeEnum {
5
- JSON = "application/json;charset=UTF-8",
6
- TEXT = "text/plain;charset=UTF-8",
7
- FORM_URLENCODED = "application/x-www-form-urlencoded;charset=UTF-8",
8
- FORM_DATA = "multipart/form-data;charset=UTF-8"
9
- }
10
- export declare enum RequestEnum {
11
- GET = "GET",
12
- POST = "POST",
13
- PUT = "PUT",
14
- DELETE = "DELETE"
15
- }
2
+ import { type CreateAxiosOptions, type RequestOptions } from './types';
16
3
  export declare class Axios {
17
4
  private axiosInstance;
18
- private readonly options;
5
+ private options;
19
6
  constructor(options: CreateAxiosOptions);
20
7
  /**
21
8
  * @description create axios instance
@@ -40,12 +27,17 @@ export declare class Axios {
40
27
  supportFormData(config: AxiosRequestConfig): AxiosRequestConfig<any>;
41
28
  request<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T>;
42
29
  get<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T>;
30
+ get<T = any>(url: string, config?: AxiosRequestConfig, options?: RequestOptions): Promise<T>;
43
31
  post<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T>;
32
+ post<T = any>(url: string, data?: any, config?: AxiosRequestConfig, options?: RequestOptions): Promise<T>;
44
33
  put<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T>;
34
+ put<T = any>(url: string, data?: any, config?: AxiosRequestConfig, options?: RequestOptions): Promise<T>;
45
35
  delete<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T>;
36
+ delete<T = any>(url: string, config?: AxiosRequestConfig, options?: RequestOptions): Promise<T>;
37
+ patch<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T>;
38
+ patch<T = any>(url: string, data?: any, config?: AxiosRequestConfig, options?: RequestOptions): Promise<T>;
46
39
  /**
47
40
  * @description Interceptor configuration
48
41
  */
49
42
  private setupInterceptors;
50
43
  }
51
- export {};