@yxlib/core
v1.0.4
Published
印象核心库,提供组件注册、域名鉴权等核心功能
Maintainers
Readme
YX-Core
印象核心库,提供组件注册、域名鉴权等核心功能。
特性
- 🎯 组件注册系统 - 发布-订阅模式的组件注册机制
- 🔐 域名鉴权 - 域名白名单验证,支持多区域配置
- 🚀 自动共享 - 多个插件自动共享鉴权状态
- 💾 智能缓存 - 7天缓存 + 5分钟后台刷新
- 🌍 多区域支持 - 支持国内(cn)和国外(foreign)区域
- 📦 模块化设计 - 清晰的模块划分,易于扩展
安装
npm install yx-core快速开始
插件开发者
// yx-video/src/index.js
import { RegComponents } from 'yx-core';
import YxVideo from './yx-video';
// 方式一:回调函数(适合需要区域信息的场景)
RegComponents((region) => {
customElements.define("yx-video", YxVideo);
console.log(`YxVideo 已注册,区域:${region}`);
});
// 方式二:直接注册(更简洁)
RegComponents('yx-video', YxVideo);
export const version = '1.0.0';项目使用者
// main.js
import { InitRegion } from 'yx-core';
// 导入所有插件
import 'yx-video';
import 'yx-audio';
import 'yx-player';
// 初始化(自动完成域名鉴权并注册所有组件)
try {
await InitRegion('cn');
console.log('所有插件已注册');
} catch (err) {
console.error('域名鉴权失败:', err);
}API 文档
RegComponents(nameOrCallback, component?)
注册 Web Components(插件方调用)。支持两种调用方式。
方式一:回调函数
RegComponents(callback)参数:
callback(Function): 组件注册回调函数,接收region参数
示例:
import { RegComponents } from 'yx-core';
import YxVideo from './yx-video';
RegComponents((region) => {
customElements.define("yx-video", YxVideo);
console.log(`YxVideo 已注册,区域:${region}`);
});方式二:直接注册组件
RegComponents(name, Component)参数:
name(String): 组件名称Component(Function): Web Component 类
示例:
import { RegComponents } from 'yx-core';
import YxVideo from './yx-video';
// 直接注册组件
RegComponents('yx-video', YxVideo);InitRegion(region?)
初始化区域(项目方调用)。验证域名后自动执行所有已注册的插件。
参数:
region(String, 可选): 区域,'cn' 或 'foreign',默认 'cn'
返回:
Promise: 鉴权完成的 Promise
示例:
import { InitRegion } from 'yx-core';
import 'yx-video';
import 'yx-audio';
try {
await InitRegion('cn');
console.log('所有插件已注册');
} catch (err) {
console.error('域名鉴权失败:', err);
}isAuthed()
检查是否已完成鉴权。
返回:
Boolean: 是否已鉴权
示例:
import { isAuthed } from 'yx-core';
if (isAuthed()) {
console.log('已完成鉴权');
} else {
console.log('未鉴权');
}getRegion()
获取当前区域。
返回:
String|null: 当前区域('cn' 或 'foreign'),未初始化时返回 null
示例:
import { getRegion } from 'yx-core';
const region = getRegion();
console.log('当前区域:', region); // 'cn' 或 'foreign' 或 null项目结构
yx-core/
├── src/
│ ├── index.js # 主入口
│ ├── core/
│ │ └── component-registry.js # 组件注册核心
│ ├── modules/
│ │ └── domain-check/ # 域名检查模块
│ │ ├── index.js
│ │ ├── validator.js
│ │ └── region-map.js
│ └── utils/
│ └── cache.js # 缓存工具
└── test/
├── index.test.js
├── utils-cache.test.js
└── modules/
└── domain-check/
└── validator.test.js全局变量
鉴权成功后,会在 window 对象上设置全局变量:
window.__YX_LIVE_AUTH__ = {
siteid: 1001, // 站点 ID
hostname: 'xxx.com', // 当前域名
region: 'cn' // 区域
};访问鉴权状态(推荐使用 API):
import { isAuthed, getRegion } from 'yx-core';
// 推荐:使用 API 检查鉴权状态
if (isAuthed()) {
const region = getRegion();
console.log('已鉴权,区域:', region);
}
// 或者:直接访问全局变量
if (window.__YX_LIVE_AUTH__) {
console.log('鉴权信息:', window.__YX_LIVE_AUTH__);
}插件开发建议
设置为外部依赖
在插件的 vite.config.js 中:
export default defineConfig({
build: {
lib: {
entry: './src/index.js',
name: 'YxVideo',
fileName: 'yx-video'
},
rollupOptions: {
external: ['yx-core'], // 设置为外部依赖
output: {
globals: {
'yx-core': 'YxCore'
}
}
}
}
});在插件的 package.json 中:
{
"peerDependencies": {
"yx-core": "^2.0.0"
},
"devDependencies": {
"yx-core": "^2.0.0"
}
}不要导出组件类
为了防止用户绕过鉴权,建议不要导出组件类:
// ✅ 推荐
import { RegComponents } from 'yx-core';
import YxVideo from './yx-video';
RegComponents('yx-video', YxVideo);
export const version = '1.0.0';// ❌ 不推荐
import { RegComponents } from 'yx-core';
import YxVideo from './yx-video';
RegComponents('yx-video', YxVideo);
export { YxVideo }; // 用户可以绕过鉴权直接注册更新日志
v2.0.0 (最新)
- 🎉 重大重构 - 项目重命名为 yx-core
- 📦 模块化架构 - 清晰的模块划分
core/- 组件注册核心modules/- 功能模块(domain-check 等)utils/- 通用工具
- 🔧 职责分离 - domain-check 只负责验证,不管理全局状态
- ✅ 84 个测试用例全部通过
- 📝 完善的文档和示例
v1.0.6
- 🌟 核心重构 - 精简到只保留 4 个核心 API
- ✨ 新增
RegComponents()- 支持回调和组件两种方式 - ✨ 新增
InitRegion()- 统一初始化 - ✨ 新增
isAuthed()- 检查鉴权状态 - ✨ 新增
getRegion()- 获取当前区域
License
Commercial
