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

@saltify/milky-tea

v0.2.2

Published

Client library for Milky Protocol.

Readme

Milky Tea

CI Coverage

Milky 的 TypeScript SDK,提供类型安全的 API 调用和事件流支持。

安装

npm i @saltify/milky-tea @saltify/milky-types

如果运行环境不支持 EventSource(例如 Node.js 环境)且需要 SSE 支持,则需要安装 eventsource

npm i eventsource

使用方法

调用 API

下面是一个使用 createMilkyClient 创建客户端并调用 API 的示例:

import { createMilkyClient } from '@saltify/milky-tea'

const client = createMilkyClient({
  baseURL: 'https://milky.example.com',
  token: process.env.MILKY_TOKEN,
})

const login = await client.system.getLoginInfo()
const friend = await client.system.getFriendInfo({ user_id: 10001 })

console.log(login.nickname)
console.log(friend.friend.nickname)

通过 createMilkyClient 创建一个客户端实例,传入 baseURLtoken,之后就可以通过 client.{category}.{endpoint}(params) 的方式调用 API 了。例如,调用 quit_group API:

await client.group.quitGroup({ group_id: 10001 }, { timeout: false })

在这里,第二个参数是可选的,可以覆盖默认的 baseURLtokentimeout 等设置。

监听事件

通过 client.event() 创建一个事件连接,支持 WebSocket 和 SSE 两种连接方式。连接模式有如下几种:

  • auto:首先尝试 WebSocket,如果在连接打开之前失败,则回退到 SSE
  • websocket:仅使用 WebSocket
  • sse:仅使用 Server-Sent Events
const source = client.event('auto', {
  reconnect: {
    interval: 1000,
    attempts: 'always',
  },
})

// 监听连接打开
source.on('open', () => {
  console.log('connected')
})

// 监听所有事件
source.on('push', (event) => {
  console.log(event.event_type, event)
})

// 监听特定类型的事件
source.on('foobar', (event) => {
  console.log(event.message.content)
})

// 监听错误
source.on('error', (event) => {
  console.error(event.message)
})

// 使用 async iteration
for await (const event of source) {
  console.log(event.event_type)
  if (shouldStop)
    break
}

source.close()

注意: 事件对象是深度只读的(immutable),所有嵌套属性都被冻结,无法修改。

createMilkyEventSource

如果需要更底层的事件源控制,可以使用 createMilkyEventSource 直接创建事件源。

import { createMilkyEventSource } from '@saltify/milky-tea'

// 使用连接类型和选项
const source = createMilkyEventSource('websocket', {
  baseURL: 'https://milky.example.com',
  token: process.env.MILKY_TOKEN,
  timeout: 15000,
  reconnect: {
    interval: 1000,
    attempts: 5,
  },
})

// 或使用自定义传输工厂
const source = createMilkyEventSource(
  async (options, signal) => {
    // 返回 WebSocket 或 EventSource 实例
    return new WebSocket('wss://milky.example.com/event')
  },
  {
    timeout: 10000,
  },
)

source.on('open', () => console.log('Connected'))
source.on('push', event => console.log(event))
source.close()

参数:

  • kind: 连接类型 ('auto' | 'websocket' | 'sse')
  • factory: 自定义传输工厂函数
  • options:
    • baseURL: 服务器地址(使用 kind 时必需)
    • token: 访问令牌
    • timeout: 连接超时时间(默认 15000ms)
    • reconnect: 重连配置
      • interval: 重连间隔(毫秒)
      • attempts: 重连次数('always' 或数字)

createMilkyFetch

createMilkyFetch 提供了一个更底层的 fetch 封装,允许直接调用原始的 API endpoint。

import { createMilkyFetch } from '@saltify/milky-tea'

const milkyFetch = createMilkyFetch({
  baseURL: 'https://milky.example.com',
  strict: false,
})

const login = await milkyFetch('get_login_info', undefined)
console.log(login.uin)

strict 默认为 true。关闭后会跳过请求参数和响应数据的 zod 校验;也可以在单次请求的 override 里单独设置。

开发

pnpm install
pnpm generate-api
pnpm typecheck
pnpm test
pnpm test:coverage
pnpm build