# @critical-path/server 中文文档

规范地址：https://npmcn.com/package/%40critical-path/server/

版本：0\.37\.1
npm 元数据更新时间：2026-11-05T14:29:43.495Z
中文译文更新时间：2026-11-11T01:04:19.475Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40critical-path/server
原文对照：https://npmcn.com/package/%40critical-path/server/?lang=original
项目主页：https://github.com/Pixerate/critical-path#readme
源码仓库：https://github.com/Pixerate/critical-path

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：204 kB，28 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- Node.js 要求：\>=24\.0\.0
- 运行依赖：2 个
- 首次发布：2026-08-24T12:22:41.795Z
- 最新版本发布：2026-11-05T14:29:43.123Z
- 近 30 天下载量：3589（2026-10-11 至 2026-11-09，npm 官方下载统计）
- 已发布版本数：82（近 12 个月 82 个）

## 中文简介

Web 获取 Next.js App 路由器、 SvelteKit 和 Express 的 API 兼容服务器路由器和适配器

## 安装

```sh
npm install @critical-path/server
```

也可以使用：`pnpm add @critical-path/server`、`yarn add @critical-path/server`、`bun add @critical-path/server`

## README

# @ 关键路径/服务器

> **Web获取关键路径兼容的API服务器路由器和框架适配器.**

> **升级?** 升级一次 `@critical-path/*` 组合在一起。 编码代理: 遵循 [更新(md)](https://github.com/Pixerate/critical-path/blob/main/UPGRADING.md); 已标记断开的更改 **刹车** 此软件包中 `CHANGELOG.md`。 。 。 .

`@critical-path/server` 桥标准 Web 获取 API `Request` 财务报告和审定财务报表 `Response` 对象为 `@critical-path/core`,提供统包适配器 **Next.js App路由器** 财务报告和审定财务报表 **斯维尔特基特**。 。 。 .

* * *

## \* 安装

```bash
npm install @critical-path/core @critical-path/server
# or
pnpm add @critical-path/core @critical-path/server
```

* * *

## \\\\ next.js App 路由器集成

文件 : `app/api/critical-path/[...path]/route.ts`

```ts
import { createNextHandler } from '@critical-path/server';
import { SQLiteStore } from '@critical-path/core';

export const { GET, POST, PUT, PATCH, DELETE, OPTIONS } = createNextHandler({
  store: new SQLiteStore({ filename: 'critical-path.db' })
});

// Equivalent: the handler is also callable, so `export { handler as GET, ... }` works too.
```

* * *

## SvelteKit 集成系统

文件 : `src/routes/api/critical-path/[...path]/+server.ts`

```ts
import { createSvelteKitHandler } from '@critical-path/server';

const handler = createSvelteKitHandler();

export const GET = handler.GET;
export const POST = handler.POST;
export const PUT = handler.PUT;
export const PATCH = handler.PATCH;
export const DELETE = handler.DELETE;
export const OPTIONS = handler.OPTIONS;
```

* * *

## QQ 认证, CORS 和挂载路径

路由器选项是第二个参数 `createNextHandler`, (中文(简体) ), `createSvelteKitHandler`, (中文(简体) ), `createUniversalHandler` 财务报告和审定财务报表 `new CriticalPathRouter(config, options)`数字 :

| 选项 | 默认 | 说明 |
| :-- | :-- | :-- |
| `getContext(request)` | 无 | 解决呼叫者( R)`{ userId, userName?, actorType?, tenantId?, roles? }`) (中文(简体) ). `tenantId` 财务报告和审定财务报表 `roles` 给引擎的租户隔离 `authorize` 政策。 每个突变都归于该用户,或 `anonymous` (单位:千美元)`ANONYMOUS_ACTOR`当没有解决用户时。 请求人从不携带身份 对SvelteKit来说,它收到 `RequestEvent`这样 `event.locals` 已提供. |
| `requireAuth` | `false` | 返回时 `401` 除非 `getContext` 产量a `userId`。 。 。 . `OPTIONS` 飞行前总是允许的. |
| `maxBodyBytes` | 10个小米B | 接受请求最多的机构; 较大的机构得到 `413` (在流出时强制执行,而不仅仅是通过) `Content-Length`) (中文(简体) ). |
| `basePath` | 脱到第一 `/critical-path` | 精确挂载路径, 例如 . `/api/pm`. 外部请求返回 `404`。 。 。 . |
| `cors` | `false` (无CORS信头) | `{ origins: string[] | '*', credentials?, allowHeaders?, maxAge? }`,或 `false` 对于没有CORS头. `credentials` 无法与 `'*'`。 。 。 . |
| `onError`, (中文(简体) ), `exposeErrors` | 见下文 | 意外错误报告 . |

```ts
export const { GET, POST, PUT, PATCH, DELETE, OPTIONS } = createNextHandler(
  { store },
  {
    getContext: async () => {
      const session = await auth();
      return session?.user ? { userId: session.user.id } : null;
    },
    requireAuth: true,
    cors: { origins: ['https://app.example.com'], credentials: true } // omit for same-origin apps
  }
);
```

用于其它获取运行时间( 工人、 Deno、 Bun、 Hono) , 使用 `createUniversalHandler(config, options)`返回时 `(request) => Promise<Response>`。 。 。 .

* * *

## & OpenAPI 请求验证

每个请求体都用Zod的图解 `@critical-path/core/schemas` 在到达引擎前:

-   错误类型或缺少所需的字段返回 `400` 与 `issues: [{ path, message }]`。 。 。 .
-   机构严格: 服务器指定字段( Q)`id`, (中文(简体) ), `createdAt`, (中文(简体) ), `updatedAt`,任务 `key`,拥有 `projectId` 关于更新的),身份字段(`actorId`, (中文(简体) ), `authorId`, (中文(简体) ), `userId`, (中文(简体) ), `uploaderId`) 和未知密钥被拒绝 `400`,所以一个 `PATCH` 无法将任务移动到另一个工程或重写时间戳.
-   评论、反应、附件和时间条目的作者是解答者,或 `anonymous`删除与 `DELETE /comments/:id/reactions?emoji=👍`。 。 。 .

Webhooks 的管理地点为 `GET/POST /webhooks` 财务报告和审定财务报表 `GET/PATCH/DELETE /webhooks/:id` (单位:千美元)`workspace.manage` (根据角色政策). `POST` 返回签名 `secret` 一次 一次吗.

`GET /openapi.json` 为 OpenAPI 3.1 文档服务,其请求机构来自同一计划(位于与其他路径相同的认证之后)。 它包括插件路由,由它们描述 `openapi` 元数据(或 `hidden: true` 以略去一个)。 静态地发表:

```ts
import { buildOpenApiDocument } from '@critical-path/server';

const doc = buildOpenApiDocument({ serverUrl: 'https://app.example.com/api/critical-path', pluginRoutes: engine.plugins.getRoutes() });
```

* * *

## QQ 错误反应

| 状态 | 何时 |
| :-- | :-- |
| `400` | 错误的 JSON, 无效的正文( 有 `issues`, (中文(简体) ), `ValidationError`、工作流程过渡或自定义字段验证失败 |
| `401` | `requireAuth` 设置, 没有解决用户 |
| `403` | 发动机的 `authorize` 政策拒绝采取行动(`ForbiddenError`(中文(简体) ) |
| `404` | 未知的路由( 路径必须精确匹配; 附加段从不会掉入母路径), `NotFoundError`,或 `DELETE` 资源不存在(成功删除返回) `{ "success": true }`(中文(简体) ) |
| `405` | 已知路径, 错误的方法( 有 `Allow` 页眉) |
| `409` | `CircularDependencyError` (单位包括 `cyclePath`(中文(简体) ) |
| `413` | 请求体积大于 `maxBodyBytes` |
| `415` | 非空体未作为 `application/json` (区块跨站台) |
| `500` | 意外错误 。 身体总是这样 `{ "error": "Internal Server Error" }`; 已登录真实错误 `console.error`。 。 。 . |

`OPTIONS` 飞行前请求返回 `204` 和CORS头.

### 报告并曝光出不预期错误

两个适配器(和 `new CriticalPathRouter(config, options)`) 接受路由器选项作为第二个参数:

```ts
export const { GET, POST, PUT, PATCH, DELETE, OPTIONS } = createNextHandler(
  { store },
  {
    // Called only for unexpected (500) errors. Return a Response to replace the default.
    onError: (error, request) => {
      Sentry.captureException(error, { extra: { url: request.url } });
    },
    // Include real error messages in 500 responses.
    // Defaults to true only when NODE_ENV === 'development'.
    exposeErrors: false
  }
);
```

无 `onError`中,将输入出乎意料的错误 `console.error`. 预期错误(400、404、409)始终包含其信息,永远无法达到 `onError`。 。 。 .

* * *

## 许可证

麻省理工学院 [关键路径](https://github.com/Pixerate/Critical-Path)

## 常见问题

### @critical-path/server 是什么？

Web 获取 Next\.js App 路由器、 SvelteKit 和 Express 的 API 兼容服务器路由器和适配器

### 如何安装 @critical-path/server？

在项目目录运行 npm install @critical-path/server，或使用 pnpm add / yarn add / bun add。

### @critical-path/server 支持 TypeScript 吗？

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

### @critical-path/server 支持 ESM 还是 CommonJS？

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

### @critical-path/server 需要什么版本的 Node\.js？

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

### @critical-path/server 可以商用吗？

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

### @critical-path/server 的下载量有多少？

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

### @critical-path/server 还在维护吗？

最新版本 0\.37\.1 发布于 2026/11/05（5 天前）；至今共发布 82 个版本，其中过去 12 个月发布了 82 个；首次发布于 2026/08/24。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

