feathers-utils 3.1.0 → 3.1.1

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.md CHANGED
@@ -23,7 +23,9 @@ npm i feathers-utils
23
23
 
24
24
  - `checkMulti`: throws if the request is **multi** data, but the services `allowsMulti(method)` returns `false`
25
25
  - `createRelated`: simply create related items from a hook.
26
+ - `forEach`
26
27
  - `onDelete`: simply remove/set null related items from a hook.
28
+ - `parseFields`
27
29
  - `removeRelated`: simple remove related items from a hook. Basically `cascade` at feathers level.
28
30
  - `runPerItem`: run a function for every item. Meant for `multi:true`.
29
31
  - `setData`: map properties from `context` to `data`. Something like `userId: context.params.user.id`
@@ -43,5 +45,6 @@ npm i feathers-utils
43
45
  - `mergeQuery`: deeply merges queries
44
46
  - `mergeArrays`: merges arrays with intersection options
45
47
  - `pushSet`: if existing array: *push*, else *set*
48
+ - `setQueryKeySafely`
46
49
  - `setResultEmpty`
47
50
  - `shouldSkip`: checks `context.params.skipHooks` for `'all' | '${hookName}' | '${type}:${hookName}'` - also see `markHookForSkip`
package/dist/index.cjs CHANGED
@@ -69,6 +69,7 @@ function mergeArrays(targetArr, sourceArr, handle, prependKey, actionOnEmptyInte
69
69
  const hasOwnProperty = (obj, ...keys) => {
70
70
  return keys.some((x) => Object.prototype.hasOwnProperty.call(obj, x));
71
71
  };
72
+ const isPlainObject$1 = (value) => value && [void 0, Object].includes(value.constructor);
72
73
 
73
74
  function handleArray(target, source, key, options) {
74
75
  const targetVal = _get__default(target, key);
@@ -580,9 +581,9 @@ const shouldSkip = (hookName, context, options) => {
580
581
  return false;
581
582
  };
582
583
 
583
- const isPlainObject$1 = (value) => commons._.isObject(value) && value.constructor === {}.constructor;
584
+ const isPlainObject = (value) => commons._.isObject(value) && value.constructor === {}.constructor;
584
585
  const validateQueryProperty = (query, operators = []) => {
585
- if (!isPlainObject$1(query)) {
586
+ if (!isPlainObject(query)) {
586
587
  return query;
587
588
  }
588
589
  for (const key of Object.keys(query)) {
@@ -590,7 +591,7 @@ const validateQueryProperty = (query, operators = []) => {
590
591
  throw new errors.BadRequest(`Invalid query parameter ${key}`, query);
591
592
  }
592
593
  const value = query[key];
593
- if (isPlainObject$1(value)) {
594
+ if (isPlainObject(value)) {
594
595
  query[key] = validateQueryProperty(value, operators);
595
596
  }
596
597
  }
@@ -606,7 +607,6 @@ const toJSON = (context) => {
606
607
  return context;
607
608
  };
608
609
 
609
- const isPlainObject = (value) => value && [void 0, Object].includes(value.constructor);
610
610
  const setQueryKeySafely = (params, key, value, operator = "$eq", options) => {
611
611
  var _a;
612
612
  const { mutate = false } = options || {};
@@ -626,12 +626,7 @@ const setQueryKeySafely = (params, key, value, operator = "$eq", options) => {
626
626
  }
627
627
  return params;
628
628
  }
629
- console.log(
630
- "params.query[key]",
631
- params.query[key],
632
- isPlainObject(params.query[key])
633
- );
634
- if (isPlainObject(params.query[key]) && !(operator in params.query[key])) {
629
+ if (isPlainObject$1(params.query[key]) && !(operator in params.query[key])) {
635
630
  params.query[key][operator] = value;
636
631
  } else {
637
632
  (_a = params.query).$and ?? (_a.$and = []);
package/dist/index.d.cts CHANGED
@@ -214,7 +214,7 @@ declare const toJSON: (context: HookContext) => HookContext<_feathersjs_feathers
214
214
  type SetQueryKeySafelyOptions = {
215
215
  mutate?: boolean;
216
216
  };
217
- declare const setQueryKeySafely: (params: Params, key: string, value: any, operator?: string, options?: SetQueryKeySafelyOptions) => Params<_feathersjs_feathers.Query>;
217
+ declare const setQueryKeySafely: <P extends Params<_feathersjs_feathers.Query> = Params<_feathersjs_feathers.Query>>(params: P, key: string, value: any, operator?: string, options?: SetQueryKeySafelyOptions) => P;
218
218
 
219
219
  declare const filterArray: <T extends string[]>(...keys: T) => { [key in T[number]]: (value: any, options: FilterQueryOptions$1) => any; };
220
220
 
@@ -266,4 +266,4 @@ type InferRemoveResultFromPath<App extends Application, Path extends string, IdO
266
266
  type InferDataFromPath<App extends Application, Path extends string, Method extends "create" | "update" | "patch"> = Method extends "create" ? InferCreateDataFromPath<App, Path> : Method extends "update" ? InferUpdateDataFromPath<App, Path> : Method extends "patch" ? InferPatchDataFromPath<App, Path> : never;
267
267
  type InferResultFromPath<App extends Application, Path extends string, Method extends "get" | "find" | "create" | "update" | "patch" | "remove"> = Method extends "get" ? InferGetResultFromPath<App, Path> : Method extends "find" ? InferFindResultFromPath<App, Path> : Method extends "create" ? InferCreateResultFromPath<App, Path> : Method extends "update" ? InferUpdateResultFromPath<App, Path> : Method extends "patch" ? InferPatchResultFromPath<App, Path> : Method extends "remove" ? InferRemoveResultFromPath<App, Path> : never;
268
268
 
269
- export { type ActionOnEmptyIntersect, type CreateRelatedOptions, type DebouncedFunctionApp, type DebouncedService, DebouncedStore, type DebouncedStoreOptions, type FilterQueryOptions, type FirstLast, type GetItemsIsArrayOptions, type GetItemsIsArrayResult, type GetService, type Handle, type HookForEachOptions, type HookRunPerItemOptions, type HookSetDataOptions, type InferCreateData, type InferCreateDataFromPath, type InferCreateDataSingle, type InferCreateDataSingleFromPath, type InferCreateResult, type InferCreateResultFromPath, type InferCreateResultSingle, type InferCreateResultSingleFromPath, type InferDataFromPath, type InferFindResult, type InferFindResultFromPath, type InferGetResult, type InferGetResultFromPath, type InferPatchData, type InferPatchDataFromPath, type InferPatchResult, type InferPatchResultFromPath, type InferRemoveResult, type InferRemoveResultFromPath, type InferResultFromPath, type InferUpdateData, type InferUpdateDataFromPath, type InferUpdateResult, type InferUpdateResultFromPath, type InitDebounceMixinOptions, type MergeQueryOptions, type OnDeleteAction, type OnDeleteOptions, type Predicate, type PredicateWithContext, type PushSetOptions, type RemoveRelatedOptions, type ShouldSkipOptions, checkMulti, createRelated, debounceMixin, filterArray, filterObject, filterQuery, forEach, getItemsIsArray, getPaginate, isMulti, isPaginated, makeDefaultOptions, markHookForSkip, mergeArrays, mergeQuery, onDelete, parseFields, pushSet, removeRelated, runPerItem, setData, setQueryKeySafely, setResultEmpty, shouldSkip, toJSON, validateQueryProperty };
269
+ export { type ActionOnEmptyIntersect, type CreateRelatedOptions, type DebouncedFunctionApp, type DebouncedService, DebouncedStore, type DebouncedStoreOptions, type FilterQueryOptions, type FirstLast, type GetItemsIsArrayOptions, type GetItemsIsArrayResult, type GetService, type Handle, type HookForEachOptions, type HookRunPerItemOptions, type HookSetDataOptions, type InferCreateData, type InferCreateDataFromPath, type InferCreateDataSingle, type InferCreateDataSingleFromPath, type InferCreateResult, type InferCreateResultFromPath, type InferCreateResultSingle, type InferCreateResultSingleFromPath, type InferDataFromPath, type InferFindResult, type InferFindResultFromPath, type InferGetResult, type InferGetResultFromPath, type InferPatchData, type InferPatchDataFromPath, type InferPatchResult, type InferPatchResultFromPath, type InferRemoveResult, type InferRemoveResultFromPath, type InferResultFromPath, type InferUpdateData, type InferUpdateDataFromPath, type InferUpdateResult, type InferUpdateResultFromPath, type InitDebounceMixinOptions, type MergeQueryOptions, type OnDeleteAction, type OnDeleteOptions, type Predicate, type PredicateWithContext, type PushSetOptions, type RemoveRelatedOptions, type SetQueryKeySafelyOptions, type ShouldSkipOptions, checkMulti, createRelated, debounceMixin, filterArray, filterObject, filterQuery, forEach, getItemsIsArray, getPaginate, isMulti, isPaginated, makeDefaultOptions, markHookForSkip, mergeArrays, mergeQuery, onDelete, parseFields, pushSet, removeRelated, runPerItem, setData, setQueryKeySafely, setResultEmpty, shouldSkip, toJSON, validateQueryProperty };
package/dist/index.d.mts CHANGED
@@ -214,7 +214,7 @@ declare const toJSON: (context: HookContext) => HookContext<_feathersjs_feathers
214
214
  type SetQueryKeySafelyOptions = {
215
215
  mutate?: boolean;
216
216
  };
217
- declare const setQueryKeySafely: (params: Params, key: string, value: any, operator?: string, options?: SetQueryKeySafelyOptions) => Params<_feathersjs_feathers.Query>;
217
+ declare const setQueryKeySafely: <P extends Params<_feathersjs_feathers.Query> = Params<_feathersjs_feathers.Query>>(params: P, key: string, value: any, operator?: string, options?: SetQueryKeySafelyOptions) => P;
218
218
 
219
219
  declare const filterArray: <T extends string[]>(...keys: T) => { [key in T[number]]: (value: any, options: FilterQueryOptions$1) => any; };
220
220
 
@@ -266,4 +266,4 @@ type InferRemoveResultFromPath<App extends Application, Path extends string, IdO
266
266
  type InferDataFromPath<App extends Application, Path extends string, Method extends "create" | "update" | "patch"> = Method extends "create" ? InferCreateDataFromPath<App, Path> : Method extends "update" ? InferUpdateDataFromPath<App, Path> : Method extends "patch" ? InferPatchDataFromPath<App, Path> : never;
267
267
  type InferResultFromPath<App extends Application, Path extends string, Method extends "get" | "find" | "create" | "update" | "patch" | "remove"> = Method extends "get" ? InferGetResultFromPath<App, Path> : Method extends "find" ? InferFindResultFromPath<App, Path> : Method extends "create" ? InferCreateResultFromPath<App, Path> : Method extends "update" ? InferUpdateResultFromPath<App, Path> : Method extends "patch" ? InferPatchResultFromPath<App, Path> : Method extends "remove" ? InferRemoveResultFromPath<App, Path> : never;
268
268
 
269
- export { type ActionOnEmptyIntersect, type CreateRelatedOptions, type DebouncedFunctionApp, type DebouncedService, DebouncedStore, type DebouncedStoreOptions, type FilterQueryOptions, type FirstLast, type GetItemsIsArrayOptions, type GetItemsIsArrayResult, type GetService, type Handle, type HookForEachOptions, type HookRunPerItemOptions, type HookSetDataOptions, type InferCreateData, type InferCreateDataFromPath, type InferCreateDataSingle, type InferCreateDataSingleFromPath, type InferCreateResult, type InferCreateResultFromPath, type InferCreateResultSingle, type InferCreateResultSingleFromPath, type InferDataFromPath, type InferFindResult, type InferFindResultFromPath, type InferGetResult, type InferGetResultFromPath, type InferPatchData, type InferPatchDataFromPath, type InferPatchResult, type InferPatchResultFromPath, type InferRemoveResult, type InferRemoveResultFromPath, type InferResultFromPath, type InferUpdateData, type InferUpdateDataFromPath, type InferUpdateResult, type InferUpdateResultFromPath, type InitDebounceMixinOptions, type MergeQueryOptions, type OnDeleteAction, type OnDeleteOptions, type Predicate, type PredicateWithContext, type PushSetOptions, type RemoveRelatedOptions, type ShouldSkipOptions, checkMulti, createRelated, debounceMixin, filterArray, filterObject, filterQuery, forEach, getItemsIsArray, getPaginate, isMulti, isPaginated, makeDefaultOptions, markHookForSkip, mergeArrays, mergeQuery, onDelete, parseFields, pushSet, removeRelated, runPerItem, setData, setQueryKeySafely, setResultEmpty, shouldSkip, toJSON, validateQueryProperty };
269
+ export { type ActionOnEmptyIntersect, type CreateRelatedOptions, type DebouncedFunctionApp, type DebouncedService, DebouncedStore, type DebouncedStoreOptions, type FilterQueryOptions, type FirstLast, type GetItemsIsArrayOptions, type GetItemsIsArrayResult, type GetService, type Handle, type HookForEachOptions, type HookRunPerItemOptions, type HookSetDataOptions, type InferCreateData, type InferCreateDataFromPath, type InferCreateDataSingle, type InferCreateDataSingleFromPath, type InferCreateResult, type InferCreateResultFromPath, type InferCreateResultSingle, type InferCreateResultSingleFromPath, type InferDataFromPath, type InferFindResult, type InferFindResultFromPath, type InferGetResult, type InferGetResultFromPath, type InferPatchData, type InferPatchDataFromPath, type InferPatchResult, type InferPatchResultFromPath, type InferRemoveResult, type InferRemoveResultFromPath, type InferResultFromPath, type InferUpdateData, type InferUpdateDataFromPath, type InferUpdateResult, type InferUpdateResultFromPath, type InitDebounceMixinOptions, type MergeQueryOptions, type OnDeleteAction, type OnDeleteOptions, type Predicate, type PredicateWithContext, type PushSetOptions, type RemoveRelatedOptions, type SetQueryKeySafelyOptions, type ShouldSkipOptions, checkMulti, createRelated, debounceMixin, filterArray, filterObject, filterQuery, forEach, getItemsIsArray, getPaginate, isMulti, isPaginated, makeDefaultOptions, markHookForSkip, mergeArrays, mergeQuery, onDelete, parseFields, pushSet, removeRelated, runPerItem, setData, setQueryKeySafely, setResultEmpty, shouldSkip, toJSON, validateQueryProperty };
package/dist/index.d.ts CHANGED
@@ -214,7 +214,7 @@ declare const toJSON: (context: HookContext) => HookContext<_feathersjs_feathers
214
214
  type SetQueryKeySafelyOptions = {
215
215
  mutate?: boolean;
216
216
  };
217
- declare const setQueryKeySafely: (params: Params, key: string, value: any, operator?: string, options?: SetQueryKeySafelyOptions) => Params<_feathersjs_feathers.Query>;
217
+ declare const setQueryKeySafely: <P extends Params<_feathersjs_feathers.Query> = Params<_feathersjs_feathers.Query>>(params: P, key: string, value: any, operator?: string, options?: SetQueryKeySafelyOptions) => P;
218
218
 
219
219
  declare const filterArray: <T extends string[]>(...keys: T) => { [key in T[number]]: (value: any, options: FilterQueryOptions$1) => any; };
220
220
 
@@ -266,4 +266,4 @@ type InferRemoveResultFromPath<App extends Application, Path extends string, IdO
266
266
  type InferDataFromPath<App extends Application, Path extends string, Method extends "create" | "update" | "patch"> = Method extends "create" ? InferCreateDataFromPath<App, Path> : Method extends "update" ? InferUpdateDataFromPath<App, Path> : Method extends "patch" ? InferPatchDataFromPath<App, Path> : never;
267
267
  type InferResultFromPath<App extends Application, Path extends string, Method extends "get" | "find" | "create" | "update" | "patch" | "remove"> = Method extends "get" ? InferGetResultFromPath<App, Path> : Method extends "find" ? InferFindResultFromPath<App, Path> : Method extends "create" ? InferCreateResultFromPath<App, Path> : Method extends "update" ? InferUpdateResultFromPath<App, Path> : Method extends "patch" ? InferPatchResultFromPath<App, Path> : Method extends "remove" ? InferRemoveResultFromPath<App, Path> : never;
268
268
 
269
- export { type ActionOnEmptyIntersect, type CreateRelatedOptions, type DebouncedFunctionApp, type DebouncedService, DebouncedStore, type DebouncedStoreOptions, type FilterQueryOptions, type FirstLast, type GetItemsIsArrayOptions, type GetItemsIsArrayResult, type GetService, type Handle, type HookForEachOptions, type HookRunPerItemOptions, type HookSetDataOptions, type InferCreateData, type InferCreateDataFromPath, type InferCreateDataSingle, type InferCreateDataSingleFromPath, type InferCreateResult, type InferCreateResultFromPath, type InferCreateResultSingle, type InferCreateResultSingleFromPath, type InferDataFromPath, type InferFindResult, type InferFindResultFromPath, type InferGetResult, type InferGetResultFromPath, type InferPatchData, type InferPatchDataFromPath, type InferPatchResult, type InferPatchResultFromPath, type InferRemoveResult, type InferRemoveResultFromPath, type InferResultFromPath, type InferUpdateData, type InferUpdateDataFromPath, type InferUpdateResult, type InferUpdateResultFromPath, type InitDebounceMixinOptions, type MergeQueryOptions, type OnDeleteAction, type OnDeleteOptions, type Predicate, type PredicateWithContext, type PushSetOptions, type RemoveRelatedOptions, type ShouldSkipOptions, checkMulti, createRelated, debounceMixin, filterArray, filterObject, filterQuery, forEach, getItemsIsArray, getPaginate, isMulti, isPaginated, makeDefaultOptions, markHookForSkip, mergeArrays, mergeQuery, onDelete, parseFields, pushSet, removeRelated, runPerItem, setData, setQueryKeySafely, setResultEmpty, shouldSkip, toJSON, validateQueryProperty };
269
+ export { type ActionOnEmptyIntersect, type CreateRelatedOptions, type DebouncedFunctionApp, type DebouncedService, DebouncedStore, type DebouncedStoreOptions, type FilterQueryOptions, type FirstLast, type GetItemsIsArrayOptions, type GetItemsIsArrayResult, type GetService, type Handle, type HookForEachOptions, type HookRunPerItemOptions, type HookSetDataOptions, type InferCreateData, type InferCreateDataFromPath, type InferCreateDataSingle, type InferCreateDataSingleFromPath, type InferCreateResult, type InferCreateResultFromPath, type InferCreateResultSingle, type InferCreateResultSingleFromPath, type InferDataFromPath, type InferFindResult, type InferFindResultFromPath, type InferGetResult, type InferGetResultFromPath, type InferPatchData, type InferPatchDataFromPath, type InferPatchResult, type InferPatchResultFromPath, type InferRemoveResult, type InferRemoveResultFromPath, type InferResultFromPath, type InferUpdateData, type InferUpdateDataFromPath, type InferUpdateResult, type InferUpdateResultFromPath, type InitDebounceMixinOptions, type MergeQueryOptions, type OnDeleteAction, type OnDeleteOptions, type Predicate, type PredicateWithContext, type PushSetOptions, type RemoveRelatedOptions, type SetQueryKeySafelyOptions, type ShouldSkipOptions, checkMulti, createRelated, debounceMixin, filterArray, filterObject, filterQuery, forEach, getItemsIsArray, getPaginate, isMulti, isPaginated, makeDefaultOptions, markHookForSkip, mergeArrays, mergeQuery, onDelete, parseFields, pushSet, removeRelated, runPerItem, setData, setQueryKeySafely, setResultEmpty, shouldSkip, toJSON, validateQueryProperty };
package/dist/index.mjs CHANGED
@@ -55,6 +55,7 @@ function mergeArrays(targetArr, sourceArr, handle, prependKey, actionOnEmptyInte
55
55
  const hasOwnProperty = (obj, ...keys) => {
56
56
  return keys.some((x) => Object.prototype.hasOwnProperty.call(obj, x));
57
57
  };
58
+ const isPlainObject$1 = (value) => value && [void 0, Object].includes(value.constructor);
58
59
 
59
60
  function handleArray(target, source, key, options) {
60
61
  const targetVal = _get(target, key);
@@ -566,9 +567,9 @@ const shouldSkip = (hookName, context, options) => {
566
567
  return false;
567
568
  };
568
569
 
569
- const isPlainObject$1 = (value) => _.isObject(value) && value.constructor === {}.constructor;
570
+ const isPlainObject = (value) => _.isObject(value) && value.constructor === {}.constructor;
570
571
  const validateQueryProperty = (query, operators = []) => {
571
- if (!isPlainObject$1(query)) {
572
+ if (!isPlainObject(query)) {
572
573
  return query;
573
574
  }
574
575
  for (const key of Object.keys(query)) {
@@ -576,7 +577,7 @@ const validateQueryProperty = (query, operators = []) => {
576
577
  throw new BadRequest(`Invalid query parameter ${key}`, query);
577
578
  }
578
579
  const value = query[key];
579
- if (isPlainObject$1(value)) {
580
+ if (isPlainObject(value)) {
580
581
  query[key] = validateQueryProperty(value, operators);
581
582
  }
582
583
  }
@@ -592,7 +593,6 @@ const toJSON = (context) => {
592
593
  return context;
593
594
  };
594
595
 
595
- const isPlainObject = (value) => value && [void 0, Object].includes(value.constructor);
596
596
  const setQueryKeySafely = (params, key, value, operator = "$eq", options) => {
597
597
  var _a;
598
598
  const { mutate = false } = options || {};
@@ -612,12 +612,7 @@ const setQueryKeySafely = (params, key, value, operator = "$eq", options) => {
612
612
  }
613
613
  return params;
614
614
  }
615
- console.log(
616
- "params.query[key]",
617
- params.query[key],
618
- isPlainObject(params.query[key])
619
- );
620
- if (isPlainObject(params.query[key]) && !(operator in params.query[key])) {
615
+ if (isPlainObject$1(params.query[key]) && !(operator in params.query[key])) {
621
616
  params.query[key][operator] = value;
622
617
  } else {
623
618
  (_a = params.query).$and ?? (_a.$and = []);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "feathers-utils",
3
- "version": "3.1.0",
3
+ "version": "3.1.1",
4
4
  "description": "Some utils for projects using '@feathersjs/feathers'",
5
5
  "author": "fratzinger",
6
6
  "repository": {
@@ -4,3 +4,6 @@ export const hasOwnProperty = (
4
4
  ): boolean => {
5
5
  return keys.some((x) => Object.prototype.hasOwnProperty.call(obj, x));
6
6
  };
7
+
8
+ export const isPlainObject = (value) =>
9
+ value && [undefined, Object].includes(value.constructor);
@@ -1,19 +1,17 @@
1
+ import { isPlainObject } from "./internal.utils";
1
2
  import type { Params } from "@feathersjs/feathers";
2
3
 
3
- type SetQueryKeySafelyOptions = {
4
+ export type SetQueryKeySafelyOptions = {
4
5
  mutate?: boolean;
5
6
  };
6
7
 
7
- const isPlainObject = (value) =>
8
- value && [undefined, Object].includes(value.constructor);
9
-
10
- export const setQueryKeySafely = (
11
- params: Params,
8
+ export const setQueryKeySafely = <P extends Params = Params>(
9
+ params: P,
12
10
  key: string,
13
11
  value: any,
14
12
  operator = "$eq",
15
13
  options?: SetQueryKeySafelyOptions,
16
- ) => {
14
+ ): P => {
17
15
  const { mutate = false } = options || {};
18
16
 
19
17
  // TODO: mutate params
@@ -38,12 +36,6 @@ export const setQueryKeySafely = (
38
36
  return params;
39
37
  }
40
38
 
41
- console.log(
42
- "params.query[key]",
43
- params.query[key],
44
- isPlainObject(params.query[key]),
45
- );
46
-
47
39
  if (isPlainObject(params.query[key]) && !(operator in params.query[key])) {
48
40
  params.query[key][operator] = value;
49
41
  } else {