@denosaurs/typefetch

中文文档
0.0.34发布于 2025/04/18MITES Module构建来源已验证近 30 天下载 5377

📤 Magically generate fetch types from OpenAPI schemas for zero-cost browser-native api clients

原始使用文档
文档目录5 节
  1. Usage
  2. Example
  3. Maintainers
  4. Contributing
  5. Licence

TypeFetch

I dream of proper meta-programming in the wild west of ECMAScript. A parallel universe where ECMAScript had types, macros, and a proper module system. Where modules work regardless of environment and runtime. Where interfacing with the system is standardized, and code from server runtimes work in the browser. But alas, we are stuck in this bad joke of a universe where TypeScript is the next best thing. So, I present to you, TypeFetch, a tool for generating zero-cost type-safe fetch clients from OpenAPI schemas. It works everywhere you can call fetch, and anywhere you can use TypeScript.

Usage

To use TypeFetch, you need to have an OpenAPI schema. You can either provide a local file path or a URL to the schema (which can be either json or yaml), then all you need to do is run the cli using deno, node, or any other JavaScript runtime supported by the jsr registry.

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)

Example

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

This will generate a typefetch.d.ts file in the current directory, which will modify the global scope and the type definitions for the fetch function and all types defined within the schema. For example:

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>>;
      })
    )
  >;
  ...
}

Which now allows you to use the fetch function with the https://api.jsr.io/packages endpoint as following:

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());
}

Maintainers

Contributing

Pull request, issues and feedback are very welcome. Code style is formatted with deno fmt and commit messages are done following Conventional Commits spec.

Please use it! And let us know if you have any issues, feedback or requests.

Licence

Copyright 2024, the Denosaurs team. All rights reserved. MIT license.

版本与发布记录

@denosaurs/typefetch 共发布 12 个版本,首次发布于 2024/09/24;过去 12 个月发布了 0 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.0.34latest2025/04/181.6 年前
0.0.332025/04/101.6 年前
0.0.322025/04/041.6 年前
0.0.312024/12/181.9 年前
0.0.302024/09/252 年前
0.0.292024/09/252 年前

关于 @denosaurs/typefetch 的常见问题

@denosaurs/typefetch 是什么?

📤 Magically generate fetch types from OpenAPI schemas for zero-cost browser-native api clients

如何安装 @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 和提交记录判断。