feathers-utils 3.1.1 → 3.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -459,12 +459,14 @@ const getItemsIsArray = (context, options) => {
459
459
  let itemOrItems;
460
460
  if (from === "automatic") {
461
461
  itemOrItems = context.type === "before" ? context.data : context.result;
462
- itemOrItems = itemOrItems && context.method === "find" ? itemOrItems.data || itemOrItems : itemOrItems;
463
462
  } else if (from === "data") {
464
463
  itemOrItems = context.data;
465
464
  } else if (from === "result") {
466
465
  itemOrItems = context.result;
467
466
  }
467
+ if ((from === "automatic" || from === "result") && context.type === "after") {
468
+ itemOrItems = itemOrItems && context.method === "find" ? itemOrItems.data || itemOrItems : itemOrItems;
469
+ }
468
470
  const isArray = Array.isArray(itemOrItems);
469
471
  return {
470
472
  items: isArray ? itemOrItems : itemOrItems != null ? [itemOrItems] : [],
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;
@@ -26,8 +26,9 @@ interface CreateRelatedOptions<S = Record<string, any>> {
26
26
  */
27
27
  declare function createRelated<S = Record<string, any>, H extends HookContext = HookContext>({ service, multi, data, createItemsInDataArraySeparately, }: CreateRelatedOptions<S>): (context: H) => Promise<H>;
28
28
 
29
+ type GetItemsIsArrayFrom = "data" | "result" | "automatic";
29
30
  type GetItemsIsArrayOptions = {
30
- from: "data" | "result" | "automatic";
31
+ from?: GetItemsIsArrayFrom;
31
32
  };
32
33
  interface GetItemsIsArrayResult<T = any> {
33
34
  items: T[];
@@ -39,7 +40,7 @@ interface HookForEachOptions {
39
40
  wait?: "sequential" | "parallel" | false;
40
41
  items?: GetItemsIsArrayOptions["from"];
41
42
  }
42
- declare const forEach: (actionPerItem: (item: any, context: HookContext) => Promisable<any>, _options?: HookForEachOptions) => ReturnAsyncHook;
43
+ 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
44
 
44
45
  type OnDeleteAction = "cascade" | "set null";
45
46
  interface OnDeleteOptions {
@@ -79,7 +80,7 @@ interface HookRunPerItemOptions {
79
80
  * hook to run a hook for each item in the context
80
81
  * uses `context.result` if it is existent. otherwise uses context.data
81
82
  */
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>;
83
+ 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
84
 
84
85
  type Predicate<T = any> = (item: T) => boolean;
85
86
  type PredicateWithContext<T = any> = (item: T, context: HookContext) => boolean;
@@ -266,4 +267,4 @@ type InferRemoveResultFromPath<App extends Application, Path extends string, IdO
266
267
  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
268
  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
269
 
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 };
270
+ export { type ActionOnEmptyIntersect, type CreateRelatedOptions, type DebouncedFunctionApp, type DebouncedService, DebouncedStore, type DebouncedStoreOptions, type FilterQueryOptions, type FirstLast, type GetItemsIsArrayFrom, 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;
@@ -26,8 +26,9 @@ interface CreateRelatedOptions<S = Record<string, any>> {
26
26
  */
27
27
  declare function createRelated<S = Record<string, any>, H extends HookContext = HookContext>({ service, multi, data, createItemsInDataArraySeparately, }: CreateRelatedOptions<S>): (context: H) => Promise<H>;
28
28
 
29
+ type GetItemsIsArrayFrom = "data" | "result" | "automatic";
29
30
  type GetItemsIsArrayOptions = {
30
- from: "data" | "result" | "automatic";
31
+ from?: GetItemsIsArrayFrom;
31
32
  };
32
33
  interface GetItemsIsArrayResult<T = any> {
33
34
  items: T[];
@@ -39,7 +40,7 @@ interface HookForEachOptions {
39
40
  wait?: "sequential" | "parallel" | false;
40
41
  items?: GetItemsIsArrayOptions["from"];
41
42
  }
42
- declare const forEach: (actionPerItem: (item: any, context: HookContext) => Promisable<any>, _options?: HookForEachOptions) => ReturnAsyncHook;
43
+ 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
44
 
44
45
  type OnDeleteAction = "cascade" | "set null";
45
46
  interface OnDeleteOptions {
@@ -79,7 +80,7 @@ interface HookRunPerItemOptions {
79
80
  * hook to run a hook for each item in the context
80
81
  * uses `context.result` if it is existent. otherwise uses context.data
81
82
  */
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>;
83
+ 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
84
 
84
85
  type Predicate<T = any> = (item: T) => boolean;
85
86
  type PredicateWithContext<T = any> = (item: T, context: HookContext) => boolean;
@@ -266,4 +267,4 @@ type InferRemoveResultFromPath<App extends Application, Path extends string, IdO
266
267
  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
268
  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
269
 
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 };
270
+ export { type ActionOnEmptyIntersect, type CreateRelatedOptions, type DebouncedFunctionApp, type DebouncedService, DebouncedStore, type DebouncedStoreOptions, type FilterQueryOptions, type FirstLast, type GetItemsIsArrayFrom, 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;
@@ -26,8 +26,9 @@ interface CreateRelatedOptions<S = Record<string, any>> {
26
26
  */
27
27
  declare function createRelated<S = Record<string, any>, H extends HookContext = HookContext>({ service, multi, data, createItemsInDataArraySeparately, }: CreateRelatedOptions<S>): (context: H) => Promise<H>;
28
28
 
29
+ type GetItemsIsArrayFrom = "data" | "result" | "automatic";
29
30
  type GetItemsIsArrayOptions = {
30
- from: "data" | "result" | "automatic";
31
+ from?: GetItemsIsArrayFrom;
31
32
  };
32
33
  interface GetItemsIsArrayResult<T = any> {
33
34
  items: T[];
@@ -39,7 +40,7 @@ interface HookForEachOptions {
39
40
  wait?: "sequential" | "parallel" | false;
40
41
  items?: GetItemsIsArrayOptions["from"];
41
42
  }
42
- declare const forEach: (actionPerItem: (item: any, context: HookContext) => Promisable<any>, _options?: HookForEachOptions) => ReturnAsyncHook;
43
+ 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
44
 
44
45
  type OnDeleteAction = "cascade" | "set null";
45
46
  interface OnDeleteOptions {
@@ -79,7 +80,7 @@ interface HookRunPerItemOptions {
79
80
  * hook to run a hook for each item in the context
80
81
  * uses `context.result` if it is existent. otherwise uses context.data
81
82
  */
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>;
83
+ 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
84
 
84
85
  type Predicate<T = any> = (item: T) => boolean;
85
86
  type PredicateWithContext<T = any> = (item: T, context: HookContext) => boolean;
@@ -266,4 +267,4 @@ type InferRemoveResultFromPath<App extends Application, Path extends string, IdO
266
267
  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
268
  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
269
 
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 };
270
+ export { type ActionOnEmptyIntersect, type CreateRelatedOptions, type DebouncedFunctionApp, type DebouncedService, DebouncedStore, type DebouncedStoreOptions, type FilterQueryOptions, type FirstLast, type GetItemsIsArrayFrom, 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
@@ -445,12 +445,14 @@ const getItemsIsArray = (context, options) => {
445
445
  let itemOrItems;
446
446
  if (from === "automatic") {
447
447
  itemOrItems = context.type === "before" ? context.data : context.result;
448
- itemOrItems = itemOrItems && context.method === "find" ? itemOrItems.data || itemOrItems : itemOrItems;
449
448
  } else if (from === "data") {
450
449
  itemOrItems = context.data;
451
450
  } else if (from === "result") {
452
451
  itemOrItems = context.result;
453
452
  }
453
+ if ((from === "automatic" || from === "result") && context.type === "after") {
454
+ itemOrItems = itemOrItems && context.method === "find" ? itemOrItems.data || itemOrItems : itemOrItems;
455
+ }
454
456
  const isArray = Array.isArray(itemOrItems);
455
457
  return {
456
458
  items: isArray ? itemOrItems : itemOrItems != null ? [itemOrItems] : [],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "feathers-utils",
3
- "version": "3.1.1",
3
+ "version": "3.1.3",
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;
@@ -1,7 +1,9 @@
1
1
  import type { HookContext } from "@feathersjs/feathers";
2
2
 
3
+ export type GetItemsIsArrayFrom = "data" | "result" | "automatic";
4
+
3
5
  export type GetItemsIsArrayOptions = {
4
- from: "data" | "result" | "automatic";
6
+ from?: GetItemsIsArrayFrom;
5
7
  };
6
8
 
7
9
  export interface GetItemsIsArrayResult<T = any> {
@@ -20,16 +22,19 @@ export const getItemsIsArray = <T = any, H extends HookContext = HookContext>(
20
22
 
21
23
  if (from === "automatic") {
22
24
  itemOrItems = context.type === "before" ? context.data : context.result;
23
- itemOrItems =
24
- itemOrItems && context.method === "find"
25
- ? itemOrItems.data || itemOrItems
26
- : itemOrItems;
27
25
  } else if (from === "data") {
28
26
  itemOrItems = context.data;
29
27
  } else if (from === "result") {
30
28
  itemOrItems = context.result;
31
29
  }
32
30
 
31
+ if ((from === "automatic" || from === "result") && context.type === "after") {
32
+ itemOrItems =
33
+ itemOrItems && context.method === "find"
34
+ ? itemOrItems.data || itemOrItems
35
+ : itemOrItems;
36
+ }
37
+
33
38
  const isArray = Array.isArray(itemOrItems);
34
39
  return {
35
40
  items: isArray ? itemOrItems : itemOrItems != null ? [itemOrItems] : [],