npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

ouroboros-sdk

v1.6.0-beta.6

Published

平台提供的能力包

Readme

Ouroboros SDK 用户手册

Ouroboros SDK 是为微前端应用提供的平台能力集成工具包,支持与主应用的无缝交互。

目录


快速开始

安装

npm install ouroboros-sdk

基础使用

import { initMicroApp, toast, request } from 'ouroboros-sdk'

initMicroApp({
  render: async (props) => {
    // 渲染应用
  },
  beforeMount: async (props) => {
    // 挂载前逻辑
  }
})

// 使用平台能力
toast.success('操作成功')
request.get('/api/data')

微前端初始化

initMicroApp

标准微前端应用初始化。

import { initMicroApp } from 'ouroboros-sdk'

initMicroApp({
  render: async (props) => {
    // 渲染应用逻辑
    return app
  },
  beforeMount: async (props) => {
    // 挂载前钩子
  },
  afterMount: async (props) => {
    // 挂载后钩子
  },
  afterUnmount: async (props, app) => {
    // 卸载后钩子
  },
  afterUpdate: async (props) => {
    // 更新后钩子
  },
  onBootstrap: async () => {
    // 启动钩子
  }
})

initLayoutComponent

业务组件式微前端初始化,用于布局组件。

import { initLayoutComponent } from 'ouroboros-sdk'

initLayoutComponent({
  render: async (props) => {
    const { container, appName, logo, menu, theme, userInfo } = props
    // 渲染布局组件
  },
  beforeMount: async (props) => {},
  afterUnmount: async (props, app) => {},
  afterUpdate: async (props) => {},
  onBootstrap: async () => {}
})

initAmisComponentLib

Amis 组件库微前端初始化。

import { initAmisComponentLib, FormItem, Renderer } from 'ouroboros-sdk'

initAmisComponentLib({
  beforeMount: async (props) => {
    // 注册 Amis 组件
    FormItem({ type: 'my-input' })(MyInputComponent)
    Renderer({ type: 'my-renderer' })(MyRendererComponent)
  }
})

平台能力

UI 交互

openDialog

打开对话框,返回 Promise。

参数:

  • schema (Object) - 对话框配置
    • title (string) - 标题
    • body (Object) - Amis Schema,通常是 form
    • size (string) - 尺寸:'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'full'
    • actions (Array) - 自定义按钮
    • 其他 Dialog Schema 属性
  • ctx (Object) - 上下文数据(可选)
    • onConfirm (Function) - 确认回调
    • onCancel (Function) - 取消回调

返回值:

  • 确认:表单数据数组 values[]
  • 取消:false
import { openDialog } from 'ouroboros-sdk'

// 使用 await 获取结果
const result = await openDialog({
  title: '对话框标题',
  size: 'md',
  body: {
    type: 'form',
    body: [
      { type: 'input-text', name: 'name', label: '姓名' }
    ]
  }
})

if (result !== false) {
  console.log('用户提交的数据', result) // [{ name: 'John' }]
} else {
  console.log('用户取消了')
}

// 使用回调方式(可选)
openDialog(
  {
    title: '对话框标题',
    body: { type: 'form', body: [...] }
  },
  {
    onConfirm: (data) => console.log('确认', data),
    onCancel: () => console.log('取消')
  }
)

openDrawer

打开抽屉,返回 Promise。

参数:

  • schema (Object) - 抽屉配置
    • title (string) - 标题
    • body (Object) - Amis Schema
    • size (string) - 尺寸:'xs' | 'sm' | 'md' | 'lg' | 'xl'
    • position (string) - 位置:'left' | 'right' | 'top' | 'bottom'
    • overlay (boolean) - 是否显示遮罩
    • resizable (boolean) - 是否可调整大小
    • width (number | string) - 宽度
    • height (number | string) - 高度
  • ctx (Object) - 上下文数据(可选)
    • onConfirm (Function) - 确认回调
    • onCancel (Function) - 取消回调

返回值:

  • 确认:表单数据数组 values[]
  • 取消:false
import { openDrawer } from 'ouroboros-sdk'

// 使用 await 获取结果
const result = await openDrawer({
  title: '抽屉标题',
  body: { type: 'page', body: '内容' },
  size: 'lg',
  position: 'right',
  resizable: true
})

if (result !== false) {
  console.log('用户提交的数据', result)
} else {
  console.log('用户关闭了抽屉')
}

confirm / alert / prompt

确认框、提示框、输入框。

confirm 参数:

  • content (string | ReactNode) - 提示内容
  • title (string) - 标题(可选)
  • optionsOrConfirmText (string | Object) - 确认按钮文本或选项对象(可选)
    • confirmText (string) - 确认按钮文本
    • cancelText (string) - 取消按钮文本
    • confirmBtnLevel (string) - 确认按钮样式
    • cancelBtnLevel (string) - 取消按钮样式
    • size (string) - 尺寸
    • closeOnEsc (boolean) - 按 ESC 关闭
  • cancelText (string) - 取消按钮文本(可选)
  • 返回值: true 确认 | false 取消

alert 参数:

  • content (string) - 提示内容
  • title (string) - 标题(可选)
  • className (string) - 样式类名(可选)
  • 返回值: Promise<void>

prompt 参数:

  • controls (Object | Array | string) - 输入框配置
    • 对象:单个输入框
    • 数组:多个输入框
    • 字符串:兼容浏览器标准用法
  • defaultValue (Object | string) - 默认值(可选)
  • title (string) - 标题(可选)
  • confirmText (string) - 确认按钮文本(可选)
  • 返回值: 表单值对象 { name: value, ... } | false 取消
import { confirm, alert, prompt } from 'ouroboros-sdk'

// 确认框 - 返回 true/false,不会 reject
const result = await confirm('确定删除吗?', '删除确认')
if (result) {
  console.log('用户点击了确认')
  // 执行删除操作
} else {
  console.log('用户点击了取消')
}

// 带选项的确认框
const result = await confirm('确定删除吗?', '删除确认', {
  confirmText: '确定',
  cancelText: '取消',
  confirmBtnLevel: 'danger',
  size: 'md'
})

// 提示框
await alert('操作成功', '提示')
await alert('警告信息', '警告', 'modal-warning')

// 输入框 - 单个输入
const result = await prompt(
  { type: 'input-text', name: 'username', label: '用户名' },
  { username: 'admin' },  // 默认值
  '请输入用户名',
  '确定'  // 确认按钮文本
)
if (result !== false) {
  console.log('用户输入:', result.username)
} else {
  console.log('用户取消了')
}

// 输入框 - 多个输入
const result = await prompt(
  [
    { type: 'input-text', name: 'username', label: '用户名', required: true },
    { type: 'input-email', name: 'email', label: '邮箱' },
    { type: 'input-password', name: 'password', label: '密码' }
  ],
  { username: 'admin' },  // 默认值
  '请输入用户信息'
)
if (result !== false) {
  console.log('用户输入:', result)
  // { username: 'john', email: '[email protected]', password: '123456' }
}

// 兼容浏览器标准用法
const result = await prompt('请输入姓名', 'John')
// 等价于单个 text 输入框,返回 { text: '输入的值' }

标签页管理

openTab

打开新标签页。

参数:

  • fullPath (string) - 页面路径(必需)
  • tabOption (Object) - 标签页选项(可选)
    • title (string) - 标签页标题
    • icon (string) - 图标类名
    • breadcrumbs (Array) - 面包屑导航
    • callback (Function) - 标签页关闭时的回调函数

返回值: tabId (string) - 标签页 ID

closeTab

关闭标签页。

参数:

  • id (string) - 标签页 ID,不传则关闭当前标签页(可选)
  • result (any) - 返回给父标签页的数据(可选)

openOrSwitchTab

打开或切换到已存在的标签页。

参数:

  • fullPath (string) - 页面路径(必需)
  • tabOption (Object) - 标签页选项(可选)
    • title (string) - 标签页标题
    • callback (Function) - 回调函数

返回值: tabId (string) - 标签页 ID

updateTabTitle

更新标签页标题。

参数:

  • id (string) - 标签页 ID,不传则更新当前标签页(可选)
  • title (string) - 新标题(必需)

sendToOpenerTab

向打开当前标签页的父标签页发送消息。

参数:

  • id (string) - 当前标签页 ID,不传则使用当前标签页(可选)
  • data (any) - 要发送的数据(必需)
import { openTab, closeTab, openOrSwitchTab, updateTabTitle, sendToOpenerTab } from 'ouroboros-sdk'

// 打开新标签页
const tabId = openTab('/path/to/page', {
  title: '标签页标题',
  icon: 'fa fa-home',
  breadcrumbs: [{ label: '首页', path: '/' }],
  callback: (result) => {
    console.log('标签页返回的数据', result)
  }
})

// 关闭标签页
closeTab('tab-id', { result: 'data' })

// 打开或切换到已存在的标签页
openOrSwitchTab('/path/to/page', { 
  title: '标题',
  callback: (result) => console.log(result)
})

// 更新标签页标题
updateTabTitle('tab-id', '新标题')

标签页间通信

import { sendToOpenerTab, closeTab } from 'ouroboros-sdk'

// 父标签页打开子标签页
openTab('/child-page', {
  title: '子页面',
  callback: (result) => {
    console.log('子页面返回的数据', result)
  }
})

// 子页面中发送消息给父标签页
// 方式1:关闭时返回数据
closeTab(null, { success: true, data: 'result' })

// 方式2:不关闭,只发送消息
sendToOpenerTab(null, { message: 'hello from parent' })

消息提示

toast

显示消息提示。

方法:

  • toast.success(content, conf) - 成功提示
  • toast.error(content, conf) - 错误提示
  • toast.info(content, conf) - 信息提示
  • toast.warning(content, conf) - 警告提示

参数:

  • content (string) - 提示内容(必需)
  • conf (Object) - 配置选项(可选)
    • timeout (number) - 显示时长(毫秒)
    • 其他 toast 配置
import { toast } from 'ouroboros-sdk'

toast.success('操作成功')
toast.error('操作失败')
toast.info('提示信息')
toast.warning('警告信息')

// 带配置
toast.success('成功', { timeout: 3000 })

网络请求

基于 axios 的请求封装,自动携带认证信息。

方法:

  • request(config) - 通用请求
  • request.get(url, config) - GET 请求
  • request.post(url, data, config) - POST 请求
  • request.put(url, data, config) - PUT 请求
  • request.delete(url, config) - DELETE 请求
  • request.patch(url, data, config) - PATCH 请求

参数:

  • url (string) - 请求地址
  • data (any) - 请求数据
  • config (Object) - axios 配置选项

返回值: Promise - 响应数据

import { request } from 'ouroboros-sdk'

// GET 请求
const data = await request.get('/api/users')
const data = await request.get('/api/users', { params: { page: 1 } })

// POST 请求
await request.post('/api/users', { name: 'John' })

// PUT 请求
await request.put('/api/users/1', { name: 'Jane' })

// DELETE 请求
await request.delete('/api/users/1')

// PATCH 请求
await request.patch('/api/users/1', { status: 'active' })

// 自定义配置
await request({
  url: '/api/data',
  method: 'GET',
  headers: { 'X-Custom': 'value' }
})

事件总线

跨应用事件通信。

EventBus.subscribe

订阅事件。

参数:

  • event (string) - 事件名(必需)
  • filter (Function) - 过滤器函数(可选)
  • handler (Function) - 事件处理函数(必需)

返回值: unsubscribe (Function) - 取消订阅函数

EventBus.subscribeOnce

一次性订阅事件。

参数:subscribe

EventBus.publish

发布事件。

参数:

  • event (string) - 事件名(必需)
  • data (any) - 事件数据(必需)
  • options (Object) - 选项(可选)
    • replayScope (string) - 重放范围
    • replayStage (string) - 重放阶段

EventBus.unsubscribe

取消订阅。

参数:

  • event (string) - 事件名(必需)
  • handler (Function) - 事件处理函数(必需)
import { EventBus } from 'ouroboros-sdk'

// 订阅事件
const unsubscribe = EventBus.subscribe('event-name', (data) => {
  console.log('收到事件', data)
})

// 带过滤器的订阅
EventBus.subscribe('event-name', 
  (data) => data.type === 'specific', // 过滤器
  (data) => console.log('匹配的事件', data)
)

// 一次性订阅
EventBus.subscribeOnce('event-name', (data) => {
  console.log('只触发一次', data)
})

// 发布事件
EventBus.publish('event-name', { key: 'value' })

// 带选项的发布
EventBus.publish('event-name', data, {
  replayScope: 'SESSION',
  replayStage: 'stage-1'
})

// 取消订阅
EventBus.unsubscribe('event-name', handler)

WebSocket (Stomp)

基于 Stomp 协议的 WebSocket 通信。

连接管理

  • Stomp.connect() - 连接服务器
  • Stomp.disconnect() - 断开连接
  • Stomp.reconnect() - 重新连接

状态检查

  • Stomp.isConnected() - 检查是否已连接
  • Stomp.isActive() - 检查是否已激活(包括正在连接和已连接状态)
  • Stomp.isStompAuthenticated() - 检查是否已认证

返回值: boolean

import { Stomp } from 'ouroboros-sdk'

if (Stomp.isConnected()) {
  console.log('已连接到服务器')
}

if (Stomp.isStompAuthenticated()) {
  console.log('已通过认证')
}

连接事件订阅

订阅 Stomp 连接状态变化事件。

onStompConnected 参数:

  • handler (Function) - 事件处理函数(必需)
    • 参数:data (Object)
      • isReconnect (boolean) - 是否为重连
      • timestamp (number) - 时间戳
      • message (any) - 连接消息

返回值: unsubscribe (Function) - 取消订阅函数

onStompDisconnected 参数:

  • handler (Function) - 事件处理函数(必需)
    • 参数:data (Object)
      • timestamp (number) - 时间戳

返回值: unsubscribe (Function) - 取消订阅函数

import { Stomp } from 'ouroboros-sdk'

// 订阅连接成功事件
const unsubscribeConnected = Stomp.onStompConnected((data) => {
  if (data.isReconnect) {
    console.log('重连成功', data.timestamp)
  } else {
    console.log('首次连接成功', data.timestamp)
  }
})

// 订阅断开连接事件
const unsubscribeDisconnected = Stomp.onStompDisconnected((data) => {
  console.log('连接已断开', data.timestamp)
})

// 取消订阅
unsubscribeConnected()
unsubscribeDisconnected()

订阅方法

参数:

  • destination (string) - 目标地址(必需)
  • callback (Function) - 消息回调函数(必需)
    • 参数:message (Object) - 消息对象
      • body - 消息体
      • headers - 消息头

返回值: unsubscribe (Function) - 取消订阅函数

  • Stomp.subscribeTopic(destination, callback) - 订阅主题
  • Stomp.subscribeQueue(destination, callback) - 订阅队列
  • Stomp.subscribeUserTopic(destination, callback) - 订阅用户主题
  • Stomp.subscribe(destination, callback) - 通用订阅

取消订阅

参数:

  • destination (string) - 目标地址(必需)

  • Stomp.unsubscribeTopic(destination) - 取消订阅主题

  • Stomp.unsubscribeQueue(destination) - 取消订阅队列

  • Stomp.unsubscribeUserTopic(destination) - 取消订阅用户主题

  • Stomp.unsubscribe(destination) - 通用取消订阅

  • Stomp.unsubscribeAll() - 取消所有订阅

  • Stomp.unsubscribeAllTopics() - 取消所有主题订阅

  • Stomp.unsubscribeAllQueues() - 取消所有队列订阅

  • Stomp.unsubscribeAllUserTopics() - 取消所有用户主题订阅

发送消息

参数:

  • destination (string) - 目标地址(必需)

  • body (any) - 消息体(必需)

  • headers (Object) - 消息头(可选)

  • Stomp.sendJson(destination, body, headers) - 发送 JSON

  • Stomp.sendText(destination, body, headers) - 发送文本

  • Stomp.sendBinary(destination, body, headers) - 发送二进制

  • Stomp.send(destination, body, headers) - 通用发送

import { Stomp } from 'ouroboros-sdk'

// 连接
Stomp.connect()

// 订阅主题
const unsubscribe = Stomp.subscribeTopic('/topic/messages', (message) => {
  console.log('收到消息', message.body)
})

// 订阅队列
Stomp.subscribeQueue('/queue/tasks', (message) => {
  console.log('收到任务', message.body)
})

// 订阅用户主题
Stomp.subscribeUserTopic('/user/notifications', (message) => {
  console.log('收到通知', message.body)
})

// 发送消息
Stomp.sendJson('/app/message', { content: 'Hello' })
Stomp.sendText('/app/message', 'Hello')
Stomp.sendBinary('/app/upload', binaryData)

// 取消订阅
unsubscribe()
Stomp.unsubscribeAll()

// 断开连接
Stomp.disconnect()

// 重连
Stomp.reconnect()

先订阅后注入

Stomp 支持在平台注入前进行订阅。订阅请求会被缓存,待平台注入真实 Stomp 对象后自动执行。

import { Stomp } from 'ouroboros-sdk'

// 在平台注入前订阅(订阅请求会被缓存)
const unsubscribe = Stomp.subscribeTopic('/topic/messages', (message) => {
  console.log('收到消息', message)
})

// 平台注入后,缓存的订阅会自动执行
// 取消订阅函数在注入前后都能正常工作
unsubscribe()

国际化 (i18n)

多语言支持。

i18n.getLocale

获取当前语言。

返回值: string - 语言代码,如 'zh-CN' | 'en-US'

i18n.getLanguages

获取可用语言列表。

返回值: Array<LanguageOption> - 语言列表

  • value (string) - 语言代码
  • label (string) - 语言名称
  • version (string) - 版本号
  • isDefault (boolean) - 是否默认

i18n.switchLanguage

切换语言。

参数:

  • locale (string) - 语言代码(必需)

返回值: Promise<void>

i18n.t

通用翻译。

参数:

  • key (string) - 翻译键(必需)
  • defaultText (string) - 默认文本(可选)
  • params (Object) - 参数对象(可选)

返回值: string - 翻译后的文本

i18n.tn

命名空间翻译。

参数:

  • namespace (string) - 命名空间(必需)
  • key (string) - 翻译键(必需)
  • defaultText (string) - 默认文本(可选)
  • params (Object) - 参数对象(可选)

返回值: string - 翻译后的文本

i18n.tMenu

菜单翻译。

参数:

  • key (string) - 翻译键(必需)
  • defaultText (string) - 默认文本(可选)

返回值: string - 翻译后的文本

i18n.tCommon

通用文本翻译。

参数:

  • key (string) - 翻译键(必需)
  • defaultText (string) - 默认文本(可选)
  • params (Object) - 参数对象(可选)

返回值: string - 翻译后的文本

i18n.useNamespace

使用命名空间。

参数:

  • namespace (string) - 命名空间(必需)

返回值: Function - 翻译函数 (key, defaultText, params) => string

i18n.getLocaleData

获取语言数据。

返回值: Object - 当前语言的所有翻译数据

i18n.onLocaleChange

监听语言变化。

参数:

  • callback (Function) - 回调函数(必需)
    • 参数:locale (string) - 新的语言代码

返回值: Function - 取消监听函数

import { i18n } from 'ouroboros-sdk'

// 获取当前语言
const locale = i18n.getLocale() // 'zh-CN' | 'en-US'

// 获取可用语言列表
const languages = i18n.getLanguages()
// [{ value: 'zh-CN', label: '简体中文', version: '1.0.0' }, { value: 'en-US', label: 'English', version: '1.0.0' }]

// 切换语言
await i18n.switchLanguage('en-US')

// 翻译文本
const text = i18n.t('common.save', '保存')

// 带参数的翻译
const text = i18n.t('common.welcome', '欢迎 {name}', { name: 'John' })

// 命名空间翻译
const text = i18n.tn('myapp', 'title', '我的应用')

// 菜单翻译
const menuText = i18n.tMenu('menu.home', '首页')

// 通用文本翻译
const commonText = i18n.tCommon('save', '保存')

// 使用命名空间
const t = i18n.useNamespace('myapp')
const text = t('title', '我的应用')

// 获取语言数据
const localeData = i18n.getLocaleData()

// 监听语言变化
const unsubscribe = i18n.onLocaleChange((locale) => {
  console.log('语言已切换为', locale)
})
// 取消监听
unsubscribe()

主题管理

import { getTheme, setTheme } from 'ouroboros-sdk'

// 获取当前主题
const theme = getTheme() // 'light' | 'dark'

// 设置主题
setTheme('dark')

用户认证

主题管理

getTheme

获取当前主题。

返回值: string - 主题名称,如 'light' | 'dark'

setTheme

设置主题。

参数:

  • theme (string) - 主题名称(必需)
import { getTheme, setTheme } from 'ouroboros-sdk'

// 获取当前主题
const theme = getTheme() // 'light' | 'dark'

// 设置主题
setTheme('dark')

用户认证

login

用户登录。

参数:

  • params (Object) - 登录参数(必需)
    • username (string) - 用户名
    • password (string) - 密码
    • 其他登录参数

返回值: Promise<any> - 登录结果

logout

用户登出。

返回值: Promise<void>

getUserInfo

获取用户信息。

返回值: Promise<Object> - 用户信息对象

hasAuth

权限检查。

参数:

  • authKey (string) - 权限键(必需)

返回值: boolean - 是否有权限

import { login, logout, getUserInfo, hasAuth } from 'ouroboros-sdk'

// 登录
await login({ username: 'user', password: 'pass' })

// 登出
await logout()

// 获取用户信息
const userInfo = await getUserInfo()

// 权限检查
if (hasAuth('user:create')) {
  // 有权限
}

Amis 集成

注册组件

FormItem

注册表单项。

import { FormItem } from 'ouroboros-sdk'

@FormItem({
  type: 'my-input',
  label: '自定义输入框'
})
class MyInput extends React.Component {
  render() {
    const { value, onChange } = this.props
    return <input value={value} onChange={e => onChange(e.target.value)} />
  }
}

Renderer

注册渲染器。

import { Renderer } from 'ouroboros-sdk'

@Renderer({
  type: 'my-component',
  name: 'my-component'
})
class MyComponent extends React.Component {
  render() {
    return <div>{this.props.body}</div>
  }
}

OptionsControl

注册选项控件。

import { OptionsControl } from 'ouroboros-sdk'

@OptionsControl({
  type: 'my-select'
})
class MySelect extends React.Component {
  render() {
    const { options, value, onChange } = this.props
    return (
      <select value={value} onChange={e => onChange(e.target.value)}>
        {options.map(opt => <option key={opt.value} value={opt.value}>{opt.label}</option>)}
      </select>
    )
  }
}

注册功能

registerAction

注册自定义动作。

参数:

  • actionType (string) - 动作名称(必需)
  • action (Function) - 动作处理函数(必需)
    • 参数:(action, renderer, event) => void | Promise<void>
import { registerAction } from 'ouroboros-sdk'

registerAction('my-action', async (action, renderer, event) => {
  console.log('执行自定义动作', action)
  // 执行逻辑
})

registerFormula

注册公式。

参数:

  • name (string) - 公式名称(必需)
  • fn (Function) - 公式函数(必需)
import { registerFormula } from 'ouroboros-sdk'

registerFormula('DOUBLE', (value) => value * 2)

// 在 schema 中使用: "${DOUBLE(amount)}"

registerValidator

注册验证器。

参数:

  • name (string) - 验证器名称(必需)
  • fn (Function) - 验证函数(必需)
    • 参数:(values, value) => true | string
    • 返回:true 验证通过 | string 错误消息
import { registerValidator } from 'ouroboros-sdk'

registerValidator('phone', (values, value) => {
  if (!/^1[3-9]\d{9}$/.test(value)) {
    return '请输入正确的手机号'
  }
  return true
})

addRule

添加验证规则。

参数:

  • ruleName (string) - 规则名称(必需)
  • validatorFn (Function) - 验证函数(必需)
    • 参数:(value) => boolean
  • errorMessage (string) - 错误消息(必需)
import { addRule } from 'ouroboros-sdk'

addRule(
  'idcard',
  (value) => /^\d{17}[\dXx]$/.test(value),
  '请输入正确的身份证号'
)

registerIcon

注册图标。

参数:

  • key (string) - 图标键名(必需)
  • component (Function | Component) - 图标组件(必需)
import { registerIcon } from 'ouroboros-sdk'

registerIcon('my-icon', () => <svg>...</svg>)

编辑器扩展

registerEditorPlugin

注册编辑器插件。

参数:

  • plugin (Object) - 插件对象(必需)

registerActionPanel

注册动作面板。

参数:

  • panel (Object) - 面板对象(必需)
import { registerEditorPlugin, registerActionPanel } from 'ouroboros-sdk'

// 注册编辑器插件
registerEditorPlugin(MyEditorPlugin)

// 注册动作面板
registerActionPanel(MyActionPanel)

渲染 Amis Schema

renderAmisSchema

渲染 Amis Schema 到指定容器。

参数:

  • containerEl (HTMLElement) - 容器元素(必需)
  • schema (Object) - Amis Schema(必需)
  • props (Object) - 属性对象(可选)
    • data (Object) - 初始数据
    • 其他 Amis 属性
  • env (Object) - 环境配置(可选)
    • theme (string) - 主题
    • 其他环境配置
import { renderAmisSchema } from 'ouroboros-sdk'

const schema = {
  type: 'page',
  body: {
    type: 'form',
    body: [
      { type: 'input-text', name: 'name', label: '姓名' }
    ]
  }
}

renderAmisSchema(
  document.getElementById('container'),
  schema,
  { data: { name: 'John' } },
  { theme: 'cxd' }
)

Amis 工具函数

import { amisUtils } from 'ouroboros-sdk'

// 使用 amis-core 提供的工具函数
amisUtils.guid()
amisUtils.isObject(value)
amisUtils.createObject(parent, props)
// ... 更多工具函数

类型定义

SDK 提供完整的 TypeScript 类型定义。

import type {
  TabOption,
  MenuInfo,
  ConfirmOptions,
  LoginParams,
  StompHeaders,
  IMessage,
  InitMicroAppOptions,
  InitLayoutComponentOptions,
  InitAmisComponentLibOptions,
  MicroAppLifecycle,
  OuroborosDialogSchema,
  OuroborosDrawerSchema,
  OuroborosModalContext
} from 'ouroboros-sdk'

主要类型

// 微前端初始化选项
interface InitMicroAppOptions {
  render?: (props: any) => Promise<any>
  beforeMount?: (props: any) => Promise<void>
  afterMount?: (props: any) => Promise<void>
  afterUnmount?: (props: any, app: any) => Promise<void>
  afterUpdate?: (props: any) => Promise<void>
  onBootstrap?: () => Promise<void>
}

// 标签页选项
interface TabOption {
  title?: string
  breadcrumbs?: Object[]
  callback?: Function
  icon?: string
}

// 对话框 Schema
interface OuroborosDialogSchema {
  title: string
  body: any
  size?: 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'full'
  actions?: any[]
}

// 抽屉 Schema
interface OuroborosDrawerSchema {
  title: string
  body: any
  size?: 'xs' | 'sm' | 'md' | 'lg' | 'xl'
  position?: 'left' | 'right' | 'top' | 'bottom'
}

注意事项

  1. ES6 活绑定机制:SDK 使用 ES6 模块的活绑定特性,平台能力在微前端挂载时动态注入。

  2. 独立运行:微前端应用可以独立运行,此时 SDK 方法会输出警告信息。

  3. 类型安全:建议使用 TypeScript 开发,SDK 提供完整的类型定义。

  4. 组件注册时机:Amis 组件应在 beforeMountinitAmisComponentLib 中注册。

  5. 事件清理:组件卸载时记得取消事件订阅和 WebSocket 连接。


示例项目

参考 monorepo 中的示例项目:

  • ouroboros-logicflow-editor - 逻辑流设计器
  • ouroboros-debug-console-topbar - 调试工具栏

版本历史

遵循语义化版本 2.0.0规范。

查看 CHANGELOG 了解详细更新记录。