@dugjason/front-node

中文文档
0.6.0发布于 2026/11/10MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 2530

Modern Typescript SDK for the Front.com API

原始使用文档
文档目录6 节
  1. Install
  2. Usage
  3. Development
  4. Release
  5. Rate limits
  6. Custom fetch

front-node

A modern TypeScript SDK for the Front API.

Install

npm install @dugjason/front-node

Usage

import { Front } from "@dugjason/front-node";

const front = new Front({ apiKey: process.env.FRONT_API_TOKEN });

const teammates = await front.teammates.list();

// ID-scoped operations do not require fetching the resource first.
await front.channels.createMessage("cha_123", {
  body: "Hello from Front",
  options: { archive: true },
  to: ["[email protected]"],
});
await front.conversations.addTag("cnv_123", { tag_ids: ["tag_123"] });
await front.messages.markSeen("msg_123");

Credentials are exclusive: pass { apiKey } or { accessToken, refreshToken }, never both. Automatic OAuth refresh defaults to false. To refresh manually, call refreshOAuthToken:

const front = new Front({
  accessToken,
  refreshToken,
  onTokenRefresh: async (tokens) => {
    await persistTokens(tokens.access_token, tokens.refresh_token);
  },
});

const tokens = await front.refreshOAuthToken({
  clientId: process.env.FRONT_CLIENT_ID,
  clientSecret: process.env.FRONT_CLIENT_SECRET,
});

refreshOAuthToken() awaits onTokenRefresh before updating the client's credentials and returning the tokens. The callback receives the same access_token and refresh_token fields as the return value. If persistence fails, the refresh rejects and the client keeps its previous credentials. Front may already have rotated the refresh token, so recover the persistence failure before attempting another refresh.

To enable automatic refresh, provide the OAuth application's clientId and clientSecret in the constructor:

const front = new Front({
  accessToken,
  refreshToken,
  autoRefresh: true,
  clientId,
  clientSecret,
  onTokenRefresh: async (tokens) => {
    await persistTokens(tokens.access_token, tokens.refresh_token);
  },
});

With autoRefresh: true, an API 401 triggers refreshOAuthToken(). The client awaits onTokenRefresh, adopts the new tokens, and retries the original request once. A second 401 throws FrontApiError without another refresh. Other HTTP statuses do not trigger refresh. These rules apply to JSON requests and raw responses, including attachment downloads.

Concurrent 401s share one token exchange and save callback per client. If another request has already replaced the failed request's token, that request retries with the current token. This coordination does not extend across client instances or manually initiated refresh calls.

Token exchange failures propagate as FrontApiError with the token endpoint's status, headers, and parsed body. For example, body.error === "invalid_grant" identifies a rejected refresh token; a 5xx response preserves the provider failure. Network errors and save callback errors propagate unchanged. Neither failure is replaced by the original API 401, and neither causes a request retry. A failed save leaves the client's credentials unchanged, even though Front may already have rotated the refresh token.

API-key clients do not automatically refresh. OAuth clients with autoRefresh omitted or set to false keep the manual behavior above.

Development

bun install
bun test
bun run check

Release

Releases are managed by Changesets. Add a changeset for user-facing changes:

bun run changeset

Merging to main opens or updates a version PR. Merging that version PR publishes to npm as @dugjason/front-node through npm trusted publishing.

Rate limits

HTTP 429 responses retry twice by default, waiting for Retry-After seconds plus 0–999 ms of jitter. If absent, retries use X-RateLimit-Reset. Invalid delays or delays over 60 seconds use 60 seconds. This includes raw responses and OAuth token requests.

Disable SDK retries when your application handles them:

const front = new Front({ apiKey: "...", rateLimitRetry: false });

Disabled or exhausted retries throw FrontRateLimitError, extending FrontApiError with retryAfterMs before jitter. Use error instanceof FrontRateLimitError to handle it; the final response metadata remains available.

Custom fetch

You can use the library with a custom fetch library, compatible with node:fetch. An illustrative example could be a fetch implementation with built-in tracing or custom telemetry;

import { Front } from "@dugjason/front-node";

const loggingFetch: typeof fetch = async (input, init) => {
  const t0 = performance.now();
  const response = await globalThis.fetch(input, init);
  console.log(
    `${init?.method ?? "GET"} ${input} ${response.status} ${Math.round(performance.now() - t0)}ms`,
  );
  return response;
};
const front = new Front({ fetch: loggingFetch });

版本与发布记录

@dugjason/front-node 共发布 16 个版本,首次发布于 2025/07/02;过去 12 个月发布了 10 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.6.0latest2026/11/103 天前
0.5.02026/10/2420 天前
0.4.02026/10/041 个月前
0.3.02026/10/041 个月前
0.2.12026/09/141 个月前
0.2.02026/09/022 个月前

关于 @dugjason/front-node 的常见问题

@dugjason/front-node 是什么?

Modern Typescript SDK for the Front.com API

如何安装 @dugjason/front-node?

在项目目录运行 npm install @dugjason/front-node,或使用 pnpm add / yarn add / bun add。

@dugjason/front-node 支持 TypeScript 吗?

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

@dugjason/front-node 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@dugjason/front-node 可以商用吗?

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

@dugjason/front-node 的下载量有多少?

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

@dugjason/front-node 还在维护吗?

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