@lark-apaas/client-toolkit 1.2.51-alpha.5 → 1.2.51-alpha.6
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/lib/apis/components/UniversalLink.d.ts +0 -3
- package/lib/apis/components/UniversalLink.js +3 -42
- package/lib/integrations/__test__/dataloom.test.d.ts +1 -0
- package/lib/integrations/__test__/dataloom.test.js +83 -0
- package/lib/integrations/dataloom.d.ts +1 -1
- package/lib/integrations/dataloom.js +5 -11
- package/lib/types/iframe-events.d.ts +1 -12
- package/lib/utils/postMessage.js +4 -5
- package/package.json +2 -2
|
@@ -8,8 +8,5 @@ export interface UniversalLinkProps extends Omit<React.AnchorHTMLAttributes<HTML
|
|
|
8
8
|
* - 内部路由(/dashboard)→ react-router Link
|
|
9
9
|
* - Hash 锚点(#section)→ <a>
|
|
10
10
|
* - 外链(https://...)→ <a target="_blank">
|
|
11
|
-
*
|
|
12
|
-
* 预览态(iframe 内)时,通过 postMessage 通知父页面处理链接跳转,
|
|
13
|
-
* 避免浏览器安全策略限制导致链接无法正常打开。
|
|
14
11
|
*/
|
|
15
12
|
export declare const UniversalLink: React.ForwardRefExoticComponent<UniversalLinkProps & React.RefAttributes<HTMLAnchorElement>>;
|
|
@@ -1,62 +1,23 @@
|
|
|
1
1
|
import { jsx } from "react/jsx-runtime";
|
|
2
2
|
import react from "react";
|
|
3
3
|
import { Link } from "react-router-dom";
|
|
4
|
-
import { isPreview } from "../../utils/utils.js";
|
|
5
|
-
import { submitPostMessage } from "../../utils/postMessage.js";
|
|
6
4
|
function isInternalRoute(to) {
|
|
7
5
|
return !to.startsWith('#') && !to.startsWith('http://') && !to.startsWith('https://') && !to.startsWith('//');
|
|
8
6
|
}
|
|
9
7
|
function isExternalLink(to) {
|
|
10
8
|
return to.startsWith('http://') || to.startsWith('https://') || to.startsWith('//');
|
|
11
9
|
}
|
|
12
|
-
const
|
|
13
|
-
function isIframeUnderMiaoda() {
|
|
14
|
-
if ('undefined' == typeof window) return false;
|
|
15
|
-
if (window.parent === window) return false;
|
|
16
|
-
try {
|
|
17
|
-
if (!document.referrer) return false;
|
|
18
|
-
const refOrigin = new URL(document.referrer).origin;
|
|
19
|
-
return MIAODA_PARENT_ORIGIN_RE.test(refOrigin);
|
|
20
|
-
} catch {
|
|
21
|
-
return false;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
const UniversalLink_UniversalLink = /*#__PURE__*/ react.forwardRef(function({ to, onClick, ...props }, ref) {
|
|
25
|
-
const isExternal = isExternalLink(to);
|
|
26
|
-
const preview = (isPreview() || isIframeUnderMiaoda()) && isExternal;
|
|
27
|
-
if (preview) {
|
|
28
|
-
const handlePreviewClick = (e)=>{
|
|
29
|
-
e.preventDefault();
|
|
30
|
-
onClick?.(e);
|
|
31
|
-
submitPostMessage({
|
|
32
|
-
type: 'OpenIframeLink',
|
|
33
|
-
data: {
|
|
34
|
-
href: to,
|
|
35
|
-
external: true
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
};
|
|
39
|
-
return /*#__PURE__*/ jsx("a", {
|
|
40
|
-
href: to,
|
|
41
|
-
ref: ref,
|
|
42
|
-
...props,
|
|
43
|
-
onClick: handlePreviewClick,
|
|
44
|
-
target: props.target ?? '_blank',
|
|
45
|
-
rel: props.rel ?? 'noopener noreferrer'
|
|
46
|
-
});
|
|
47
|
-
}
|
|
10
|
+
const UniversalLink_UniversalLink = /*#__PURE__*/ react.forwardRef(function({ to, ...props }, ref) {
|
|
48
11
|
if (isInternalRoute(to)) return /*#__PURE__*/ jsx(Link, {
|
|
49
12
|
to: to,
|
|
50
13
|
ref: ref,
|
|
51
|
-
...props
|
|
52
|
-
onClick: onClick
|
|
14
|
+
...props
|
|
53
15
|
});
|
|
54
16
|
return /*#__PURE__*/ jsx("a", {
|
|
55
17
|
href: to,
|
|
56
18
|
ref: ref,
|
|
57
19
|
...props,
|
|
58
|
-
|
|
59
|
-
...isExternal && {
|
|
20
|
+
...isExternalLink(to) && {
|
|
60
21
|
target: props.target ?? '_blank',
|
|
61
22
|
rel: props.rel ?? 'noopener noreferrer'
|
|
62
23
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { getDataloom } from "../dataloom.js";
|
|
3
|
+
const { getInitialInfoMock, getAppPublishedMock, getAppIdMock, createClientMock, authUserMock, authSessionMock } = vi.hoisted(()=>({
|
|
4
|
+
getInitialInfoMock: vi.fn(),
|
|
5
|
+
getAppPublishedMock: vi.fn(),
|
|
6
|
+
getAppIdMock: vi.fn(),
|
|
7
|
+
createClientMock: vi.fn(),
|
|
8
|
+
authUserMock: {},
|
|
9
|
+
authSessionMock: {}
|
|
10
|
+
}));
|
|
11
|
+
vi.mock('../../utils/getInitialInfo', ()=>({
|
|
12
|
+
getInitialInfo: getInitialInfoMock,
|
|
13
|
+
getAppPublished: getAppPublishedMock
|
|
14
|
+
}));
|
|
15
|
+
vi.mock('../../utils/getAppId', ()=>({
|
|
16
|
+
getAppId: getAppIdMock
|
|
17
|
+
}));
|
|
18
|
+
vi.mock('@lark-apaas/dataloom', ()=>({
|
|
19
|
+
createClient: createClientMock
|
|
20
|
+
}));
|
|
21
|
+
vi.mock('@lark-apaas/auth-sdk', ()=>({
|
|
22
|
+
authClient: {
|
|
23
|
+
user: authUserMock,
|
|
24
|
+
session: authSessionMock
|
|
25
|
+
}
|
|
26
|
+
}));
|
|
27
|
+
beforeEach(()=>{
|
|
28
|
+
getInitialInfoMock.mockReset();
|
|
29
|
+
getAppPublishedMock.mockReset();
|
|
30
|
+
getAppIdMock.mockReset();
|
|
31
|
+
createClientMock.mockReset();
|
|
32
|
+
});
|
|
33
|
+
afterEach(()=>{
|
|
34
|
+
vi.restoreAllMocks();
|
|
35
|
+
});
|
|
36
|
+
describe('getDataloom', ()=>{
|
|
37
|
+
it('返回-createClient实例-改用getInitialInfo-新签名不取token/url-appId注入-并发去重-缓存复用', async ()=>{
|
|
38
|
+
getAppIdMock.mockReturnValue('app-1');
|
|
39
|
+
getInitialInfoMock.mockResolvedValue({
|
|
40
|
+
app_runtime_extra: {
|
|
41
|
+
token: 'pat',
|
|
42
|
+
url: 'https://x'
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
const client = {
|
|
46
|
+
__id: 'client-1'
|
|
47
|
+
};
|
|
48
|
+
createClientMock.mockReturnValue(client);
|
|
49
|
+
const p1 = getDataloom();
|
|
50
|
+
const p2 = getDataloom();
|
|
51
|
+
expect(p1).toBe(p2);
|
|
52
|
+
const [c1, c2] = await Promise.all([
|
|
53
|
+
p1,
|
|
54
|
+
p2
|
|
55
|
+
]);
|
|
56
|
+
expect(c1).toBe(client);
|
|
57
|
+
expect(c2).toBe(client);
|
|
58
|
+
expect(getInitialInfoMock).toHaveBeenCalledTimes(1);
|
|
59
|
+
expect(getAppPublishedMock).not.toHaveBeenCalled();
|
|
60
|
+
expect(createClientMock).toHaveBeenCalledTimes(1);
|
|
61
|
+
const args = createClientMock.mock.calls[0];
|
|
62
|
+
expect(args).toHaveLength(1);
|
|
63
|
+
const options = args[0];
|
|
64
|
+
expect(typeof options).toBe('object');
|
|
65
|
+
expect(options).not.toBe('https://x');
|
|
66
|
+
expect(options).not.toBe('pat');
|
|
67
|
+
expect(options.global).toBeDefined();
|
|
68
|
+
expect(options.global).not.toHaveProperty('url');
|
|
69
|
+
expect(options.global).not.toHaveProperty('key');
|
|
70
|
+
expect(options.global?.appId).toBe('app-1');
|
|
71
|
+
expect(options.global?.accountServices).toEqual({
|
|
72
|
+
user: authUserMock,
|
|
73
|
+
session: authSessionMock
|
|
74
|
+
});
|
|
75
|
+
const cached1 = await getDataloom();
|
|
76
|
+
const cached2 = await getDataloom();
|
|
77
|
+
expect(cached1).toBe(client);
|
|
78
|
+
expect(cached2).toBe(client);
|
|
79
|
+
expect(cached1).toBe(cached2);
|
|
80
|
+
expect(getInitialInfoMock).toHaveBeenCalledTimes(1);
|
|
81
|
+
expect(createClientMock).toHaveBeenCalledTimes(1);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
declare const createDataLoomClient: (
|
|
1
|
+
declare const createDataLoomClient: () => import("@lark-apaas/dataloom").DataloomClient;
|
|
2
2
|
/** 获取dataloom实例 */
|
|
3
3
|
export declare function getDataloom(): Promise<ReturnType<typeof createDataLoomClient>>;
|
|
4
4
|
export {};
|
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
import { splitWorkspaceUrl } from "../utils/url.js";
|
|
2
1
|
import { createClient } from "@lark-apaas/dataloom";
|
|
3
2
|
import { authClient } from "@lark-apaas/auth-sdk";
|
|
4
3
|
import { getAppId } from "../utils/getAppId.js";
|
|
5
|
-
import {
|
|
6
|
-
const createDataLoomClient = (
|
|
7
|
-
const { baseUrl } = url ? splitWorkspaceUrl(url) : {
|
|
8
|
-
baseUrl: ''
|
|
9
|
-
};
|
|
4
|
+
import { getInitialInfo } from "../utils/getInitialInfo.js";
|
|
5
|
+
const createDataLoomClient = ()=>{
|
|
10
6
|
const appId = getAppId();
|
|
11
|
-
return createClient(
|
|
7
|
+
return createClient({
|
|
12
8
|
global: {
|
|
13
9
|
enableDataloomLog: 'production' !== process.env.NODE_ENV,
|
|
14
10
|
requestRateLimit: 'production' !== process.env.NODE_ENV ? 100 : void 0,
|
|
@@ -26,10 +22,8 @@ let pendingPromise = null;
|
|
|
26
22
|
function getDataloom() {
|
|
27
23
|
if (dataloom) return Promise.resolve(dataloom);
|
|
28
24
|
if (pendingPromise) return pendingPromise;
|
|
29
|
-
pendingPromise =
|
|
30
|
-
|
|
31
|
-
const DATALOOM_PAT = info?.app_runtime_extra?.token;
|
|
32
|
-
dataloom = createDataLoomClient(DATALOOM_CLIENT_URL, DATALOOM_PAT);
|
|
25
|
+
pendingPromise = getInitialInfo().then(()=>{
|
|
26
|
+
dataloom = createDataLoomClient();
|
|
33
27
|
return dataloom;
|
|
34
28
|
}).finally(()=>{
|
|
35
29
|
pendingPromise = null;
|
|
@@ -46,18 +46,7 @@ export interface DevServerMessage extends IframeMessage<{
|
|
|
46
46
|
}> {
|
|
47
47
|
type: 'DevServerMessage';
|
|
48
48
|
}
|
|
49
|
-
|
|
50
|
-
* 预览态下链接点击通知父页面处理。
|
|
51
|
-
* iframe 内直接跳转可能被浏览器安全策略拦截,
|
|
52
|
-
* 通过 postMessage 让父页面在新标签页或外壳中打开链接。
|
|
53
|
-
*/
|
|
54
|
-
export interface OpenIframeLinkMessage extends IframeMessage<{
|
|
55
|
-
href: string;
|
|
56
|
-
external: boolean;
|
|
57
|
-
}> {
|
|
58
|
-
type: 'OpenIframeLink';
|
|
59
|
-
}
|
|
60
|
-
export type OutgoingMessage = PreviewReadyMessage | HmrMessage | ConsoleMessage | ChildLocationChangeMessage | CreatePageMessage | RenderErrorMessage | RenderErrorRepairMessage | PageScreenshotMessage | DevServerMessage | UpdateRoutesMessage | OpenIframeLinkMessage;
|
|
49
|
+
export type OutgoingMessage = PreviewReadyMessage | HmrMessage | ConsoleMessage | ChildLocationChangeMessage | CreatePageMessage | RenderErrorMessage | RenderErrorRepairMessage | PageScreenshotMessage | DevServerMessage | UpdateRoutesMessage;
|
|
61
50
|
export interface GetRoutesMessage extends IframeMessage<Record<string, never>> {
|
|
62
51
|
type: 'GetRoutes';
|
|
63
52
|
}
|
package/lib/utils/postMessage.js
CHANGED
|
@@ -23,23 +23,22 @@ function getLegacyParentOrigin() {
|
|
|
23
23
|
if (origin.includes('fsapp.kundou.cn') || origin.includes('miaoda-pre.feishuapp.net')) return 'https://miaoda.feishu-pre.cn';
|
|
24
24
|
return 'https://miaoda.feishu-boe.cn';
|
|
25
25
|
}
|
|
26
|
-
const MIAODA_PARENT_ORIGIN_RE = /^https:\/\/(miaoda|force)\.feishu(-pre|-boe|-staging)?\.cn$/;
|
|
27
26
|
function resolveParentOrigin() {
|
|
28
|
-
const paramOrigin = getParentOriginFromParams();
|
|
29
|
-
if (paramOrigin) return paramOrigin;
|
|
30
27
|
try {
|
|
31
28
|
if (document.referrer) {
|
|
32
29
|
const referrerOrigin = new URL(document.referrer).origin;
|
|
33
|
-
if (
|
|
30
|
+
if (referrerOrigin.startsWith('http://localhost') || referrerOrigin.startsWith('http://127.0.0.1')) return referrerOrigin;
|
|
34
31
|
}
|
|
35
32
|
} catch {}
|
|
33
|
+
const paramOrigin = getParentOriginFromParams();
|
|
34
|
+
if (paramOrigin) return paramOrigin;
|
|
36
35
|
return process.env?.FORCE_FRAMEWORK_DOMAIN_MAIN ?? getLegacyParentOrigin();
|
|
37
36
|
}
|
|
38
37
|
function submitPostMessage(message, targetOrigin) {
|
|
39
38
|
try {
|
|
40
39
|
const parentOrigin = resolveParentOrigin();
|
|
41
40
|
const origin = targetOrigin ?? parentOrigin;
|
|
42
|
-
if (!origin) return
|
|
41
|
+
if (!origin) return;
|
|
43
42
|
window.parent.postMessage(message, origin);
|
|
44
43
|
} catch (e) {
|
|
45
44
|
console.error('postMessage error', e);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lark-apaas/client-toolkit",
|
|
3
|
-
"version": "1.2.51-alpha.
|
|
3
|
+
"version": "1.2.51-alpha.6",
|
|
4
4
|
"types": "./lib/index.d.ts",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"files": [
|
|
@@ -101,7 +101,7 @@
|
|
|
101
101
|
"@lark-apaas/aily-web-sdk": "^0.0.11",
|
|
102
102
|
"@lark-apaas/auth-sdk": "^0.1.5",
|
|
103
103
|
"@lark-apaas/client-capability": "^0.1.7",
|
|
104
|
-
"@lark-apaas/dataloom": "
|
|
104
|
+
"@lark-apaas/dataloom": "0.1.4-alpha.0",
|
|
105
105
|
"@lark-apaas/internal-slardar": "^0.0.3",
|
|
106
106
|
"@lark-apaas/miaoda-inspector": "^1.0.23",
|
|
107
107
|
"@lark-apaas/observable-web": "^1.0.6",
|