@dudousxd/nestjs-client

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

Framework-neutral runtime for nestjs-codegen output: typed fetcher with optional superjson transformer. No Inertia dependency.

原始使用文档
文档目录10 节
  1. Install
  2. Usage
  3. Bring your own axios instance
  4. Global headers
  5. superjson & transformer pipelines
  6. Errors
  7. The Fetcher contract
  8. How it fits with @dudousxd/nestjs-codegen
  9. Documentation
  10. License

@dudousxd/nestjs-client

The framework-neutral runtime for @dudousxd/nestjs-codegen output: a typed fetcher with pluggable transports and an optional superjson transformer. No Inertia dependency.

npm

The code generated by @dudousxd/nestjs-codegen (api.ts) imports the Fetcher type from this package and runs on a Fetcher instance you pass into createApi(fetcher). This package provides that fetcher: it owns URL building, headers, error mapping (ApiHttpError), and the payload transformer, while the actual network call goes through a pluggable transport (native fetch by default).

Because the generated client calls into it at runtime, this is a normal dependency — not a devDependency. The Fetcher interface is fully generic (get<T>(path, opts?): Promise<T>), and that typed surface is what lets the generated client and query options infer response types. A missing or any-typed fetcher silently degrades the whole generated client to any.

Install

pnpm add @dudousxd/nestjs-client

Usage

Create a fetcher, then hand it to the createApi exported by your generated api.ts:

import { createFetcher } from '@dudousxd/nestjs-client';
import { createApi } from './api'; // generated by @dudousxd/nestjs-codegen

const fetcher = createFetcher({
  baseUrl: '/api',
  headers: () => ({ authorization: `Bearer ${getToken()}` }),
});

const api = createApi(fetcher);

The default transport is native fetch. The headers function is called once per request, so dynamic auth tokens always stay current.

Bring your own axios instance

Pass axiosTransport(http) to reuse an existing axios instance with its own interceptors, auth, and withCredentials:

import axios from 'axios';
import { createFetcher, axiosTransport } from '@dudousxd/nestjs-client';

const http = axios.create({ baseURL: '/api', withCredentials: true });

const fetcher = createFetcher({ transport: axiosTransport(http) });

Set the base URL on the axios instance (not createFetcher's baseUrl) so it isn't prefixed twice.

Global headers

setGlobalHeaders registers a function applied to every request across all fetchers — handy for app-wide auth or tracing headers:

import { setGlobalHeaders } from '@dudousxd/nestjs-client';

setGlobalHeaders(() => ({ authorization: `Bearer ${getToken()}` }));

superjson & transformer pipelines

A transformer is a { stringify, parse } pair. Pass superjson to round-trip rich types (Date, Map, Set, BigInt) — the server must use the same transformer:

import superjson from 'superjson';
import { createFetcher } from '@dudousxd/nestjs-client';

const fetcher = createFetcher({ transformer: superjson });

Pass an array to compose a pipeline with composeTransformers: a base value↔string serializer first, then string↔string wrappers (compression, encryption) applied left-to-right on stringify and unwound right-to-left on parse:

import superjson from 'superjson';
import { createFetcher } from '@dudousxd/nestjs-client';
import { compress } from './my-compress'; // { stringify, parse } over strings

const fetcher = createFetcher({ transformer: [superjson, compress] });

Errors

Non-2xx responses reject with an ApiHttpError carrying status, statusText, and the parsed body, plus isUnauthorized / isForbidden / isNotFound / isClient / isServer helpers:

import { ApiHttpError } from '@dudousxd/nestjs-client';

try {
  await api.users.get({ params: { id } });
} catch (err) {
  if (err instanceof ApiHttpError && err.isNotFound) {
    // handle 404
  }
}

The Fetcher contract

The generated client depends on this typed surface — keep T generic so response types flow through:

interface Fetcher {
  get<T>(path: string, opts?: RequestOpts): Promise<T>;
  post<T>(path: string, opts?: RequestOpts): Promise<T>;
  put<T>(path: string, opts?: RequestOpts): Promise<T>;
  patch<T>(path: string, opts?: RequestOpts): Promise<T>;
  delete<T>(path: string, opts?: RequestOpts): Promise<T>;
}

createFetcher returns a value of this exact shape. Any object that satisfies the Fetcher interface works — which is why frameworks with their own client can supply a compatible createFetcher (see below).

How it fits with @dudousxd/nestjs-codegen

@dudousxd/nestjs-codegen is the codegen half — it runs in dev/CI and emits a fully typed api.ts from your NestJS controllers. That generated file imports the Fetcher type from this package and exposes a createApi(fetcher) factory.

@dudousxd/nestjs-client is the runtime half — the dependency the generated client actually executes against. You install it in your app, build a fetcher with createFetcher, and pass it to createApi.

Frameworks that ship their own client (for example @dudousxd/nestjs-inertia-client) expose a compatible createFetcher that produces a Fetcher-shaped object, so the same generated client runs unchanged on top of them.

Documentation

License

MIT

版本与发布记录

@dudousxd/nestjs-client 共发布 15 个版本,首次发布于 2026/06/11;过去 12 个月发布了 15 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.10.0latest2026/11/122 天前
0.9.22026/09/301 个月前
0.9.12026/09/301 个月前
0.9.02026/09/291 个月前
0.8.12026/08/292 个月前
0.8.02026/07/223 个月前

关于 @dudousxd/nestjs-client 的常见问题

@dudousxd/nestjs-client 是什么?

Framework-neutral runtime for nestjs-codegen output: typed fetcher with optional superjson transformer. No Inertia dependency.

如何安装 @dudousxd/nestjs-client?

在项目目录运行 npm install @dudousxd/nestjs-client,或使用 pnpm add / yarn add / bun add。

@dudousxd/nestjs-client 支持 TypeScript 吗?

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

@dudousxd/nestjs-client 支持 ESM 还是 CommonJS?

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

@dudousxd/nestjs-client 需要什么版本的 Node.js?

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

@dudousxd/nestjs-client 可以商用吗?

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

@dudousxd/nestjs-client 的下载量有多少?

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

@dudousxd/nestjs-client 还在维护吗?

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