@dudousxd/nestjs-filter-client

中文文档
2.0.1发布于 2026/11/12MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 2566

Client-side query builder for @dudousxd/nestjs-filter.

原始使用文档
文档目录12 节
  1. Installation
  2. Usage
  3. Convenience methods
  4. Composing with OR / AND
  5. Query string output
  6. Using with fetch
  7. Using as POST body
  8. API
  9. Types
  10. UnpagedFilterQuery — the query minus the page
  11. ColumnFilterClause — predicates and group-only clauses
  12. License

@dudousxd/nestjs-filter-client

Client-side query builder for @dudousxd/nestjs-filter. Zero dependencies, runs in browser and Node.js.

Installation

npm install @dudousxd/nestjs-filter-client

Usage

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

// Build a filter query
const query = filterQuery()
  .where('name', 'contains', 'fleet')
  .where('status', ['COMPLETED', 'FAILED'])
  .where('age', 'gte', 18)
  .build();
// → { where: [
//     { field: 'name', operator: 'contains', value: 'fleet' },
//     { field: 'status', operator: 'in', value: ['COMPLETED', 'FAILED'] },
//     { field: 'age', operator: 'gte', value: 18 },
//   ] }

Convenience methods

filterQuery()
  .equals('status', 'active')
  .contains('name', 'fleet')
  .in('role', ['admin', 'editor'])
  .between('age', 18, 65)
  .gte('createdAt', '2026-01-01')
  .isNull('deletedAt')
  .set('page', 1)
  .set('size', 25)
  .build();

Composing with OR / AND

filterQuery()
  .where('status', 'active')
  .or(q => q
    .where('name', 'contains', 'sync')
    .where('email', 'contains', 'sync')
  )
  .build();

Query string output

const qs = filterQuery()
  .where('name', 'contains', 'fleet')
  .where('status', ['COMPLETED', 'FAILED'])
  .toQueryString();
// → "name[contains]=fleet&status[]=COMPLETED&status[]=FAILED"

Using with fetch

const qs = filterQuery()
  .contains('name', 'fleet')
  .gte('createdAt', '2026-01-01')
  .set('page', 1)
  .toQueryString();

const res = await fetch(`/api/users?${qs}`);

Using as POST body

const body = filterQuery()
  .where('name', 'contains', 'fleet')
  .or(q => q
    .where('role', 'admin')
    .where('role', 'editor')
  )
  .build();

const res = await fetch('/api/users', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify(body),
});

API

MethodDescription
where(field, value)Equals filter (arrays auto-detect as in)
where(field, operator, value)Filter with explicit operator
equals(field, value)Shorthand for equals
contains(field, value)Shorthand for contains
in(field, values)Shorthand for in
between(field, low, high)Shorthand for between
gt / gte / lt / lteComparison shorthands
isNull / isNotNullNull check shorthands
isEmpty / isNotEmptyEmpty check shorthands
startsWith / endsWithString match shorthands
or(callback)OR group
and(callback)AND group
set(key, value)Add extra keys (e.g. page, size)
build()Returns FilterQueryResult object
toQueryString()Returns URL query string
toFlatObject()Returns flat object for auto-fields

Types

TypeWhat it is
FilterQueryResultWhat build() returns: the whole envelope, paging included
UnpagedFilterQueryThe same envelope without paginate — FilterQueryResult extends it
ColumnFilterOne predicate: field + operator (+ value)
ColumnFilterGroupA pure boolean group: { OR: [...] } / { AND: [...] }, no field, no operator
ColumnFilterClauseColumnFilter | ColumnFilterGroup — what one entry of filter.where may be

UnpagedFilterQuery — the query minus the page

An export that hands the paging window to the server (a CSV export, a report), a count-only request, a prefetch, or a cache key all want the query that says which rows without saying which slice. That is UnpagedFilterQuery:

import type { UnpagedFilterQuery } from '@dudousxd/nestjs-filter-client';

function exportBody(query: UnpagedFilterQuery) {
  return JSON.stringify(query); // the server decides how much it streams
}

const { paginate, ...unpaged } = filterQuery().contains('name', 'fleet').page(0, 25).build();
exportBody(unpaged);

Do not reach for Omit<FilterQueryResult, 'paginate'> instead. FilterQueryResult carries [key: string]: unknown so set() extras survive, and Omit rebuilds a type from keyof — which for an index-signature type is just string | number. The Omit therefore collapses to { [x: string]: unknown }: every named key gone, every typo accepted, and no compile error to tell you. UnpagedFilterQuery is declared as the base FilterQueryResult extends, so the two cannot drift.

ColumnFilterClause — predicates and group-only clauses

A clause is either a predicate or a group that only composes other clauses. Both are valid on the wire (the server's validator has an explicit group-node branch), so both are typed:

import type { ColumnFilterClause } from '@dudousxd/nestjs-filter-client';

const where: ColumnFilterClause[] = [
  { field: 'status', operator: 'equals', value: 'active' },   // predicate
  { OR: [                                                      // group — no field, no operator
    { field: 'name', operator: 'contains', value: 'sync' },
    { field: 'email', operator: 'contains', value: 'sync' },
  ] },
];

ColumnFilter itself is unchanged and still requires field and operator: a group is a separate member of the union rather than a loosening, so { field: 'status', value: 'active' } — a predicate whose operator was forgotten — is still a compile error.

License

MIT

版本与发布记录

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

版本发布日期距今
2.0.1latest2026/11/121 天前
1.37.02026/11/084 天前
1.33.02026/09/042 个月前
1.32.02026/09/032 个月前
1.27.02026/08/063 个月前
1.26.02026/08/013 个月前

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

@dudousxd/nestjs-filter-client 是什么?

Client-side query builder for @dudousxd/nestjs-filter.

如何安装 @dudousxd/nestjs-filter-client?

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

@dudousxd/nestjs-filter-client 支持 TypeScript 吗?

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

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

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

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

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

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

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

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

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

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

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