@querykitjs/web 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Suhrobbek Soatov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.en.md ADDED
@@ -0,0 +1,203 @@
1
+ <div align="right">
2
+
3
+ **English** · [O'zbekcha](./README.md)
4
+
5
+ </div>
6
+
7
+ # @querykitjs/web
8
+
9
+ > Typed **query-building** for frontends: build filter/sort/pagination payloads, map response meta, sync URL state. It **does not send requests** — you pass the payload to your own `fetch`/`axios`.
10
+
11
+ React dashboards hand-write the same boilerplate on every list page: build `IFilter[]` from `searchParams`, drop empty filters, `sortType` ↔ `{name,direction}`, "reset page on any change", snake→camel meta. `@querykitjs/web` removes all of it. The payload matches what the querykit backend (`@querykitjs/drizzle-pg`) accepts.
12
+
13
+ ```ts
14
+ import { buildListParams, f, mapMeta } from "@querykitjs/web";
15
+
16
+ const params = buildListParams({
17
+ filter: f.and(f.contains("buyerName", search), f.eq("status", status)),
18
+ sort: "-createdAt",
19
+ page,
20
+ perPage: 20,
21
+ });
22
+ const { data, meta } = (await http.post("/buyers/list", params)).data; // your own axios/fetch
23
+ setMeta(mapMeta(meta));
24
+ ```
25
+
26
+ ## Features
27
+
28
+ - **Two filter styles** — `f.eq()...` builder AND object/array (`{key, operation, value}`). Fully compatible with legacy `IFilter[]`.
29
+ - **Normalization** — empty-filter pruning, sort decoding. Values pass through unchanged.
30
+ - **Declarative schema** — `defineListSchema` maps URL params → filters; kills per-page hand-building.
31
+ - **URL-state sync** — `searchParams` ↔ params, `sortType` encode/decode, page-reset. `searchParams` is injected (router-agnostic).
32
+ - **React hook** — `useListParams` (peer `react`, `./react` subpath).
33
+ - **`mapMeta`** — snake→camel pagination meta.
34
+ - **Zero dependency** — no axios/react-query/react-router/dayjs. `react` is an optional peer, only for the hook.
35
+
36
+ ## Install
37
+
38
+ ```bash
39
+ bun add @querykitjs/web
40
+ # For the React hook, having react in your project (peer) is enough
41
+ ```
42
+
43
+ ## Filters — two styles
44
+
45
+ ```ts
46
+ import { createFilters } from "@querykitjs/web";
47
+ const f = createFilters<Buyer>(); // column-name autocomplete
48
+
49
+ // 1) Builder
50
+ filter: f.and(f.contains("buyerName", s), f.eq("status", st));
51
+
52
+ // 2) Object/array (like idistr) — a flat array is implicit AND
53
+ filter: [
54
+ { key: "buyerName", operation: "%_%", value: s },
55
+ { key: "status", operation: "=", value: st },
56
+ ];
57
+ ```
58
+
59
+ **Operators:** `= != > >= < <=`, `%_%`/`contains`, `%_`/`startsWith`, `_%`/`endsWith`, `like`, `ilike`, `notLike`, `in`, `notIn`, `between`, `isNull`, `isNotNull` (same set as the querykit backend). Builder helpers: `f.eq/ne/gt/gte/lt/lte`, `f.contains/startsWith/endsWith`, `f.like/ilike/notLike`, `f.in/notIn`, `f.between`, `f.range` (date range → two conditions), `f.isNull/isNotNull`, `f.and/or/not`.
60
+
61
+ ## Building the payload
62
+
63
+ ```ts
64
+ import { buildParams, buildListParams } from "@querykitjs/web";
65
+
66
+ const payload = buildListParams({
67
+ filter, // builder or array
68
+ sort: "-createdAt", // or { name, direction }
69
+ page,
70
+ perPage: 20,
71
+ with: { supervisor: true }, // relations (default `with`)
72
+ });
73
+ // -> { filter:[...pruned], sort:{name,direction}, columns, with, page, perPage }
74
+ ```
75
+
76
+ Empty filters are dropped (`""`/`null`/`undefined`/`[]`), but `0`/`false` are kept. Values are sent through unchanged.
77
+
78
+ ## Pagination modes (offset / infinite / cursor)
79
+
80
+ Three builders matching the backend's three modes. Each mode's response meta is **different**, so there's a separate mapper for each:
81
+
82
+ ```ts
83
+ import { buildListParams, buildInfiniteParams, buildCursorParams, mapMeta, mapInfiniteMeta, mapCursorMeta } from "@querykitjs/web";
84
+
85
+ // 1) Offset — page / perPage
86
+ const p = buildListParams({ filter, page: 2, perPage: 20 });
87
+ mapMeta(res.meta); // { totalPages, totalCount, currentPage, perPage, hasNext, hasPrev }
88
+
89
+ // 2) Infinite — limit / offset
90
+ const p = buildInfiniteParams({ filter, limit: 20, offset: 40 });
91
+ mapInfiniteMeta(res.meta); // { limit, offset, count, hasMore, nextOffset }
92
+
93
+ // 3) Cursor — limit / cursor / order / direction (sort is ignored)
94
+ const p = buildCursorParams({ filter, limit: 20, cursor, order: "asc" });
95
+ mapCursorMeta(res.meta); // { limit, hasNext, hasPrev, nextCursor, prevCursor }
96
+
97
+ // withDeleted — include soft-deleted rows
98
+ buildListParams({ filter, withDeleted: true });
99
+ ```
100
+
101
+ ## Declarative list schema
102
+
103
+ Declare once instead of hand-building `IFilter[]` on each page:
104
+
105
+ ```ts
106
+ import { defineListSchema, searchParamsToPayload } from "@querykitjs/web";
107
+
108
+ const buyersSchema = defineListSchema({
109
+ id: { operation: "=" },
110
+ buyerName: { operation: "%_%", trim: true },
111
+ status: { operation: "=" },
112
+ createdAt: { range: ["fromDate", "toDate"] }, // → >= and <=
113
+ });
114
+
115
+ const params = searchParamsToPayload(buyersSchema, searchParams);
116
+ ```
117
+
118
+ ## React hook
119
+
120
+ ```tsx
121
+ import { useSearchParams } from "react-router-dom"; // or any source
122
+ import { useListParams } from "@querykitjs/web/react";
123
+
124
+ function BuyersList() {
125
+ const [searchParams, setSearchParams] = useSearchParams();
126
+ const { params, setParam, setPage, setSort } = useListParams({
127
+ searchParams,
128
+ setSearchParams,
129
+ schema: buyersSchema,
130
+ });
131
+
132
+ const { data, meta } = useMyList(params); // your own fetch/react-query
133
+
134
+ // setParam("status", "active") -> updates URL, resets page
135
+ // setPage(2), setSort("-createdAt")
136
+ }
137
+ ```
138
+
139
+ `searchParams` (`URLSearchParams`) and `setSearchParams` are injected — the library is not tied to `react-router`.
140
+
141
+ ## You send the request
142
+
143
+ The library only handles the work **up to** the request. No transport:
144
+
145
+ ```ts
146
+ // fetch
147
+ const res = await fetch("/api/buyers/list", {
148
+ method: "POST",
149
+ headers: { "Content-Type": "application/json" },
150
+ body: JSON.stringify(params),
151
+ });
152
+ const { data, meta } = await res.json();
153
+
154
+ // axios
155
+ const { data } = await http.post("/buyers/list", params);
156
+ setMeta(mapMeta(data.meta));
157
+ ```
158
+
159
+ ## Other / legacy backends (custom field names)
160
+
161
+ Defaults are querykit-canonical camelCase (`perPage`, `with`). For a legacy snake-case backend (e.g. idistr), configure it:
162
+
163
+ ```ts
164
+ import { createQuery } from "@querykitjs/web";
165
+
166
+ const q = createQuery({ perPageField: "per_page", withField: "withPopulates" });
167
+ const params = q.list({ filter, page, perPage });
168
+ ```
169
+
170
+ ## API reference
171
+
172
+ | Function | Purpose |
173
+ | ---------------------------------------------- | -------------------------------------- |
174
+ | `createFilters<T>()` / `f` | typed / untyped filter builder |
175
+ | `buildParams(input)` | normalize params (no pagination) |
176
+ | `buildListParams(input)` | + `page`/`perPage` (offset) |
177
+ | `buildInfiniteParams(input)` | + `limit`/`offset` (infinite) |
178
+ | `buildCursorParams(input)` | + `limit`/`cursor`/`order`/`direction` |
179
+ | `createQuery(config)` | builder with custom field names |
180
+ | `defineListSchema(schema)` | URL param → filter descriptor |
181
+ | `schemaToFilter(schema, sp)` | searchParams → `FieldCondition[]` |
182
+ | `searchParamsToPayload(schema, sp)` | searchParams → full payload |
183
+ | `readListParams(schema, sp)` | searchParams → `ListParams` |
184
+ | `setParam/setPage/setSize/setSort/resetParams` | URL writes (immutable, page-reset) |
185
+ | `encodeSort/decodeSort` | `{name,direction}` ↔ `"-createdAt"` |
186
+ | `mapMeta(raw)` | offset meta → camel |
187
+ | `mapInfiniteMeta(raw)` | infinite meta → camel |
188
+ | `mapCursorMeta(raw)` | cursor meta → camel |
189
+ | `useListParams(opts)` (`/react`) | URL-sync hook |
190
+
191
+ ## Development
192
+
193
+ ```bash
194
+ bun install
195
+ bun run typecheck
196
+ bun run lint
197
+ bun run build # tsup -> dist (index + react, ESM + CJS + .d.ts)
198
+ bun run --filter @querykitjs/web test:smoke
199
+ ```
200
+
201
+ ## License
202
+
203
+ MIT © Suhrobbek Soatov
package/README.md ADDED
@@ -0,0 +1,203 @@
1
+ <div align="right">
2
+
3
+ [English](./README.en.md) · **O'zbekcha**
4
+
5
+ </div>
6
+
7
+ # @querykitjs/web
8
+
9
+ > Frontend uchun **tipli query-building**: filter/sort/pagination payloadini quradi, javob meta'sini map qiladi, URL-state sync beradi. **So'rov yubormaydi** — chiqqan payload'ni o'z `fetch`/`axios`ingizga uzatasiz.
10
+
11
+ React dashboardlar har list sahifasida bir xil boilerplate'ni qo'lda yozadi: `searchParams`'dan `IFilter[]` qurish, bo'sh filterlarni tashlash, `sortType` ↔ `{name,direction}`, "har o'zgarishda page-reset", meta snake→camel. `@querykitjs/web` shularni bartaraf qiladi. Payload querykit backend (`@querykitjs/drizzle-pg`) qabul qiladigan formatda.
12
+
13
+ ```ts
14
+ import { buildListParams, f, mapMeta } from "@querykitjs/web";
15
+
16
+ const params = buildListParams({
17
+ filter: f.and(f.contains("buyerName", search), f.eq("status", status)),
18
+ sort: "-createdAt",
19
+ page,
20
+ perPage: 20,
21
+ });
22
+ const { data, meta } = (await http.post("/buyers/list", params)).data; // o'z axios/fetch
23
+ setMeta(mapMeta(meta));
24
+ ```
25
+
26
+ ## Imkoniyatlar
27
+
28
+ - **Ikkala filter uslubi** — `f.eq()...` builder VA object/massiv (`{key, operation, value}`). Legacy `IFilter[]` bilan to'liq mos.
29
+ - **Normalizatsiya** — bo'sh filterlarni prune, sort decode. Qiymatlar o'zgartirilmasdan o'tadi.
30
+ - **Declarative schema** — `defineListSchema` bilan URL param → filter; har sahifadagi qo'lda qurishni yo'q qiladi.
31
+ - **URL-state sync** — `searchParams` ↔ params, `sortType` encode/decode, page-reset. `searchParams` tashqaridan olinadi (router-agnostik).
32
+ - **React hook** — `useListParams` (peer `react`, `./react` subpath).
33
+ - **`mapMeta`** — snake→camel pagination meta.
34
+ - **Zero dependency** — axios/react-query/react-router/dayjs YO'Q. `react` faqat hook uchun ixtiyoriy peer.
35
+
36
+ ## O'rnatish
37
+
38
+ ```bash
39
+ bun add @querykitjs/web
40
+ # React hook uchun loyihangizda react bo'lishi kifoya (peer)
41
+ ```
42
+
43
+ ## Filter — ikkala uslub
44
+
45
+ ```ts
46
+ import { createFilters } from "@querykitjs/web";
47
+ const f = createFilters<Buyer>(); // maydon nomi autocomplete
48
+
49
+ // 1) Builder
50
+ filter: f.and(f.contains("buyerName", s), f.eq("status", st));
51
+
52
+ // 2) Object/massiv (idistr kabi) — flat massiv = implicit AND
53
+ filter: [
54
+ { key: "buyerName", operation: "%_%", value: s },
55
+ { key: "status", operation: "=", value: st },
56
+ ];
57
+ ```
58
+
59
+ **Operatorlar:** `= != > >= < <=`, `%_%`/`contains`, `%_`/`startsWith`, `_%`/`endsWith`, `like`, `ilike`, `notLike`, `in`, `notIn`, `between`, `isNull`, `isNotNull` (querykit backend bilan bir xil to'plam). Builder helperlar: `f.eq/ne/gt/gte/lt/lte`, `f.contains/startsWith/endsWith`, `f.like/ilike/notLike`, `f.in/notIn`, `f.between`, `f.range` (sana diapazoni → ikki shart), `f.isNull/isNotNull`, `f.and/or/not`.
60
+
61
+ ## Payload qurish
62
+
63
+ ```ts
64
+ import { buildParams, buildListParams } from "@querykitjs/web";
65
+
66
+ const payload = buildListParams({
67
+ filter, // builder yoki massiv
68
+ sort: "-createdAt", // yoki { name, direction }
69
+ page,
70
+ perPage: 20,
71
+ with: { supervisor: true }, // relations (default `with`)
72
+ });
73
+ // -> { filter:[...pruned], sort:{name,direction}, columns, with, page, perPage }
74
+ ```
75
+
76
+ Bo'sh qiymatli filterlar tashlanadi (`""`/`null`/`undefined`/`[]`), lekin `0`/`false` saqlanadi. Qiymatlar o'zgartirilmasdan yuboriladi.
77
+
78
+ ## Paginatsiya rejimlari (offset / infinite / cursor)
79
+
80
+ Backend'ning 3 rejimiga mos 3 builder. Har birining javob meta'si **har xil**, shuning uchun alohida mapper bor:
81
+
82
+ ```ts
83
+ import { buildListParams, buildInfiniteParams, buildCursorParams, mapMeta, mapInfiniteMeta, mapCursorMeta } from "@querykitjs/web";
84
+
85
+ // 1) Offset — page / perPage
86
+ const p = buildListParams({ filter, page: 2, perPage: 20 });
87
+ mapMeta(res.meta); // { totalPages, totalCount, currentPage, perPage, hasNext, hasPrev }
88
+
89
+ // 2) Infinite — limit / offset
90
+ const p = buildInfiniteParams({ filter, limit: 20, offset: 40 });
91
+ mapInfiniteMeta(res.meta); // { limit, offset, count, hasMore, nextOffset }
92
+
93
+ // 3) Cursor — limit / cursor / order / direction (sort ishlatilmaydi)
94
+ const p = buildCursorParams({ filter, limit: 20, cursor, order: "asc" });
95
+ mapCursorMeta(res.meta); // { limit, hasNext, hasPrev, nextCursor, prevCursor }
96
+
97
+ // withDeleted — soft-delete'lilarni ham ko'rish
98
+ buildListParams({ filter, withDeleted: true });
99
+ ```
100
+
101
+ ## Declarative list schema
102
+
103
+ Har sahifada qo'lda `IFilter[]` qurish o'rniga bir marta e'lon qiling:
104
+
105
+ ```ts
106
+ import { defineListSchema, searchParamsToPayload } from "@querykitjs/web";
107
+
108
+ const buyersSchema = defineListSchema({
109
+ id: { operation: "=" },
110
+ buyerName: { operation: "%_%", trim: true },
111
+ status: { operation: "=" },
112
+ createdAt: { range: ["fromDate", "toDate"] }, // → >= va <=
113
+ });
114
+
115
+ const params = searchParamsToPayload(buyersSchema, searchParams);
116
+ ```
117
+
118
+ ## React hook
119
+
120
+ ```tsx
121
+ import { useSearchParams } from "react-router-dom"; // yoki boshqa manba
122
+ import { useListParams } from "@querykitjs/web/react";
123
+
124
+ function BuyersList() {
125
+ const [searchParams, setSearchParams] = useSearchParams();
126
+ const { params, setParam, setPage, setSort } = useListParams({
127
+ searchParams,
128
+ setSearchParams,
129
+ schema: buyersSchema,
130
+ });
131
+
132
+ const { data, meta } = useMyList(params); // o'z fetch/react-query
133
+
134
+ // setParam("status", "active") -> URL yangilanadi, page reset bo'ladi
135
+ // setPage(2), setSort("-createdAt")
136
+ }
137
+ ```
138
+
139
+ `searchParams` (`URLSearchParams`) va `setSearchParams` tashqaridan uzatiladi — kutubxona `react-router`ga bog'lanmaydi.
140
+
141
+ ## So'rovni o'zingiz yuborasiz
142
+
143
+ Kutubxona faqat **so'rovgacha** ishlaydi. Transport yo'q:
144
+
145
+ ```ts
146
+ // fetch
147
+ const res = await fetch("/api/buyers/list", {
148
+ method: "POST",
149
+ headers: { "Content-Type": "application/json" },
150
+ body: JSON.stringify(params),
151
+ });
152
+ const { data, meta } = await res.json();
153
+
154
+ // axios
155
+ const { data } = await http.post("/buyers/list", params);
156
+ setMeta(mapMeta(data.meta));
157
+ ```
158
+
159
+ ## Boshqa / legacy backend (custom field nomlari)
160
+
161
+ Default querykit-canonical camelCase (`perPage`, `with`). Eski snake-backend (masalan idistr) uchun sozlang:
162
+
163
+ ```ts
164
+ import { createQuery } from "@querykitjs/web";
165
+
166
+ const q = createQuery({ perPageField: "per_page", withField: "withPopulates" });
167
+ const params = q.list({ filter, page, perPage });
168
+ ```
169
+
170
+ ## API ma'lumotnoma
171
+
172
+ | Funksiya | Vazifasi |
173
+ | ---------------------------------------------- | -------------------------------------- |
174
+ | `createFilters<T>()` / `f` | tipli filter builder / tipsiz |
175
+ | `buildParams(input)` | params normalizatsiya (paginatsiyasiz) |
176
+ | `buildListParams(input)` | + `page`/`perPage` (offset) |
177
+ | `buildInfiniteParams(input)` | + `limit`/`offset` (infinite) |
178
+ | `buildCursorParams(input)` | + `limit`/`cursor`/`order`/`direction` |
179
+ | `createQuery(config)` | custom field nomlari bilan builder |
180
+ | `defineListSchema(schema)` | URL param → filter tavsifi |
181
+ | `schemaToFilter(schema, sp)` | searchParams → `FieldCondition[]` |
182
+ | `searchParamsToPayload(schema, sp)` | searchParams → to'liq payload |
183
+ | `readListParams(schema, sp)` | searchParams → `ListParams` |
184
+ | `setParam/setPage/setSize/setSort/resetParams` | URL yozish (immutable, page-reset) |
185
+ | `encodeSort/decodeSort` | `{name,direction}` ↔ `"-createdAt"` |
186
+ | `mapMeta(raw)` | offset meta → camel |
187
+ | `mapInfiniteMeta(raw)` | infinite meta → camel |
188
+ | `mapCursorMeta(raw)` | cursor meta → camel |
189
+ | `useListParams(opts)` (`/react`) | URL-sync hook |
190
+
191
+ ## Ishlab chiqish
192
+
193
+ ```bash
194
+ bun install
195
+ bun run typecheck
196
+ bun run lint
197
+ bun run build # tsup -> dist (index + react, ESM + CJS + .d.ts)
198
+ bun run --filter @querykitjs/web test:smoke
199
+ ```
200
+
201
+ ## Litsenziya
202
+
203
+ MIT © Suhrobbek Soatov