@enerlab/thingsboard-client

中文文档
4.3.1-pe.1.5发布于 2026/10/24MITTypeScriptES Module构建来源已验证近 30 天下载 3876

Type-safe TypeScript client for the ThingsBoard REST API with Zod v4 runtime validation

原始使用文档
文档目录16 节
  1. Features
  2. Versioning
  3. Supported TB versions / branch layout
  4. Installation
  5. Quick Start
  6. Using an Existing Token
  7. Using a Separate Client Instance
  8. Tree-Shaking
  9. Zod Schemas
  10. Development
  11. Prerequisites
  12. Setup
  13. Updating the Spec
  14. Project Structure
  15. ThingsBoard API Reference
  16. License

@enerlab/thingsboard-client

Type-safe TypeScript client for the ThingsBoard REST API with Zod v4 runtime validation.

Generated from the official ThingsBoard OpenAPI 3.1 spec using @hey-api/openapi-ts.

Features

  • Full ThingsBoard REST API coverage, generated from a TB Professional Edition spec (PE is a superset of CE — CE-only consumers will see SDK functions for PE-only endpoints that 404 on their server)
  • login() / logout() helpers (not in the OpenAPI spec, provided manually)
  • TypeScript types for all endpoints, request bodies, and responses
  • Zod schemas for runtime validation at API boundaries
  • Native fetch — works in Node.js 20+, Bun, Deno, and browsers
  • Tree-shakeable — import only the endpoints you use for minimal bundle size

Versioning

The package version mirrors the targeted ThingsBoard server version, plus a prerelease tag that pins the exact upstream spec build:

MAJOR.MINOR.PATCH-<edition>.<build>.<client-revision>
  • MAJOR.MINOR.PATCH matches the TB server version exactly (e.g. 4.3.1).
  • <edition> is the TB edition the spec was sourced from: pe or ce.
  • <build> is TB's build counter — the trailing segment in TB's info.version (e.g. 4.3.1.1PE → 1).
  • <client-revision> increments for client-only changes (codegen tweaks, patch-script updates, helper additions) against the same upstream spec build. Starts at 0 per new spec fetch.

Bare versions like 4.3.1 are never published — every release carries the full prerelease tag, so the upstream source is always visible. Consumers using ^4.3.1 will not pick up our prereleases by accident; pin explicitly, e.g.:

"@enerlab/thingsboard-client": "4.3.1-pe.1.0"
EventVersion
First client built from TB 4.3.1.1PE4.3.1-pe.1.0
Client-only fix; same spec build4.3.1-pe.1.1
Re-fetched against TB 4.3.1.2PE4.3.1-pe.2.0
TB ships 4.3.2 (build 0, PE)4.3.2-pe.0.0

Supported TB versions / branch layout

main tracks the latest TB minor the package targets. When the package moves to a new TB minor, a release/<MAJOR>.<MINOR>.x branch is cut off main first; client-only fixes can then be backported onto that branch and published as new -N revisions for the older line. Currently only main exists — it targets TB 4.3.x.

Installation

pnpm add @enerlab/thingsboard-client

Quick Start

import { client, login, getDeviceByIdUsingGet } from '@enerlab/thingsboard-client'

// Configure the base URL
client.setConfig({
  baseUrl: 'https://your-thingsboard.com',
})

// Login — auto-stores the JWT token on the client
await login('[email protected]', 'tenant')

// Make type-safe API calls
const { data, error } = await getDeviceByIdUsingGet({
  path: { deviceId: 'your-device-id' },
})

if (error) {
  console.error('Failed to fetch device:', error)
} else {
  console.log('Device:', data.name)
}

Using an Existing Token

If you already have a JWT (e.g. from a previous session), set it directly:

import { client, getDeviceByIdUsingGet } from '@enerlab/thingsboard-client'

client.setConfig({
  baseUrl: 'https://your-thingsboard.com',
  auth: 'eyJ...',
})

const { data } = await getDeviceByIdUsingGet({
  path: { deviceId: 'your-device-id' },
})

Using a Separate Client Instance

For multiple ThingsBoard instances or isolated auth contexts, create a dedicated client:

import { createClient, createConfig, login, getDeviceByIdUsingGet } from '@enerlab/thingsboard-client'

const myClient = createClient(createConfig({
  baseUrl: 'https://other-thingsboard.com',
}))

await login('[email protected]', 'admin', { client: myClient })

const { data } = await getDeviceByIdUsingGet({
  path: { deviceId: 'device-id' },
  client: myClient,
})

Tree-Shaking

All SDK functions are exported as standalone, tree-shakeable functions. Your bundler will only include the endpoints you actually import, keeping your bundle small — especially important for frontend and mobile apps.

// Only getDeviceByIdUsingGet and its Zod validator end up in your bundle
import { getDeviceByIdUsingGet } from '@enerlab/thingsboard-client'

Zod Schemas

Import Zod schemas for runtime validation:

import { zDevice } from '@enerlab/thingsboard-client/zod'

// Validate external data
const result = zDevice.safeParse(unknownData)
if (result.success) {
  console.log('Valid device:', result.data.name)
}

Development

Prerequisites

  • Node.js 20+
  • pnpm 8+

Setup

# Install dependencies
pnpm install

# Download the ThingsBoard OpenAPI spec
# Default URL is https://demo.thingsboard.io (Community Edition).
# Override with THINGSBOARD_SPEC_URL to target a PE instance or a specific TB version.
pnpm fetch-spec

# Generate the TypeScript client
pnpm generate

# Type-check
pnpm typecheck

# Run tests
pnpm test

# Lint
pnpm lint

# Build
pnpm build

Updating the Spec

To update to a newer ThingsBoard API version:

# From demo.thingsboard.io (latest Community Edition)
pnpm fetch-spec

# Or from your own instance
THINGSBOARD_SPEC_URL=https://my-tb.example.com/v3/api-docs?group=thingsboard pnpm fetch-spec

# Regenerate the client
pnpm generate

Project Structure

thingsboard-client/
├── src/
│   ├── index.ts              # Package entry — re-exports everything
│   ├── auth.ts               # login() / logout() helpers
│   └── generated/            # Auto-generated by @hey-api/openapi-ts (gitignored)
│       ├── client.gen.ts     # Fetch client instance
│       ├── sdk.gen.ts        # SDK functions for every endpoint
│       ├── types.gen.ts      # TypeScript types
│       └── zod.gen.ts        # Zod schemas
├── tests/
│   ├── auth.test.ts          # login/logout tests
│   └── zod-contracts.test.ts # Zod schema contract tests
├── spec/
│   └── thingsboard-openapi.json  # Downloaded OpenAPI spec (committed)
├── scripts/
│   └── fetch-spec.ts         # Script to download the spec
├── vitest.config.ts
├── openapi-ts.config.ts      # @hey-api/openapi-ts configuration
├── tsconfig.json
└── package.json

ThingsBoard API Reference

The full API documentation is available at:

License

MIT

版本与发布记录

@enerlab/thingsboard-client 共发布 13 个版本,首次发布于 2026/03/03;过去 12 个月发布了 13 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.3.1-pe.1.5latest2026/10/2420 天前
4.3.1-pe.1.42026/06/035 个月前
4.3.1-pe.1.32026/05/305 个月前
4.3.1-pe.1.22026/05/215 个月前
0.5.12026/03/197 个月前
0.5.02026/03/177 个月前

关于 @enerlab/thingsboard-client 的常见问题

@enerlab/thingsboard-client 是什么?

Type-safe TypeScript client for the ThingsBoard REST API with Zod v4 runtime validation

如何安装 @enerlab/thingsboard-client?

在项目目录运行 npm install @enerlab/thingsboard-client,或使用 pnpm add / yarn add / bun add。

@enerlab/thingsboard-client 支持 TypeScript 吗?

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

@enerlab/thingsboard-client 支持 ESM 还是 CommonJS?

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

@enerlab/thingsboard-client 需要什么版本的 Node.js?

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

@enerlab/thingsboard-client 可以商用吗?

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

@enerlab/thingsboard-client 的下载量有多少?

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

@enerlab/thingsboard-client 还在维护吗?

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