UNPKG

@lark-project/cli

Version:

飞书项目插件开发工具

72 lines (71 loc) 4.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fillWebhookTokens = void 0; const TOKEN_SLOTS = [ { bucket: 'aiNode', single: true, requiresUrl: false, getUrl: p => p === null || p === void 0 ? void 0 : p.url, getToken: p => p === null || p === void 0 ? void 0 : p.token, setToken: (p, t) => { p.token = t; } }, { bucket: 'aiField', single: true, requiresUrl: false, getUrl: p => p === null || p === void 0 ? void 0 : p.url, getToken: p => p === null || p === void 0 ? void 0 : p.token, setToken: (p, t) => { p.token = t; } }, { bucket: 'aiOperation', single: true, requiresUrl: false, getUrl: p => p === null || p === void 0 ? void 0 : p.url, getToken: p => p === null || p === void 0 ? void 0 : p.token, setToken: (p, t) => { p.token = t; } }, { bucket: 'intercept', requiresUrl: true, getUrl: p => p === null || p === void 0 ? void 0 : p.url, getToken: p => p === null || p === void 0 ? void 0 : p.token, setToken: (p, t) => { p.token = t; } }, { bucket: 'listen_event', requiresUrl: true, fallbackKey: 'listen_event', getUrl: p => p === null || p === void 0 ? void 0 : p.url, getToken: p => p === null || p === void 0 ? void 0 : p.token, setToken: (p, t) => { p.token = t; } }, { bucket: 'control', requiresUrl: true, getUrl: p => p === null || p === void 0 ? void 0 : p.url, getToken: p => p === null || p === void 0 ? void 0 : p.token, setToken: (p, t) => { p.token = t; } }, { bucket: 'control', slotId: 'table_url', requiresUrl: true, getUrl: p => { var _a, _b, _c; return (_c = (_b = (_a = p === null || p === void 0 ? void 0 : p.platform) === null || _a === void 0 ? void 0 : _a.web) === null || _b === void 0 ? void 0 : _b.table_url) === null || _c === void 0 ? void 0 : _c.url; }, getToken: p => { var _a, _b, _c; return (_c = (_b = (_a = p === null || p === void 0 ? void 0 : p.platform) === null || _a === void 0 ? void 0 : _a.web) === null || _b === void 0 ? void 0 : _b.table_url) === null || _c === void 0 ? void 0 : _c.token; }, setToken: (p, t) => { p.platform.web.table_url.token = t; }, }, { bucket: 'customField', slotId: 'table_data', requiresUrl: true, getUrl: p => { var _a, _b; return (_b = (_a = p === null || p === void 0 ? void 0 : p.platform) === null || _a === void 0 ? void 0 : _a.web) === null || _b === void 0 ? void 0 : _b.table_data_url; }, getToken: p => { var _a, _b; return (_b = (_a = p === null || p === void 0 ? void 0 : p.platform) === null || _a === void 0 ? void 0 : _a.web) === null || _b === void 0 ? void 0 : _b.table_data_token; }, setToken: (p, t) => { p.platform.web.table_data_token = t; }, }, ]; const str = (v) => (typeof v === 'string' ? v : ''); function fillWebhookTokens(localConfig, remoteConfig, // 不传则只从远端补、两端皆空时原样不动(diff 预演用,只读不生成);push 路径传 randomUUID 才会生成。 generate) { const filledFromRemote = []; const generated = []; for (const slot of TOKEN_SLOTS) { // 本地 / 远端取该 bucket 的点位列表(单对象归一成单元素数组),按身份配对。 const toList = (cfg) => { const raw = cfg === null || cfg === void 0 ? void 0 : cfg[slot.bucket]; if (!raw) return []; return slot.single ? [raw] : Array.isArray(raw) ? raw : []; }; // 配对身份:点位 key,缺失时退回 slot.fallbackKey(仅 listen_event 无 key)。两者皆无则跳过。 const idOf = (p) => (typeof (p === null || p === void 0 ? void 0 : p.key) === 'string' ? p.key : slot.fallbackKey); const remoteById = new Map(); for (const p of toList(remoteConfig)) { const id = idOf(p); if (typeof id === 'string') remoteById.set(id, p); } for (const point of toList(localConfig)) { if (!point) continue; const id = idOf(point); if (typeof id !== 'string') continue; if (str(slot.getToken(point))) continue; // 本地已有 → 不覆盖(可轮换) if (slot.requiresUrl && !str(slot.getUrl(point))) continue; // 没配端点 → 不填、不生成 const label = slot.slotId ? `${slot.bucket}[${id}].${slot.slotId}` : `${slot.bucket}[${id}]`; const remoteToken = str(slot.getToken(remoteById.get(id))); if (remoteToken) { slot.setToken(point, remoteToken); filledFromRemote.push(label); } else if (generate) { slot.setToken(point, generate()); generated.push(label); } // else(diff 预演:无 generate + 远端也无)→ 原样不动,不生成 } } return { filledFromRemote, generated }; } exports.fillWebhookTokens = fillWebhookTokens;