# 工作站 HTTP 接入

让现有业务系统提供模板、草稿和授权，使用条件请求防止覆盖其他人的修改。

## 连接已有服务

服务端的 `handleOFDHttpRequest` 接收标准 Fetch `Request`，默认挂载点为 `/ofd`。它使用宿主为当前登录者提供的目录、草稿存储和可选队列。鉴权、院区或租户隔离、持久化和审计归宿主所有。前端可用 `createOFDHttpDraftStore` 等适配器连接同一地址。

### recipes/workstation.ts

```ts
import { handleOFDHttpRequest, type OFDHttpServices } from "@quirekit/ofd-editor/http";

/**
 * Installs the OFD routes behind the host's existing session and access policy.
 * @param servicesFor - Resolves services restricted to this caller and tenant.
 * @returns A Fetch handler mounted at /ofd/.
 */
export function workstationHandler(servicesFor: (request: Request) => Promise<OFDHttpServices | undefined>) {
  return async (request: Request): Promise<Response> => {
    const services = await servicesFor(request);
    if (!services) return new Response(null, { status: 403 });
    return handleOFDHttpRequest(request, services, "/ofd");
  };
}

```

<a href="/downloads/workstations.openapi.json" download="workstations.openapi.json">下载工作站 OpenAPI JSON</a>。契约里的 `127.0.0.1:8998/ofd` 是仓库预览的合成业务服务。部署时替换为自己的地址。官网静态示例提供本地文档操作。

## 从版本化模板生成 OFD

| 步骤 | 请求 | 结果 |
| --- | --- | --- |
| 选择模板 | `GET /ofd/templates` | 在可见条目中选择 `id` 和 `revision` |
| 查询字段 | `GET /ofd/templates/{id}/fields` | 标量字段、明细组与 v3 填写示例 |
| 预检填写 | `POST /ofd/templates/{id}/preflight` | 在生成前核对数据与目标字段 |
| 生成文件 | `POST /ofd/templates/{id}/draft` | `201` 和 `application/ofd` 文件字节 |
| 登记业务草稿 | `PUT /ofd/drafts/{id}?name=report.ofd` | 保存 OFD 字节并返回该业务记录的版本 |

模板的字段、预检和生成请求传同一强 `If-Match`。例如目录修订为 `1` 时，头部写作 `If-Match: "1"`。JSON 写入使用 `Content-Type: application/json`，登记草稿使用 `application/ofd`。首次按业务 ID 登记用 `If-None-Match: *`，再次保存用打开该草稿时的强 ETag。

## 修改已保存的草稿

先 `GET /ofd/drafts/{id}`，再用该草稿版本调用 `POST /ofd/drafts/{id}/preflight` 和 `POST /ofd/drafts/{id}/fill`。必需的条件头缺失会得到 `428`，版本已经变化会得到 `412`。重新读取并预检后再确认变更。批量操作使用 `batch-preflight` 和 `batch-drafts`，逐条读取结果。[统一填写数据](/integration/data)
