@coastpay/embedded-widget-sdk

中文文档
0.8.3发布于 2026/08/11SEE LICENSE IN LICENSE.mdTypeScriptES Module零依赖近 30 天下载 1.3万

Embedded widget SDK for Coast

原始使用文档
文档目录5 节
  1. Getting Started
  2. API
  3. coast.initialize(options)
  4. widget.logout()
  5. widget.destroy()

Coast Widget SDK

Embed Coast in your web application.

Getting Started

npm install @coastpay/embedded-widget-sdk

Load the SDK and initialize:

import { loadCoast } from '@coastpay/embedded-widget-sdk';

const coast = await loadCoast();

const widget = coast.initialize({
  container: 'coast-container', // element id or HTMLElement
  embedToken,
  onReady: () => {
    // the embedded app has finished loading and is ready to be used
    console.log('Coast widget is ready');
  },
  onError: (error) => {
    console.error('Coast widget error:', error);
  },
});

API

coast.initialize(options)

Mounts the widget iframe into the container element and returns a CoastWidget instance.

Single-instance limit: Only one widget may be active at a time. Calling initialize() while a widget already exists automatically destroys the previous one.

OptionTypeRequiredDescription
containerstring | HTMLElementYesElement id or a direct HTMLElement reference
embedTokenstringYesOpaque session token from your backend's Embed Token API call
onReady() => voidNoCalled when the embedded app has finished loading and is ready to be used
onError(error: Error) => voidNoCalled on initialization failure or if the embedded app fails to load within 30 seconds
onLogin() => voidNoCalled once the user is authenticated in the embedded app. Fires for restored sessions too and may fire more than once for the same user — make your handler idempotent. Client-side notification only — confirm server-side before recording state
onLogout() => voidNoCalled when the user logs out from within the embedded app (e.g. session expiry)

widget.logout()

Notifies the embedded app that the host user has logged out. Call this when your own session ends.

widget.destroy()

Removes the iframe and cleans up all event listeners.

版本与发布记录

@coastpay/embedded-widget-sdk 共发布 5 个版本,首次发布于 2026/04/07;过去 12 个月发布了 5 个版本。

版本发布日期距今
0.8.3latest2026/08/112 个月前
0.8.22026/05/145 个月前
0.8.12026/05/125 个月前
0.7.42026/04/097 个月前
0.7.22026/04/077 个月前

关于 @coastpay/embedded-widget-sdk 的常见问题

@coastpay/embedded-widget-sdk 是什么?

Embedded widget SDK for Coast

如何安装 @coastpay/embedded-widget-sdk?

在项目目录运行 npm install @coastpay/embedded-widget-sdk,或使用 pnpm add / yarn add / bun add。

@coastpay/embedded-widget-sdk 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@coastpay/embedded-widget-sdk 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@coastpay/embedded-widget-sdk 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@coastpay/embedded-widget-sdk 可以商用吗?

@coastpay/embedded-widget-sdk 使用 SEE LICENSE IN LICENSE.md 许可。许可条款写在包内的独立文件中,请阅读该文件确认可否商用。以上为概括,不构成法律意见,请以许可原文为准。

@coastpay/embedded-widget-sdk 的下载量有多少?

根据 npm 官方下载统计,@coastpay/embedded-widget-sdk 在 2026/10/10 – 2026/11/08 这 30 天内被下载 13,419 次,日均约 447 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@coastpay/embedded-widget-sdk 还在维护吗?

最新版本 0.8.3 发布于 2026/08/11(2 个月前);至今共发布 5 个版本,其中过去 12 个月发布了 5 个;首次发布于 2026/04/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。