feathers-utils 3.1.0 → 3.1.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.
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
@@ -13,7 +13,7 @@ declare function checkMulti<H extends HookContext = HookContext>(): (context: H)
13
13
  type MaybeArray<T> = T | T[];
14
14
  type Promisable<T> = T | Promise<T>;
15
15
  type Path = Array<string | number>;
16
- type ReturnAsyncHook = (context: HookContext$1) => Promise<HookContext$1>;
16
+ type ReturnAsyncHook<H extends HookContext$1 = HookContext$1> = (context: H) => Promise<H>;
17
17
 
18
18
  interface CreateRelatedOptions<S = Record<string, any>> {
19
19
  service: keyof S;
@@ -39,7 +39,7 @@ interface HookForEachOptions {
39
39
  wait?: "sequential" | "parallel" | false;
40
40
  items?: GetItemsIsArrayOptions["from"];
41
41
  }
42
- declare const forEach: (actionPerItem: (item: any, context: HookContext) => Promisable<any>, _options?: HookForEachOptions) => ReturnAsyncHook;
42
+ declare const forEach: <H extends HookContext<_feathersjs_feathers.Application<any, any>, any> = HookContext<_feathersjs_feathers.Application<any, any>, any>, T = any>(actionPerItem: (item: T, context: H) => Promisable<any>, _options?: HookForEachOptions) => ReturnAsyncHook<H>;
43
43
 
44
44
  type OnDeleteAction = "cascade" | "set null";
45
45
  interface OnDeleteOptions {
@@ -79,7 +79,7 @@ interface HookRunPerItemOptions {
79
79
  * hook to run a hook for each item in the context
80
80
  * uses `context.result` if it is existent. otherwise uses context.data
81
81
  */
82
- declare const runPerItem: <H extends HookContext<_feathersjs_feathers.Application<any, any>, any> = HookContext<_feathersjs_feathers.Application<any, any>, any>>(actionPerItem: (item: any, context: H) => Promisable<any>, _options?: HookRunPerItemOptions) => (context: H) => Promise<H>;
82
+ declare const runPerItem: <H extends HookContext<_feathersjs_feathers.Application<any, any>, any> = HookContext<_feathersjs_feathers.Application<any, any>, any>, T = any>(actionPerItem: (item: T, context: H) => Promisable<any>, _options?: HookRunPerItemOptions) => (context: H) => Promise<H>;
83
83
 
84
84
  type Predicate<T = any> = (item: T) => boolean;
85
85
  type PredicateWithContext<T = any> = (item: T, context: HookContext) => boolean;
@@ -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
@@ -13,7 +13,7 @@ declare function checkMulti<H extends HookContext = HookContext>(): (context: H)
13
13
  type MaybeArray<T> = T | T[];
14
14
  type Promisable<T> = T | Promise<T>;
15
15
  type Path = Array<string | number>;
16
- type ReturnAsyncHook = (context: HookContext$1) => Promise<HookContext$1>;
16
+ type ReturnAsyncHook<H extends HookContext$1 = HookContext$1> = (context: H) => Promise<H>;
17
17
 
18
18
  interface CreateRelatedOptions<S = Record<string, any>> {
19
19
  service: keyof S;
@@ -39,7 +39,7 @@ interface HookForEachOptions {
39
39
  wait?: "sequential" | "parallel" | false;
40
40
  items?: GetItemsIsArrayOptions["from"];
41
41
  }
42
- declare const forEach: (actionPerItem: (item: any, context: HookContext) => Promisable<any>, _options?: HookForEachOptions) => ReturnAsyncHook;
42
+ declare const forEach: <H extends HookContext<_feathersjs_feathers.Application<any, any>, any> = HookContext<_feathersjs_feathers.Application<any, any>, any>, T = any>(actionPerItem: (item: T, context: H) => Promisable<any>, _options?: HookForEachOptions) => ReturnAsyncHook<H>;
43
43
 
44
44
  type OnDeleteAction = "cascade" | "set null";
45
45
  interface OnDeleteOptions {
@@ -79,7 +79,7 @@ interface HookRunPerItemOptions {
79
79
  * hook to run a hook for each item in the context
80
80
  * uses `context.result` if it is existent. otherwise uses context.data
81
81
  */
82
- declare const runPerItem: <H extends HookContext<_feathersjs_feathers.Application<any, any>, any> = HookContext<_feathersjs_feathers.Application<any, any>, any>>(actionPerItem: (item: any, context: H) => Promisable<any>, _options?: HookRunPerItemOptions) => (context: H) => Promise<H>;
82
+ declare const runPerItem: <H extends HookContext<_feathersjs_feathers.Application<any, any>, any> = HookContext<_feathersjs_feathers.Application<any, any>, any>, T = any>(actionPerItem: (item: T, context: H) => Promisable<any>, _options?: HookRunPerItemOptions) => (context: H) => Promise<H>;
83
83
 
84
84
  type Predicate<T = any> = (item: T) => boolean;
85
85
  type PredicateWithContext<T = any> = (item: T, context: HookContext) => boolean;
@@ -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
@@ -13,7 +13,7 @@ declare function checkMulti<H extends HookContext = HookContext>(): (context: H)
13
13
  type MaybeArray<T> = T | T[];
14
14
  type Promisable<T> = T | Promise<T>;
15
15
  type Path = Array<string | number>;
16
- type ReturnAsyncHook = (context: HookContext$1) => Promise<HookContext$1>;
16
+ type ReturnAsyncHook<H extends HookContext$1 = HookContext$1> = (context: H) => Promise<H>;
17
17
 
18
18
  interface CreateRelatedOptions<S = Record<string, any>> {
19
19
  service: keyof S;
@@ -39,7 +39,7 @@ interface HookForEachOptions {
39
39
  wait?: "sequential" | "parallel" | false;
40
40
  items?: GetItemsIsArrayOptions["from"];
41
41
  }
42
- declare const forEach: (actionPerItem: (item: any, context: HookContext) => Promisable<any>, _options?: HookForEachOptions) => ReturnAsyncHook;
42
+ declare const forEach: <H extends HookContext<_feathersjs_feathers.Application<any, any>, any> = HookContext<_feathersjs_feathers.Application<any, any>, any>, T = any>(actionPerItem: (item: T, context: H) => Promisable<any>, _options?: HookForEachOptions) => ReturnAsyncHook<H>;
43
43
 
44
44
  type OnDeleteAction = "cascade" | "set null";
45
45
  interface OnDeleteOptions {
@@ -79,7 +79,7 @@ interface HookRunPerItemOptions {
79
79
  * hook to run a hook for each item in the context
80
80
  * uses `context.result` if it is existent. otherwise uses context.data
81
81
  */
82
- declare const runPerItem: <H extends HookContext<_feathersjs_feathers.Application<any, any>, any> = HookContext<_feathersjs_feathers.Application<any, any>, any>>(actionPerItem: (item: any, context: H) => Promisable<any>, _options?: HookRunPerItemOptions) => (context: H) => Promise<H>;
82
+ declare const runPerItem: <H extends HookContext<_feathersjs_feathers.Application<any, any>, any> = HookContext<_feathersjs_feathers.Application<any, any>, any>, T = any>(actionPerItem: (item: T, context: H) => Promisable<any>, _options?: HookRunPerItemOptions) => (context: H) => Promise<H>;
83
83
 
84
84
  type Predicate<T = any> = (item: T) => boolean;
85
85
  type PredicateWithContext<T = any> = (item: T, context: HookContext) => boolean;
@@ -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.2",
4
4
  "description": "Some utils for projects using '@feathersjs/feathers'",
5
5
  "author": "fratzinger",
6
6
  "repository": {
@@ -40,29 +40,29 @@
40
40
  "lint": "eslint . --ext .js,.jsx,.ts,.tsx"
41
41
  },
42
42
  "dependencies": {
43
- "@feathersjs/adapter-commons": "^5.0.10",
44
- "@feathersjs/commons": "^5.0.10",
45
- "@feathersjs/errors": "^5.0.10",
43
+ "@feathersjs/adapter-commons": "^5.0.11",
44
+ "@feathersjs/commons": "^5.0.11",
45
+ "@feathersjs/errors": "^5.0.11",
46
46
  "fast-equals": "^5.0.1",
47
47
  "feathers-hooks-common": "^8.1.1",
48
48
  "lodash": "^4.17.21"
49
49
  },
50
50
  "devDependencies": {
51
- "@feathersjs/feathers": "^5.0.10",
52
- "@feathersjs/memory": "^5.0.10",
53
- "@types/lodash": "^4.14.199",
54
- "@types/node": "^20.8.3",
55
- "@typescript-eslint/eslint-plugin": "^6.7.4",
56
- "@typescript-eslint/parser": "^6.7.4",
51
+ "@feathersjs/feathers": "^5.0.11",
52
+ "@feathersjs/memory": "^5.0.11",
53
+ "@types/lodash": "^4.14.201",
54
+ "@types/node": "^20.9.0",
55
+ "@typescript-eslint/eslint-plugin": "^6.10.0",
56
+ "@typescript-eslint/parser": "^6.10.0",
57
57
  "@vitest/coverage-v8": "^0.34.6",
58
- "eslint": "^8.51.0",
58
+ "eslint": "^8.53.0",
59
59
  "eslint-config-prettier": "^9.0.0",
60
60
  "eslint-import-resolver-typescript": "^3.6.1",
61
- "eslint-plugin-import": "^2.28.1",
62
- "eslint-plugin-prettier": "^5.0.0",
61
+ "eslint-plugin-import": "^2.29.0",
62
+ "eslint-plugin-prettier": "^5.0.1",
63
63
  "eslint-plugin-security": "^1.7.1",
64
64
  "np": "^8.0.4",
65
- "prettier": "^3.0.3",
65
+ "prettier": "^3.1.0",
66
66
  "shx": "^0.3.4",
67
67
  "typescript": "^5.2.2",
68
68
  "unbuild": "^2.0.0",
@@ -10,18 +10,18 @@ export interface HookForEachOptions {
10
10
  items?: GetItemsIsArrayOptions["from"];
11
11
  }
12
12
 
13
- export const forEach = (
13
+ export const forEach = <H extends HookContext = HookContext, T = any>(
14
14
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
- actionPerItem: (item: any, context: HookContext) => Promisable<any>,
15
+ actionPerItem: (item: T, context: H) => Promisable<any>,
16
16
  _options?: HookForEachOptions,
17
- ): ReturnAsyncHook => {
17
+ ): ReturnAsyncHook<H> => {
18
18
  const options: Required<HookForEachOptions> = {
19
19
  wait: "parallel",
20
20
  items: "automatic",
21
21
  ..._options,
22
22
  };
23
23
 
24
- return async (context: HookContext): Promise<HookContext> => {
24
+ return async (context: H): Promise<H> => {
25
25
  if (shouldSkip("runForItems", context)) {
26
26
  return context;
27
27
  }
@@ -21,9 +21,9 @@ const makeOptions = (
21
21
  * hook to run a hook for each item in the context
22
22
  * uses `context.result` if it is existent. otherwise uses context.data
23
23
  */
24
- export const runPerItem = <H extends HookContext = HookContext>(
24
+ export const runPerItem = <H extends HookContext = HookContext, T = any>(
25
25
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
26
- actionPerItem: (item: any, context: H) => Promisable<any>,
26
+ actionPerItem: (item: T, context: H) => Promisable<any>,
27
27
  _options?: HookRunPerItemOptions,
28
28
  ) => {
29
29
  const options = makeOptions(_options);
@@ -34,7 +34,7 @@ export const runPerItem = <H extends HookContext = HookContext>(
34
34
 
35
35
  const { items } = getItemsIsArray(context);
36
36
 
37
- const promises = items.map(async (item: unknown) => {
37
+ const promises = items.map(async (item: T) => {
38
38
  await actionPerItem(item, context);
39
39
  });
40
40
 
@@ -15,5 +15,10 @@ export type ServiceMethodName =
15
15
  | "update"
16
16
  | "patch"
17
17
  | "remove";
18
- export type ReturnSyncHook = (context: HookContext) => HookContext;
19
- export type ReturnAsyncHook = (context: HookContext) => Promise<HookContext>;
18
+
19
+ export type ReturnSyncHook<H extends HookContext = HookContext> = (
20
+ context: H,
21
+ ) => H;
22
+ export type ReturnAsyncHook<H extends HookContext = HookContext> = (
23
+ context: H,
24
+ ) => Promise<H>;
@@ -41,8 +41,8 @@ export type InferCreateResult<S, D = unknown> = S extends {
41
41
  ? D extends any[]
42
42
  ? AsArray<Awaited<R>>
43
43
  : D extends InferCreateDataSingle<S>
44
- ? Single<Awaited<R>>
45
- : Awaited<R>
44
+ ? Single<Awaited<R>>
45
+ : Awaited<R>
46
46
  : never;
47
47
 
48
48
  export type InferCreateResultSingle<S> = Single<InferCreateResult<S>>;
@@ -59,8 +59,8 @@ export type InferPatchResult<S, IdOrNullable = any> = S extends {
59
59
  ? IdOrNullable extends Id
60
60
  ? Single<Awaited<R>>
61
61
  : IdOrNullable extends null
62
- ? AsArray<Awaited<R>>
63
- : Awaited<R>
62
+ ? AsArray<Awaited<R>>
63
+ : Awaited<R>
64
64
  : never;
65
65
 
66
66
  export type InferRemoveResult<S, IdOrNullable = any> = S extends {
@@ -69,8 +69,8 @@ export type InferRemoveResult<S, IdOrNullable = any> = S extends {
69
69
  ? IdOrNullable extends Id
70
70
  ? Single<Awaited<R>>
71
71
  : IdOrNullable extends null
72
- ? AsArray<Awaited<R>>
73
- : Awaited<R>
72
+ ? AsArray<Awaited<R>>
73
+ : Awaited<R>
74
74
  : never;
75
75
 
76
76
  export type GetService<
@@ -138,10 +138,10 @@ export type InferDataFromPath<
138
138
  > = Method extends "create"
139
139
  ? InferCreateDataFromPath<App, Path>
140
140
  : Method extends "update"
141
- ? InferUpdateDataFromPath<App, Path>
142
- : Method extends "patch"
143
- ? InferPatchDataFromPath<App, Path>
144
- : never;
141
+ ? InferUpdateDataFromPath<App, Path>
142
+ : Method extends "patch"
143
+ ? InferPatchDataFromPath<App, Path>
144
+ : never;
145
145
 
146
146
  export type InferResultFromPath<
147
147
  App extends Application,
@@ -150,13 +150,13 @@ export type InferResultFromPath<
150
150
  > = Method extends "get"
151
151
  ? InferGetResultFromPath<App, Path>
152
152
  : Method extends "find"
153
- ? InferFindResultFromPath<App, Path>
154
- : Method extends "create"
155
- ? InferCreateResultFromPath<App, Path>
156
- : Method extends "update"
157
- ? InferUpdateResultFromPath<App, Path>
158
- : Method extends "patch"
159
- ? InferPatchResultFromPath<App, Path>
160
- : Method extends "remove"
161
- ? InferRemoveResultFromPath<App, Path>
162
- : never;
153
+ ? InferFindResultFromPath<App, Path>
154
+ : Method extends "create"
155
+ ? InferCreateResultFromPath<App, Path>
156
+ : Method extends "update"
157
+ ? InferUpdateResultFromPath<App, Path>
158
+ : Method extends "patch"
159
+ ? InferPatchResultFromPath<App, Path>
160
+ : Method extends "remove"
161
+ ? InferRemoveResultFromPath<App, Path>
162
+ : never;
@@ -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 {