next-data-kit 9.9.2 → 10.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/MIGRATION.md +43 -0
- package/README.md +107 -782
- package/dist/index.d.ts +79 -111
- package/dist/index.js +98 -205
- package/dist/index.js.map +1 -1
- package/dist/server.d.ts +121 -89
- package/dist/server.js +96 -285
- package/dist/server.js.map +1 -1
- package/package.json +50 -104
- package/registry/r/data-kit.json +58 -0
- package/registry/r/registry.json +23 -0
- package/dist/action-DCmAhOQa.d.cts +0 -196
- package/dist/action-DCmAhOQa.d.ts +0 -196
- package/dist/chunk-5TIEIY7M.cjs +0 -393
- package/dist/chunk-5TIEIY7M.cjs.map +0 -1
- package/dist/chunk-MTJATM72.cjs +0 -2423
- package/dist/chunk-MTJATM72.cjs.map +0 -1
- package/dist/chunk-O377UO7C.js +0 -2379
- package/dist/chunk-O377UO7C.js.map +0 -1
- package/dist/chunk-VFSNUXOW.js +0 -382
- package/dist/chunk-VFSNUXOW.js.map +0 -1
- package/dist/client/components/data-kit-infinity.d.ts +0 -23
- package/dist/client/components/data-kit-infinity.d.ts.map +0 -1
- package/dist/client/components/data-kit-table.d.ts +0 -34
- package/dist/client/components/data-kit-table.d.ts.map +0 -1
- package/dist/client/components/data-kit.d.ts +0 -25
- package/dist/client/components/data-kit.d.ts.map +0 -1
- package/dist/client/components/index.d.ts +0 -4
- package/dist/client/components/index.d.ts.map +0 -1
- package/dist/client/components/ui/button.d.ts +0 -14
- package/dist/client/components/ui/button.d.ts.map +0 -1
- package/dist/client/components/ui/checkbox.d.ts +0 -5
- package/dist/client/components/ui/checkbox.d.ts.map +0 -1
- package/dist/client/components/ui/dropdown-menu.d.ts +0 -28
- package/dist/client/components/ui/dropdown-menu.d.ts.map +0 -1
- package/dist/client/components/ui/index.d.ts +0 -8
- package/dist/client/components/ui/index.d.ts.map +0 -1
- package/dist/client/components/ui/pagination.d.ts +0 -14
- package/dist/client/components/ui/pagination.d.ts.map +0 -1
- package/dist/client/components/ui/popover.d.ts +0 -10
- package/dist/client/components/ui/popover.d.ts.map +0 -1
- package/dist/client/components/ui/select.d.ts +0 -18
- package/dist/client/components/ui/select.d.ts.map +0 -1
- package/dist/client/components/ui/table.d.ts +0 -11
- package/dist/client/components/ui/table.d.ts.map +0 -1
- package/dist/client/context/index.d.ts +0 -22
- package/dist/client/context/index.d.ts.map +0 -1
- package/dist/client/hooks/index.d.ts +0 -7
- package/dist/client/hooks/index.d.ts.map +0 -1
- package/dist/client/hooks/useDataKit.d.ts +0 -9
- package/dist/client/hooks/useDataKit.d.ts.map +0 -1
- package/dist/client/hooks/usePagination.d.ts +0 -18
- package/dist/client/hooks/usePagination.d.ts.map +0 -1
- package/dist/client/hooks/useSelection.d.ts +0 -13
- package/dist/client/hooks/useSelection.d.ts.map +0 -1
- package/dist/client/index.d.ts +0 -11
- package/dist/client/index.d.ts.map +0 -1
- package/dist/client/utils/cn.d.ts +0 -10
- package/dist/client/utils/cn.d.ts.map +0 -1
- package/dist/client/utils/index.d.ts +0 -60
- package/dist/client/utils/index.d.ts.map +0 -1
- package/dist/client.cjs +0 -85
- package/dist/client.cjs.map +0 -1
- package/dist/client.d.cts +0 -62
- package/dist/client.d.ts +0 -7
- package/dist/client.d.ts.map +0 -1
- package/dist/client.js +0 -4
- package/dist/client.js.map +0 -1
- package/dist/index-DUVfK8q6.d.ts +0 -284
- package/dist/index-qXbjvUTB.d.cts +0 -284
- package/dist/index.cjs +0 -300
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -117
- package/dist/internal/hooks.cjs +0 -12
- package/dist/internal/hooks.cjs.map +0 -1
- package/dist/internal/hooks.d.cts +0 -1
- package/dist/internal/hooks.d.ts +0 -1
- package/dist/internal/hooks.js +0 -3
- package/dist/internal/hooks.js.map +0 -1
- package/dist/server/utils.d.ts +0 -52
- package/dist/server/utils.d.ts.map +0 -1
- package/dist/server.cjs +0 -315
- package/dist/server.cjs.map +0 -1
- package/dist/server.d.cts +0 -99
- package/dist/styles.css +0 -2
- package/dist/types/client/component.d.ts +0 -129
- package/dist/types/client/component.d.ts.map +0 -1
- package/dist/types/client/hook.d.ts +0 -116
- package/dist/types/client/hook.d.ts.map +0 -1
- package/dist/types/client/selectable.d.ts +0 -43
- package/dist/types/client/selectable.d.ts.map +0 -1
- package/dist/types/index.cjs +0 -4
- package/dist/types/index.cjs.map +0 -1
- package/dist/types/index.d.cts +0 -291
- package/dist/types/index.d.ts +0 -12
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/index.js +0 -3
- package/dist/types/index.js.map +0 -1
- package/dist/types/next-data-kit.d.ts +0 -95
- package/dist/types/next-data-kit.d.ts.map +0 -1
- package/dist/types/server/action.d.ts +0 -44
- package/dist/types/server/action.d.ts.map +0 -1
- package/dist/types/server/database/mongo.d.ts +0 -56
- package/dist/types/server/database/mongo.d.ts.map +0 -1
- package/dist/useDataKit-XFp3_mUc.d.cts +0 -276
- package/dist/useDataKit-XFp3_mUc.d.ts +0 -276
package/dist/server.d.ts
CHANGED
|
@@ -1,99 +1,131 @@
|
|
|
1
|
-
import { Model } from 'mongoose';
|
|
2
|
-
import { T as TMongooseOptions, a as TExtractDocType, b as TDataKitResult, c as TAdapterOptions, d as TMongoModel, e as TMongoFilterQuery, f as TFilterCustomConfigWithFilter, g as TSortOptions, h as TDataKitAdapter, i as TPaginationInfo } from './action-DCmAhOQa.js';
|
|
3
|
-
export { k as TDataKitInput, m as TFilterConfig, l as TFilterCustomConfig, j as TSortOrder } from './action-DCmAhOQa.js';
|
|
4
1
|
import { z } from 'zod';
|
|
2
|
+
import { Model } from 'mongoose';
|
|
5
3
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* next-data-kit - Mongoose Adapter
|
|
24
|
-
*
|
|
25
|
-
* Database adapter for Mongoose/MongoDB.
|
|
26
|
-
*/
|
|
4
|
+
type TDataKitSort = {
|
|
5
|
+
field: string;
|
|
6
|
+
direction: 'asc' | 'desc';
|
|
7
|
+
};
|
|
8
|
+
type TDataKitInput = {
|
|
9
|
+
page: {
|
|
10
|
+
number: number;
|
|
11
|
+
size: number;
|
|
12
|
+
};
|
|
13
|
+
sort: TDataKitSort[];
|
|
14
|
+
filter: Record<string, unknown>;
|
|
15
|
+
};
|
|
16
|
+
type TDataKitResult<TItem> = TDataKitInput & {
|
|
17
|
+
items: TItem[];
|
|
18
|
+
total: number;
|
|
19
|
+
};
|
|
27
20
|
|
|
28
|
-
|
|
29
|
-
filter
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
21
|
+
type TDataKitAdapter<TDocument, TQuery> = (props: Readonly<{
|
|
22
|
+
filter: TQuery[];
|
|
23
|
+
sort: TDataKitSort[];
|
|
24
|
+
skip: number;
|
|
25
|
+
limit: number;
|
|
26
|
+
}>) => Promise<{
|
|
27
|
+
items: TDocument[];
|
|
28
|
+
total: number;
|
|
29
|
+
}>;
|
|
33
30
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
31
|
+
declare const urlEncode: (props: Readonly<{
|
|
32
|
+
input: TDataKitInput;
|
|
33
|
+
key?: string;
|
|
34
|
+
search?: string;
|
|
35
|
+
}>) => string;
|
|
36
|
+
declare const urlDecode: (props: Readonly<{
|
|
37
|
+
searchParams: URLSearchParams | Record<string, string | string[] | undefined>;
|
|
38
|
+
key?: string;
|
|
39
|
+
}>) => {
|
|
40
|
+
page: {
|
|
41
|
+
number: string | undefined;
|
|
42
|
+
size: string | undefined;
|
|
43
|
+
};
|
|
44
|
+
sort: {
|
|
45
|
+
field: string;
|
|
46
|
+
direction: string;
|
|
47
|
+
}[];
|
|
48
|
+
filter: {
|
|
49
|
+
[k: string]: string;
|
|
50
|
+
};
|
|
51
|
+
};
|
|
40
52
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}>) =>
|
|
53
|
+
declare const dataKitDefine: <TAdapter extends TDataKitAdapter<any, any>, TItem extends {
|
|
54
|
+
id: string;
|
|
55
|
+
}, TFilter extends Record<string, z.ZodType>, TContext = undefined, TDocument = Awaited<ReturnType<TAdapter>>["items"][number], TQuery = Parameters<TAdapter>[0]["filter"][number]>(props: Readonly<{
|
|
56
|
+
adapter: TAdapter;
|
|
57
|
+
scope?: (props: Readonly<{
|
|
58
|
+
context: TContext;
|
|
59
|
+
}>) => NoInfer<TQuery>;
|
|
60
|
+
filter?: { [TKey_1 in keyof TFilter]: {
|
|
61
|
+
schema: TFilter[TKey_1];
|
|
62
|
+
query: (props: Readonly<{
|
|
63
|
+
value: z.output<TFilter[TKey_1]>;
|
|
64
|
+
context: TContext;
|
|
65
|
+
}>) => NoInfer<TQuery> | Promise<NoInfer<TQuery>>;
|
|
66
|
+
}; };
|
|
67
|
+
sort?: {
|
|
68
|
+
field: readonly string[];
|
|
69
|
+
default?: TDataKitSort[];
|
|
70
|
+
};
|
|
71
|
+
page?: {
|
|
72
|
+
size?: {
|
|
73
|
+
default?: number;
|
|
74
|
+
max?: number;
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
item: (props: Readonly<{
|
|
78
|
+
document: NoInfer<TDocument>;
|
|
79
|
+
}>) => TItem | Promise<TItem>;
|
|
80
|
+
}>) => {
|
|
81
|
+
schema: z.ZodObject<{
|
|
82
|
+
page: z.ZodCatch<z.ZodObject<{
|
|
83
|
+
number: z.ZodCatch<z.ZodCoercedNumber<unknown>>;
|
|
84
|
+
size: z.ZodCatch<z.ZodCoercedNumber<unknown>>;
|
|
85
|
+
}, z.core.$strip>>;
|
|
86
|
+
sort: z.ZodCatch<z.ZodPipe<z.ZodArray<z.ZodObject<{
|
|
87
|
+
field: z.ZodString;
|
|
88
|
+
direction: z.ZodEnum<{
|
|
89
|
+
asc: "asc";
|
|
90
|
+
desc: "desc";
|
|
91
|
+
}>;
|
|
92
|
+
}, z.core.$strip>>, z.ZodTransform<TDataKitSort[], {
|
|
93
|
+
field: string;
|
|
94
|
+
direction: "asc" | "desc";
|
|
95
|
+
}[]>>>;
|
|
96
|
+
filter: z.ZodCatch<z.ZodObject<{ -readonly [P in keyof { [TKey in keyof TFilter]: z.ZodCatch<z.ZodOptional<TFilter[TKey]>>; }]: { [TKey in keyof TFilter]: z.ZodCatch<z.ZodOptional<TFilter[TKey]>>; }[P]; }, z.core.$strip>>;
|
|
97
|
+
}, z.core.$strip>;
|
|
98
|
+
parse: (props: Readonly<{
|
|
99
|
+
searchParams: Parameters<typeof urlDecode>[0]["searchParams"];
|
|
100
|
+
key?: string;
|
|
101
|
+
}>) => TDataKitInput;
|
|
102
|
+
query: (props: Readonly<{
|
|
103
|
+
input: TDataKitInput;
|
|
104
|
+
context?: TContext;
|
|
105
|
+
page?: {
|
|
106
|
+
size: number;
|
|
107
|
+
};
|
|
108
|
+
}>) => Promise<TDataKitResult<TItem>>;
|
|
109
|
+
};
|
|
48
110
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
111
|
+
declare const adapterMongoose: <TModel extends Model<any, any, any>, THydrate extends boolean = false>(props: Readonly<{
|
|
112
|
+
model: TModel;
|
|
113
|
+
hydrate?: THydrate;
|
|
114
|
+
}>) => TDataKitAdapter<THydrate extends true ? InstanceType<TModel> : TModel extends Model<infer TRaw, any, any> ? TRaw & {
|
|
115
|
+
_id: unknown;
|
|
116
|
+
} : never, Record<string, unknown>>;
|
|
52
117
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
declare const isProvided: (value: unknown) => boolean;
|
|
57
|
-
/**
|
|
58
|
-
* Check if a key is safe (not a prototype pollution key)
|
|
59
|
-
*/
|
|
60
|
-
declare const isSafeKey: (key: string) => boolean;
|
|
61
|
-
/**
|
|
62
|
-
* Helper to escape regex special characters in a string
|
|
63
|
-
*/
|
|
64
|
-
declare const escapeRegex: (str: string) => string;
|
|
65
|
-
/**
|
|
66
|
-
* Create a search filter for multiple fields
|
|
67
|
-
*
|
|
68
|
-
* @example
|
|
69
|
-
* ```typescript
|
|
70
|
-
* filterCustom: {
|
|
71
|
-
* search: createSearchFilter(['name', 'email', 'phone'])
|
|
72
|
-
* }
|
|
73
|
-
* ```
|
|
74
|
-
*/
|
|
75
|
-
declare const createSearchFilter: <T>(fields: (keyof T | string)[]) => ((value: unknown) => TMongoFilterQuery<T>);
|
|
76
|
-
/**
|
|
77
|
-
* Calculate pagination info from page, limit, and total
|
|
78
|
-
*/
|
|
79
|
-
declare const calculatePagination: (page: number, limit: number, total: number) => TPaginationInfo;
|
|
118
|
+
declare const adapterMemory: <TDocument>(props: Readonly<{
|
|
119
|
+
items: TDocument[];
|
|
120
|
+
}>) => TDataKitAdapter<TDocument, (document: TDocument) => boolean>;
|
|
80
121
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
query
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
type: 'REGEX' | 'EXACT';
|
|
89
|
-
field?: string;
|
|
90
|
-
}>;
|
|
91
|
-
sorts?: {
|
|
92
|
-
path: string;
|
|
93
|
-
value: 1 | -1;
|
|
94
|
-
}[];
|
|
95
|
-
sort?: Record<string, 1 | -1>;
|
|
122
|
+
declare const filterSearch: (props: Readonly<{
|
|
123
|
+
field: string[];
|
|
124
|
+
}>) => {
|
|
125
|
+
schema: z.ZodString;
|
|
126
|
+
query: (props: Readonly<{
|
|
127
|
+
value: string;
|
|
128
|
+
}>) => Record<string, unknown>;
|
|
96
129
|
};
|
|
97
|
-
declare const dataKitSchemaZod: z.ZodType<TDataKitSchemaZod>;
|
|
98
130
|
|
|
99
|
-
export { TDataKitAdapter,
|
|
131
|
+
export { type TDataKitAdapter, type TDataKitInput, type TDataKitResult, type TDataKitSort, adapterMemory, adapterMongoose, dataKitDefine, filterSearch, urlDecode, urlEncode };
|
package/dist/server.js
CHANGED
|
@@ -1,305 +1,116 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
|
|
3
|
-
// src/server/
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
var matchesExact = (rowValue, needle) => {
|
|
19
|
-
if (Array.isArray(needle)) return needle.includes(rowValue);
|
|
20
|
-
return rowValue === needle;
|
|
21
|
-
};
|
|
22
|
-
var matchesRegexLike = (rowValue, needle) => {
|
|
23
|
-
if (!isProvided(needle)) return true;
|
|
24
|
-
if (rowValue === void 0 || rowValue === null) return false;
|
|
25
|
-
const hay = String(rowValue);
|
|
26
|
-
if (needle instanceof RegExp) return needle.test(hay);
|
|
27
|
-
const n = String(needle);
|
|
28
|
-
return hay.toLowerCase().includes(n.toLowerCase());
|
|
29
|
-
};
|
|
30
|
-
var compareValues = (a, b) => {
|
|
31
|
-
if (a === b) return 0;
|
|
32
|
-
if (a === void 0 || a === null) return -1;
|
|
33
|
-
if (b === void 0 || b === null) return 1;
|
|
34
|
-
if (typeof a === "number" && typeof b === "number") return a - b;
|
|
35
|
-
if (typeof a === "bigint" && typeof b === "bigint") return a < b ? -1 : 1;
|
|
36
|
-
const as = String(a);
|
|
37
|
-
const bs = String(b);
|
|
38
|
-
return as.localeCompare(bs);
|
|
39
|
-
};
|
|
40
|
-
var normalizeSorts = (sorts) => Array.isArray(sorts) ? sorts.filter((s) => !!s?.path && (s.value === 1 || s.value === -1)) : [];
|
|
41
|
-
var isSafeKey = (key) => {
|
|
42
|
-
const unsafeKeys = ["__proto__", "constructor", "prototype"];
|
|
43
|
-
return !unsafeKeys.includes(key);
|
|
44
|
-
};
|
|
45
|
-
var escapeRegex = (str) => {
|
|
46
|
-
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
3
|
+
// src/server/define.ts
|
|
4
|
+
|
|
5
|
+
// src/url/index.ts
|
|
6
|
+
var urlEncode = (props) => {
|
|
7
|
+
const { input, key, search } = props;
|
|
8
|
+
const prefix = key ? `${key}.` : "";
|
|
9
|
+
const params = new URLSearchParams(search);
|
|
10
|
+
[...params.keys()].filter((name) => key ? name.startsWith(prefix) : ["page", "size", "sort"].includes(name) || name in input.filter).forEach((name) => params.delete(name));
|
|
11
|
+
if (input.page.number > 1) params.set(`${prefix}page`, String(input.page.number));
|
|
12
|
+
params.set(`${prefix}size`, String(input.page.size));
|
|
13
|
+
if (input.sort.length) params.set(`${prefix}sort`, input.sort.map((sort) => `${sort.field}.${sort.direction}`).join(","));
|
|
14
|
+
Object.entries(input.filter).forEach(([name, value]) => {
|
|
15
|
+
if (value !== void 0 && value !== null && value !== "") params.set(`${prefix}${name}`, String(value));
|
|
16
|
+
});
|
|
17
|
+
return params.toString();
|
|
47
18
|
};
|
|
48
|
-
var
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}))
|
|
58
|
-
};
|
|
19
|
+
var urlDecode = (props) => {
|
|
20
|
+
const { searchParams, key } = props;
|
|
21
|
+
const prefix = key ? `${key}.` : "";
|
|
22
|
+
const entries = (searchParams instanceof URLSearchParams ? [...searchParams.entries()] : Object.entries(searchParams).map(([name, value]) => [name, Array.isArray(value) ? value[0] : value])).filter((entry) => typeof entry[1] === "string" && entry[0].startsWith(prefix)).map(([name, value]) => [name.slice(prefix.length), value]);
|
|
23
|
+
const values = Object.fromEntries(entries);
|
|
24
|
+
return {
|
|
25
|
+
page: { number: values.page, size: values.size },
|
|
26
|
+
sort: (values.sort ?? "").split(",").filter(Boolean).map((sort) => ({ field: sort.slice(0, sort.lastIndexOf(".")), direction: sort.slice(sort.lastIndexOf(".") + 1) })),
|
|
27
|
+
filter: Object.fromEntries(entries.filter(([name]) => !["page", "size", "sort"].includes(name)))
|
|
59
28
|
};
|
|
60
29
|
};
|
|
61
|
-
var calculatePagination = (page, limit, total) => ({
|
|
62
|
-
currentPage: page,
|
|
63
|
-
totalPages: Math.ceil(total / limit),
|
|
64
|
-
totalItems: total,
|
|
65
|
-
itemsPerPage: limit,
|
|
66
|
-
hasNextPage: page * limit < total,
|
|
67
|
-
hasPrevPage: page > 1
|
|
68
|
-
});
|
|
69
30
|
|
|
70
|
-
// src/server/
|
|
71
|
-
var
|
|
72
|
-
const {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
if (filterCustom && filterCustom[key]) return;
|
|
103
|
-
if (isProvided(value) && isSafeKey(key) && input.filterConfig?.[key]) {
|
|
104
|
-
const config = input.filterConfig[key];
|
|
105
|
-
const fieldName = config?.field ?? key;
|
|
106
|
-
if (config?.type === "REGEX") {
|
|
107
|
-
filterQuery[fieldName] = {
|
|
108
|
-
$regex: escapeRegex(String(value)),
|
|
109
|
-
$options: "i"
|
|
110
|
-
};
|
|
111
|
-
} else if (config?.type === "EXACT") {
|
|
112
|
-
filterQuery[fieldName] = value;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
} else {
|
|
117
|
-
Object.entries(filter).forEach(([key, value]) => {
|
|
118
|
-
if (filterCustom && filterCustom[key]) return;
|
|
119
|
-
if (isProvided(value) && isSafeKey(key)) {
|
|
120
|
-
if (typeof value === "string") {
|
|
121
|
-
filterQuery[key] = {
|
|
122
|
-
$regex: escapeRegex(value),
|
|
123
|
-
$options: "i"
|
|
124
|
-
};
|
|
125
|
-
} else if (typeof value === "number" || typeof value === "boolean") {
|
|
126
|
-
filterQuery[key] = value;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
});
|
|
31
|
+
// src/server/define.ts
|
|
32
|
+
var dataKitDefine = (props) => {
|
|
33
|
+
const { adapter, scope, filter = {}, sort = { field: [] }, page = {}, item } = props;
|
|
34
|
+
const size = { default: page.size?.default ?? 20, max: page.size?.max ?? 100 };
|
|
35
|
+
const sortDefault = sort.default ?? [];
|
|
36
|
+
const schema = z.object({
|
|
37
|
+
page: z.object({
|
|
38
|
+
number: z.coerce.number().int().min(1).catch(1),
|
|
39
|
+
size: z.coerce.number().int().min(1).max(size.max).catch(size.default)
|
|
40
|
+
}).catch({ number: 1, size: size.default }),
|
|
41
|
+
sort: z.array(z.object({ field: z.string().refine((field) => sort.field.includes(field)), direction: z.enum(["asc", "desc"]) })).transform((value) => value.length ? value : sortDefault).catch(sortDefault),
|
|
42
|
+
// ** Unknown keys are stripped: an undeclared filter never reaches the adapter
|
|
43
|
+
filter: z.object(Object.fromEntries(Object.entries(filter).map(([name, declared]) => [name, declared.schema.optional().catch(void 0)]))).catch({})
|
|
44
|
+
});
|
|
45
|
+
return {
|
|
46
|
+
schema,
|
|
47
|
+
// ** searchParams from a server component (or a URLSearchParams) → validated input
|
|
48
|
+
parse: (props2) => schema.parse(urlDecode(props2)),
|
|
49
|
+
query: async (props2) => {
|
|
50
|
+
const { input, context, page: override } = props2;
|
|
51
|
+
const limit = override?.size ?? input.page.size;
|
|
52
|
+
const queries = await Promise.all([
|
|
53
|
+
...scope ? [scope({ context })] : [],
|
|
54
|
+
...Object.entries(input.filter).filter(([name, value]) => value !== void 0 && name in filter).map(([name, value]) => filter[name].query({ value, context }))
|
|
55
|
+
]);
|
|
56
|
+
const order = input.sort.length ? input.sort : sortDefault;
|
|
57
|
+
let number = input.page.number;
|
|
58
|
+
let result = await adapter({ filter: queries, sort: order, skip: (number - 1) * limit, limit });
|
|
59
|
+
const last = Math.max(1, Math.ceil(result.total / limit));
|
|
60
|
+
if (number > last) {
|
|
61
|
+
number = last;
|
|
62
|
+
result = await adapter({ filter: queries, sort: order, skip: (number - 1) * limit, limit });
|
|
130
63
|
}
|
|
64
|
+
const items = await Promise.all(result.items.map((document) => item({ document })));
|
|
65
|
+
if (process.env.NODE_ENV !== "production" && items[0] && (typeof items[0].id !== "string" || Object.values(items[0]).some((value) => typeof value === "object" && value !== null && "_bsontype" in value))) {
|
|
66
|
+
console.warn("[next-data-kit] item() must return plain JSON with a string `id` (convert ObjectId with .toString()).");
|
|
67
|
+
}
|
|
68
|
+
return { items, total: result.total, page: { number, size: limit }, sort: order, filter: input.filter };
|
|
131
69
|
}
|
|
132
|
-
if (filterCustom && filter) {
|
|
133
|
-
Object.entries(filter).forEach(([key, value]) => {
|
|
134
|
-
if (isProvided(value) && isSafeKey(key) && filterCustom[key]) {
|
|
135
|
-
const customFilter2 = filterCustom[key](value);
|
|
136
|
-
filterQuery = { ...filterQuery, ...customFilter2 };
|
|
137
|
-
}
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
const total = await model.countDocuments(filterQuery);
|
|
141
|
-
const items = await model.find(filterQuery).sort(sortOption).limit(limit).skip(skip);
|
|
142
|
-
return { items, total };
|
|
143
70
|
};
|
|
144
71
|
};
|
|
145
72
|
|
|
146
|
-
// src/server/
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
if (val !== void 0) {
|
|
159
|
-
safeQuery[key] = val;
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
input.query = safeQuery;
|
|
163
|
-
}
|
|
164
|
-
if (input.filter) {
|
|
165
|
-
const safeFilter = {};
|
|
166
|
-
Object.keys(input.filter).forEach((key) => {
|
|
167
|
-
if (filterAllowed && !filterAllowed.includes(key)) {
|
|
168
|
-
throw new Error(`[Security] Filter field '${key}' is not allowed.`);
|
|
169
|
-
}
|
|
170
|
-
const val = input.filter[key];
|
|
171
|
-
if (val !== null && typeof val === "object") {
|
|
172
|
-
throw new Error(`[Security] Filter value for '${key}' must be a primitive.`);
|
|
173
|
-
}
|
|
174
|
-
if (val !== void 0) {
|
|
175
|
-
safeFilter[key] = val;
|
|
176
|
-
}
|
|
177
|
-
});
|
|
178
|
-
input.filter = safeFilter;
|
|
179
|
-
}
|
|
180
|
-
if (input.sorts && sortAllowed) {
|
|
181
|
-
input.sorts.forEach((sort) => {
|
|
182
|
-
if (!sortAllowed.includes(sort.path)) {
|
|
183
|
-
throw new Error(`[Security] Sort field '${sort.path}' is not allowed.`);
|
|
184
|
-
}
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
switch (input.action ?? "FETCH") {
|
|
188
|
-
case "FETCH": {
|
|
189
|
-
if (!input.limit || !input.page) {
|
|
190
|
-
throw new Error("Invalid input: missing limit or page");
|
|
191
|
-
}
|
|
192
|
-
const limit = Math.min(input.limit, maxLimit);
|
|
193
|
-
const skip = limit * (input.page - 1);
|
|
194
|
-
const { items, total } = await adapter({
|
|
195
|
-
filter: input.filter ?? {},
|
|
196
|
-
sorts: input.sorts ?? [],
|
|
197
|
-
limit,
|
|
198
|
-
page: input.page,
|
|
199
|
-
skip,
|
|
200
|
-
input
|
|
201
|
-
});
|
|
202
|
-
const processedItems = await Promise.all(items.map((dataItem) => item(dataItem)));
|
|
203
|
-
return {
|
|
204
|
-
type: "ITEMS",
|
|
205
|
-
items: processedItems,
|
|
206
|
-
documentTotal: total
|
|
207
|
-
};
|
|
208
|
-
}
|
|
209
|
-
default:
|
|
210
|
-
throw new Error(`Unsupported action: ${input.action}`);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
async function dataKitServerAction(props) {
|
|
214
|
-
const { input, item, maxLimit = 100, queryAllowed, filterAllowed: explicitFilterAllowed, sortAllowed } = props;
|
|
215
|
-
const filterAllowed = explicitFilterAllowed;
|
|
216
|
-
let finalAdapter;
|
|
217
|
-
if ("adapter" in props && props.adapter) {
|
|
218
|
-
finalAdapter = props.adapter;
|
|
219
|
-
} else if ("model" in props && props.model) {
|
|
220
|
-
const model = props.model;
|
|
221
|
-
finalAdapter = mongooseAdapter(model, {
|
|
222
|
-
filter: props.filter,
|
|
223
|
-
filterCustom: props.filterCustom,
|
|
224
|
-
defaultSort: props.defaultSort
|
|
225
|
-
});
|
|
226
|
-
} else {
|
|
227
|
-
throw new Error("Either model or adapter must be provided");
|
|
228
|
-
}
|
|
229
|
-
return executeDataKit(input, finalAdapter, item, maxLimit, filterAllowed, queryAllowed, sortAllowed);
|
|
230
|
-
}
|
|
73
|
+
// src/server/adapter/mongoose.ts
|
|
74
|
+
var adapterMongoose = (props) => {
|
|
75
|
+
const { model, hydrate } = props;
|
|
76
|
+
return async (props2) => {
|
|
77
|
+
const { filter, sort, skip, limit } = props2;
|
|
78
|
+
const query = filter.length ? { $and: filter } : {};
|
|
79
|
+
const order = Object.fromEntries([...sort.map((entry) => [entry.field, entry.direction === "asc" ? 1 : -1]), ...sort.some((entry) => entry.field === "_id") ? [] : [["_id", 1]]]);
|
|
80
|
+
const find = model.find(query).sort(order).skip(skip).limit(limit);
|
|
81
|
+
const [items, total] = await Promise.all([hydrate ? find.exec() : find.lean().exec(), model.countDocuments(query).exec()]);
|
|
82
|
+
return { items, total };
|
|
83
|
+
};
|
|
84
|
+
};
|
|
231
85
|
|
|
232
|
-
// src/server/
|
|
233
|
-
var adapterMemory = (
|
|
234
|
-
const {
|
|
235
|
-
return async (
|
|
236
|
-
const
|
|
237
|
-
const
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
const
|
|
242
|
-
if (
|
|
243
|
-
}
|
|
244
|
-
return true;
|
|
245
|
-
});
|
|
246
|
-
const effectiveFilter = filter ?? {};
|
|
247
|
-
rows = rows.filter((row) => {
|
|
248
|
-
for (const [key, value] of Object.entries(effectiveFilter)) {
|
|
249
|
-
if (!isProvided(value)) continue;
|
|
250
|
-
const config = filterConfig?.[key];
|
|
251
|
-
const field = config?.field ?? key;
|
|
252
|
-
const rowValue = getValueByPath(row, field);
|
|
253
|
-
const type = config?.type ?? defaultFilterType;
|
|
254
|
-
if (type === "regex") {
|
|
255
|
-
if (!matchesRegexLike(rowValue, value)) return false;
|
|
256
|
-
} else {
|
|
257
|
-
if (!matchesExact(rowValue, value)) return false;
|
|
258
|
-
}
|
|
86
|
+
// src/server/adapter/memory.ts
|
|
87
|
+
var adapterMemory = (props) => {
|
|
88
|
+
const { items } = props;
|
|
89
|
+
return async (props2) => {
|
|
90
|
+
const { filter, sort, skip, limit } = props2;
|
|
91
|
+
const read = (document, field) => field.split(".").reduce((value, part) => value?.[part], document);
|
|
92
|
+
const matched = items.filter((document) => filter.every((query) => query(document)));
|
|
93
|
+
const sorted = [...matched].sort((a, b) => {
|
|
94
|
+
for (const entry of sort) {
|
|
95
|
+
const [x, y] = [read(a, entry.field), read(b, entry.field)];
|
|
96
|
+
if (x !== y) return (x < y ? -1 : 1) * (entry.direction === "asc" ? 1 : -1);
|
|
259
97
|
}
|
|
260
|
-
return
|
|
98
|
+
return 0;
|
|
261
99
|
});
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
});
|
|
100
|
+
return { items: sorted.slice(skip, skip + limit), total: matched.length };
|
|
101
|
+
};
|
|
102
|
+
};
|
|
103
|
+
var filterSearch = (props) => {
|
|
104
|
+
const { field } = props;
|
|
105
|
+
return {
|
|
106
|
+
schema: z.string().trim().min(1),
|
|
107
|
+
query: (props2) => {
|
|
108
|
+
const pattern = props2.value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
109
|
+
return { $or: field.map((name) => ({ [name]: { $regex: pattern, $options: "i" } })) };
|
|
273
110
|
}
|
|
274
|
-
const total = rows.length;
|
|
275
|
-
const items = rows.slice(skip, skip + limit);
|
|
276
|
-
return { items, total };
|
|
277
111
|
};
|
|
278
112
|
};
|
|
279
|
-
var dataKitSchemaZod = z.object({
|
|
280
|
-
action: z.literal("FETCH").optional(),
|
|
281
|
-
page: z.number().int().positive().optional(),
|
|
282
|
-
limit: z.number().int().positive().optional(),
|
|
283
|
-
query: z.record(z.string(), z.union([z.string(), z.number(), z.boolean()])).optional(),
|
|
284
|
-
filter: z.record(z.string(), z.union([z.string(), z.number(), z.boolean(), z.null()])).optional(),
|
|
285
|
-
filterConfig: z.record(
|
|
286
|
-
z.string(),
|
|
287
|
-
z.object({
|
|
288
|
-
type: z.enum(["REGEX", "EXACT"]),
|
|
289
|
-
field: z.string().optional()
|
|
290
|
-
})
|
|
291
|
-
).optional(),
|
|
292
|
-
sorts: z.array(
|
|
293
|
-
z.object({
|
|
294
|
-
path: z.string().max(100),
|
|
295
|
-
// Limit path length to prevent abuse
|
|
296
|
-
value: z.literal(-1).or(z.literal(1))
|
|
297
|
-
})
|
|
298
|
-
).max(5).optional(),
|
|
299
|
-
// Limit to 5 sort fields
|
|
300
|
-
sort: z.record(z.string(), z.literal(1).or(z.literal(-1))).optional()
|
|
301
|
-
});
|
|
302
113
|
|
|
303
|
-
export { adapterMemory,
|
|
114
|
+
export { adapterMemory, adapterMongoose, dataKitDefine, filterSearch, urlDecode, urlEncode };
|
|
304
115
|
//# sourceMappingURL=server.js.map
|
|
305
116
|
//# sourceMappingURL=server.js.map
|