# @denosaurs/typefetch 中文文档

规范地址：https://npmcn.com/package/%40denosaurs/typefetch/

版本：0\.0\.34
npm 元数据更新时间：2025-04-17T20:09:52.605Z
中文译文更新时间：2026-11-11T22:47:31.680Z
软件包许可：MIT

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

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

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

- 包体积（解压后）：567 kB，376 个文件
- 模块格式：ES Module
- TypeScript 类型：未声明
- 运行依赖：4 个
- 首次发布：2024-09-24T12:34:03.665Z
- 最新版本发布：2025-04-17T20:09:52.210Z
- 近 30 天下载量：5377（2026-10-12 至 2026-11-10，npm 官方下载统计）
- 已发布版本数：12（近 12 个月 0 个）

## 中文简介

魔法生成 `fetch` 来自 OpenAPI 的零成本浏览器本地端客户端类型

## 安装

```sh
npm install @denosaurs/typefetch
```

也可以使用：`pnpm add @denosaurs/typefetch`、`yarn add @denosaurs/typefetch`、`bun add @denosaurs/typefetch`

## README

# 类型

我梦想着在ECMAScript西部的野外进行适当的元编程. 平行 eMAScript拥有类型、宏和适当的模块系统的宇宙。 何处 模块不管环境和运行时间。 和我们之间 系统是标准化的,来自服务器运行时间的代码在浏览器中工作. 不过 唉,我们被困在一个糟糕的笑话 宇宙中TypeScript是下一个 最好的东西。 所以,我介绍给你,TypeFatch, 一个零成本生成工具 类型安全 `fetch` openAPI的客户端计划. 随处可见 `fetch`,可以在任何地方使用 TypeScript .

## 使用量

要使用TypeFetch,需要有一个OpenAPI schema. 您可以提供 本地文件路径或 URL 到 schema( 可以是 json 或 yaml) , 那么 您只需要使用 deno、 节点或任何其他 JavaScript 运行 cli 运行时间 [日语](https://jsr.io/) 登记.

```sh
deno run -A jsr:@denosaurs/typefetch
```

```
Usage: typefetch [OPTIONS] <PATH>

Options:
  -h, --help                           Print this help message
  -V, --version                        Print the version of TypeFetch
  -o, --output    <PATH>               Output file path                                                   (default: typefetch.d.ts)
      --config    <PATH>               File path to the tsconfig.json file
      --import    <PATH>               Import path for TypeFetch                                          (default: https://raw.githubusercontent.com/denosaurs/typefetch/main)
      --base-urls <URLS>               A comma separated list of custom base urls for paths to start with
      --include-server-urls            Include server URLs from the schema in the generated paths         (default: true)
      --include-absolute-url           Include absolute URLs in the generated paths                       (default: false)
      --include-relative-url           Include relative URLs in the generated paths                       (default: false)
      --experimental-urlsearchparams   Enable the experimental fully typed URLSearchParams type           (default: false)
```

## 示例

```sh
deno run -A jsr:@denosaurs/typefetch https://api.jsr.io/.well-known/openapi
```

这将生成一个 `typefetch.d.ts` 在当前目录中的文件,它将 修改全球范围和 `fetch` 函数和 计划中定义的所有类型。 例如:

```ts
declare global {
  ...
  /**
   * Returns a list of packages
   * @summary List packages
   */
  function fetch(
    input: `https://api.jsr.io/packages` | `/packages`,
    init?: Omit<RequestInit, "method" | "body" | "headers"> & {
      method: "GET";
    },
  ): Promise<
    & Omit<
      Response,
      "ok" | "status" | "arrayBuffer" | "blob" | "formData" | "json" | "text"
    >
    & (
      | ({
        ok: true;
        status: 200;
        json(): Promise<{ items: Package[]; total: number }>;
        text(): Promise<JSONString<{ items: Package[]; total: number }>>;
      })
      | ({
        ok: false;
        status: 400;
        json(): Promise<Error>;
        text(): Promise<JSONString<Error>>;
      })
    )
  >;
  ...
}
```

现在允许您使用 `fetch` 函数与 `https://api.jsr.io/packages` 终点如下:

```ts
const response = await fetch("https://api.jsr.io/packages");

// The ok and status properties work as discriminators here to narrow the
// types of the response. That way the json and text methods are made
// type-safe.
if (response.ok) {
  console.assert(response.status === 200);

  const { items, total } = await response.json();
  // even this works because we augment the global JSON object!
  const { items, total } = JSON.parse(await response.text());
} else {
  console.assert(response.status === 400);

  const error = await response.json();
  // or
  const error = JSON.parse(await response.text());
}
```

## 维护者

-   伊莱亚斯·斯约格林(英语:Elias Sjögreen (U[@eliassjogreen (英语)](https://github.com/eliassjogreen)(中文(简体) )

## 捐款

拉出请求、问题和反馈是非常受欢迎的。 代码样式格式化 `deno fmt` 并按常规提交信息.

请用! 如果你有任何问题、反馈或要求,请告诉我们.

## 许可证

版权2024 德诺瑟尔团队. 版权所有。 麻省理工学院执照.

## 常见问题

### @denosaurs/typefetch 是什么？

魔法生成 fetch 来自 OpenAPI 的零成本浏览器本地端客户端类型

### 如何安装 @denosaurs/typefetch？

在项目目录运行 npm install @denosaurs/typefetch，或使用 pnpm add / yarn add / bun add。它还提供命令行工具 typefetch，可以用 npx typefetch 直接运行。

### @denosaurs/typefetch 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/denosaurs\_\_typefetch 是否存在。

### @denosaurs/typefetch 支持 ESM 还是 CommonJS？

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

### @denosaurs/typefetch 可以商用吗？

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

### @denosaurs/typefetch 的下载量有多少？

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

### @denosaurs/typefetch 还在维护吗？

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

