@hkyhy/marsun-components-core 0.1.57 → 0.1.59
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.ts
CHANGED
|
@@ -12,7 +12,7 @@ export type { BuildLoginPathOptions } from './utils/authRedirect';
|
|
|
12
12
|
export { DEFAULT_LAST_ACTIVITY_STORAGE_KEY, getLastActivityTime, touchLastActivity, clearLastActivity, } from './utils/sessionActivity';
|
|
13
13
|
export { USER_ROLE_PERMISSIONS_KEY, PERMISSION_DEFINITIONS_KEY, PERMISSIONS_STORAGE_KEY, loadUserRolePermissions, saveUserRolePermissions, loadPermissionDefinitions, savePermissionDefinitions, getStoredUserPermissions, getStoredPermissionMap, } from './utils/permissionStorage';
|
|
14
14
|
export { createMarsunRequest } from './utils/createMarsunRequest';
|
|
15
|
-
export type { MarsunApiResponse, CreateMarsunRequestOptions, MarsunRequestClient, } from './utils/createMarsunRequest';
|
|
15
|
+
export type { MarsunApiResponse, CreateMarsunRequestOptions, MarsunRequestClient, MarsunRequestErrorResponse, } from './utils/createMarsunRequest';
|
|
16
16
|
export type { UserRolePermissions, PermissionDefinition, PermissionDefinitionsResponse, } from './types/auth';
|
|
17
17
|
export { buildDepartmentPathMapFromTree, buildDepartmentPathMapFromFlat, getDepartmentPath, buildLeafDepartmentPathMap, buildDepartmentPathMaps, mergeDepartmentPathMaps, toDepartmentPathMaps, } from './utils/department/departmentPath';
|
|
18
18
|
export type { DepartmentPathMaps } from './utils/department/departmentPath';
|
|
@@ -3,6 +3,11 @@ export interface MarsunApiResponse<T = unknown> {
|
|
|
3
3
|
message?: string;
|
|
4
4
|
data?: T;
|
|
5
5
|
}
|
|
6
|
+
export interface MarsunRequestErrorResponse {
|
|
7
|
+
data?: unknown;
|
|
8
|
+
status?: number;
|
|
9
|
+
config?: unknown;
|
|
10
|
+
}
|
|
6
11
|
export interface CreateMarsunRequestOptions {
|
|
7
12
|
baseURL?: string;
|
|
8
13
|
timeout?: number;
|
|
@@ -14,6 +19,10 @@ export interface CreateMarsunRequestOptions {
|
|
|
14
19
|
isPublicUrl?: (url: string) => boolean;
|
|
15
20
|
isOnLoginPage?: () => boolean;
|
|
16
21
|
showError?: (msg: string) => void;
|
|
22
|
+
/** 自定义错误信息提取;默认按 message/detail/msg 顺序兜底 */
|
|
23
|
+
getResponseError?: (response: MarsunRequestErrorResponse | undefined) => string;
|
|
24
|
+
/** 信封 code != successCode 时不 reject、不 showError,原样返回信封由调用方自判(用于复用既有 unwrap 逻辑的接入方) */
|
|
25
|
+
passThroughEnvelopeError?: boolean;
|
|
17
26
|
}
|
|
18
27
|
/** 与 axios 实例兼容的请求客户端(避免对外暴露 axios 类型,消除多副本 axios 的类型冲突) */
|
|
19
28
|
export interface MarsunRequestClient {
|
|
@@ -1,55 +1,61 @@
|
|
|
1
|
-
import { message as
|
|
1
|
+
import { message as y } from "antd";
|
|
2
2
|
import "../node_modules/axios/index.js";
|
|
3
|
-
import
|
|
4
|
-
const
|
|
5
|
-
function
|
|
6
|
-
return !!(
|
|
3
|
+
import h from "../node_modules/axios/lib/axios.js";
|
|
4
|
+
const v = "X-Skip-Error-Handler";
|
|
5
|
+
function m(o) {
|
|
6
|
+
return !!(o != null && o[v]);
|
|
7
7
|
}
|
|
8
|
-
function
|
|
8
|
+
function q(o = {}) {
|
|
9
9
|
const {
|
|
10
|
-
baseURL:
|
|
11
|
-
timeout:
|
|
12
|
-
successCode:
|
|
13
|
-
withCredentials:
|
|
14
|
-
getToken:
|
|
15
|
-
onUnauthorized:
|
|
16
|
-
isPublicUrl:
|
|
17
|
-
isOnLoginPage:
|
|
18
|
-
showError:
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
10
|
+
baseURL: E = "/api",
|
|
11
|
+
timeout: g = 3e4,
|
|
12
|
+
successCode: R = 0,
|
|
13
|
+
withCredentials: b = !1,
|
|
14
|
+
getToken: i,
|
|
15
|
+
onUnauthorized: c,
|
|
16
|
+
isPublicUrl: d = () => !1,
|
|
17
|
+
isOnLoginPage: P = () => !1,
|
|
18
|
+
showError: f = (e) => y.error(e),
|
|
19
|
+
getResponseError: l,
|
|
20
|
+
passThroughEnvelopeError: j = !1
|
|
21
|
+
} = o, p = (e) => {
|
|
22
|
+
if (l) return l(e);
|
|
23
|
+
const r = e == null ? void 0 : e.data;
|
|
24
|
+
return (r == null ? void 0 : r.message) || (r == null ? void 0 : r.detail) || (r == null ? void 0 : r.msg) || "请求失败";
|
|
25
|
+
}, a = h.create({
|
|
26
|
+
baseURL: E,
|
|
27
|
+
timeout: g,
|
|
28
|
+
withCredentials: b,
|
|
23
29
|
headers: {
|
|
24
30
|
"Content-Type": "application/json"
|
|
25
31
|
}
|
|
26
32
|
});
|
|
27
33
|
return a.interceptors.request.use(
|
|
28
34
|
(e) => {
|
|
29
|
-
const
|
|
30
|
-
return
|
|
35
|
+
const r = i == null ? void 0 : i();
|
|
36
|
+
return r && (e.headers.Authorization = `Bearer ${r}`), e;
|
|
31
37
|
},
|
|
32
38
|
(e) => Promise.reject(e)
|
|
33
39
|
), a.interceptors.response.use(
|
|
34
40
|
(e) => {
|
|
35
|
-
|
|
36
|
-
|
|
41
|
+
var n;
|
|
42
|
+
const r = (n = e.config) == null ? void 0 : n.responseType;
|
|
43
|
+
if (r === "blob" || r === "arraybuffer")
|
|
44
|
+
return e.data;
|
|
45
|
+
const t = e.data;
|
|
46
|
+
if (!(t && typeof t == "object" && "code" in t))
|
|
47
|
+
return t;
|
|
48
|
+
const s = t;
|
|
49
|
+
return s.code !== R ? j ? t : (m(e.config.headers) || f(s.message || "请求失败"), Promise.reject(new Error(s.message || "请求失败"))) : t;
|
|
37
50
|
},
|
|
38
51
|
(e) => {
|
|
39
|
-
var
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
!j(s) && !w() && (n == null || n());
|
|
43
|
-
else if (!H) {
|
|
44
|
-
const q = ((f = (m = e.response) == null ? void 0 : m.data) == null ? void 0 : f.message) || e.message || "请求失败";
|
|
45
|
-
o(q);
|
|
46
|
-
}
|
|
47
|
-
const k = ((l = (p = e.response) == null ? void 0 : p.data) == null ? void 0 : l.message) || e.message || "请求失败";
|
|
48
|
-
return Promise.reject(new Error(k));
|
|
52
|
+
var u, s, n;
|
|
53
|
+
const r = ((u = e.config) == null ? void 0 : u.url) ?? "", t = m((s = e.config) == null ? void 0 : s.headers);
|
|
54
|
+
return ((n = e.response) == null ? void 0 : n.status) === 401 ? !d(r) && !P() && (c == null || c()) : t || f(p(e.response)), Promise.reject(new Error(p(e.response)));
|
|
49
55
|
}
|
|
50
56
|
), a;
|
|
51
57
|
}
|
|
52
58
|
export {
|
|
53
|
-
|
|
59
|
+
q as createMarsunRequest
|
|
54
60
|
};
|
|
55
61
|
//# sourceMappingURL=createMarsunRequest.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createMarsunRequest.js","sources":["../../src/utils/createMarsunRequest.ts"],"sourcesContent":["import { message } from 'antd';\nimport axios from 'axios';\n\nexport interface MarsunApiResponse<T = unknown> {\n code: number;\n message?: string;\n data?: T;\n}\n\nexport interface CreateMarsunRequestOptions {\n baseURL?: string;\n timeout?: number;\n successCode?: number;\n /** Cookie 会话 SSO:跨域携带凭证 */\n withCredentials?: boolean;\n getToken?: () => string | null;\n onUnauthorized?: () => void;\n isPublicUrl?: (url: string) => boolean;\n isOnLoginPage?: () => boolean;\n showError?: (msg: string) => void;\n}\n\n/** 与 axios 实例兼容的请求客户端(避免对外暴露 axios 类型,消除多副本 axios 的类型冲突) */\nexport interface MarsunRequestClient {\n get<T = unknown>(url: string, config?: unknown): Promise<T>;\n post<T = unknown>(url: string, data?: unknown, config?: unknown): Promise<T>;\n put<T = unknown>(url: string, data?: unknown, config?: unknown): Promise<T>;\n patch<T = unknown>(url: string, data?: unknown, config?: unknown): Promise<T>;\n delete<T = unknown>(url: string, config?: unknown): Promise<T>;\n request<T = unknown>(config: unknown): Promise<T>;\n}\n\nconst SKIP_ERROR_HEADER = 'X-Skip-Error-Handler';\n\nfunction getSkipHandler(headers: unknown): boolean {\n return Boolean((headers as Record<string, string> | undefined)?.[SKIP_ERROR_HEADER]);\n}\n\n/** 创建带鉴权、统一错误处理的 axios 实例 */\nexport function createMarsunRequest(options: CreateMarsunRequestOptions = {}): MarsunRequestClient {\n const {\n baseURL = '/api',\n timeout = 30000,\n successCode = 0,\n withCredentials = false,\n getToken,\n onUnauthorized,\n isPublicUrl = () => false,\n isOnLoginPage = () => false,\n showError = (msg) => message.error(msg),\n } = options;\n\n const request = axios.create({\n baseURL,\n timeout,\n withCredentials,\n headers: {\n 'Content-Type': 'application/json',\n },\n });\n\n request.interceptors.request.use(\n (config) => {\n const token = getToken?.();\n if (token) {\n config.headers.Authorization = `Bearer ${token}`;\n }\n return config;\n },\n (error) => Promise.reject(error),\n );\n\n request.interceptors.response.use(\n (response) => {\n const data = response.data as MarsunApiResponse;\n if (
|
|
1
|
+
{"version":3,"file":"createMarsunRequest.js","sources":["../../src/utils/createMarsunRequest.ts"],"sourcesContent":["import { message } from 'antd';\nimport axios from 'axios';\n\nexport interface MarsunApiResponse<T = unknown> {\n code: number;\n message?: string;\n data?: T;\n}\n\nexport interface MarsunRequestErrorResponse {\n data?: unknown;\n status?: number;\n config?: unknown;\n}\n\nexport interface CreateMarsunRequestOptions {\n baseURL?: string;\n timeout?: number;\n successCode?: number;\n /** Cookie 会话 SSO:跨域携带凭证 */\n withCredentials?: boolean;\n getToken?: () => string | null;\n onUnauthorized?: () => void;\n isPublicUrl?: (url: string) => boolean;\n isOnLoginPage?: () => boolean;\n showError?: (msg: string) => void;\n /** 自定义错误信息提取;默认按 message/detail/msg 顺序兜底 */\n getResponseError?: (response: MarsunRequestErrorResponse | undefined) => string;\n /** 信封 code != successCode 时不 reject、不 showError,原样返回信封由调用方自判(用于复用既有 unwrap 逻辑的接入方) */\n passThroughEnvelopeError?: boolean;\n}\n\n/** 与 axios 实例兼容的请求客户端(避免对外暴露 axios 类型,消除多副本 axios 的类型冲突) */\nexport interface MarsunRequestClient {\n get<T = unknown>(url: string, config?: unknown): Promise<T>;\n post<T = unknown>(url: string, data?: unknown, config?: unknown): Promise<T>;\n put<T = unknown>(url: string, data?: unknown, config?: unknown): Promise<T>;\n patch<T = unknown>(url: string, data?: unknown, config?: unknown): Promise<T>;\n delete<T = unknown>(url: string, config?: unknown): Promise<T>;\n request<T = unknown>(config: unknown): Promise<T>;\n}\n\nconst SKIP_ERROR_HEADER = 'X-Skip-Error-Handler';\n\nfunction getSkipHandler(headers: unknown): boolean {\n return Boolean((headers as Record<string, string> | undefined)?.[SKIP_ERROR_HEADER]);\n}\n\n/** 创建带鉴权、统一错误处理的 axios 实例 */\nexport function createMarsunRequest(options: CreateMarsunRequestOptions = {}): MarsunRequestClient {\n const {\n baseURL = '/api',\n timeout = 30000,\n successCode = 0,\n withCredentials = false,\n getToken,\n onUnauthorized,\n isPublicUrl = () => false,\n isOnLoginPage = () => false,\n showError = (msg) => message.error(msg),\n getResponseError,\n passThroughEnvelopeError = false,\n } = options;\n\n const extractError = (response: MarsunRequestErrorResponse | undefined): string => {\n if (getResponseError) return getResponseError(response);\n const data = response?.data as { message?: string; detail?: string; msg?: string } | undefined;\n return data?.message || data?.detail || data?.msg || '请求失败';\n };\n\n const request = axios.create({\n baseURL,\n timeout,\n withCredentials,\n headers: {\n 'Content-Type': 'application/json',\n },\n });\n\n request.interceptors.request.use(\n (config) => {\n const token = getToken?.();\n if (token) {\n config.headers.Authorization = `Bearer ${token}`;\n }\n return config;\n },\n (error) => Promise.reject(error),\n );\n\n request.interceptors.response.use(\n (response) => {\n const responseType = (response.config as { responseType?: string } | undefined)?.responseType;\n if (responseType === 'blob' || responseType === 'arraybuffer') {\n return response.data;\n }\n const data = response.data;\n const isEnvelope = data && typeof data === 'object' && 'code' in data;\n if (!isEnvelope) {\n return data;\n }\n const envelope = data as MarsunApiResponse;\n if (envelope.code !== successCode) {\n if (passThroughEnvelopeError) {\n return data;\n }\n if (!getSkipHandler(response.config.headers)) {\n showError(envelope.message || '请求失败');\n }\n return Promise.reject(new Error(envelope.message || '请求失败'));\n }\n return data;\n },\n (error) => {\n const url = error.config?.url ?? '';\n const skipHandler = getSkipHandler(error.config?.headers);\n\n if (error.response?.status === 401) {\n if (!isPublicUrl(url) && !isOnLoginPage()) {\n onUnauthorized?.();\n }\n } else if (!skipHandler) {\n showError(extractError(error.response));\n }\n return Promise.reject(new Error(extractError(error.response)));\n },\n );\n\n return request as MarsunRequestClient;\n}\n"],"names":["SKIP_ERROR_HEADER","getSkipHandler","headers","createMarsunRequest","options","baseURL","timeout","successCode","withCredentials","getToken","onUnauthorized","isPublicUrl","isOnLoginPage","showError","msg","message","getResponseError","passThroughEnvelopeError","extractError","response","data","request","axios","config","token","error","responseType","_a","envelope","url","skipHandler","_b","_c"],"mappings":";;;AA0CA,MAAMA,IAAoB;AAE1B,SAASC,EAAeC,GAA2B;AACjD,SAAO,GAASA,KAAA,QAAAA,EAAiDF;AACnE;AAGO,SAASG,EAAoBC,IAAsC,IAAyB;AACjG,QAAM;AAAA,IACJ,SAAAC,IAAU;AAAA,IACV,SAAAC,IAAU;AAAA,IACV,aAAAC,IAAc;AAAA,IACd,iBAAAC,IAAkB;AAAA,IAClB,UAAAC;AAAA,IACA,gBAAAC;AAAA,IACA,aAAAC,IAAc,MAAM;AAAA,IACpB,eAAAC,IAAgB,MAAM;AAAA,IACtB,WAAAC,IAAY,CAACC,MAAQC,EAAQ,MAAMD,CAAG;AAAA,IACtC,kBAAAE;AAAA,IACA,0BAAAC,IAA2B;AAAA,EAAA,IACzBb,GAEEc,IAAe,CAACC,MAA6D;AACjF,QAAIH,EAAkB,QAAOA,EAAiBG,CAAQ;AACtD,UAAMC,IAAOD,KAAA,gBAAAA,EAAU;AACvB,YAAOC,KAAA,gBAAAA,EAAM,aAAWA,KAAA,gBAAAA,EAAM,YAAUA,KAAA,gBAAAA,EAAM,QAAO;AAAA,EACvD,GAEMC,IAAUC,EAAM,OA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
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hkyhy/marsun-components-core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.59",
|
|
4
4
|
"description": "Marsun pure UI component library",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -117,6 +117,7 @@
|
|
|
117
117
|
"@vitejs/plugin-react": "^4.3.4",
|
|
118
118
|
"antd": "^6.0.0",
|
|
119
119
|
"axios": "^1.7.7",
|
|
120
|
+
"axios-mock-adapter": "^2.1.0",
|
|
120
121
|
"dayjs": "^1.11.13",
|
|
121
122
|
"eslint": "^9.39.4",
|
|
122
123
|
"eslint-config-prettier": "^9.1.2",
|