{"id":50445221,"url":"https://github.com/saltifydev/milky-tea","last_synced_at":"2026-05-31T21:02:10.875Z","repository":{"id":344569759,"uuid":"1182196468","full_name":"SaltifyDev/milky-tea","owner":"SaltifyDev","description":"Milky 协议的 TypeScript SDK","archived":false,"fork":false,"pushed_at":"2026-04-08T06:28:47.000Z","size":274,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-04-08T08:16:49.680Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SaltifyDev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-03-15T07:05:07.000Z","updated_at":"2026-04-08T06:28:05.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/SaltifyDev/milky-tea","commit_stats":null,"previous_names":["saltifydev/milky-ts","saltifydev/milky-tea"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/SaltifyDev/milky-tea","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SaltifyDev%2Fmilky-tea","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SaltifyDev%2Fmilky-tea/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SaltifyDev%2Fmilky-tea/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SaltifyDev%2Fmilky-tea/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SaltifyDev","download_url":"https://codeload.github.com/SaltifyDev/milky-tea/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SaltifyDev%2Fmilky-tea/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33748607,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-05-31T02:00:06.040Z","response_time":95,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-05-31T21:02:06.557Z","updated_at":"2026-05-31T21:02:10.862Z","avatar_url":"https://github.com/SaltifyDev.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Milky Tea\n\n[![CI](https://github.com/SaltifyDev/milky-tea/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/SaltifyDev/milky-tea/actions/workflows/ci.yml)\n[![Coverage](https://img.shields.io/endpoint?url=https%3A%2F%2Fraw.githubusercontent.com%2FSaltifyDev%2Fmilky-tea%2Fbadges%2Fcoverage-badge.json)](https://github.com/SaltifyDev/milky-tea/actions/workflows/ci.yml)\n\nMilky 的 TypeScript SDK，提供类型安全的 API 调用和事件流支持。\n\n## 安装\n\n```bash\nnpm i @saltify/milky-tea @saltify/milky-types\n```\n\n如果运行环境不支持 EventSource（例如 Node.js 环境）且需要 SSE 支持，则需要安装 `eventsource`：\n\n```bash\nnpm i eventsource\n```\n\n## 使用方法\n\n### 调用 API\n\n下面是一个使用 `createMilkyClient` 创建客户端并调用 API 的示例：\n\n```ts\nimport { createMilkyClient } from '@saltify/milky-tea'\n\nconst client = createMilkyClient({\n  baseURL: 'https://milky.example.com',\n  token: process.env.MILKY_TOKEN,\n})\n\nconst login = await client.system.getLoginInfo()\nconst friend = await client.system.getFriendInfo({ user_id: 10001 })\n\nconsole.log(login.nickname)\nconsole.log(friend.friend.nickname)\n```\n\n通过 `createMilkyClient` 创建一个客户端实例，传入 `baseURL` 和 `token`，之后就可以通过 `client.{category}.{endpoint}(params)` 的方式调用 API 了。例如，调用 `quit_group` API：\n\n```ts\nawait client.group.quitGroup({ group_id: 10001 }, { timeout: false })\n```\n\n在这里，第二个参数是可选的，可以覆盖默认的 `baseURL`、`token`、`timeout` 等设置。\n\n### 监听事件\n\n通过 `client.event()` 创建一个事件连接，支持 WebSocket 和 SSE 两种连接方式。连接模式有如下几种：\n\n- `auto`：首先尝试 WebSocket，如果在连接打开之前失败，则回退到 SSE\n- `websocket`：仅使用 WebSocket\n- `sse`：仅使用 Server-Sent Events\n\n```ts\nconst source = client.event('auto', {\n  reconnect: {\n    interval: 1000,\n    attempts: 'always',\n  },\n})\n\n// 监听连接打开\nsource.on('open', () =\u003e {\n  console.log('connected')\n})\n\n// 监听所有事件\nsource.on('push', (event) =\u003e {\n  console.log(event.event_type, event)\n})\n\n// 监听特定类型的事件\nsource.on('foobar', (event) =\u003e {\n  console.log(event.message.content)\n})\n\n// 监听错误\nsource.on('error', (event) =\u003e {\n  console.error(event.message)\n})\n\n// 使用 async iteration\nfor await (const event of source) {\n  console.log(event.event_type)\n  if (shouldStop)\n    break\n}\n\nsource.close()\n```\n\n**注意**: 事件对象是深度只读的（immutable），所有嵌套属性都被冻结，无法修改。\n\n### `createMilkyEventSource`\n\n如果需要更底层的事件源控制，可以使用 `createMilkyEventSource` 直接创建事件源。\n\n```ts\nimport { createMilkyEventSource } from '@saltify/milky-tea'\n\n// 使用连接类型和选项\nconst source = createMilkyEventSource('websocket', {\n  baseURL: 'https://milky.example.com',\n  token: process.env.MILKY_TOKEN,\n  timeout: 15000,\n  reconnect: {\n    interval: 1000,\n    attempts: 5,\n  },\n})\n\n// 或使用自定义传输工厂\nconst source = createMilkyEventSource(\n  async (options, signal) =\u003e {\n    // 返回 WebSocket 或 EventSource 实例\n    return new WebSocket('wss://milky.example.com/event')\n  },\n  {\n    timeout: 10000,\n  },\n)\n\nsource.on('open', () =\u003e console.log('Connected'))\nsource.on('push', event =\u003e console.log(event))\nsource.close()\n```\n\n**参数**:\n\n- `kind`: 连接类型 (`'auto'` | `'websocket'` | `'sse'`)\n- `factory`: 自定义传输工厂函数\n- `options`:\n  - `baseURL`: 服务器地址（使用 kind 时必需）\n  - `token`: 访问令牌\n  - `timeout`: 连接超时时间（默认 15000ms）\n  - `reconnect`: 重连配置\n    - `interval`: 重连间隔（毫秒）\n    - `attempts`: 重连次数（`'always'` 或数字）\n\n### `createMilkyFetch`\n\n`createMilkyFetch` 提供了一个更底层的 fetch 封装，允许直接调用原始的 API endpoint。\n\n```ts\nimport { createMilkyFetch } from '@saltify/milky-tea'\n\nconst milkyFetch = createMilkyFetch({\n  baseURL: 'https://milky.example.com',\n  strict: false,\n})\n\nconst login = await milkyFetch('get_login_info', undefined)\nconsole.log(login.uin)\n```\n\n`strict` 默认为 `true`。关闭后会跳过请求参数和响应数据的 zod 校验；也可以在单次请求的 override 里单独设置。\n\n## 开发\n\n```bash\npnpm install\npnpm generate-api\npnpm typecheck\npnpm test\npnpm test:coverage\npnpm build\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsaltifydev%2Fmilky-tea","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsaltifydev%2Fmilky-tea","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsaltifydev%2Fmilky-tea/lists"}