# @amityco/ts-sdk 中文文档

规范地址：https://npmcn.com/package/%40amityco/ts-sdk/

版本：7\.33\.1
npm 元数据更新时间：2026-11-06T08:23:30.546Z
中文译文更新时间：2026-11-06T08:23:59.685Z
软件包许可：CC-BY-ND-4\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40amityco/ts-sdk
原文对照：https://npmcn.com/package/%40amityco/ts-sdk/?lang=original

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：6 MB，3400 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=12
- 运行依赖：11 个
- 首次发布：2021-08-11T18:22:56.203Z
- 最新版本发布：2026-11-06T08:23:30.360Z
- 近 30 天下载量：44314（2026-10-06 至 2026-11-04，npm 官方下载统计）
- 已发布版本数：2485（近 12 个月 492 个）

## 中文简介

友好社交云打字机 SDK

## 安装

```sh
npm install @amityco/ts-sdk
```

也可以使用：`pnpm add @amityco/ts-sdk`、`yarn add @amityco/ts-sdk`、`bun add @amityco/ts-sdk`

## README

# 友好社交云打字机 SDK

## 开始

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

## 运行时间支持

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

## 创建客户端实例

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

```ts
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` 在您创建应用程序时给出 [友好门户](http://portal.amity.co/)。 。 。 .

您可以使用 `onClientDisconnected` 函数 :

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

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

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

````ts
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的用法状态和使用效果:

```ts
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的用法状态和使用效果:

```ts
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)}
}
```

## 文档

查阅综合文件请访问 [我们的文档页面](https://docs.amity.co/)。 。 。 .

如果你有任何问题,你可以拜访我们 [论坛](https://forum.amity.co/)。 。 。 .

## 常见问题

### @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 和提交记录判断。

