@deadair/sdk

中文文档
0.97.4发布于 2026/11/11MITTypeScriptES Module构建来源已验证近 30 天下载 1.7万

A typed client for the deadair station's HTTP API, generated from the same contracts the station serves.

原始使用文档
文档目录6 节
  1. Which version
  2. Getting started
  3. Dates and decimals
  4. Errors
  5. Your own fetch
  6. Where it comes from

@deadair/sdk

A typed client for a deadair station's HTTP API. It is generated from the same contracts the station serves its routes from, so a method, its parameters and the shape it answers are the station's own and not a hand-kept copy of them. The console is built on it.

npm install @deadair/sdk

It is an ES module only, and it runs anywhere with a global fetch: Node 20 and later, Deno, Bun and every current browser.

Which version

The SDK is released with the station and carries the station's version: SDK 0.6.0 is the client for station 0.6.0. Use the version that matches the station you talk to. Before 1.0 a minor step of the station can change a route, and the SDK changes with it.

Getting started

Every station is self-hosted, so the address is your own. The API is served under /api on the station's one published port, which on a default install is http://localhost:8080/api.

import { DeadairSdk } from '@deadair/sdk';

const sdk = new DeadairSdk({ baseUrl: 'http://localhost:8080/api' });

const now = await sdk.nowplaying.getNowPlaying();
console.log(now.onAir && now.track ? `${now.track.artist} - ${now.track.title}` : 'off air');

getNowPlaying is the one route that needs no sign-in. For the rest, ask for a token with a password grant and send it on every request. headers may be a function, called once per request, so a token you replace later is picked up without building a new client:

import { DeadairSdk } from '@deadair/sdk';

let token: string | undefined;
const sdk = new DeadairSdk({
    baseUrl: 'http://localhost:8080/api',
    headers: (): Record<string, string> => (token ? { Authorization: `Bearer ${token}` } : {}),
});

const answer = await sdk.authentication.requestToken({ grant_type: 'password', username: '[email protected]', password: '...' });
if (answer.result === 'mfa_required') {
    throw new Error('this account has a second factor to verify before it gets a token');
}
token = answer.access_token;

const page = await sdk.history.readHistory({ limit: 5 });
for (const entry of page.entries) {
    console.log(entry.airedAt.toRelative(), entry.artists, '-', entry.title);
}

What each route needs, what it answers and the permissions behind it are in the API reference, which is generated from the same contracts and names the method for every route.

Dates and decimals

A date-time field arrives as a Luxon DateTime and a decimal field as a decimal.js Decimal, revived from the wire before the method returns. Both are dependencies of this package, and Luxon's types come with it.

Importing the SDK sets decimal.js's toExpNeg and toExpPos to their limits, so a Decimal prints as plain digits and never in exponent notation. That setting is global to the copy of decimal.js the SDK shares with your code. Use Decimal.clone() for a constructor of your own if you depend on the defaults.

Errors

A response at or above 400 throws SdkError, which carries the status, the statusText, the headers and the body, parsed as JSON when it is JSON. Every request carries a fresh X-Request-ID header. Pass requestIdFactory to make the ids yourself.

Your own fetch

fetch in the options replaces the whole transport: the SDK hands it a path relative to the API root and a RequestInit, and parses whatever Response it returns. createSdkFetch(options) builds the default one, which is also what throws SdkError, so a wrapper can add behaviour around it, such as timing every request or retrying a 401 once after refreshing the token:

import { createSdkFetch, DeadairSdk, type SdkFetch } from '@deadair/sdk';

const base = createSdkFetch({ baseUrl: 'http://localhost:8080/api' });
const withLogging: SdkFetch = async (url, init) => {
    const started = Date.now();
    try {
        return await base(url, init);
    } finally {
        console.debug(init.method, url, `${Date.now() - started}ms`);
    }
};
const sdk = new DeadairSdk({ baseUrl: 'http://localhost:8080/api', fetch: withLogging });

Where it comes from

Everything under src/ is generated by ContractKit from the .ck contracts in apps/api/data/contracts, and none of it is edited by hand. A change to the client is a change to a contract. The same contracts generate the station's routes, the API reference, an OpenAPI document for any other language, and the Kotlin, C# and Swift clients the listener apps are built on.

版本与发布记录

@deadair/sdk 共发布 220 个版本,首次发布于 2026/09/15;过去 12 个月发布了 220 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.97.4latest2026/11/11今天
0.97.32026/11/11今天
0.97.22026/11/101 天前
0.97.12026/11/093 天前
0.97.02026/11/083 天前
0.96.52026/11/084 天前

关于 @deadair/sdk 的常见问题

@deadair/sdk 是什么?

A typed client for the deadair station's HTTP API, generated from the same contracts the station serves.

如何安装 @deadair/sdk?

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

@deadair/sdk 支持 TypeScript 吗?

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

@deadair/sdk 支持 ESM 还是 CommonJS?

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

@deadair/sdk 可以商用吗?

@deadair/sdk 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@deadair/sdk 的下载量有多少?

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

@deadair/sdk 还在维护吗?

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