next-data-kit 7.1.0 → 7.3.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/dist/index.d.cts CHANGED
@@ -160,6 +160,7 @@ type TBaseOptions<TDoc, R> = {
160
160
  filterAllowed?: string[];
161
161
  maxLimit?: number;
162
162
  queryAllowed?: (keyof TDoc | string)[];
163
+ sortAllowed?: (keyof TDoc | string)[];
163
164
  };
164
165
  /**
165
166
  * Options when using a Mongoose model
@@ -565,6 +566,10 @@ declare const DataKit: <TAction extends (input: TDataKitInput<unknown>) => Promi
565
566
  limit?: {
566
567
  default: number;
567
568
  };
569
+ defaultSort?: {
570
+ path: string;
571
+ value: 1 | -1;
572
+ }[];
568
573
  className?: string;
569
574
  autoFetch?: boolean;
570
575
  debounce?: number;
package/dist/index.d.ts CHANGED
@@ -160,6 +160,7 @@ type TBaseOptions<TDoc, R> = {
160
160
  filterAllowed?: string[];
161
161
  maxLimit?: number;
162
162
  queryAllowed?: (keyof TDoc | string)[];
163
+ sortAllowed?: (keyof TDoc | string)[];
163
164
  };
164
165
  /**
165
166
  * Options when using a Mongoose model
@@ -565,6 +566,10 @@ declare const DataKit: <TAction extends (input: TDataKitInput<unknown>) => Promi
565
566
  limit?: {
566
567
  default: number;
567
568
  };
569
+ defaultSort?: {
570
+ path: string;
571
+ value: 1 | -1;
572
+ }[];
568
573
  className?: string;
569
574
  autoFetch?: boolean;
570
575
  debounce?: number;
package/dist/index.js CHANGED
@@ -153,7 +153,7 @@ var mongooseAdapter = (model, options = {}) => {
153
153
  };
154
154
 
155
155
  // src/server/action.ts
156
- async function executeDataKit(input, adapter, item, maxLimit, filterAllowed, queryAllowed) {
156
+ async function executeDataKit(input, adapter, item, maxLimit, filterAllowed, queryAllowed, sortAllowed) {
157
157
  if (input.query) {
158
158
  const safeQuery = {};
159
159
  Object.keys(input.query).forEach((key) => {
@@ -186,6 +186,13 @@ async function executeDataKit(input, adapter, item, maxLimit, filterAllowed, que
186
186
  });
187
187
  input.filter = safeFilter;
188
188
  }
189
+ if (input.sorts && sortAllowed) {
190
+ input.sorts.forEach((sort) => {
191
+ if (!sortAllowed.includes(sort.path)) {
192
+ throw new Error(`[Security] Sort field '${sort.path}' is not allowed.`);
193
+ }
194
+ });
195
+ }
189
196
  switch (input.action ?? "FETCH") {
190
197
  case "FETCH": {
191
198
  if (!input.limit || !input.page) {
@@ -213,7 +220,7 @@ async function executeDataKit(input, adapter, item, maxLimit, filterAllowed, que
213
220
  }
214
221
  }
215
222
  async function dataKitServerAction(props) {
216
- const { input, item, maxLimit = 100, queryAllowed, filterAllowed: explicitFilterAllowed } = props;
223
+ const { input, item, maxLimit = 100, queryAllowed, filterAllowed: explicitFilterAllowed, sortAllowed } = props;
217
224
  const filterCustom = "filterCustom" in props ? props.filterCustom : void 0;
218
225
  const filterAllowed = explicitFilterAllowed ?? (filterCustom ? Object.keys(filterCustom) : void 0);
219
226
  let finalAdapter;
@@ -229,7 +236,7 @@ async function dataKitServerAction(props) {
229
236
  } else {
230
237
  throw new Error("Either model or adapter must be provided");
231
238
  }
232
- return executeDataKit(input, finalAdapter, item, maxLimit, filterAllowed, queryAllowed);
239
+ return executeDataKit(input, finalAdapter, item, maxLimit, filterAllowed, queryAllowed, sortAllowed);
233
240
  }
234
241
 
235
242
  // src/server/adapters/memory.ts
@@ -1401,6 +1408,7 @@ var DataKitInner = (props, ref) => {
1401
1408
  filterConfig,
1402
1409
  filters = [],
1403
1410
  limit: limitConfig,
1411
+ defaultSort = [],
1404
1412
  className,
1405
1413
  autoFetch = true,
1406
1414
  debounce: debounce2 = 300,
@@ -1423,7 +1431,7 @@ var DataKitInner = (props, ref) => {
1423
1431
  initial: {
1424
1432
  limit: limitConfig?.default ?? 10,
1425
1433
  query: query ?? {},
1426
- sorts: [],
1434
+ sorts: defaultSort,
1427
1435
  filter: filters.reduce((acc, f) => {
1428
1436
  if (f.defaultValue !== void 0) acc[f.id] = f.defaultValue;
1429
1437
  return acc;