@amityco/ts-sdk

中文文档
7.33.1发布于 2026/11/06CC-BY-ND-4.0TypeScriptESM + CJS近 30 天下载 4.4万

友好社交云打字机 SDK

中文使用文档
文档目录6 节
  1. 开始
  2. 运行时间支持
  3. 创建客户端实例
  4. 使用 Live 对象观察对象
  5. 使用 Live Collection 监视收藏
  6. 文档

友好社交云打字机 SDK

开始

  • 以 npm 为单位 : npm install @amityco/ts-sdk
  • 用纱线: yarn add @amityco/ts-sdk

运行时间支持

  • 现代浏览器( 符合 2017)
  • 节点JS(通过esm进口)
  • 反应- Native

创建客户端实例

要将您的用户连接到我们的平台,您需要创建客户端实例并将其连接.

import { Client } from '@amityco/ts-sdk';

(async () => {
  const sessionHandler: Amity.SessionHandler = {
    sessionWillRenewAccessToken(renewal: Amity.Renewal) {
      renewal.renew();
      /*
       * If using an auth token
       *
       * try {
       *  renew.renewWithAuthToken(authToken)
       * } catch() {
       *  sdk will try to renew again at a later time
       *
       *  renew.unableToRetrieveAuthToken()
       * }
       */
    },
  };
  const client = Client.createClient(API_KEY, API_REGION);

  let isConnected = await Client.login({ userId: USER_ID }, sessionHandler);
})();

这个 API_KEY 财务报告和审定财务报表 API_REGION 在您创建应用程序时给出 友好门户。 。 。 .

您可以使用 onClientDisconnected 函数 :

import { Client } from '@amityco/ts-sdk';

const unsub = Client.onClientDisconnected(() => (isConnected = false));

这个 unsub返回值为 处理函数 用于清理客户端应用程序的内存。 它被设计成与React完全吻合,在诸如:

useEffect(() => onClientConnected(() => setConnected(false)), [])

## Going further

### Domain types

All the necessary types are automatically exposed in the `Amity` namespace. As long as you import the SDK once, the namespace will be available in your workspace. You can start to type `Amity.` and see the list of types in your intellisense after having imported the SDK in any file of your project.

### Subscribing to real time events

We expose a couple of functions for your to receive simply the events that could fire from our servers, for example, if another user would change their display name, or if a channel would receive a new message.

The `subscribeTopic` method retuns an unsubscriber that can be called on clean up to stop recieving further events.

```ts
import { subscribeTopic, getUserTopic } from '@amityco/ts-sdk'

const unsub = subscribeTopic(getUserTopic({} as Amity.User))

使用 Live 对象观察对象

对于一个 "一无所有" 方法,我们暴露活的物体, 它允许获取物体 财务报告和审定财务报表 登记所有更改。 它的设计符合React的用法状态和使用效果:

import { UserRepository } from '@amityco/ts-sdk'

const Component = ({ userId }: { userId: string }) => {
  const [user, setUser] = useState<Amity.User>({})

  useEffect(() => UserRepository.getUser(userId, ({ data: user, loading, error }) => {
    // ensure you call unsub() on cleanup or unmount
    const unsub = subscribeTopic(getUserTopic(user))

    setUser(user)
  }), [userId])

  return <div>{JSON.stringify(user, null, 2)}
}

使用 Live Collection 监视收藏

与活物概念类似,我们揭露活物收藏,可以取出活物收藏 财务报告和审定财务报表 登记所有更改。 它的设计符合React的用法状态和使用效果:

import { UserRepository.getUsers } from '@amityco/ts-sdk'

const Component = () => {
  const [users, setUsers] = useState<Amity.User[]>({})

  useEffect(() => UserRepository.getUsers({}, ({ data, loading, error, nextPage, hasNextPage }) => {
    // ensure you call unsub() on cleanup or unmount
    // Subscribe to each user in collection for getting real time updates

    setUsers(users)
  }), [userId])

  return <div>{JSON.stringify(user, null, 2)}
}

文档

查阅综合文件请访问 我们的文档页面。 。 。 .

如果你有任何问题,你可以拜访我们 论坛。 。 。 .

版本与发布记录

@amityco/ts-sdk 共发布 2,485 个版本,首次发布于 2021/08/12;过去 12 个月发布了 492 个版本。

版本发布日期距今
7.33.1latest2026/11/061 天前
7.33.1-ed90356d.02026/11/061 天前
7.33.1-3b1a9bbf.02026/11/052 天前
7.33.1-37a3a70.02026/11/034 天前
7.33.1-b8614357.02026/11/034 天前
7.33.1-d2b15777.02026/11/034 天前

其他发布通道:dev/43 → 0.0.1-8cd7315.0,dev/40 → 0.0.1-0cf87e0.0,dev/95 → 0.0.1-3edad36.0,dev/123 → 0.0.1-fdf0d8d.0,dev/268 → 0.0.1-0fe3ce9.0,dev/277 → 0.0.1-83fb808.0。

关于 @amityco/ts-sdk 的常见问题

@amityco/ts-sdk 是什么?

友好社交云打字机 SDK

如何安装 @amityco/ts-sdk?

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

@amityco/ts-sdk 支持 TypeScript 吗?

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

@amityco/ts-sdk 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@amityco/ts-sdk 需要什么版本的 Node.js?

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

@amityco/ts-sdk 可以商用吗?

@amityco/ts-sdk 使用 CC-BY-ND-4.0 许可。声明的许可为 CC-BY-ND-4.0,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@amityco/ts-sdk 的下载量有多少?

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

@amityco/ts-sdk 还在维护吗?

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