feathers-utils 10.0.1 → 10.0.2

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.
Files changed (60) hide show
  1. package/dist/{hooks-CzxNpt2c.mjs → hooks-yFJ5FmU5.mjs} +37 -33
  2. package/dist/hooks-yFJ5FmU5.mjs.map +1 -0
  3. package/dist/hooks.d.mts +47 -7
  4. package/dist/hooks.mjs +4 -4
  5. package/dist/{index-CNrzxmWo.d.mts → index-CgAuzxiv.d.mts} +49 -4
  6. package/dist/index.d.mts +3 -3
  7. package/dist/index.mjs +7 -7
  8. package/dist/{internal.utils-BWQ25nOd.mjs → internal.utils-3BrB8lRY.mjs} +8 -2
  9. package/dist/internal.utils-3BrB8lRY.mjs.map +1 -0
  10. package/dist/{mutate-result.util-CCBWix-G.mjs → mutate-result.util-B6FOkwrd.mjs} +73 -11
  11. package/dist/mutate-result.util-B6FOkwrd.mjs.map +1 -0
  12. package/dist/{predicates-C2SeOKGd.mjs → predicates-BC8p_YLo.mjs} +13 -15
  13. package/dist/predicates-BC8p_YLo.mjs.map +1 -0
  14. package/dist/predicates.d.mts +2 -2
  15. package/dist/predicates.mjs +1 -1
  16. package/dist/{resolve-B81gQqXW.mjs → resolve-ClHfNFlm.mjs} +3 -3
  17. package/dist/{resolve-B81gQqXW.mjs.map → resolve-ClHfNFlm.mjs.map} +1 -1
  18. package/dist/resolvers.mjs +1 -1
  19. package/dist/{transform-result.hook-DifNj7zf.mjs → transform-result.hook-CanJtTPV.mjs} +2 -2
  20. package/dist/{transform-result.hook-DifNj7zf.mjs.map → transform-result.hook-CanJtTPV.mjs.map} +1 -1
  21. package/dist/transformers.mjs +8 -4
  22. package/dist/transformers.mjs.map +1 -1
  23. package/dist/{unless.hook-CVD7SrZh.d.mts → unless.hook-BX0mOvdN.d.mts} +2 -2
  24. package/dist/{utils-ByzrJKGQ.mjs → utils-BrvpblXB.mjs} +38 -19
  25. package/dist/utils-BrvpblXB.mjs.map +1 -0
  26. package/dist/utils.d.mts +2 -2
  27. package/dist/utils.mjs +4 -4
  28. package/package.json +1 -1
  29. package/src/common/clone.ts +9 -7
  30. package/src/hooks/cache/cache-utils.ts +5 -18
  31. package/src/hooks/cache/cache.hook.ts +28 -9
  32. package/src/hooks/disallow/disallow.hook.ts +3 -1
  33. package/src/hooks/on-delete/on-delete.hook.ts +60 -40
  34. package/src/hooks/params-for-server/params-for-server.hook.ts +4 -0
  35. package/src/hooks/set-field/set-field.hook.ts +30 -8
  36. package/src/hooks/set-result/set-result.hook.ts +7 -0
  37. package/src/hooks/skippable/skippable.hook.ts +6 -4
  38. package/src/hooks/traverse/traverse.hook.ts +20 -1
  39. package/src/internal.utils.ts +6 -1
  40. package/src/predicates/and/and.predicate.ts +5 -5
  41. package/src/predicates/is-context/is-context.predicate.ts +1 -1
  42. package/src/predicates/or/or.predicate.ts +6 -8
  43. package/src/transformers/parse-date/parse-date.transformer.ts +6 -1
  44. package/src/utils/chunk-find/chunk-find.util.ts +7 -0
  45. package/src/utils/combine/combine.util.ts +8 -15
  46. package/src/utils/get-paginate/get-paginate.util.ts +1 -3
  47. package/src/utils/index.ts +2 -0
  48. package/src/utils/iterate-find/iterate-find.util.ts +8 -0
  49. package/src/utils/mutate-data/mutate-data.util.ts +21 -4
  50. package/src/utils/mutate-result/mutate-result.util.ts +4 -10
  51. package/src/utils/patch-batch/patch-batch.util.ts +32 -14
  52. package/src/utils/replace-data/replace-data.util.ts +26 -0
  53. package/src/utils/replace-result/replace-result.util.ts +60 -0
  54. package/src/utils/sort-query-properties/sort-query-properties.util.ts +15 -8
  55. package/src/utils/walk-query/walk-query.util.ts +2 -2
  56. package/dist/hooks-CzxNpt2c.mjs.map +0 -1
  57. package/dist/internal.utils-BWQ25nOd.mjs.map +0 -1
  58. package/dist/mutate-result.util-CCBWix-G.mjs.map +0 -1
  59. package/dist/predicates-C2SeOKGd.mjs.map +0 -1
  60. package/dist/utils-ByzrJKGQ.mjs.map +0 -1
@@ -64,7 +64,7 @@ declare function iff<H extends HookContext = HookContext>(predicate: boolean | P
64
64
  * @see https://utils.feathersjs.com/hooks/skippable.html
65
65
  */
66
66
  declare const skippable: <H extends HookContext = HookContext>(innerHook: HookFunction<H>, predicate: PredicateFn<H>) => {
67
- (context: H): void;
67
+ (context: H): H | void | Promise<H | void>;
68
68
  (context: H, next: NextFunction): Promise<void>;
69
69
  };
70
70
  //#endregion
@@ -115,4 +115,4 @@ declare const throwIf: <H extends HookContext = HookContext>(predicate: Predicat
115
115
  declare function unless<H extends HookContext = HookContext>(predicate: boolean | PredicateFn, ...hooks: HookFunction<H>[]): (this: any, ctx: H, next?: _$_feathersjs_feathers0.NextFunction) => HookContext<_$_feathersjs_feathers0.Application<any, any>, any> | Promise<any>;
116
116
  //#endregion
117
117
  export { IffHook as a, skippable as i, ThrowIfOptions as n, iff as o, throwIf as r, iffElse as s, unless as t };
118
- //# sourceMappingURL=unless.hook-CVD7SrZh.d.mts.map
118
+ //# sourceMappingURL=unless.hook-BX0mOvdN.d.mts.map
@@ -1,6 +1,5 @@
1
- import { d as isPaginated, m as isContext, p as isMulti } from "./predicates-C2SeOKGd.mjs";
2
- import { i as getDataIsArray, r as getResultIsArray } from "./mutate-result.util-CCBWix-G.mjs";
3
- import isObject from "lodash/isObject.js";
1
+ import { d as isPaginated, m as isContext, p as isMulti } from "./predicates-BC8p_YLo.mjs";
2
+ import { a as getResultIsArray, o as getDataIsArray } from "./mutate-result.util-B6FOkwrd.mjs";
4
3
  import { dequal } from "dequal";
5
4
  import * as feathers from "@feathersjs/feathers";
6
5
  //#region src/utils/sort-query-properties/sort-query-properties.util.ts
@@ -12,6 +11,7 @@ const arrayOperators = new Set([
12
11
  "$in",
13
12
  "$nin"
14
13
  ]);
14
+ const isPlainObjectLike = (value) => value !== null && typeof value === "object";
15
15
  /**
16
16
  * Recursively normalizes a Feathers query object for order-independent comparison.
17
17
  * Sorts object keys and sorts arrays within `$or`, `$and`, `$nor`, `$not`, `$in`,
@@ -40,11 +40,17 @@ const sortQueryProperties = (query) => {
40
40
  };
41
41
  const normalize = (value) => {
42
42
  if (Array.isArray(value)) return value.map(normalize);
43
- if (!isObject(value)) return value;
43
+ if (!isPlainObjectLike(value)) return value;
44
44
  const sorted = {};
45
45
  for (const key of Object.keys(value).sort()) {
46
46
  const val = value[key];
47
- if (arrayOperators.has(key) && Array.isArray(val)) sorted[key] = val.map(normalize).sort((a, b) => JSON.stringify(a) < JSON.stringify(b) ? -1 : 1);
47
+ if (arrayOperators.has(key) && Array.isArray(val)) sorted[key] = val.map((el) => {
48
+ const normalized = normalize(el);
49
+ return {
50
+ k: JSON.stringify(normalized),
51
+ v: normalized
52
+ };
53
+ }).sort((a, b) => a.k < b.k ? -1 : a.k > b.k ? 1 : 0).map((entry) => entry.v);
48
54
  else sorted[key] = normalize(val);
49
55
  }
50
56
  return sorted;
@@ -123,6 +129,7 @@ async function* chunkFind(app, servicePath, options) {
123
129
  let result;
124
130
  do {
125
131
  result = await service.find(params);
132
+ if (!result.data.length) break;
126
133
  yield result.data;
127
134
  params.query.$skip = (params.query.$skip ?? 0) + result.data.length;
128
135
  } while (result.total > params.query.$skip);
@@ -293,11 +300,15 @@ async function* iterateFind(app, servicePath, options) {
293
300
  $limit: options?.params?.query?.$limit,
294
301
  $skip: options?.params?.query?.$skip ?? 0
295
302
  },
296
- paginate: { default: options?.params?.paginate?.default ?? 10 }
303
+ paginate: {
304
+ default: options?.params?.paginate?.default ?? 10,
305
+ max: options?.params?.paginate?.max ?? 100
306
+ }
297
307
  };
298
308
  let result;
299
309
  do {
300
310
  result = await service.find(params);
311
+ if (!result.data.length) break;
301
312
  for (const item of result.data) yield item;
302
313
  params.query.$skip = (params.query.$skip ?? 0) + result.data.length;
303
314
  } while (result.total > params.query.$skip);
@@ -305,6 +316,14 @@ async function* iterateFind(app, servicePath, options) {
305
316
  //#endregion
306
317
  //#region src/utils/patch-batch/patch-batch.util.ts
307
318
  /**
319
+ * Deterministic, key-order-independent serialization used to group items with
320
+ * equal patch data in O(1) per item.
321
+ */
322
+ const stableKey = (value) => JSON.stringify(value, (_key, val) => val && typeof val === "object" && !Array.isArray(val) ? Object.keys(val).sort().reduce((acc, k) => {
323
+ acc[k] = val[k];
324
+ return acc;
325
+ }, {}) : val);
326
+ /**
308
327
  * Batch patching utility that takes an array of items to be changed and returns an array of arguments to be called with the `patch` method.
309
328
  *
310
329
  * This utility is useful when you need to patch multiple items with varying data in as few requests as possible.
@@ -330,22 +349,22 @@ async function* iterateFind(app, servicePath, options) {
330
349
  * @see https://utils.feathersjs.com/utils/patch-batch.html
331
350
  */
332
351
  function patchBatch(items, options) {
333
- const map = [];
334
352
  const idKey = options?.id ?? "id";
335
- for (const _data of items) {
336
- const data = _data;
337
- const id = _data[idKey];
353
+ const groups = /* @__PURE__ */ new Map();
354
+ for (const item of items) {
355
+ const source = item;
356
+ const id = source[idKey];
357
+ const data = { ...source };
338
358
  delete data[idKey];
339
- const index = map.findIndex((item) => {
340
- return dequal(item.data, data);
341
- });
342
- if (index === -1) map.push({
359
+ const key = stableKey(data);
360
+ const existing = groups.get(key);
361
+ if (existing) existing.ids.push(id);
362
+ else groups.set(key, {
343
363
  ids: [id],
344
364
  data
345
365
  });
346
- else map[index].ids.push(id);
347
366
  }
348
- return map.map(({ ids, data }) => {
367
+ return [...groups.values()].map(({ ids, data }) => {
349
368
  return ids.length === 1 ? [
350
369
  ids[0],
351
370
  data,
@@ -452,7 +471,7 @@ const _walkQueryUtil = (query, walker, options) => {
452
471
  }
453
472
  query[key] = value;
454
473
  }
455
- for (const key in query) if ((key === "$or" || key === "$and" || key === "$nor" || key === "$not") && Array.isArray(query[key])) {
474
+ for (const key in query) if ((key === "$or" || key === "$and" || key === "$nor") && Array.isArray(query[key])) {
456
475
  let array = query[key];
457
476
  let copiedArray = false;
458
477
  for (let i = 0, n = array.length; i < n; i++) {
@@ -507,7 +526,7 @@ const _walkQueryUtil = (query, walker, options) => {
507
526
  return query;
508
527
  };
509
528
  /**
510
- * Walks every property of a Feathers query (including nested `$and`/`$or`/`$nor`/`$not` arrays)
529
+ * Walks every property of a Feathers query (including nested `$and`/`$or`/`$nor` arrays)
511
530
  * and calls the `walker` function for each one. The walker receives the property name, operator,
512
531
  * value, and path, and can return a replacement value. Returns a new query only if changes were made.
513
532
  *
@@ -567,4 +586,4 @@ function zipDataResult(context, options) {
567
586
  //#endregion
568
587
  export { skipResult as a, getExposedMethods as c, checkContext as d, addToQuery as f, sortQueryProperties as h, toPaginated as i, defineHooks as l, addSkip as m, walkQuery as n, patchBatch as o, chunkFind as p, transformParams as r, iterateFind as s, zipDataResult as t, contextToJson as u };
569
588
 
570
- //# sourceMappingURL=utils-ByzrJKGQ.mjs.map
589
+ //# sourceMappingURL=utils-BrvpblXB.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils-BrvpblXB.mjs","names":["deepEqual"],"sources":["../src/utils/sort-query-properties/sort-query-properties.util.ts","../src/utils/add-skip/add-skip.util.ts","../src/utils/chunk-find/chunk-find.util.ts","../src/utils/add-to-query/add-to-query.util.ts","../src/utils/check-context/check-context.util.ts","../src/utils/context-to-json/context-to-json.util.ts","../src/utils/define-hooks/define-hooks.util.ts","../src/feathers-cjs-fix.ts","../src/utils/get-exposed-methods/get-exposed-methods.util.ts","../src/utils/iterate-find/iterate-find.util.ts","../src/utils/patch-batch/patch-batch.util.ts","../src/utils/skip-result/skip-result.util.ts","../src/utils/to-paginated/to-paginated.util.ts","../src/utils/transform-params/transform-params.util.ts","../src/utils/walk-query/walk-query.util.ts","../src/utils/zip-data-result/zip-data-result.util.ts"],"sourcesContent":["import type { Query } from '@feathersjs/feathers'\n\nconst arrayOperators = new Set(['$or', '$and', '$nor', '$not', '$in', '$nin'])\n\nconst isPlainObjectLike = (value: unknown): value is Record<string, any> =>\n value !== null && typeof value === 'object'\n\n/**\n * Recursively normalizes a Feathers query object for order-independent comparison.\n * Sorts object keys and sorts arrays within `$or`, `$and`, `$nor`, `$not`, `$in`,\n * and `$nin` operators so that different orderings produce the same result.\n *\n * This is useful for generating stable cache keys where\n * `{ $or: [{ a: 1 }, { b: 2 }] }` and `{ $or: [{ b: 2 }, { a: 1 }] }`\n * should be treated as equivalent.\n *\n * @example\n * ```ts\n * import { sortQueryProperties } from 'feathers-utils/utils'\n *\n * const normalized = sortQueryProperties({\n * $or: [{ name: 'Jane' }, { name: 'John' }],\n * age: { $in: [30, 25] },\n * })\n * // => { $or: [{ name: 'John' }, { name: 'Jane' }], age: { $in: [25, 30] } }\n * // (sorted by stable stringify comparison)\n * ```\n *\n * @see https://utils.feathersjs.com/utils/sort-query-properties.html\n */\nexport const sortQueryProperties = <Q extends Query>(query: Q): Q => {\n return normalize(query) as Q\n}\n\nconst normalize = (value: any): any => {\n if (Array.isArray(value)) {\n return value.map(normalize)\n }\n\n if (!isPlainObjectLike(value)) {\n return value\n }\n\n const sorted: Record<string, any> = {}\n\n for (const key of Object.keys(value).sort()) {\n const val = value[key]\n\n if (arrayOperators.has(key) && Array.isArray(val)) {\n // Schwartzian transform: serialize each normalized element once, sort by\n // that key, then unwrap. Avoids the O(n log n) repeated JSON.stringify of\n // the previous comparator (which also returned 1 for equal elements).\n sorted[key] = val\n .map((el) => {\n const normalized = normalize(el)\n return { k: JSON.stringify(normalized), v: normalized }\n })\n .sort((a, b) => (a.k < b.k ? -1 : a.k > b.k ? 1 : 0))\n .map((entry) => entry.v)\n } else {\n sorted[key] = normalize(val)\n }\n }\n\n return sorted\n}\n","import type { HookContext, HookType } from '@feathersjs/feathers'\nimport type { MaybeArray } from '../../internal.utils.js'\n\nexport type SkipHookName =\n | 'all'\n | HookType\n | `${HookType}:${string}`\n | (string & {})\n\n/**\n * Adds hook names to `context.params.skipHooks` so that `skippable`-wrapped hooks\n * will be bypassed for the current service call. Accepts a single name or an array.\n * Duplicates are automatically removed.\n *\n * @example\n * ```ts\n * import { addSkip } from 'feathers-utils/utils'\n *\n * // Inside a hook or custom code:\n * addSkip(context, 'myHook')\n * addSkip(context, ['hookA', 'hookB'])\n * ```\n *\n * @see https://utils.feathersjs.com/utils/add-skip.html\n */\nexport const addSkip = <H extends HookContext>(\n context: H,\n hooks: MaybeArray<SkipHookName>,\n) => {\n const names = Array.isArray(hooks) ? hooks : [hooks]\n\n if (context.params.skipHooks === undefined) {\n context.params = {\n ...context.params,\n skipHooks: [...names],\n }\n } else {\n if (!Array.isArray(context.params.skipHooks)) {\n throw new Error('Invalid skipHooks parameter')\n }\n\n context.params = {\n ...context.params,\n skipHooks: [...new Set([...context.params.skipHooks, ...names])],\n }\n }\n}\n","import type { Application, Params } from '@feathersjs/feathers'\nimport type { KeyOf } from '../../internal.utils.js'\nimport type {\n InferFindParams,\n InferFindResultSingle,\n} from '../../utility-types/infer-service-methods.js'\n\ntype PaginateOption = { default?: number; max?: number }\n\ntype ChunkFindOptions<P extends Params = Params> = {\n params?: P & { paginate?: PaginateOption }\n}\n\n/**\n * Use `for await` to iterate over chunks (pages) of results from a `find` method.\n *\n * This function is useful for processing large datasets in batches without loading everything into memory at once.\n * It uses pagination to fetch results in chunks, yielding each page's data array.\n *\n * @example\n * ```ts\n * import { chunkFind } from 'feathers-utils/utils'\n *\n * const app = feathers()\n *\n * // Assuming 'users' service has many records\n * for await (const users of chunkFind(app, 'users', {\n * params: { query: { active: true }, // Custom query parameters\n * } })) {\n * console.log(users) // Process each chunk of user records\n * }\n * ```\n *\n * @see https://utils.feathersjs.com/utils/chunk-find.html\n */\nexport async function* chunkFind<\n Services,\n Path extends KeyOf<Services>,\n Service extends Services[Path] = Services[Path],\n P extends Params = InferFindParams<Service>,\n Item = InferFindResultSingle<Service>,\n>(\n app: Application<Services>,\n servicePath: Path,\n options?: ChunkFindOptions<P>,\n): AsyncGenerator<Item[], void, unknown> {\n const service = app.service(servicePath)\n\n if (!service || !('find' in service)) {\n throw new Error(`Service '${servicePath}' does not have a 'find' method.`)\n }\n\n const params = {\n ...options?.params,\n query: {\n ...(options?.params?.query ?? {}),\n $limit: options?.params?.query?.$limit ?? 10,\n $skip: options?.params?.query?.$skip ?? 0,\n },\n paginate: {\n default: options?.params?.paginate?.default ?? 10,\n max: options?.params?.paginate?.max ?? 100,\n },\n }\n\n let result\n\n do {\n result = await (service as any).find(params)\n\n // Guard against an infinite loop: an empty page never advances $skip, so\n // `total > $skip` could stay true forever (e.g. $limit:0, or a stale total\n // when items are concurrently removed / filtered out by hooks).\n if (!result.data.length) {\n break\n }\n\n yield result.data\n\n params.query.$skip = (params.query.$skip ?? 0) + result.data.length\n } while (result.total > params.query.$skip)\n}\n","import type { Query } from '@feathersjs/feathers'\nimport { dequal as deepEqual } from 'dequal'\n\n/**\n * Safely merges properties into a Feathers query object. If a property already exists\n * with a different value, it wraps both in a `$and` array to preserve both conditions.\n * If the exact same key-value pair already exists, no changes are made.\n *\n * @example\n * ```ts\n * import { addToQuery } from 'feathers-utils/utils'\n *\n * const query = { status: 'active' }\n * addToQuery(query, { role: 'admin' })\n * // => { status: 'active', role: 'admin' }\n * ```\n *\n * @see https://utils.feathersjs.com/utils/add-to-query.html\n */\nexport function addToQuery<Q extends Query>(targetQuery: Q, query: Q): Q {\n targetQuery ??= {} as Q\n\n if (Object.keys(query).length === 0) {\n return targetQuery\n }\n\n const entries = Object.entries(query) as [keyof Q, any][]\n\n if (entries.every(([property]) => !(property in targetQuery))) {\n // if none of the properties exist, merge them directly\n return {\n ...targetQuery,\n ...query,\n }\n }\n\n function isAlreadyInQuery(targetQuery: Q, entries: [keyof Q, any][]) {\n return entries.every(\n ([property, value]) =>\n property in targetQuery && deepEqual(targetQuery[property], value),\n )\n }\n\n if (isAlreadyInQuery(targetQuery, entries)) {\n // if all properties already exist with the exact same value, do nothing\n return targetQuery\n }\n\n if (!targetQuery.$and) {\n return {\n ...targetQuery,\n $and: [{ ...query }],\n }\n }\n\n // check if the exact same value already exists in $and\n if (targetQuery.$and.some((q: any) => isAlreadyInQuery(q, entries))) {\n return targetQuery\n }\n\n return {\n ...targetQuery,\n $and: [...targetQuery.$and, { ...query }],\n }\n}\n","import type { HookContext } from '@feathersjs/feathers'\nimport type { HookType, MethodName } from '../../types.js'\nimport {\n isContext,\n type IsContextOptions,\n} from '../../predicates/is-context/is-context.predicate.js'\nimport type { UnpackMaybeArray } from '../../internal.utils.js'\n\ntype NarrowedContext<H extends HookContext, O> = H &\n (O extends { path: infer P }\n ? [P] extends [undefined | null]\n ? unknown\n : { path: UnpackMaybeArray<P> }\n : unknown) &\n (O extends { type: infer T }\n ? [T] extends [undefined | null]\n ? unknown\n : { type: UnpackMaybeArray<T> }\n : unknown) &\n (O extends { method: infer M }\n ? [M] extends [undefined | null]\n ? unknown\n : { method: UnpackMaybeArray<M> }\n : unknown)\n\nexport type CheckContextOptions<H extends HookContext = HookContext> =\n IsContextOptions<H> & {\n label?: string\n }\n\n/**\n * Validates that the hook context matches the expected type(s) and method(s).\n * Throws an error if the context is invalid, preventing hooks from running in\n * unsupported configurations. Typically used internally by other hooks.\n * Also narrows the context type based on the passed options.\n *\n * @example\n * ```ts\n * import { checkContext } from 'feathers-utils/utils'\n *\n * const myHook = (context) => {\n * checkContext(context, ['before', 'around'], ['create', 'patch'], 'myHook')\n * // or with options object:\n * checkContext(context, { type: ['before', 'around'], method: ['create', 'patch'], label: 'myHook' })\n * // context.type is now 'before' | 'around', context.method is now 'create' | 'patch'\n * }\n * ```\n *\n * @see https://utils.feathersjs.com/utils/check-context.html\n */\nexport function checkContext<\n H extends HookContext,\n const O extends CheckContextOptions<NoInfer<H>>,\n>(context: H, options: O): asserts context is NarrowedContext<H, O>\nexport function checkContext<\n H extends HookContext,\n const T extends HookType | HookType[] | null | undefined = undefined,\n const M extends MethodName | MethodName[] | null | undefined = undefined,\n>(\n context: H,\n type?: T,\n methods?: M,\n label?: string,\n): asserts context is NarrowedContext<H, { type: T; method: M }>\nexport function checkContext<H extends HookContext = HookContext>(\n context: H,\n typeOrOptions?:\n | HookType\n | HookType[]\n | CheckContextOptions<NoInfer<H>>\n | null,\n methods?: MethodName | MethodName[] | null,\n label = 'anonymous',\n): void {\n let options: IsContextOptions\n let hookLabel: string\n\n if (\n typeOrOptions != null &&\n typeof typeOrOptions === 'object' &&\n !Array.isArray(typeOrOptions)\n ) {\n const { label: optLabel, ...rest } = typeOrOptions\n options = rest\n hookLabel = optLabel ?? 'anonymous'\n } else {\n options = {\n method: methods ?? undefined,\n type: typeOrOptions ?? undefined,\n }\n hookLabel = label\n }\n\n if (!isContext(options)(context)) {\n const details: string[] = []\n\n if (options.type != null) {\n details.push(\n `type: expected '${Array.isArray(options.type) ? options.type.join(\"' | '\") : options.type}' but got '${context.type}'`,\n )\n }\n if (options.method != null) {\n details.push(\n `method: expected '${Array.isArray(options.method) ? options.method.join(\"' | '\") : options.method}' but got '${context.method}'`,\n )\n }\n if (options.path != null) {\n details.push(\n `path: expected '${Array.isArray(options.path) ? options.path.join(\"' | '\") : options.path}' but got '${context.path}'`,\n )\n }\n\n throw new Error(\n `The '${hookLabel}' hook has invalid context (${details.join(', ')}).`,\n )\n }\n}\n","import type { HookContext } from '@feathersjs/feathers'\n\n/**\n * Converts a FeathersJS HookContext to a plain JSON object by calling `toJSON()` if available.\n * This is important when using lodash `get`/`has` on the context, since the HookContext\n * class uses getters that may not be enumerable.\n *\n * @example\n * ```ts\n * import { contextToJson } from 'feathers-utils/utils'\n *\n * const json = contextToJson(context)\n * console.log(json)\n * ```\n *\n * @see https://utils.feathersjs.com/utils/context-to-json.html\n */\nexport const contextToJson = (context: HookContext) => {\n if (context.toJSON) {\n return context.toJSON()\n }\n return context\n}\n","import type { Application, HookOptions } from '@feathersjs/feathers'\n\n/**\n * TypeScript helper that provides full type inference and autocompletion when defining\n * service hooks. It is an identity function that simply returns its input,\n * but enables your IDE to infer the correct hook context types.\n *\n * @example\n * ```ts\n * import { defineHooks } from 'feathers-utils/utils'\n *\n * export const userHooks = defineHooks({\n * before: { create: [validateUser()] },\n * after: { all: [sanitizeResult()] }\n * })\n * ```\n *\n * @see https://utils.feathersjs.com/utils/define-hooks.html\n */\nexport function defineHooks<\n A extends Application = Application,\n S = {\n find: any\n get: any\n create: any\n update: any\n patch: any\n remove: any\n },\n Options = HookOptions<A, S>,\n>(hooks: Options): Options {\n return hooks\n}\n","// src/feathers.ts\nimport * as feathers from '@feathersjs/feathers'\n\n// Type-safe re-export of only what you need\nexport const SERVICE = (feathers as any).SERVICE || feathers.default?.SERVICE\n","import type { Service } from '@feathersjs/feathers'\nimport { SERVICE } from '../../feathers-cjs-fix.js'\n\n/**\n * Returns the list of method names that are publicly exposed by a Feathers service.\n * Reads the internal `[SERVICE].methods` property set during service registration.\n * Throws if the service does not have any exposed methods configured.\n *\n * @example\n * ```ts\n * import { getExposedMethods } from 'feathers-utils/utils'\n *\n * const methods = getExposedMethods(app.service('users'))\n * // => ['find', 'get', 'create', 'patch', 'remove']\n * ```\n *\n * @see https://utils.feathersjs.com/utils/get-exposed-methods.html\n */\nexport function getExposedMethods(service: Service) {\n const result = (service as any)[SERVICE].methods\n\n if (!result || !Array.isArray(result)) {\n throw new Error(`Service does not have exposed methods`)\n }\n\n return result\n}\n","import type { Application, Params } from '@feathersjs/feathers'\nimport type { KeyOf } from '../../internal.utils.js'\nimport type {\n InferFindParams,\n InferFindResultSingle,\n} from '../../utility-types/infer-service-methods.js'\n\ntype PaginateOption = { default?: number; max?: number }\n\ntype IterateFindOptions<P extends Params = Params> = {\n params?: P & { paginate?: PaginateOption }\n}\n\n/**\n * Use `for await` to iterate over the results of a `find` method.\n *\n * This function is useful for iterating over large datasets without loading everything into memory at once.\n * It uses pagination to fetch results in chunks, allowing you to process each item as it is retrieved.\n *\n * @example\n * ```ts\n * import { iterateFind } from 'feathers-utils/utils'\n *\n * const app = feathers()\n *\n * // Assuming 'users' service has many records\n * for await (const user of iterateFind(app, 'users', {\n * params: { query: { active: true }, // Custom query parameters\n * } })) {\n * console.log(user) // Process each user record\n * }\n * ```\n *\n * @see https://utils.feathersjs.com/utils/iterate-find.html\n */\nexport async function* iterateFind<\n Services,\n Path extends KeyOf<Services>,\n Service extends Services[Path] = Services[Path],\n P extends Params = InferFindParams<Service>,\n Item = InferFindResultSingle<Service>,\n>(\n app: Application<Services>,\n servicePath: Path,\n options?: IterateFindOptions<P>,\n): AsyncGenerator<Item, void, unknown> {\n const service = app.service(servicePath)\n\n if (!service || !('find' in service)) {\n throw new Error(`Service '${servicePath}' does not have a 'find' method.`)\n }\n\n const params = {\n ...options?.params,\n query: {\n ...(options?.params?.query ?? {}),\n $limit: options?.params?.query?.$limit,\n $skip: options?.params?.query?.$skip ?? 0,\n },\n paginate: {\n default: options?.params?.paginate?.default ?? 10,\n max: options?.params?.paginate?.max ?? 100,\n },\n }\n\n let result\n\n do {\n result = await (service as any).find(params)\n\n // Guard against an infinite loop: an empty page never advances $skip, so\n // `total > $skip` could stay true forever (e.g. $limit:0, or a stale total\n // when items are concurrently removed / filtered out by hooks).\n if (!result.data.length) {\n break\n }\n\n for (const item of result.data) {\n yield item\n }\n\n params.query.$skip = (params.query.$skip ?? 0) + result.data.length\n } while (result.total > params.query.$skip)\n}\n","import type { Id, Params } from '@feathersjs/feathers'\nimport type { KeyOf } from '../../internal.utils.js'\n\n/**\n * Deterministic, key-order-independent serialization used to group items with\n * equal patch data in O(1) per item.\n */\nconst stableKey = (value: any): string =>\n JSON.stringify(value, (_key, val) =>\n val && typeof val === 'object' && !Array.isArray(val)\n ? Object.keys(val)\n .sort()\n .reduce<Record<string, any>>((acc, k) => {\n acc[k] = val[k]\n return acc\n }, {})\n : val,\n )\n\nexport type PatchBatchOptions<IdKey extends string> = {\n /** the key of the id property */\n id?: IdKey\n}\n\nexport type PatchBatchResultItem<T = Record<string, unknown>, P = Params> = [\n Id | null,\n T,\n P | undefined,\n]\n\n/**\n * Batch patching utility that takes an array of items to be changed and returns an array of arguments to be called with the `patch` method.\n *\n * This utility is useful when you need to patch multiple items with varying data in as few requests as possible.\n *\n * @example\n * ```ts\n * const items = [\n * { id: 1, value: 10 },\n * { id: 2, value: 10 },\n * { id: 3, value: 20 },\n * ];\n *\n * const batched = patchBatch(items, { id: 'id' });\n * // batched will be:\n * // [\n * // [null, { value: 10 }, { query: { id: { $in: [1, 2] } } }],\n * // [3, { value: 20 }, undefined],\n * // ]\n *\n * await Promise.all(batched.map(args => service.patch(...args)));\n * ```\n *\n * @see https://utils.feathersjs.com/utils/patch-batch.html\n */\nexport function patchBatch<\n T extends Record<string, any>,\n IdKey extends KeyOf<T>,\n P extends Params,\n R extends Omit<T, IdKey> = Omit<T, IdKey>,\n>(\n items: T[],\n options?: PatchBatchOptions<IdKey>,\n): PatchBatchResultItem<R, P>[] {\n const idKey = options?.id ?? 'id'\n\n // group items with identical (id-stripped) data in O(n) via a Map keyed by a\n // stable serialization, instead of an O(n^2) findIndex + deepEqual scan.\n const groups = new Map<string, { ids: Id[]; data: R }>()\n\n for (const item of items) {\n const source = item as Record<string, any>\n const id = source[idKey] as Id\n // shallow copy then drop the id key, so the caller's input is never mutated.\n const data = { ...source }\n delete data[idKey as any]\n\n const key = stableKey(data)\n const existing = groups.get(key)\n\n if (existing) {\n existing.ids.push(id)\n } else {\n groups.set(key, { ids: [id], data: data as unknown as R })\n }\n }\n\n return [...groups.values()].map(({ ids, data }) => {\n return ids.length === 1\n ? ([ids[0], data, undefined] as PatchBatchResultItem<R, P>)\n : ([\n null,\n data,\n {\n query: {\n [idKey]: { $in: ids },\n },\n },\n ] as PatchBatchResultItem<R, P>)\n })\n}\n","import type { HookContext } from '@feathersjs/feathers'\nimport { isMulti, isPaginated } from '../../predicates/index.js'\n\n/**\n * Sets `context.result` to an appropriate empty value based on the hook method.\n * Returns an empty paginated object for paginated `find`, an empty array for multi\n * operations, or `null` for single-item operations. Does nothing if a result already exists.\n *\n * @example\n * ```ts\n * import { skipResult } from 'feathers-utils/utils'\n *\n * // In a before hook to skip the actual database call:\n * skipResult(context)\n * ```\n *\n * @see https://utils.feathersjs.com/utils/skip-result.html\n */\nexport const skipResult = <H extends HookContext = HookContext>(context: H) => {\n if (context.result) {\n return context\n }\n\n const multi = isMulti(context)\n\n if (multi) {\n if (context.method === 'find' && isPaginated(context)) {\n context.result = {\n total: 0,\n skip: 0,\n limit: 0,\n data: [],\n }\n } else {\n context.result = []\n }\n } else {\n context.result = null\n }\n\n return context\n}\n","import type { Paginated } from '@feathersjs/feathers'\n\n/**\n * Ensures a result is in Feathers paginated format (`{ total, limit, skip, data }`).\n * If the input is already paginated, it is returned as-is. If it is a plain array,\n * it is wrapped in a paginated object with `total` and `limit` set to the array length.\n *\n * @example\n * ```ts\n * import { toPaginated } from 'feathers-utils/utils'\n *\n * const paginated = toPaginated([{ id: 1 }, { id: 2 }])\n * // => { total: 2, limit: 2, skip: 0, data: [{ id: 1 }, { id: 2 }] }\n * ```\n *\n * @see https://utils.feathersjs.com/utils/to-paginated.html\n */\nexport function toPaginated<R>(result: R[] | Paginated<R>): Paginated<R> {\n if (Array.isArray(result)) {\n return {\n total: result.length,\n limit: result.length,\n skip: 0,\n data: result,\n }\n }\n return result\n}\n","import type { Params } from '@feathersjs/feathers'\nimport type { TransformParamsFn } from '../../types.js'\n\n/**\n * Safely applies a `transformParams` function to a params object.\n * If no function is provided, the original params are returned unchanged.\n * The function receives a shallow copy of params, so the original is not mutated.\n *\n * @example\n * ```ts\n * import { transformParams } from 'feathers-utils/utils'\n *\n * const params = transformParams(context.params, (p) => { delete p.provider; return p })\n * ```\n *\n * @see https://utils.feathersjs.com/utils/transform-params.html\n */\nexport const transformParams = <P extends Params = Params>(\n params: P,\n fn: TransformParamsFn<P> | undefined,\n): P => {\n if (!fn) {\n return params\n }\n\n const result = fn({ ...params })\n\n return result ?? params\n}\n","import type { Query } from '@feathersjs/feathers'\n\ntype WalkQueryOptionsInit = {\n property?: string\n operator?: string | undefined\n value?: any\n path: (string | number)[]\n}\n\nexport type WalkQueryOptions = {\n property: string\n operator: string | undefined\n value: any\n path: (string | number)[]\n}\n\nexport type WalkQueryCallback = (options: WalkQueryOptions) => any\n\nconst _walkQueryUtil = <Q extends Query>(\n query: Q,\n walker: WalkQueryCallback,\n options?: WalkQueryOptionsInit | WalkQueryOptions,\n): Q => {\n let cloned = false\n const clonedSecond: Record<string, boolean> = {}\n function set(key: string, value: any, secondKey?: string | number) {\n if (!cloned) {\n query = { ...query }\n cloned = true\n }\n\n if (secondKey !== undefined) {\n if (!clonedSecond[key]) {\n ;(query as any)[key] = { ...query[key] }\n clonedSecond[key] = true\n }\n query[key][secondKey] = value\n return\n }\n\n ;(query as any)[key] = value\n }\n\n for (const key in query) {\n if (\n (key === '$or' || key === '$and' || key === '$nor') &&\n Array.isArray(query[key])\n ) {\n let array = query[key]\n\n let copiedArray = false\n\n for (let i = 0, n = array.length; i < n; i++) {\n const nestedQuery = array[i]\n const transformed = _walkQueryUtil(nestedQuery, walker, {\n ...options,\n path: [...(options?.path || []), key, i],\n })\n\n if (transformed !== nestedQuery) {\n if (!copiedArray) {\n array = [...array] as any\n copiedArray = true\n }\n\n array[i] = transformed\n }\n }\n\n if (copiedArray) {\n set(key, array)\n }\n } else if (\n typeof query[key] === 'object' &&\n query[key] !== null &&\n !Array.isArray(query[key])\n ) {\n let hasOperator = false\n for (const operator in query[key]) {\n if (operator.startsWith('$')) {\n hasOperator = true\n const value = walker({\n operator,\n path: [...(options?.path ?? []), key],\n property: key,\n value: query[key][operator],\n })\n\n if (value !== undefined && value !== query[key][operator]) {\n set(key, value, operator)\n }\n }\n }\n\n if (!hasOperator) {\n const value = walker({\n operator: undefined,\n path: [...(options?.path ?? []), key],\n property: key,\n value: query[key],\n })\n\n if (value !== undefined && value !== query[key]) {\n set(key, value)\n }\n }\n } else {\n const value = walker({\n operator: undefined,\n path: [...(options?.path ?? []), key],\n property: key,\n value: query[key],\n })\n\n if (value !== undefined && value !== query[key]) {\n set(key, value)\n }\n }\n }\n\n return query\n}\n\n/**\n * Walks every property of a Feathers query (including nested `$and`/`$or`/`$nor` arrays)\n * and calls the `walker` function for each one. The walker receives the property name, operator,\n * value, and path, and can return a replacement value. Returns a new query only if changes were made.\n *\n * @example\n * ```ts\n * import { walkQuery } from 'feathers-utils/utils'\n *\n * const query = walkQuery({ age: { $gt: '18' } }, ({ value, operator }) => {\n * if (operator === '$gt') return Number(value)\n * })\n * // => { age: { $gt: 18 } }\n * ```\n *\n * @see https://utils.feathersjs.com/utils/walk-query.html\n */\nexport const walkQuery = <Q extends Query>(\n query: Q,\n walker: WalkQueryCallback,\n): Q => {\n return _walkQueryUtil(query, walker)\n}\n","import type { HookContext } from '@feathersjs/feathers'\nimport { getDataIsArray } from '../get-data-is-array/get-data-is-array.util.js'\nimport { getResultIsArray } from '../get-result-is-array/get-result-is-array.util.js'\nimport { checkContext } from '../check-context/check-context.util.js'\nimport type {\n DataSingleHookContext,\n ResultSingleHookContext,\n} from '../../utility-types/hook-context.js'\n\nexport type ZipDataResultOptions = {\n onMismatch?: (context: HookContext) => void\n}\n\nexport type ZipDataResultItem<D, R> = {\n data: D | undefined\n result: R | undefined\n}\n\n/**\n * Pairs each item in `context.data` with its corresponding item in `context.result` by index.\n * Handles both single-item and array data, normalizing them into an array of `{ data, result }` pairs.\n * Only works in `after`/`around` hooks for `create`, `update`, and `patch` methods.\n *\n * @example\n * ```ts\n * import { zipDataResult } from 'feathers-utils/utils'\n *\n * const pairs = zipDataResult(context)\n * pairs.forEach(({ data, result }) => { /* process each pair *\\/ })\n * ```\n *\n * @see https://utils.feathersjs.com/utils/zip-data-result.html\n */\nexport function zipDataResult<\n H extends HookContext,\n D extends DataSingleHookContext<H> = DataSingleHookContext<H>,\n R extends ResultSingleHookContext<H> = ResultSingleHookContext<H>,\n>(context: H, options?: ZipDataResultOptions): ZipDataResultItem<D, R>[] {\n checkContext(context, ['after', 'around'], ['create', 'update', 'patch'])\n\n const input = getDataIsArray(context)\n const output = getResultIsArray(context)\n\n if (\n input.isArray &&\n output.isArray &&\n input.data.length !== output.result.length\n ) {\n options?.onMismatch?.(context)\n }\n\n const result: ZipDataResultItem<D, R>[] = []\n\n const length = Math.max(input.data.length, output.result.length)\n\n for (let i = 0; i < length; i++) {\n const dataItem = input.isArray ? input.data.at(i) : input.data[0]\n const resultItem = output.result.at(i)\n\n result.push({\n data: dataItem,\n result: resultItem,\n })\n }\n\n return result\n}\n"],"mappings":";;;;;AAEA,MAAM,iBAAiB,IAAI,IAAI;CAAC;CAAO;CAAQ;CAAQ;CAAQ;CAAO;CAAO,CAAC;AAE9E,MAAM,qBAAqB,UACzB,UAAU,QAAQ,OAAO,UAAU;;;;;;;;;;;;;;;;;;;;;;;;AAyBrC,MAAa,uBAAwC,UAAgB;CACnE,OAAO,UAAU,MAAM;;AAGzB,MAAM,aAAa,UAAoB;CACrC,IAAI,MAAM,QAAQ,MAAM,EACtB,OAAO,MAAM,IAAI,UAAU;CAG7B,IAAI,CAAC,kBAAkB,MAAM,EAC3B,OAAO;CAGT,MAAM,SAA8B,EAAE;CAEtC,KAAK,MAAM,OAAO,OAAO,KAAK,MAAM,CAAC,MAAM,EAAE;EAC3C,MAAM,MAAM,MAAM;EAElB,IAAI,eAAe,IAAI,IAAI,IAAI,MAAM,QAAQ,IAAI,EAI/C,OAAO,OAAO,IACX,KAAK,OAAO;GACX,MAAM,aAAa,UAAU,GAAG;GAChC,OAAO;IAAE,GAAG,KAAK,UAAU,WAAW;IAAE,GAAG;IAAY;IACvD,CACD,MAAM,GAAG,MAAO,EAAE,IAAI,EAAE,IAAI,KAAK,EAAE,IAAI,EAAE,IAAI,IAAI,EAAG,CACpD,KAAK,UAAU,MAAM,EAAE;OAE1B,OAAO,OAAO,UAAU,IAAI;;CAIhC,OAAO;;;;;;;;;;;;;;;;;;;;ACvCT,MAAa,WACX,SACA,UACG;CACH,MAAM,QAAQ,MAAM,QAAQ,MAAM,GAAG,QAAQ,CAAC,MAAM;CAEpD,IAAI,QAAQ,OAAO,cAAc,KAAA,GAC/B,QAAQ,SAAS;EACf,GAAG,QAAQ;EACX,WAAW,CAAC,GAAG,MAAM;EACtB;MACI;EACL,IAAI,CAAC,MAAM,QAAQ,QAAQ,OAAO,UAAU,EAC1C,MAAM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OAAO"}
package/dist/utils.d.mts CHANGED
@@ -1,3 +1,3 @@
1
- import { A as SkipHookName, C as contextToJson, O as addToQuery, S as defineHooks, T as checkContext, _ as getResultIsArray, a as WalkQueryOptions, b as GetDataIsArrayReturn, c as transformParams, d as PatchBatchOptions, f as PatchBatchResultItem, g as GetResultIsArrayReturn, h as GetResultIsArrayOptions, i as WalkQueryCallback, j as addSkip, k as chunkFind, l as toPaginated, m as iterateFind, n as ZipDataResultOptions, o as walkQuery, p as patchBatch, r as zipDataResult, s as sortQueryProperties, t as ZipDataResultItem, u as skipResult, v as getPaginate, w as CheckContextOptions, x as getDataIsArray, y as getExposedMethods } from "./index-CNrzxmWo.mjs";
1
+ import { C as GetDataIsArrayReturn, D as CheckContextOptions, E as contextToJson, M as chunkFind, N as SkipHookName, O as checkContext, P as addSkip, S as getExposedMethods, T as defineHooks, _ as iterateFind, a as WalkQueryOptions, b as getResultIsArray, c as transformParams, d as ReplaceResultOptions, f as replaceResult, g as patchBatch, h as PatchBatchResultItem, i as WalkQueryCallback, j as addToQuery, l as toPaginated, m as PatchBatchOptions, n as ZipDataResultOptions, o as walkQuery, p as replaceData, r as zipDataResult, s as sortQueryProperties, t as ZipDataResultItem, u as skipResult, v as GetResultIsArrayOptions, w as getDataIsArray, x as getPaginate, y as GetResultIsArrayReturn } from "./index-CgAuzxiv.mjs";
2
2
  import { n as mutateResult, r as mutateData, t as MutateResultOptions } from "./mutate-result.util-BIeYlqqT.mjs";
3
- export { CheckContextOptions, GetDataIsArrayReturn, GetResultIsArrayOptions, GetResultIsArrayReturn, MutateResultOptions, PatchBatchOptions, PatchBatchResultItem, SkipHookName, WalkQueryCallback, WalkQueryOptions, ZipDataResultItem, ZipDataResultOptions, addSkip, addToQuery, checkContext, chunkFind, contextToJson, defineHooks, getDataIsArray, getExposedMethods, getPaginate, getResultIsArray, iterateFind, mutateData, mutateResult, patchBatch, skipResult, sortQueryProperties, toPaginated, transformParams, walkQuery, zipDataResult };
3
+ export { CheckContextOptions, GetDataIsArrayReturn, GetResultIsArrayOptions, GetResultIsArrayReturn, MutateResultOptions, PatchBatchOptions, PatchBatchResultItem, ReplaceResultOptions, SkipHookName, WalkQueryCallback, WalkQueryOptions, ZipDataResultItem, ZipDataResultOptions, addSkip, addToQuery, checkContext, chunkFind, contextToJson, defineHooks, getDataIsArray, getExposedMethods, getPaginate, getResultIsArray, iterateFind, mutateData, mutateResult, patchBatch, replaceData, replaceResult, skipResult, sortQueryProperties, toPaginated, transformParams, walkQuery, zipDataResult };
package/dist/utils.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { f as getPaginate } from "./predicates-C2SeOKGd.mjs";
2
- import { a as skipResult, c as getExposedMethods, d as checkContext, f as addToQuery, h as sortQueryProperties, i as toPaginated, l as defineHooks, m as addSkip, n as walkQuery, o as patchBatch, p as chunkFind, r as transformParams, s as iterateFind, t as zipDataResult, u as contextToJson } from "./utils-ByzrJKGQ.mjs";
3
- import { i as getDataIsArray, n as mutateData, r as getResultIsArray, t as mutateResult } from "./mutate-result.util-CCBWix-G.mjs";
4
- export { addSkip, addToQuery, checkContext, chunkFind, contextToJson, defineHooks, getDataIsArray, getExposedMethods, getPaginate, getResultIsArray, iterateFind, mutateData, mutateResult, patchBatch, skipResult, sortQueryProperties, toPaginated, transformParams, walkQuery, zipDataResult };
1
+ import { f as getPaginate } from "./predicates-BC8p_YLo.mjs";
2
+ import { a as skipResult, c as getExposedMethods, d as checkContext, f as addToQuery, h as sortQueryProperties, i as toPaginated, l as defineHooks, m as addSkip, n as walkQuery, o as patchBatch, p as chunkFind, r as transformParams, s as iterateFind, t as zipDataResult, u as contextToJson } from "./utils-BrvpblXB.mjs";
3
+ import { a as getResultIsArray, i as replaceData, n as replaceResult, o as getDataIsArray, r as mutateData, t as mutateResult } from "./mutate-result.util-B6FOkwrd.mjs";
4
+ export { addSkip, addToQuery, checkContext, chunkFind, contextToJson, defineHooks, getDataIsArray, getExposedMethods, getPaginate, getResultIsArray, iterateFind, mutateData, mutateResult, patchBatch, replaceData, replaceResult, skipResult, sortQueryProperties, toPaginated, transformParams, walkQuery, zipDataResult };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "feathers-utils",
3
- "version": "10.0.1",
3
+ "version": "10.0.2",
4
4
  "description": "Useful hooks and utils for use with feathers services.",
5
5
  "main": "./dist/index.mjs",
6
6
  "module": "./dist/index.mjs",
@@ -1,13 +1,15 @@
1
+ import { copy } from 'fast-copy'
2
+
1
3
  /**
2
- * Deep-clones an object using `JSON.parse(JSON.stringify(...))`.
3
- * Simple and fast for JSON-serializable data, but does not handle
4
- * `Date` objects, `undefined` values, functions, or circular references.
4
+ * Deep-clones a value using `fast-copy`.
5
+ *
6
+ * Unlike a `JSON.parse(JSON.stringify(...))` round-trip, this correctly handles
7
+ * `Date`, `Map`, `Set`, `RegExp`, typed arrays, `undefined` values and circular
8
+ * references — all of which FeathersJS payloads routinely contain.
5
9
  *
6
10
  * @example
7
11
  * ```ts
8
- * const copy = clone({ name: 'Alice', nested: { value: 1 } })
12
+ * const copyOf = clone({ name: 'Alice', createdAt: new Date(), nested: { value: 1 } })
9
13
  * ```
10
14
  */
11
- export function clone(obj: any) {
12
- return JSON.parse(JSON.stringify(obj))
13
- }
15
+ export const clone = copy
@@ -1,30 +1,17 @@
1
- import isObject from 'lodash/isObject.js'
2
1
  import { sortQueryProperties } from '../../utils/sort-query-properties/sort-query-properties.util.js'
3
2
 
4
3
  export { sortQueryProperties }
5
4
 
6
5
  export const stableStringify = (obj: Record<string, any>) => {
7
- if (obj.query) {
8
- obj = { ...obj, query: sortQueryProperties(obj.query) }
9
- }
6
+ // Canonicalize the whole params object once (recursive key-sort + operator-array
7
+ // sort). The JSON.stringify pass then only needs to reject non-JSON values
8
+ // instead of re-sorting and re-allocating every node.
9
+ const normalized = sortQueryProperties(obj as any)
10
10
 
11
- return JSON.stringify(obj, (key, value) => {
11
+ return JSON.stringify(normalized, (_key, value) => {
12
12
  if (typeof value === 'function') {
13
13
  throw new Error('Cannot stringify non JSON value')
14
14
  }
15
-
16
- if (isObject(value)) {
17
- return Object.keys(value)
18
- .sort()
19
- .reduce(
20
- (result, key) => {
21
- result[key] = (value as any)[key]
22
- return result
23
- },
24
- {} as Record<string, any>,
25
- )
26
- }
27
-
28
15
  return value
29
16
  })
30
17
  }
@@ -67,6 +67,16 @@ export type CacheOptions = {
67
67
  * ```
68
68
  */
69
69
  logger?: (event: CacheEvent) => void
70
+ /**
71
+ * How to clone results on store and on hit so callers can't mutate the shared
72
+ * cached object. Defaults to a `fast-copy` deep clone.
73
+ *
74
+ * Set to `false` to skip cloning entirely (fastest, but the caller MUST treat
75
+ * results as immutable), or pass a custom clone function (e.g. `structuredClone`).
76
+ *
77
+ * @default true
78
+ */
79
+ clone?: boolean | (<T>(value: T) => T)
70
80
  }
71
81
 
72
82
  /**
@@ -139,12 +149,19 @@ class ContextCacheMap {
139
149
  private options: CacheOptions
140
150
  private log: ((event: CacheEvent) => void) | undefined
141
151
  private serialize: (params: Params) => string
152
+ private clone: <T>(value: T) => T
142
153
 
143
154
  constructor(options: CacheOptions) {
144
155
  this.map = options.map
145
156
  this.options = options
146
157
  this.log = options.logger
147
158
  this.serialize = options.serialize ?? stableStringify
159
+ this.clone =
160
+ options.clone === false
161
+ ? (value) => value
162
+ : typeof options.clone === 'function'
163
+ ? options.clone
164
+ : copy
148
165
  }
149
166
 
150
167
  private stringifyCacheKey(context: HookContext) {
@@ -184,10 +201,10 @@ class ContextCacheMap {
184
201
  */
185
202
  async get(context: HookContext) {
186
203
  const key = this.stringifyCacheKey(context)
187
- const result = this.map.get(key)
204
+ const result = await this.map.get(key)
188
205
  if (result) {
189
206
  this.log?.({ type: 'hit', method: context.method, key })
190
- return copy(result) // Use copy to avoid mutation of the original result
207
+ return this.clone(result) // clone to avoid mutation of the cached result
191
208
  }
192
209
  this.log?.({ type: 'miss', method: context.method, key })
193
210
  }
@@ -200,7 +217,8 @@ class ContextCacheMap {
200
217
  async set(context: HookContext) {
201
218
  const key = this.stringifyCacheKey(context)
202
219
  this.log?.({ type: 'set', method: context.method, key })
203
- return this.map.set(key, copy(context.result)) // Use copy to avoid mutation of the original result
220
+ // clone to avoid later mutation of the cached result
221
+ return this.map.set(key, this.clone(context.result))
204
222
  }
205
223
 
206
224
  // Called after create(), update(), patch(), and remove()
@@ -222,6 +240,9 @@ class ContextCacheMap {
222
240
  return context
223
241
  }
224
242
 
243
+ // O(1) membership instead of an O(itemIds) scan per cached key.
244
+ const idSet = new Set<string>(itemIds.map((id: any) => `${id}`))
245
+
225
246
  for (const key of this.map.keys()) {
226
247
  const cachedId = this.getCachedId(key)
227
248
  if (cachedId === 'null') {
@@ -239,12 +260,10 @@ class ContextCacheMap {
239
260
  continue
240
261
  }
241
262
 
242
- for (const itemId of itemIds) {
243
- if (cachedId === itemId) {
244
- // If the cached id matches the item id, delete the cached get
245
- this.log?.({ type: 'invalidate', method: context.method, key })
246
- promises.push(this.map.delete(key))
247
- }
263
+ if (idSet.has(cachedId)) {
264
+ // If the cached id matches a mutated item id, delete the cached get
265
+ this.log?.({ type: 'invalidate', method: context.method, key })
266
+ promises.push(this.map.delete(key))
248
267
  }
249
268
  }
250
269
 
@@ -31,7 +31,9 @@ export const disallow = <H extends HookContext = HookContext>(
31
31
  function hook(context: H): void
32
32
  function hook(context: H, next: NextFunction): Promise<void>
33
33
  function hook(context: H, next?: NextFunction): void | Promise<void> {
34
- if (!transports) {
34
+ // No transports (undefined) or an empty list means "block completely".
35
+ // Fail closed for a guard hook rather than throwing a confusing internal error.
36
+ if (!transports || transportsArr.length === 0) {
35
37
  throw new MethodNotAllowed('Method not allowed')
36
38
  }
37
39
 
@@ -43,6 +43,12 @@ export interface OnDeleteOptions<
43
43
  * @default false
44
44
  */
45
45
  blocking?: boolean
46
+ /**
47
+ * Called when a non-blocking (`blocking: false`) related-service call rejects.
48
+ * Without this, fire-and-forget rejections are swallowed (but never leak as an
49
+ * unhandled rejection). In `blocking` mode the error is thrown to the caller instead.
50
+ */
51
+ onError?: (error: any, context: H) => void
46
52
  }
47
53
 
48
54
  /**
@@ -89,46 +95,60 @@ export const onDelete = <H extends HookContext = HookContext>(
89
95
  return
90
96
  }
91
97
 
92
- const promises: Promise<any>[] = []
93
-
94
- optionsMulti.forEach(
95
- async ({ keyHere, keyThere, onDelete, service, blocking, query }) => {
96
- let ids = result.map((x) => x[keyHere]).filter((x) => !!x)
97
- ids = [...new Set(ids)]
98
-
99
- if (!ids || ids.length <= 0) {
100
- return
101
- }
102
-
103
- const params = {
104
- query: {
105
- ...query,
106
- ...(ids.length === 1
107
- ? { [keyThere]: ids[0] }
108
- : { [keyThere]: { $in: ids } }),
109
- },
110
- paginate: false,
111
- }
112
-
113
- let promise: Promise<any> | undefined = undefined
114
-
115
- if (onDelete === 'cascade') {
116
- promise = context.app.service(service as string).remove(null, params)
117
- } else if (onDelete === 'set null') {
118
- const data = { [keyThere]: null }
119
- promise = context.app
120
- .service(service as string)
121
- .patch(null, data, params)
122
- }
123
-
124
- if (promise && blocking) {
125
- promises.push(promise)
126
- }
127
- },
128
- )
129
-
130
- if (promises.length) {
131
- await Promise.all(promises)
98
+ const blockingPromises: Promise<any>[] = []
99
+
100
+ for (const {
101
+ keyHere,
102
+ keyThere,
103
+ onDelete,
104
+ service,
105
+ blocking,
106
+ query,
107
+ onError,
108
+ } of optionsMulti) {
109
+ let ids = result.map((x) => x[keyHere]).filter((x) => !!x)
110
+ ids = [...new Set(ids)]
111
+
112
+ if (!ids || ids.length <= 0) {
113
+ continue
114
+ }
115
+
116
+ const params = {
117
+ query: {
118
+ ...query,
119
+ ...(ids.length === 1
120
+ ? { [keyThere]: ids[0] }
121
+ : { [keyThere]: { $in: ids } }),
122
+ },
123
+ paginate: false,
124
+ }
125
+
126
+ let promise: Promise<any> | undefined = undefined
127
+
128
+ if (onDelete === 'cascade') {
129
+ promise = context.app.service(service as string).remove(null, params)
130
+ } else if (onDelete === 'set null') {
131
+ const data = { [keyThere]: null }
132
+ promise = context.app
133
+ .service(service as string)
134
+ .patch(null, data, params)
135
+ }
136
+
137
+ if (!promise) {
138
+ continue
139
+ }
140
+
141
+ if (blocking) {
142
+ blockingPromises.push(promise)
143
+ } else {
144
+ // fire-and-forget: always attach a catch so a rejection never becomes
145
+ // an unhandled promise rejection. Surface it via `onError` if provided.
146
+ promise.catch((error) => onError?.(error, context))
147
+ }
148
+ }
149
+
150
+ if (blockingPromises.length) {
151
+ await Promise.all(blockingPromises)
132
152
  }
133
153
 
134
154
  return
@@ -64,6 +64,10 @@ export const paramsForServer = (
64
64
 
65
65
  if (!clonedParams.query[keyToHide]) {
66
66
  clonedParams.query[keyToHide] = {}
67
+ } else {
68
+ // The shallow query copy still aliases a pre-existing key object;
69
+ // clone it so we don't mutate the caller's original params.
70
+ clonedParams.query[keyToHide] = { ...clonedParams.query[keyToHide] }
67
71
  }
68
72
 
69
73
  clonedParams.query[keyToHide][key] = clonedParams[key]
@@ -6,9 +6,23 @@ import type { FeathersError } from '@feathersjs/errors'
6
6
  import { Forbidden } from '@feathersjs/errors'
7
7
  import type { HookContext, NextFunction } from '@feathersjs/feathers'
8
8
 
9
- export interface SetFieldOptions {
10
- as: string
11
- from: string
9
+ export interface SetFieldOptions<H extends HookContext = HookContext> {
10
+ /**
11
+ * The target path(s) to set. Pass an array to write the same value to several
12
+ * paths (e.g. multiple query keys).
13
+ *
14
+ * @example 'params.query.userId'
15
+ * @example ['params.query.userId', 'params.query.ownerId']
16
+ */
17
+ as: string | string[]
18
+ /**
19
+ * The source of the value: a `dot.notation` path on the context, or a function
20
+ * that derives the value from the context.
21
+ *
22
+ * @example 'params.user.id'
23
+ * @example (context) => context.params.user?.id
24
+ */
25
+ from: string | ((context: H) => unknown)
12
26
  /**
13
27
  * If set to `true`, allows the field to be undefined.
14
28
  * If the field is not available and this is `true`, the hook will not throw an error.
@@ -23,7 +37,10 @@ export interface SetFieldOptions {
23
37
  *
24
38
  * If not provided, throws a `Forbidden` error with a message indicating the missing field.
25
39
  */
26
- error?: (context: HookContext, from: string) => FeathersError
40
+ error?: (
41
+ context: H,
42
+ from: string | ((context: H) => unknown),
43
+ ) => FeathersError
27
44
  }
28
45
 
29
46
  /**
@@ -47,7 +64,9 @@ export const setField = <H extends HookContext = HookContext>({
47
64
  from,
48
65
  allowUndefined = false,
49
66
  error,
50
- }: SetFieldOptions) => {
67
+ }: SetFieldOptions<H>) => {
68
+ const targets = Array.isArray(as) ? as : [as]
69
+
51
70
  function hook(context: H): void
52
71
  function hook(context: H, next: NextFunction): Promise<void>
53
72
  function hook(context: H, next?: NextFunction): void | Promise<void> {
@@ -55,7 +74,8 @@ export const setField = <H extends HookContext = HookContext>({
55
74
 
56
75
  checkContext(context, { type: ['before', 'around'], label: 'setField' })
57
76
 
58
- const value = _get(context, from)
77
+ const value =
78
+ typeof from === 'function' ? from(context) : _get(context, from)
59
79
 
60
80
  if (value === undefined) {
61
81
  if (!params.provider || allowUndefined) {
@@ -65,10 +85,12 @@ export const setField = <H extends HookContext = HookContext>({
65
85
 
66
86
  throw error
67
87
  ? error(context, from)
68
- : new Forbidden(`Expected field ${as} not available`)
88
+ : new Forbidden(`Expected field ${targets.join(', ')} not available`)
69
89
  }
70
90
 
71
- _setWith(context, as, value, _clone)
91
+ for (const target of targets) {
92
+ _setWith(context, target, value, _clone)
93
+ }
72
94
 
73
95
  if (next) return next()
74
96