@ls-stack/utils 3.31.0 → 3.33.0

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.
@@ -1,21 +1,21 @@
1
- import { Result } from 't-result';
1
+ import { ResultValidErrors, Result } from 't-result';
2
2
 
3
3
  declare class ConcurrentCallsAggregateError extends AggregateError {
4
- errors: Error[];
4
+ errors: ResultValidErrors[];
5
5
  total: number;
6
6
  failed: number;
7
- constructor(errors: Error[], total: number, failed: number);
7
+ constructor(errors: ResultValidErrors[], total: number, failed: number);
8
8
  }
9
9
  declare class ConcurrentCallsWithMetadataAggregateError<M extends ValidMetadata> extends AggregateError {
10
- errors: Error[];
10
+ errors: ResultValidErrors[];
11
11
  errorsWithMetadata: {
12
- error: Error;
12
+ error: ResultValidErrors;
13
13
  metadata: M;
14
14
  }[];
15
15
  total: number;
16
16
  failed: number;
17
17
  constructor(errors: {
18
- error: Error;
18
+ error: ResultValidErrors;
19
19
  metadata: M;
20
20
  }[], total: number, failed: number);
21
21
  }
@@ -27,12 +27,12 @@ type SucceededCall<R, M> = {
27
27
  value: R;
28
28
  metadata: M;
29
29
  };
30
- type FailedCall<M, E extends Error = Error> = {
30
+ type FailedCall<M, E extends ResultValidErrors = Error> = {
31
31
  metadata: M;
32
32
  error: E;
33
33
  };
34
- type Action<R, E extends Error> = () => Promise<Result<R, E>>;
35
- type SettledResultWithMetadata<R, M, E extends Error = Error> = {
34
+ type Action<R, E extends ResultValidErrors> = () => Promise<Result<R, E>>;
35
+ type SettledResultWithMetadata<R, M, E extends ResultValidErrors = Error> = {
36
36
  ok: true;
37
37
  value: R;
38
38
  metadata: M;
@@ -42,8 +42,10 @@ type SettledResultWithMetadata<R, M, E extends Error = Error> = {
42
42
  error: E;
43
43
  metadata: M;
44
44
  };
45
- declare class ConcurrentCalls<R = unknown, E extends Error = Error> {
45
+ declare class ConcurrentCalls<R = unknown, E extends ResultValidErrors = Error> {
46
46
  #private;
47
+ allowResultify: boolean;
48
+ constructor(allowResultify: boolean);
47
49
  add(...calls: Action<R, E>[]): this;
48
50
  resultifyAdd(...calls: ((() => R) | (() => Promise<R>))[]): this;
49
51
  runAll({ delayStart }?: RunProps): Promise<Result<R[], E>>;
@@ -59,11 +61,12 @@ declare class ConcurrentCalls<R = unknown, E extends Error = Error> {
59
61
  * Executes multiple asynchronous calls concurrently and collects the results in a easier to use format.
60
62
  *
61
63
  * @template R - The type of the result value.
62
- * @template E - The type of the error.
63
64
  */
64
65
  declare function concurrentCalls<R = unknown>(): ConcurrentCalls<R, Error>;
65
- declare class ConcurrentCallsWithMetadata<M extends ValidMetadata, R = unknown, E extends Error = Error> {
66
+ declare class ConcurrentCallsWithMetadata<M extends ValidMetadata, R = unknown, E extends ResultValidErrors = Error> {
66
67
  #private;
68
+ allowResultify: boolean;
69
+ constructor(allowResultify: boolean);
67
70
  add(...calls: {
68
71
  fn: Action<R, E>;
69
72
  metadata: M;
@@ -85,12 +88,25 @@ declare class ConcurrentCallsWithMetadata<M extends ValidMetadata, R = unknown,
85
88
  }>;
86
89
  }
87
90
  /**
88
- * Executes multiple asynchronous calls concurrently and collects the results in a easier to use format.
91
+ * Executes multiple asynchronous calls concurrently with metadata for each call and collects the results in a easier to use format.
89
92
  *
90
93
  * @template M - The type of the call metadata.
91
94
  * @template R - The type of the result value.
92
- * @template E - The type of the error from individual Result objects.
93
95
  */
94
96
  declare function concurrentCallsWithMetadata<M extends ValidMetadata, R = unknown>(): ConcurrentCallsWithMetadata<M, R, Error>;
97
+ type ValueFromResult<R> = R extends Result<infer T, any> ? T : never;
98
+ type ErrorFromResult<R> = R extends Result<any, infer E> ? E : never;
99
+ /**
100
+ * Executes multiple asynchronous result calls concurrently and collects the results in a easier to use format.
101
+ *
102
+ * @template R - The type of the result function that will be called.
103
+ */
104
+ declare function concurrentResultCalls<ResultFn extends (...args: any[]) => Promise<Result<unknown, ResultValidErrors>>>(): ConcurrentCalls<ValueFromResult<Awaited<ReturnType<ResultFn>>>, ErrorFromResult<Awaited<ReturnType<ResultFn>>>>;
105
+ /**
106
+ * Executes multiple asynchronous result calls concurrently with metadata for each call and collects the results in a easier to use format.
107
+ *
108
+ * @template ResultFn - The type of the result function that will be called.
109
+ */
110
+ declare function concurrentResultsWithMetadata<M extends ValidMetadata, ResultFn extends (...args: any[]) => Promise<Result<unknown, ResultValidErrors>>>(): ConcurrentCallsWithMetadata<M, ValueFromResult<Awaited<ReturnType<ResultFn>>>, ErrorFromResult<Awaited<ReturnType<ResultFn>>>>;
95
111
 
96
- export { ConcurrentCallsAggregateError, ConcurrentCallsWithMetadataAggregateError, concurrentCalls, concurrentCallsWithMetadata };
112
+ export { ConcurrentCallsAggregateError, ConcurrentCallsWithMetadataAggregateError, concurrentCalls, concurrentCallsWithMetadata, concurrentResultCalls, concurrentResultsWithMetadata };
@@ -4,9 +4,12 @@ import {
4
4
  import {
5
5
  sleep
6
6
  } from "./chunk-5DZT3Z5Z.js";
7
+ import {
8
+ safeJsonStringify
9
+ } from "./chunk-VAAMRG4K.js";
7
10
  import {
8
11
  truncateArray
9
- } from "./chunk-SRVMMYSW.js";
12
+ } from "./chunk-RYBJST64.js";
10
13
  import {
11
14
  invariant
12
15
  } from "./chunk-C2SVCIWE.js";
@@ -16,7 +19,11 @@ import {
16
19
  } from "./chunk-JF2MDHOJ.js";
17
20
 
18
21
  // src/concurrentCalls.ts
19
- import { Result, resultify, unknownToError } from "t-result";
22
+ import {
23
+ Result,
24
+ resultify,
25
+ unknownToError
26
+ } from "t-result";
20
27
  function formatErrorMessagesWithCounts(messages, failed, total) {
21
28
  const errorMessageCounts = /* @__PURE__ */ new Map();
22
29
  for (const message of messages) {
@@ -38,7 +45,10 @@ var ConcurrentCallsAggregateError = class extends AggregateError {
38
45
  failed = 0;
39
46
  constructor(errors, total, failed) {
40
47
  const messages = errors.map(
41
- (error) => `- ${truncateString(error.message, 100)}`
48
+ (error) => `- ${truncateString(
49
+ error instanceof Error ? error.message : safeJsonStringify(error) ?? "???",
50
+ 100
51
+ )}`
42
52
  );
43
53
  const message = formatErrorMessagesWithCounts(messages, failed, total);
44
54
  super(errors, message);
@@ -67,7 +77,10 @@ var ConcurrentCallsWithMetadataAggregateError = class extends AggregateError {
67
77
  }
68
78
  } catch (_) {
69
79
  }
70
- return `- ${metadataPrefix}${truncateString(error.message, 100)}`;
80
+ return `- ${metadataPrefix}${truncateString(
81
+ error instanceof Error ? error.message : safeJsonStringify(error) ?? "???",
82
+ 100
83
+ )}`;
71
84
  });
72
85
  const message = formatErrorMessagesWithCounts(messages, failed, total);
73
86
  const errorInstances = errors.map((e) => e.error);
@@ -81,11 +94,20 @@ var ConcurrentCallsWithMetadataAggregateError = class extends AggregateError {
81
94
  var ConcurrentCalls = class {
82
95
  #pendingCalls = [];
83
96
  #alreadyRun = false;
97
+ allowResultify = true;
98
+ constructor(allowResultify) {
99
+ this.allowResultify = allowResultify;
100
+ }
84
101
  add(...calls) {
85
102
  this.#pendingCalls.push(...calls);
86
103
  return this;
87
104
  }
88
105
  resultifyAdd(...calls) {
106
+ if (!this.allowResultify) {
107
+ throw new Error(
108
+ "resultifyAdd is not allowed when using concurrentResults"
109
+ );
110
+ }
89
111
  const processedCalls = calls.map((call) => {
90
112
  return async () => {
91
113
  try {
@@ -166,11 +188,15 @@ var ConcurrentCalls = class {
166
188
  }
167
189
  };
168
190
  function concurrentCalls() {
169
- return new ConcurrentCalls();
191
+ return new ConcurrentCalls(true);
170
192
  }
171
193
  var ConcurrentCallsWithMetadata = class {
172
194
  #pendingCalls = [];
173
195
  #alreadyRun = false;
196
+ allowResultify = true;
197
+ constructor(allowResultify) {
198
+ this.allowResultify = allowResultify;
199
+ }
174
200
  add(...calls) {
175
201
  invariant(
176
202
  !this.#alreadyRun,
@@ -180,6 +206,11 @@ var ConcurrentCallsWithMetadata = class {
180
206
  return this;
181
207
  }
182
208
  resultifyAdd(...items) {
209
+ if (!this.allowResultify) {
210
+ throw new Error(
211
+ "resultifyAdd is not allowed when using concurrentResultsWithMetadata"
212
+ );
213
+ }
183
214
  const processedItems = items.map(({ fn, metadata }) => {
184
215
  const cb = () => resultify(async () => {
185
216
  const valueOrPromise = fn();
@@ -297,11 +328,19 @@ var ConcurrentCallsWithMetadata = class {
297
328
  }
298
329
  };
299
330
  function concurrentCallsWithMetadata() {
300
- return new ConcurrentCallsWithMetadata();
331
+ return new ConcurrentCallsWithMetadata(true);
332
+ }
333
+ function concurrentResultCalls() {
334
+ return new ConcurrentCalls(false);
335
+ }
336
+ function concurrentResultsWithMetadata() {
337
+ return new ConcurrentCallsWithMetadata(false);
301
338
  }
302
339
  export {
303
340
  ConcurrentCallsAggregateError,
304
341
  ConcurrentCallsWithMetadataAggregateError,
305
342
  concurrentCalls,
306
- concurrentCallsWithMetadata
343
+ concurrentCallsWithMetadata,
344
+ concurrentResultCalls,
345
+ concurrentResultsWithMetadata
307
346
  };
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  filterObjectOrArrayKeys
3
- } from "./chunk-J3ILVKZC.js";
4
- import "./chunk-SRVMMYSW.js";
3
+ } from "./chunk-XUNY3QUT.js";
4
+ import "./chunk-RYBJST64.js";
5
5
  import "./chunk-C2SVCIWE.js";
6
6
  import "./chunk-JF2MDHOJ.js";
7
7
  export {
@@ -69,7 +69,7 @@ function serializeXML(node, options) {
69
69
  function serializeWithLevel(node, options = {}, level) {
70
70
  const {
71
71
  indent,
72
- escapeText: globalEscapeText = true,
72
+ escapeText: globalEscapeText = false,
73
73
  validateTagName = true,
74
74
  invalidNodes = "throw"
75
75
  } = options;
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  filterAndMap
3
- } from "./chunk-SRVMMYSW.js";
3
+ } from "./chunk-RYBJST64.js";
4
4
  import "./chunk-C2SVCIWE.js";
5
5
  import {
6
6
  isTruthy
@@ -32,7 +32,7 @@ function serializeXML(node, options) {
32
32
  function serializeWithLevel(node, options = {}, level) {
33
33
  const {
34
34
  indent,
35
- escapeText: globalEscapeText = true,
35
+ escapeText: globalEscapeText = false,
36
36
  validateTagName = true,
37
37
  invalidNodes = "throw"
38
38
  } = options;
package/lib/testUtils.js CHANGED
@@ -11,7 +11,7 @@ import {
11
11
  } from "./chunk-JQFUKJU5.js";
12
12
  import {
13
13
  filterObjectOrArrayKeys
14
- } from "./chunk-J3ILVKZC.js";
14
+ } from "./chunk-XUNY3QUT.js";
15
15
  import {
16
16
  defer
17
17
  } from "./chunk-DFXNVEH6.js";
@@ -22,7 +22,7 @@ import "./chunk-3LZQMZAS.js";
22
22
  import {
23
23
  arrayWithPrevAndIndex,
24
24
  filterAndMap
25
- } from "./chunk-SRVMMYSW.js";
25
+ } from "./chunk-RYBJST64.js";
26
26
  import {
27
27
  isObject
28
28
  } from "./chunk-C2SVCIWE.js";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ls-stack/utils",
3
3
  "description": "Universal TypeScript utilities for browser and Node.js",
4
- "version": "3.31.0",
4
+ "version": "3.33.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "lib",