feathers-utils 4.2.0 → 5.0.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/dist/index.cjs CHANGED
@@ -682,27 +682,30 @@ function createRelated({
682
682
  };
683
683
  }
684
684
 
685
- const forEach = (actionPerItem, _options) => {
686
- const options = {
687
- wait: "parallel",
688
- items: "automatic",
689
- ..._options
690
- };
685
+ const forEach = (actionPerItem, options) => {
686
+ const { wait = "parallel", items: from = "automatic" } = options || {};
691
687
  return async (context) => {
692
- if (shouldSkip("runForItems", context)) {
688
+ if (shouldSkip("forEach", context)) {
693
689
  return context;
694
690
  }
695
- const { items } = getItemsIsArray(context, { from: options.items });
691
+ const { items } = getItemsIsArray(context, { from });
692
+ const forAll = options?.forAll ? await options.forAll(items, context) : {};
696
693
  const promises = [];
694
+ let i = 0;
697
695
  for (const item of items) {
698
- const promise = actionPerItem(item, context);
699
- if (options.wait === "sequential") {
696
+ const promise = actionPerItem(item, {
697
+ context,
698
+ i,
699
+ ...forAll ? { fromAll: forAll } : {}
700
+ });
701
+ if (wait === "sequential") {
700
702
  await promise;
701
703
  } else {
702
704
  promises.push(promise);
703
705
  }
706
+ i++;
704
707
  }
705
- if (options.wait === "parallel") {
708
+ if (wait === "parallel") {
706
709
  await Promise.all(promises);
707
710
  }
708
711
  return context;
package/dist/index.d.cts CHANGED
@@ -36,11 +36,18 @@ interface GetItemsIsArrayResult<T = any> {
36
36
  }
37
37
  declare const getItemsIsArray: <T = any, H extends HookContext<_feathersjs_feathers.Application<any, any>, any> = HookContext<_feathersjs_feathers.Application<any, any>, any>>(context: H, options?: GetItemsIsArrayOptions) => GetItemsIsArrayResult<T>;
38
38
 
39
- interface HookForEachOptions {
39
+ type HookForEachOptions<T = any, H = HookContext, R = any> = {
40
40
  wait?: "sequential" | "parallel" | false;
41
41
  items?: GetItemsIsArrayOptions["from"];
42
- }
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>;
42
+ forAll?: (items: T[], context: H) => Promisable<R>;
43
+ };
44
+ type ActionPerItem<T, H, R> = (item: T, options: {
45
+ context: H;
46
+ i: number;
47
+ } & (undefined extends R ? {} : {
48
+ fromAll: R;
49
+ })) => Promisable<any>;
50
+ declare const forEach: <H extends HookContext<_feathersjs_feathers.Application<any, any>, any> = HookContext<_feathersjs_feathers.Application<any, any>, any>, T = any, R = any>(actionPerItem: ActionPerItem<T, H, R>, options?: HookForEachOptions<T, H, R>) => ReturnAsyncHook<H>;
44
51
 
45
52
  type OnDeleteAction = "cascade" | "set null";
46
53
  interface OnDeleteOptions {
package/dist/index.d.mts CHANGED
@@ -36,11 +36,18 @@ interface GetItemsIsArrayResult<T = any> {
36
36
  }
37
37
  declare const getItemsIsArray: <T = any, H extends HookContext<_feathersjs_feathers.Application<any, any>, any> = HookContext<_feathersjs_feathers.Application<any, any>, any>>(context: H, options?: GetItemsIsArrayOptions) => GetItemsIsArrayResult<T>;
38
38
 
39
- interface HookForEachOptions {
39
+ type HookForEachOptions<T = any, H = HookContext, R = any> = {
40
40
  wait?: "sequential" | "parallel" | false;
41
41
  items?: GetItemsIsArrayOptions["from"];
42
- }
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>;
42
+ forAll?: (items: T[], context: H) => Promisable<R>;
43
+ };
44
+ type ActionPerItem<T, H, R> = (item: T, options: {
45
+ context: H;
46
+ i: number;
47
+ } & (undefined extends R ? {} : {
48
+ fromAll: R;
49
+ })) => Promisable<any>;
50
+ declare const forEach: <H extends HookContext<_feathersjs_feathers.Application<any, any>, any> = HookContext<_feathersjs_feathers.Application<any, any>, any>, T = any, R = any>(actionPerItem: ActionPerItem<T, H, R>, options?: HookForEachOptions<T, H, R>) => ReturnAsyncHook<H>;
44
51
 
45
52
  type OnDeleteAction = "cascade" | "set null";
46
53
  interface OnDeleteOptions {
package/dist/index.d.ts CHANGED
@@ -36,11 +36,18 @@ interface GetItemsIsArrayResult<T = any> {
36
36
  }
37
37
  declare const getItemsIsArray: <T = any, H extends HookContext<_feathersjs_feathers.Application<any, any>, any> = HookContext<_feathersjs_feathers.Application<any, any>, any>>(context: H, options?: GetItemsIsArrayOptions) => GetItemsIsArrayResult<T>;
38
38
 
39
- interface HookForEachOptions {
39
+ type HookForEachOptions<T = any, H = HookContext, R = any> = {
40
40
  wait?: "sequential" | "parallel" | false;
41
41
  items?: GetItemsIsArrayOptions["from"];
42
- }
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>;
42
+ forAll?: (items: T[], context: H) => Promisable<R>;
43
+ };
44
+ type ActionPerItem<T, H, R> = (item: T, options: {
45
+ context: H;
46
+ i: number;
47
+ } & (undefined extends R ? {} : {
48
+ fromAll: R;
49
+ })) => Promisable<any>;
50
+ declare const forEach: <H extends HookContext<_feathersjs_feathers.Application<any, any>, any> = HookContext<_feathersjs_feathers.Application<any, any>, any>, T = any, R = any>(actionPerItem: ActionPerItem<T, H, R>, options?: HookForEachOptions<T, H, R>) => ReturnAsyncHook<H>;
44
51
 
45
52
  type OnDeleteAction = "cascade" | "set null";
46
53
  interface OnDeleteOptions {
package/dist/index.mjs CHANGED
@@ -668,27 +668,30 @@ function createRelated({
668
668
  };
669
669
  }
670
670
 
671
- const forEach = (actionPerItem, _options) => {
672
- const options = {
673
- wait: "parallel",
674
- items: "automatic",
675
- ..._options
676
- };
671
+ const forEach = (actionPerItem, options) => {
672
+ const { wait = "parallel", items: from = "automatic" } = options || {};
677
673
  return async (context) => {
678
- if (shouldSkip("runForItems", context)) {
674
+ if (shouldSkip("forEach", context)) {
679
675
  return context;
680
676
  }
681
- const { items } = getItemsIsArray(context, { from: options.items });
677
+ const { items } = getItemsIsArray(context, { from });
678
+ const forAll = options?.forAll ? await options.forAll(items, context) : {};
682
679
  const promises = [];
680
+ let i = 0;
683
681
  for (const item of items) {
684
- const promise = actionPerItem(item, context);
685
- if (options.wait === "sequential") {
682
+ const promise = actionPerItem(item, {
683
+ context,
684
+ i,
685
+ ...forAll ? { fromAll: forAll } : {}
686
+ });
687
+ if (wait === "sequential") {
686
688
  await promise;
687
689
  } else {
688
690
  promises.push(promise);
689
691
  }
692
+ i++;
690
693
  }
691
- if (options.wait === "parallel") {
694
+ if (wait === "parallel") {
692
695
  await Promise.all(promises);
693
696
  }
694
697
  return context;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "feathers-utils",
3
- "version": "4.2.0",
3
+ "version": "5.0.1",
4
4
  "description": "Some utils for projects using '@feathersjs/feathers'",
5
5
  "author": "fratzinger",
6
6
  "repository": {
@@ -5,42 +5,59 @@ import type { HookContext } from "@feathersjs/feathers";
5
5
  import type { GetItemsIsArrayOptions } from "../utils/getItemsIsArray";
6
6
  import { getItemsIsArray } from "../utils/getItemsIsArray";
7
7
 
8
- export interface HookForEachOptions {
8
+ export type HookForEachOptions<T = any, H = HookContext, R = any> = {
9
9
  wait?: "sequential" | "parallel" | false;
10
10
  items?: GetItemsIsArrayOptions["from"];
11
- }
11
+ forAll?: (items: T[], context: H) => Promisable<R>;
12
+ };
13
+
14
+ type ActionPerItem<T, H, R> = (
15
+ item: T,
16
+ options: {
17
+ context: H;
18
+ i: number;
19
+ // eslint-disable-next-line @typescript-eslint/ban-types
20
+ } & (undefined extends R ? {} : { fromAll: R }),
21
+ ) => Promisable<any>;
12
22
 
13
- export const forEach = <H extends HookContext = HookContext, T = any>(
14
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
- actionPerItem: (item: T, context: H) => Promisable<any>,
16
- _options?: HookForEachOptions,
23
+ export const forEach = <H extends HookContext = HookContext, T = any, R = any>(
24
+ actionPerItem: ActionPerItem<T, H, R>,
25
+ options?: HookForEachOptions<T, H, R>,
17
26
  ): ReturnAsyncHook<H> => {
18
- const options: Required<HookForEachOptions> = {
19
- wait: "parallel",
20
- items: "automatic",
21
- ..._options,
22
- };
27
+ const { wait = "parallel", items: from = "automatic" } = options || {};
23
28
 
24
29
  return async (context: H): Promise<H> => {
25
- if (shouldSkip("runForItems", context)) {
30
+ if (shouldSkip("forEach", context)) {
26
31
  return context;
27
32
  }
28
33
 
29
- const { items } = getItemsIsArray(context, { from: options.items });
34
+ const { items } = getItemsIsArray(context, { from });
35
+
36
+ const forAll = options?.forAll
37
+ ? await options.forAll(items, context)
38
+ : ({} as R);
30
39
 
31
40
  const promises: Promise<any>[] = [];
32
41
 
42
+ let i = 0;
43
+
33
44
  for (const item of items) {
34
- const promise = actionPerItem(item, context);
45
+ const promise = actionPerItem(item, {
46
+ context,
47
+ i,
48
+ ...(forAll ? { fromAll: forAll } : {}),
49
+ } as any);
35
50
 
36
- if (options.wait === "sequential") {
51
+ if (wait === "sequential") {
37
52
  await promise;
38
53
  } else {
39
54
  promises.push(promise);
40
55
  }
56
+
57
+ i++;
41
58
  }
42
59
 
43
- if (options.wait === "parallel") {
60
+ if (wait === "parallel") {
44
61
  await Promise.all(promises);
45
62
  }
46
63