@querykitjs/web 3.0.0 → 4.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/README.en.md +130 -31
- package/README.md +130 -31
- package/dist/index.cjs +1 -355
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +39 -3
- package/dist/index.d.ts +39 -3
- package/dist/index.js +1 -331
- package/dist/index.js.map +1 -1
- package/dist/react/index.cjs +1 -147
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.d.cts +11 -4
- package/dist/react/index.d.ts +11 -4
- package/dist/react/index.js +1 -144
- package/dist/react/index.js.map +1 -1
- package/dist/{registry-DbZQk28q.d.cts → registry-DOVi_zCU.d.cts} +78 -11
- package/dist/{registry-DbZQk28q.d.ts → registry-DOVi_zCU.d.ts} +78 -11
- package/package.json +39 -15
- package/skills/querykit-web/SKILL.md +181 -0
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { S as Sort, C as CursorParams, a as CursorPayload, I as InfiniteParams, b as InfinitePayload, L as ListParams, c as ListPayload, P as Params, Q as QueryPayload, F as Filter, d as FieldCondition, e as FilterNode, f as SortInput } from './registry-
|
|
2
|
-
export { A as AdapterName, g as AndGroup, h as CursorMeta, i as CursorResult, D as DrizzleWith, j as FieldDescriptor, k as FieldKey, l as FilterOperator, m as FilterScalar, n as FilterValue, o as InfiniteMeta, p as InfiniteResult, q as ListResult, r as ListSchema, M as Meta, s as MongoosePopulate, N as NotGroup, O as OrGroup, t as PrismaInclude, R as RawCursorMeta, u as RawInfiniteMeta, v as RawMeta, w as Registry, x as RegistryConfig, y as RegistryDefaults, z as
|
|
1
|
+
import { S as Sort, C as CursorParams, a as CursorPayload, I as InfiniteParams, b as InfinitePayload, L as ListParams, c as ListPayload, P as Params, Q as QueryPayload, F as Filter, d as FieldCondition, e as FilterNode, f as SortInput } from './registry-DOVi_zCU.cjs';
|
|
2
|
+
export { A as AdapterName, g as AndGroup, h as CursorMeta, i as CursorResult, D as DrizzleWith, j as FieldDescriptor, k as FieldKey, l as FilterOperator, m as FilterScalar, n as FilterValue, o as InfiniteMeta, p as InfiniteResult, q as ListResult, r as ListSchema, M as Meta, s as MongoosePopulate, N as NotGroup, O as OrGroup, t as PrismaInclude, R as RawCursorMeta, u as RawInfiniteMeta, v as RawMeta, w as Registry, x as RegistryConfig, y as RegistryDefaults, z as RegistryTimestamps, B as Resource, E as ResourceCursorParams, G as ResourceInfiniteParams, H as ResourceKeys, J as ResourceListParams, K as ResourceParams, T as SearchOptions, U as SortDirection, V as SortItem, W as UrlConfig, X as UrlLimits, Y as UrlListParams, Z as UrlListPayload, _ as WithFor, $ as createFilters, a0 as createRegistry, a1 as decodeSort, a2 as defineListSchema, a3 as encodeSort, a4 as readListParams, a5 as resetParams, a6 as schemaToFilter, a7 as searchParamsToPayload, a8 as setPage, a9 as setParam, aa as setSize, ab as setSort } from './registry-DOVi_zCU.cjs';
|
|
3
|
+
import { SkippedCondition } from '@querykitjs/core';
|
|
3
4
|
export { f } from '@querykitjs/core';
|
|
4
5
|
|
|
5
6
|
/** {@link createQuery} sozlamalari — wire field nomlari va default qiymatlar. */
|
|
@@ -9,6 +10,7 @@ interface QueryConfig {
|
|
|
9
10
|
columnsField?: string;
|
|
10
11
|
withField?: string;
|
|
11
12
|
withDeletedField?: string;
|
|
13
|
+
withTotalField?: string;
|
|
12
14
|
pageField?: string;
|
|
13
15
|
perPageField?: string;
|
|
14
16
|
limitField?: string;
|
|
@@ -16,6 +18,22 @@ interface QueryConfig {
|
|
|
16
18
|
cursorField?: string;
|
|
17
19
|
defaultPerPage?: number;
|
|
18
20
|
defaultLimit?: number;
|
|
21
|
+
/**
|
|
22
|
+
* `perPage` yuqori chegarasi (core `DEFAULT_MAX_PER_PAGE` = 200) — backend
|
|
23
|
+
* repositorylari va `@querykitjs/zod` factory'lari bilan **bir xil manba**,
|
|
24
|
+
* shuning uchun front rad etiladigan so'rovni umuman yubormaydi.
|
|
25
|
+
*/
|
|
26
|
+
maxPerPage?: number;
|
|
27
|
+
/** `limit` yuqori chegarasi (core `DEFAULT_MAX_LIMIT` = 200). */
|
|
28
|
+
maxLimit?: number;
|
|
29
|
+
/**
|
|
30
|
+
* Sahifa chuqurligi: list'da `(page - 1) × perPage`, infinite'da `offset` (core
|
|
31
|
+
* `DEFAULT_MAX_OFFSET` = 10 000). Oshsa builder `Error` tashlaydi — jim kesish
|
|
32
|
+
* so'rov ma'nosini o'zgartirardi. Chuqurroq sahifalar — cursor.
|
|
33
|
+
*/
|
|
34
|
+
maxOffset?: number;
|
|
35
|
+
/** `sort` maydonlari soni (core `DEFAULT_MAX_SORT_FIELDS` = 3). Oshsa — `Error`. */
|
|
36
|
+
maxSortFields?: number;
|
|
19
37
|
defaultSort?: Sort;
|
|
20
38
|
/** Bo'sh qiymatli filterlarni tashlash (default `true`). */
|
|
21
39
|
pruneEmpty?: boolean;
|
|
@@ -87,4 +105,22 @@ declare function buildInfiniteParams(input?: InfiniteParams): InfinitePayload;
|
|
|
87
105
|
*/
|
|
88
106
|
declare function buildCursorParams(input?: CursorParams): CursorPayload;
|
|
89
107
|
|
|
90
|
-
|
|
108
|
+
/**
|
|
109
|
+
* The `QueryKitError.info` shape a backend may put in its 400 response body. The
|
|
110
|
+
* response envelope is the project's own; web only types this part.
|
|
111
|
+
*/
|
|
112
|
+
type QueryKitErrorBody = Pick<SkippedCondition, "site" | "key" | "reason"> & Partial<Pick<SkippedCondition, "source" | "operation">>;
|
|
113
|
+
/**
|
|
114
|
+
* Is `body` a querykit error (`reason`, `site`, `key`)? Lets the frontend tell a
|
|
115
|
+
* rejected query (forbidden key, limit exceeded, …) from any other 400.
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```ts
|
|
119
|
+
* const body = error.response?.data?.error;
|
|
120
|
+
* if (isQueryKitError(body) && body.reason === "forbidden-key") showFieldError(body.key);
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
declare function isQueryKitError(body: unknown): body is QueryKitErrorBody;
|
|
124
|
+
|
|
125
|
+
export { CursorParams, CursorPayload, FieldCondition, Filter, FilterNode, InfiniteParams, InfinitePayload, ListParams, ListPayload, Params, QueryPayload, Sort, SortInput, buildCursorParams, buildInfiniteParams, buildListParams, buildParams, createQuery, isQueryKitError, normalizeFilter, normalizeSort };
|
|
126
|
+
export type { QueryConfig, QueryKitErrorBody };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { S as Sort, C as CursorParams, a as CursorPayload, I as InfiniteParams, b as InfinitePayload, L as ListParams, c as ListPayload, P as Params, Q as QueryPayload, F as Filter, d as FieldCondition, e as FilterNode, f as SortInput } from './registry-
|
|
2
|
-
export { A as AdapterName, g as AndGroup, h as CursorMeta, i as CursorResult, D as DrizzleWith, j as FieldDescriptor, k as FieldKey, l as FilterOperator, m as FilterScalar, n as FilterValue, o as InfiniteMeta, p as InfiniteResult, q as ListResult, r as ListSchema, M as Meta, s as MongoosePopulate, N as NotGroup, O as OrGroup, t as PrismaInclude, R as RawCursorMeta, u as RawInfiniteMeta, v as RawMeta, w as Registry, x as RegistryConfig, y as RegistryDefaults, z as
|
|
1
|
+
import { S as Sort, C as CursorParams, a as CursorPayload, I as InfiniteParams, b as InfinitePayload, L as ListParams, c as ListPayload, P as Params, Q as QueryPayload, F as Filter, d as FieldCondition, e as FilterNode, f as SortInput } from './registry-DOVi_zCU.js';
|
|
2
|
+
export { A as AdapterName, g as AndGroup, h as CursorMeta, i as CursorResult, D as DrizzleWith, j as FieldDescriptor, k as FieldKey, l as FilterOperator, m as FilterScalar, n as FilterValue, o as InfiniteMeta, p as InfiniteResult, q as ListResult, r as ListSchema, M as Meta, s as MongoosePopulate, N as NotGroup, O as OrGroup, t as PrismaInclude, R as RawCursorMeta, u as RawInfiniteMeta, v as RawMeta, w as Registry, x as RegistryConfig, y as RegistryDefaults, z as RegistryTimestamps, B as Resource, E as ResourceCursorParams, G as ResourceInfiniteParams, H as ResourceKeys, J as ResourceListParams, K as ResourceParams, T as SearchOptions, U as SortDirection, V as SortItem, W as UrlConfig, X as UrlLimits, Y as UrlListParams, Z as UrlListPayload, _ as WithFor, $ as createFilters, a0 as createRegistry, a1 as decodeSort, a2 as defineListSchema, a3 as encodeSort, a4 as readListParams, a5 as resetParams, a6 as schemaToFilter, a7 as searchParamsToPayload, a8 as setPage, a9 as setParam, aa as setSize, ab as setSort } from './registry-DOVi_zCU.js';
|
|
3
|
+
import { SkippedCondition } from '@querykitjs/core';
|
|
3
4
|
export { f } from '@querykitjs/core';
|
|
4
5
|
|
|
5
6
|
/** {@link createQuery} sozlamalari — wire field nomlari va default qiymatlar. */
|
|
@@ -9,6 +10,7 @@ interface QueryConfig {
|
|
|
9
10
|
columnsField?: string;
|
|
10
11
|
withField?: string;
|
|
11
12
|
withDeletedField?: string;
|
|
13
|
+
withTotalField?: string;
|
|
12
14
|
pageField?: string;
|
|
13
15
|
perPageField?: string;
|
|
14
16
|
limitField?: string;
|
|
@@ -16,6 +18,22 @@ interface QueryConfig {
|
|
|
16
18
|
cursorField?: string;
|
|
17
19
|
defaultPerPage?: number;
|
|
18
20
|
defaultLimit?: number;
|
|
21
|
+
/**
|
|
22
|
+
* `perPage` yuqori chegarasi (core `DEFAULT_MAX_PER_PAGE` = 200) — backend
|
|
23
|
+
* repositorylari va `@querykitjs/zod` factory'lari bilan **bir xil manba**,
|
|
24
|
+
* shuning uchun front rad etiladigan so'rovni umuman yubormaydi.
|
|
25
|
+
*/
|
|
26
|
+
maxPerPage?: number;
|
|
27
|
+
/** `limit` yuqori chegarasi (core `DEFAULT_MAX_LIMIT` = 200). */
|
|
28
|
+
maxLimit?: number;
|
|
29
|
+
/**
|
|
30
|
+
* Sahifa chuqurligi: list'da `(page - 1) × perPage`, infinite'da `offset` (core
|
|
31
|
+
* `DEFAULT_MAX_OFFSET` = 10 000). Oshsa builder `Error` tashlaydi — jim kesish
|
|
32
|
+
* so'rov ma'nosini o'zgartirardi. Chuqurroq sahifalar — cursor.
|
|
33
|
+
*/
|
|
34
|
+
maxOffset?: number;
|
|
35
|
+
/** `sort` maydonlari soni (core `DEFAULT_MAX_SORT_FIELDS` = 3). Oshsa — `Error`. */
|
|
36
|
+
maxSortFields?: number;
|
|
19
37
|
defaultSort?: Sort;
|
|
20
38
|
/** Bo'sh qiymatli filterlarni tashlash (default `true`). */
|
|
21
39
|
pruneEmpty?: boolean;
|
|
@@ -87,4 +105,22 @@ declare function buildInfiniteParams(input?: InfiniteParams): InfinitePayload;
|
|
|
87
105
|
*/
|
|
88
106
|
declare function buildCursorParams(input?: CursorParams): CursorPayload;
|
|
89
107
|
|
|
90
|
-
|
|
108
|
+
/**
|
|
109
|
+
* The `QueryKitError.info` shape a backend may put in its 400 response body. The
|
|
110
|
+
* response envelope is the project's own; web only types this part.
|
|
111
|
+
*/
|
|
112
|
+
type QueryKitErrorBody = Pick<SkippedCondition, "site" | "key" | "reason"> & Partial<Pick<SkippedCondition, "source" | "operation">>;
|
|
113
|
+
/**
|
|
114
|
+
* Is `body` a querykit error (`reason`, `site`, `key`)? Lets the frontend tell a
|
|
115
|
+
* rejected query (forbidden key, limit exceeded, …) from any other 400.
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```ts
|
|
119
|
+
* const body = error.response?.data?.error;
|
|
120
|
+
* if (isQueryKitError(body) && body.reason === "forbidden-key") showFieldError(body.key);
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
declare function isQueryKitError(body: unknown): body is QueryKitErrorBody;
|
|
124
|
+
|
|
125
|
+
export { CursorParams, CursorPayload, FieldCondition, Filter, FilterNode, InfiniteParams, InfinitePayload, ListParams, ListPayload, Params, QueryPayload, Sort, SortInput, buildCursorParams, buildInfiniteParams, buildListParams, buildParams, createQuery, isQueryKitError, normalizeFilter, normalizeSort };
|
|
126
|
+
export type { QueryConfig, QueryKitErrorBody };
|
package/dist/index.js
CHANGED
|
@@ -1,332 +1,2 @@
|
|
|
1
|
-
import { createFilters
|
|
2
|
-
export { f } from '@querykitjs/core';
|
|
3
|
-
|
|
4
|
-
// src/filters.ts
|
|
5
|
-
function createFilters() {
|
|
6
|
-
return createFilters$1();
|
|
7
|
-
}
|
|
8
|
-
var DEFAULTS = {
|
|
9
|
-
filterField: "filter",
|
|
10
|
-
sortField: "sort",
|
|
11
|
-
columnsField: "columns",
|
|
12
|
-
withField: "with",
|
|
13
|
-
withDeletedField: "withDeleted",
|
|
14
|
-
pageField: "page",
|
|
15
|
-
perPageField: "perPage",
|
|
16
|
-
limitField: "limit",
|
|
17
|
-
offsetField: "offset",
|
|
18
|
-
cursorField: "cursor",
|
|
19
|
-
defaultPerPage: DEFAULT_PER_PAGE,
|
|
20
|
-
defaultLimit: DEFAULT_LIMIT,
|
|
21
|
-
defaultSort: [{ key: "createdAt", direction: "desc" }],
|
|
22
|
-
pruneEmpty: true
|
|
23
|
-
};
|
|
24
|
-
function isEmptyValue(value) {
|
|
25
|
-
if (value === void 0 || value === null || value === "") return true;
|
|
26
|
-
if (Array.isArray(value) && value.length === 0) return true;
|
|
27
|
-
return false;
|
|
28
|
-
}
|
|
29
|
-
function toSortItem(item) {
|
|
30
|
-
if (typeof item === "string") {
|
|
31
|
-
const desc = item.startsWith("-");
|
|
32
|
-
const key = (desc ? item.slice(1) : item).trim();
|
|
33
|
-
return key ? { key, direction: desc ? "desc" : "asc" } : void 0;
|
|
34
|
-
}
|
|
35
|
-
return item && item.key ? { key: item.key, direction: item.direction ?? "asc" } : void 0;
|
|
36
|
-
}
|
|
37
|
-
function normalizeSort(input, fallback) {
|
|
38
|
-
if (!input || input.length === 0) return fallback;
|
|
39
|
-
const items = input.map(toSortItem).filter((i) => i !== void 0);
|
|
40
|
-
return items.length ? items : fallback;
|
|
41
|
-
}
|
|
42
|
-
function normalizeCondition(condition, prune) {
|
|
43
|
-
const operation = condition.operation ?? "=";
|
|
44
|
-
const noValue = operation === "isNull" || operation === "isNotNull";
|
|
45
|
-
const value = condition.value;
|
|
46
|
-
if (!noValue && prune && isEmptyValue(value)) return void 0;
|
|
47
|
-
const out = { key: condition.key, operation };
|
|
48
|
-
if (!noValue) out.value = value;
|
|
49
|
-
return out;
|
|
50
|
-
}
|
|
51
|
-
function normalizeNode(node, prune) {
|
|
52
|
-
if ("and" in node) {
|
|
53
|
-
const kids = node.and.map((n) => normalizeNode(n, prune)).filter((n) => !!n);
|
|
54
|
-
return kids.length ? { and: kids } : void 0;
|
|
55
|
-
}
|
|
56
|
-
if ("or" in node) {
|
|
57
|
-
const kids = node.or.map((n) => normalizeNode(n, prune)).filter((n) => !!n);
|
|
58
|
-
return kids.length ? { or: kids } : void 0;
|
|
59
|
-
}
|
|
60
|
-
if ("not" in node) {
|
|
61
|
-
const inner = normalizeNode(node.not, prune);
|
|
62
|
-
return inner ? { not: inner } : void 0;
|
|
63
|
-
}
|
|
64
|
-
return normalizeCondition(node, prune);
|
|
65
|
-
}
|
|
66
|
-
function normalizeFilter(filter, prune) {
|
|
67
|
-
if (!filter) return [];
|
|
68
|
-
if (Array.isArray(filter)) {
|
|
69
|
-
return filter.map((c) => normalizeCondition(c, prune)).filter((c) => !!c);
|
|
70
|
-
}
|
|
71
|
-
return normalizeNode(filter, prune) ?? [];
|
|
72
|
-
}
|
|
73
|
-
function buildBase(input, cfg) {
|
|
74
|
-
const base = {
|
|
75
|
-
[cfg.filterField]: normalizeFilter(input.filter, cfg.pruneEmpty),
|
|
76
|
-
[cfg.columnsField]: input.columns ?? {},
|
|
77
|
-
[cfg.withField]: input.with ?? {}
|
|
78
|
-
};
|
|
79
|
-
if (input.withDeleted !== void 0) base[cfg.withDeletedField] = input.withDeleted;
|
|
80
|
-
return base;
|
|
81
|
-
}
|
|
82
|
-
function clampInt(value, fallback) {
|
|
83
|
-
return value === void 0 || value < 1 || Number.isNaN(value) ? fallback : Math.trunc(value);
|
|
84
|
-
}
|
|
85
|
-
function build(input, cfg) {
|
|
86
|
-
return { ...buildBase(input, cfg), [cfg.sortField]: normalizeSort(input.sort, cfg.defaultSort) };
|
|
87
|
-
}
|
|
88
|
-
function buildList(input, cfg) {
|
|
89
|
-
return {
|
|
90
|
-
...build(input, cfg),
|
|
91
|
-
[cfg.pageField]: clampInt(input.page, 1),
|
|
92
|
-
// page >= 1 (kasr/0/manfiy → 1)
|
|
93
|
-
[cfg.perPageField]: clampInt(input.perPage, cfg.defaultPerPage)
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
function buildInfinite(input, cfg) {
|
|
97
|
-
return {
|
|
98
|
-
...build(input, cfg),
|
|
99
|
-
[cfg.limitField]: clampInt(input.limit, cfg.defaultLimit),
|
|
100
|
-
[cfg.offsetField]: input.offset && input.offset > 0 ? Math.trunc(input.offset) : 0
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
function buildCursor(input, cfg) {
|
|
104
|
-
const payload = {
|
|
105
|
-
...buildBase(input, cfg),
|
|
106
|
-
[cfg.limitField]: clampInt(input.limit, cfg.defaultLimit),
|
|
107
|
-
[cfg.cursorField]: input.cursor ?? null,
|
|
108
|
-
order: input.order ?? "asc",
|
|
109
|
-
direction: input.direction ?? "forward"
|
|
110
|
-
};
|
|
111
|
-
if (input.cursorKey !== void 0) payload.cursorKey = input.cursorKey;
|
|
112
|
-
return payload;
|
|
113
|
-
}
|
|
114
|
-
function createQuery(config = {}) {
|
|
115
|
-
const cfg = { ...DEFAULTS, ...config };
|
|
116
|
-
return {
|
|
117
|
-
params: (input = {}) => build(input, cfg),
|
|
118
|
-
list: (input = {}) => buildList(input, cfg),
|
|
119
|
-
infinite: (input = {}) => buildInfinite(input, cfg),
|
|
120
|
-
cursor: (input = {}) => buildCursor(input, cfg)
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
function buildParams(input = {}) {
|
|
124
|
-
return build(input, DEFAULTS);
|
|
125
|
-
}
|
|
126
|
-
function buildListParams(input = {}) {
|
|
127
|
-
return buildList(input, DEFAULTS);
|
|
128
|
-
}
|
|
129
|
-
function buildInfiniteParams(input = {}) {
|
|
130
|
-
return buildInfinite(input, DEFAULTS);
|
|
131
|
-
}
|
|
132
|
-
function buildCursorParams(input = {}) {
|
|
133
|
-
return buildCursor(input, DEFAULTS);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// src/meta.ts
|
|
137
|
-
function mapMeta(raw) {
|
|
138
|
-
return {
|
|
139
|
-
totalPages: raw?.total_pages ?? 0,
|
|
140
|
-
totalCount: raw?.total_items ?? 0,
|
|
141
|
-
currentPage: raw?.current_page ?? 0,
|
|
142
|
-
perPage: raw?.per_page ?? 0,
|
|
143
|
-
hasNext: raw?.has_next ?? false,
|
|
144
|
-
hasPrev: raw?.has_prev ?? false
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
function mapInfiniteMeta(raw) {
|
|
148
|
-
return {
|
|
149
|
-
limit: raw?.limit ?? 0,
|
|
150
|
-
offset: raw?.offset ?? 0,
|
|
151
|
-
count: raw?.count ?? 0,
|
|
152
|
-
hasMore: raw?.has_more ?? false,
|
|
153
|
-
nextOffset: raw?.next_offset ?? null
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
function mapCursorMeta(raw) {
|
|
157
|
-
return {
|
|
158
|
-
limit: raw?.limit ?? 0,
|
|
159
|
-
hasNext: raw?.has_next ?? false,
|
|
160
|
-
hasPrev: raw?.has_prev ?? false,
|
|
161
|
-
nextCursor: raw?.next_cursor ?? null,
|
|
162
|
-
prevCursor: raw?.prev_cursor ?? null
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// src/schema.ts
|
|
167
|
-
function defineListSchema(schema) {
|
|
168
|
-
return schema;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// src/url.ts
|
|
172
|
-
var URL_DEFAULTS = {
|
|
173
|
-
pageParam: "page",
|
|
174
|
-
sizeParam: "size",
|
|
175
|
-
sortParam: "sortType"
|
|
176
|
-
};
|
|
177
|
-
function decodeSort(value) {
|
|
178
|
-
if (!value) return void 0;
|
|
179
|
-
const items = value.split(",").map((s) => s.trim()).filter(Boolean).map((tok) => {
|
|
180
|
-
const desc = tok.startsWith("-");
|
|
181
|
-
return { key: desc ? tok.slice(1) : tok, direction: desc ? "desc" : "asc" };
|
|
182
|
-
}).filter((i) => i.key);
|
|
183
|
-
return items.length ? items : void 0;
|
|
184
|
-
}
|
|
185
|
-
function encodeSort(sort) {
|
|
186
|
-
return sort.map((i) => typeof i === "string" ? i.trim() : i && i.key ? (i.direction === "desc" ? "-" : "") + i.key : "").filter(Boolean).join(",");
|
|
187
|
-
}
|
|
188
|
-
function schemaToFilter(schema, searchParams) {
|
|
189
|
-
const conditions = [];
|
|
190
|
-
const groups = [];
|
|
191
|
-
for (const [param, d] of Object.entries(schema)) {
|
|
192
|
-
const key = d.key ?? param;
|
|
193
|
-
if (d.range) {
|
|
194
|
-
const from = searchParams.get(d.range[0]);
|
|
195
|
-
const to = searchParams.get(d.range[1]);
|
|
196
|
-
if (from) conditions.push(cond(key, ">=", from));
|
|
197
|
-
if (to) conditions.push(cond(key, "<=", to));
|
|
198
|
-
continue;
|
|
199
|
-
}
|
|
200
|
-
if (d.between) {
|
|
201
|
-
const from = searchParams.get(d.between[0]);
|
|
202
|
-
const to = searchParams.get(d.between[1]);
|
|
203
|
-
if (from && to) conditions.push(cond(key, "between", [from, to]));
|
|
204
|
-
continue;
|
|
205
|
-
}
|
|
206
|
-
const raw = searchParams.get(param);
|
|
207
|
-
let value = raw !== null ? raw : d.default;
|
|
208
|
-
if (typeof value === "string" && d.trim) value = value.trim();
|
|
209
|
-
if (value === void 0 || value === null || value === "") continue;
|
|
210
|
-
if (Array.isArray(value) && value.length === 0) continue;
|
|
211
|
-
if (d.search && typeof value === "string") {
|
|
212
|
-
groups.push({ or: d.search.map((field) => cond(field, "%_%", value)) });
|
|
213
|
-
continue;
|
|
214
|
-
}
|
|
215
|
-
if (d.split && typeof value === "string") {
|
|
216
|
-
const delimiter = d.split === true ? "," : d.split;
|
|
217
|
-
const parts = value.split(delimiter).map((s) => s.trim()).filter(Boolean);
|
|
218
|
-
if (parts.length === 0) continue;
|
|
219
|
-
value = parts;
|
|
220
|
-
}
|
|
221
|
-
conditions.push(cond(key, d.operation ?? "=", value));
|
|
222
|
-
}
|
|
223
|
-
return groups.length === 0 ? conditions : { and: [...groups, ...conditions] };
|
|
224
|
-
}
|
|
225
|
-
function cond(key, operation, value) {
|
|
226
|
-
return { key, operation, value };
|
|
227
|
-
}
|
|
228
|
-
function readListParams(schema, searchParams, url = {}) {
|
|
229
|
-
const cfg = { ...URL_DEFAULTS, ...url };
|
|
230
|
-
const size = searchParams.get(cfg.sizeParam);
|
|
231
|
-
return {
|
|
232
|
-
filter: schemaToFilter(schema, searchParams),
|
|
233
|
-
sort: decodeSort(searchParams.get(cfg.sortParam)),
|
|
234
|
-
page: Number(searchParams.get(cfg.pageParam)) || 1,
|
|
235
|
-
perPage: size ? Number(size) : void 0
|
|
236
|
-
};
|
|
237
|
-
}
|
|
238
|
-
function searchParamsToPayload(schema, searchParams, url) {
|
|
239
|
-
return buildListParams(readListParams(schema, searchParams, url));
|
|
240
|
-
}
|
|
241
|
-
function clone(searchParams) {
|
|
242
|
-
return new URLSearchParams(searchParams);
|
|
243
|
-
}
|
|
244
|
-
function setParam(searchParams, name, value, url = {}) {
|
|
245
|
-
const cfg = { ...URL_DEFAULTS, ...url };
|
|
246
|
-
const next = clone(searchParams);
|
|
247
|
-
next.delete(cfg.pageParam);
|
|
248
|
-
if (value === null || value === void 0 || value === "") next.delete(name);
|
|
249
|
-
else next.set(name, value);
|
|
250
|
-
return next;
|
|
251
|
-
}
|
|
252
|
-
function setPage(searchParams, page, url = {}) {
|
|
253
|
-
const cfg = { ...URL_DEFAULTS, ...url };
|
|
254
|
-
const next = clone(searchParams);
|
|
255
|
-
next.set(cfg.pageParam, String(page));
|
|
256
|
-
return next;
|
|
257
|
-
}
|
|
258
|
-
function setSize(searchParams, size, url = {}) {
|
|
259
|
-
const cfg = { ...URL_DEFAULTS, ...url };
|
|
260
|
-
const next = clone(searchParams);
|
|
261
|
-
next.delete(cfg.pageParam);
|
|
262
|
-
next.set(cfg.sizeParam, String(size));
|
|
263
|
-
return next;
|
|
264
|
-
}
|
|
265
|
-
function setSort(searchParams, sort, url = {}) {
|
|
266
|
-
const cfg = { ...URL_DEFAULTS, ...url };
|
|
267
|
-
const next = clone(searchParams);
|
|
268
|
-
next.delete(cfg.pageParam);
|
|
269
|
-
const encoded = encodeSort(sort);
|
|
270
|
-
if (encoded) next.set(cfg.sortParam, encoded);
|
|
271
|
-
else next.delete(cfg.sortParam);
|
|
272
|
-
return next;
|
|
273
|
-
}
|
|
274
|
-
function resetParams(searchParams, keep = []) {
|
|
275
|
-
const next = new URLSearchParams();
|
|
276
|
-
for (const key of keep) {
|
|
277
|
-
const value = searchParams.get(key);
|
|
278
|
-
if (value !== null) next.set(key, value);
|
|
279
|
-
}
|
|
280
|
-
return next;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
// src/registry.ts
|
|
284
|
-
function createRegistry(config) {
|
|
285
|
-
const defaults = config.defaults ?? {};
|
|
286
|
-
const sort = normalizeSort(defaults.sort, []);
|
|
287
|
-
const cursorOrder = defaults.cursor?.order ?? "asc";
|
|
288
|
-
const qcfg = {};
|
|
289
|
-
if (defaults.perPage !== void 0) qcfg.defaultPerPage = defaults.perPage;
|
|
290
|
-
if (defaults.limit !== void 0) qcfg.defaultLimit = defaults.limit;
|
|
291
|
-
if (sort.length) qcfg.defaultSort = sort;
|
|
292
|
-
if (config.pruneEmpty !== void 0) qcfg.pruneEmpty = config.pruneEmpty;
|
|
293
|
-
const q = createQuery(qcfg);
|
|
294
|
-
const parseList = (raw) => ({ data: raw?.data ?? [], meta: mapMeta(raw?.meta) });
|
|
295
|
-
const parseInfinite = (raw) => ({
|
|
296
|
-
data: raw?.data ?? [],
|
|
297
|
-
meta: mapInfiniteMeta(raw?.meta)
|
|
298
|
-
});
|
|
299
|
-
const parseCursor = (raw) => ({
|
|
300
|
-
data: raw?.data ?? [],
|
|
301
|
-
meta: mapCursorMeta(raw?.meta)
|
|
302
|
-
});
|
|
303
|
-
function resource(name) {
|
|
304
|
-
const f2 = createFilters();
|
|
305
|
-
const keys = {
|
|
306
|
-
all: [name],
|
|
307
|
-
list: (p) => [name, "list", p],
|
|
308
|
-
infinite: (p) => [name, "infinite", p],
|
|
309
|
-
cursor: (p) => [name, "cursor", p],
|
|
310
|
-
detail: (id) => [name, "detail", id]
|
|
311
|
-
};
|
|
312
|
-
return {
|
|
313
|
-
list: (input) => q.list(input),
|
|
314
|
-
infinite: (input) => q.infinite(input),
|
|
315
|
-
cursor: (input) => q.cursor({ ...input, order: input?.order ?? cursorOrder }),
|
|
316
|
-
params: (input) => q.params(input),
|
|
317
|
-
f: f2,
|
|
318
|
-
search: (term, fields) => f.or(...fields.map((field) => f.contains(field, term))),
|
|
319
|
-
schema: (s) => defineListSchema(s),
|
|
320
|
-
fromSearchParams: (searchParams, options) => q.list({ ...readListParams(options.schema, searchParams, options.url), with: options.with }),
|
|
321
|
-
keys,
|
|
322
|
-
parseList: (raw) => parseList(raw),
|
|
323
|
-
parseInfinite: (raw) => parseInfinite(raw),
|
|
324
|
-
parseCursor: (raw) => parseCursor(raw)
|
|
325
|
-
};
|
|
326
|
-
}
|
|
327
|
-
return { adapter: config.adapter, resource, parseList, parseInfinite, parseCursor };
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
export { buildCursorParams, buildInfiniteParams, buildListParams, buildParams, createFilters, createQuery, createRegistry, decodeSort, defineListSchema, encodeSort, normalizeFilter, normalizeSort, readListParams, resetParams, schemaToFilter, searchParamsToPayload, setPage, setParam, setSize, setSort };
|
|
331
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
import {createFilters,DEFAULT_MAX_SORT_FIELDS,f,DEFAULT_MAX_PER_PAGE,DEFAULT_PER_PAGE,DEFAULT_MAX_OFFSET,DEFAULT_MAX_LIMIT,DEFAULT_LIMIT}from'@querykitjs/core';export{f}from'@querykitjs/core';function k(){return createFilters()}var P={filterField:"filter",sortField:"sort",columnsField:"columns",withField:"with",withDeletedField:"withDeleted",withTotalField:"withTotal",pageField:"page",perPageField:"perPage",limitField:"limit",offsetField:"offset",cursorField:"cursor",defaultPerPage:DEFAULT_PER_PAGE,defaultLimit:DEFAULT_LIMIT,maxPerPage:DEFAULT_MAX_PER_PAGE,maxLimit:DEFAULT_MAX_LIMIT,maxOffset:DEFAULT_MAX_OFFSET,maxSortFields:DEFAULT_MAX_SORT_FIELDS,defaultSort:[{key:"createdAt",direction:"desc"}],pruneEmpty:true};function re(e){return !!(e==null||e===""||Array.isArray(e)&&e.length===0)}function ne(e){if(typeof e=="string"){let t=e.startsWith("-"),r=(t?e.slice(1):e).trim();return r?{key:r,direction:t?"desc":"asc"}:void 0}return e&&e.key?{key:e.key,direction:e.direction??"asc"}:void 0}function T(e,t){if(!e||e.length===0)return t;let r=e.map(ne).filter(n=>n!==void 0);return r.length?r:t}function U(e,t){let r=e.operation??"=",n=r==="isNull"||r==="isNotNull",i=e.value;if(!n&&t&&re(i))return;let a={key:e.key,operation:r};return n||(a.value=i),a}function h(e,t){if("and"in e){let r=e.and.map(n=>h(n,t)).filter(n=>!!n);return r.length?{and:r}:void 0}if("or"in e){let r=e.or.map(n=>h(n,t)).filter(n=>!!n);return r.length?{or:r}:void 0}if("not"in e){let r=h(e.not,t);return r?{not:r}:void 0}return U(e,t)}function M(e,t){return e?Array.isArray(e)?e.map(r=>U(r,t)).filter(r=>!!r):h(e,t)??[]:[]}function N(e,t){let r={[t.filterField]:M(e.filter,t.pruneEmpty),[t.columnsField]:e.columns??{},[t.withField]:e.with??{}};return e.withDeleted!==void 0&&(r[t.withDeletedField]=e.withDeleted),r}function c(e,t,r=1/0){let n=e===void 0||e<1||Number.isNaN(e)?t:Math.trunc(e);return Math.min(r,n)}function K(e,t,r,n=""){return new Error(`querykit: ${e} exceeds ${t} (${r})${n}`)}function w(e,t){let r=T(e.sort,t.defaultSort);if(r.length>t.maxSortFields)throw K(`sort with ${r.length} fields`,"maxSortFields",t.maxSortFields);return {...N(e,t),[t.sortField]:r}}var E="; use cursor pagination for deeper pages";function O(e,t){let r=c(e.page,1),n=c(e.perPage,t.defaultPerPage,t.maxPerPage);if((r-1)*n>t.maxOffset)throw K(`page ${r} \xD7 perPage ${n}`,"maxOffset",t.maxOffset,E);let i={...w(e,t),[t.pageField]:r,[t.perPageField]:n};return e.withTotal!==void 0&&(i[t.withTotalField]=e.withTotal),i}function D(e,t){let r=e.offset&&e.offset>0?Math.trunc(e.offset):0;if(r>t.maxOffset)throw K(`offset ${r}`,"maxOffset",t.maxOffset,E);return {...w(e,t),[t.limitField]:c(e.limit,t.defaultLimit,t.maxLimit),[t.offsetField]:r}}function _(e,t){let r={...N(e,t),[t.limitField]:c(e.limit,t.defaultLimit,t.maxLimit),[t.cursorField]:e.cursor??null,order:e.order??"asc",direction:e.direction??"forward"};return e.cursorKey!==void 0&&(r.cursorKey=e.cursorKey),r}function b(e={}){let t={...P,...e};return {params:(r={})=>w(r,t),list:(r={})=>O(r,t),infinite:(r={})=>D(r,t),cursor:(r={})=>_(r,t)}}function ie(e={}){return w(e,P)}function A(e={}){return O(e,P)}function ae(e={}){return D(e,P)}function oe(e={}){return _(e,P)}var Q=e=>e===null?null:e??0;function V(e){return {totalPages:Q(e?.total_pages),totalCount:Q(e?.total_items),currentPage:e?.current_page??0,perPage:e?.per_page??0,hasNext:e?.has_next??false,hasPrev:e?.has_prev??false}}function v(e){return {limit:e?.limit??0,offset:e?.offset??0,count:e?.count??0,hasMore:e?.has_more??false,nextOffset:e?.next_offset??null}}function W(e){return {limit:e?.limit??0,hasNext:e?.has_next??false,hasPrev:e?.has_prev??false,nextCursor:e?.next_cursor??null,prevCursor:e?.prev_cursor??null}}function se(e){return e}var F={pageParam:"page",sizeParam:"size",sortParam:"sortType"};function z(e){if(!e)return;let t=e.split(",").map(r=>r.trim()).filter(Boolean).map(r=>{let n=r.startsWith("-");return {key:n?r.slice(1):r,direction:n?"desc":"asc"}}).filter(r=>r.key);return t.length?t:void 0}function B(e){return e.map(t=>typeof t=="string"?t.trim():t&&t.key?(t.direction==="desc"?"-":"")+t.key:"").filter(Boolean).join(",")}function q(e,t){let r=[],n=[];for(let[i,a]of Object.entries(e)){let l=a.key??i;if(a.range){let u=t.get(a.range[0]),d=t.get(a.range[1]);u&&r.push(g(l,">=",u)),d&&r.push(g(l,"<=",d));continue}if(a.between){let u=t.get(a.between[0]),d=t.get(a.between[1]);u&&d&&r.push(g(l,"between",[u,d]));continue}let f=t.get(i),s=f!==null?f:a.default;if(typeof s=="string"&&a.trim&&(s=s.trim()),!(s==null||s==="")&&!(Array.isArray(s)&&s.length===0)){if(a.search&&typeof s=="string"){n.push({or:a.search.map(u=>g(u,"%_%",s))});continue}if(a.split&&typeof s=="string"){let u=a.split===true?",":a.split,d=s.split(u).map(p=>p.trim()).filter(Boolean);if(d.length===0)continue;s=d;}r.push(g(l,a.operation??"=",s));}}return n.length===0?r:{and:[...n,...r]}}function g(e,t,r){return {key:e,operation:t,value:r}}function I(e,t){return Object.defineProperty(e,"clamped",{value:t,enumerable:false})}function fe(e,t){let r=c(e,t.perPage??DEFAULT_PER_PAGE,t.maxPerPage??DEFAULT_MAX_PER_PAGE);return Math.floor((t.maxOffset??DEFAULT_MAX_OFFSET)/r)+1}function L(e,t,r={},n={}){let i={...F,...r},a=t.get(i.sizeParam),l=a?Number(a):void 0,f=Math.trunc(Number(t.get(i.pageParam)))||1,s=z(t.get(i.sortParam)),u=fe(l,n),d=n.maxSortFields??DEFAULT_MAX_SORT_FIELDS,p=f>u,x=s!==void 0&&s.length>d;return {filter:q(e,t),sort:x?s.slice(0,d):s,page:p?u:f,perPage:l,clamped:p||x}}function pe(e,t,r){let n=L(e,t,r);return I(A(n),n.clamped)}function C(e){return new URLSearchParams(e)}function ce(e,t,r,n={}){let i={...F,...n},a=C(e);return a.delete(i.pageParam),r==null||r===""?a.delete(t):a.set(t,r),a}function ye(e,t,r={}){let n={...F,...r},i=C(e);return i.set(n.pageParam,String(t)),i}function Pe(e,t,r={}){let n={...F,...r},i=C(e);return i.delete(n.pageParam),i.set(n.sizeParam,String(t)),i}function ge(e,t,r={}){let n={...F,...r},i=C(e);i.delete(n.pageParam);let a=B(t);return a?i.set(n.sortParam,a):i.delete(n.sortParam),i}function Fe(e,t=[]){let r=new URLSearchParams;for(let n of t){let i=e.get(n);i!==null&&r.set(n,i);}return r}function xe(e){let t=e.defaults??{},r=e.timestamps?.createdAt,n=r?[{key:r,direction:"desc"}]:[],i=T(t.sort,n),a=t.cursor?.order??"asc",l={};t.perPage!==void 0&&(l.defaultPerPage=t.perPage),t.limit!==void 0&&(l.defaultLimit=t.limit),t.maxPerPage!==void 0&&(l.maxPerPage=t.maxPerPage),t.maxLimit!==void 0&&(l.maxLimit=t.maxLimit),t.maxOffset!==void 0&&(l.maxOffset=t.maxOffset),t.maxSortFields!==void 0&&(l.maxSortFields=t.maxSortFields),i.length&&(l.defaultSort=i),e.pruneEmpty!==void 0&&(l.pruneEmpty=e.pruneEmpty);let f$1=b(l),s={perPage:t.perPage,maxPerPage:t.maxPerPage,maxOffset:t.maxOffset,maxSortFields:t.maxSortFields},u=m=>({data:m?.data??[],meta:V(m?.meta)}),d=m=>({data:m?.data??[],meta:v(m?.meta)}),p=m=>({data:m?.data??[],meta:W(m?.meta)});function x(m){let $=k();return {list:o=>f$1.list(o),infinite:o=>f$1.infinite(o),cursor:o=>f$1.cursor({...o,order:o?.order??a}),params:o=>f$1.params(o),f:$,search:(o,y,R)=>f.or(...y.map(X=>f[R?.operation??"contains"](X,o))),schema:o=>o,fromSearchParams:(o,y)=>{let R=L(y.schema,o,y.url,s);return I(f$1.list({...R,with:y.with}),R.clamped)},keys:{all:[m],list:o=>[m,"list",o],infinite:o=>[m,"infinite",o],cursor:o=>[m,"cursor",o],detail:o=>[m,"detail",o]},parseList:o=>u(o),parseInfinite:o=>d(o),parseCursor:o=>p(o)}}return {adapter:e.adapter,resource:x,parseList:u,parseInfinite:d,parseCursor:p}}var Re={"unknown-key":true,"invalid-value":true,"forbidden-key":true,"forbidden-operator":true,"limit-exceeded":true,"empty-filter":true},Se={filter:true,sort:true,cursorKey:true,aggregate:true,page:true,mutation:true,with:true},G=(e,t)=>typeof t=="string"&&Object.hasOwn(e,t);function he(e){if(typeof e!="object"||e===null)return false;let t=e;return G(Re,t.reason)&&G(Se,t.site)&&typeof t.key=="string"}export{oe as buildCursorParams,ae as buildInfiniteParams,A as buildListParams,ie as buildParams,k as createFilters,b as createQuery,xe as createRegistry,z as decodeSort,se as defineListSchema,B as encodeSort,he as isQueryKitError,M as normalizeFilter,T as normalizeSort,L as readListParams,Fe as resetParams,q as schemaToFilter,pe as searchParamsToPayload,ye as setPage,ce as setParam,Pe as setSize,ge as setSort};//# sourceMappingURL=index.js.map
|
|
332
2
|
//# sourceMappingURL=index.js.map
|