@azure-net/kit 1.2.5 → 1.2.7

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,4 +1,4 @@
1
- import { createErrorParser, type AppError } from './ErrorHandler.js';
1
+ import { type AppError, createErrorParser } from './ErrorHandler.js';
2
2
  export interface AsyncActionResponse<T, D = unknown, CustomErrorField = never> {
3
3
  success: boolean;
4
4
  response: T;
@@ -9,6 +9,7 @@ export declare const createAsyncHelpers: <Custom = unknown>(opts?: {
9
9
  parseError?: ReturnType<typeof createErrorParser<Custom>>;
10
10
  }) => {
11
11
  createAsyncAction: <Res = unknown, Req = unknown>(action: ActionOrThunk<Res>, args?: {
12
+ beforeSend?: (next: () => void, abort: () => void) => void | Promise<void>;
12
13
  onSuccess?: (result: AsyncActionResponse<Res, undefined, Custom>) => Promise<unknown> | unknown;
13
14
  onError?: (result: AsyncActionResponse<never, Req, Custom>) => Promise<unknown> | unknown;
14
15
  reject?: boolean;
@@ -19,6 +20,7 @@ export declare const createAsyncHelpers: <Custom = unknown>(opts?: {
19
20
  fallbackResponse?: Res;
20
21
  }) => Promise<AsyncActionResponse<Res, Req, Custom>>;
21
22
  createAsyncResource: <Res, Req_1 = unknown>(action: ActionOrThunk<Res>, args?: {
23
+ beforeSend?: (next: () => void, abort: () => void) => void | Promise<void>;
22
24
  onSuccess?: (result: Res) => Promise<unknown> | unknown;
23
25
  onError?: (error: AppError<Req_1, Custom>) => Promise<unknown> | unknown;
24
26
  reject?: boolean;
@@ -18,8 +18,32 @@ export const createAsyncHelpers = (opts) => {
18
18
  response: args?.fallbackResponse
19
19
  };
20
20
  }
21
+ if (args?.beforeSend) {
22
+ const beforeSendResult = await new Promise((resolve) => {
23
+ const next = () => resolve('next');
24
+ const abort = () => resolve('abort');
25
+ Promise.resolve(args.beforeSend(next, abort)).catch((err) => {
26
+ console.error('Error in beforeSend:', err);
27
+ resolve('abort');
28
+ });
29
+ });
30
+ if (beforeSendResult === 'abort') {
31
+ const abortError = {
32
+ type: 'abort',
33
+ message: 'Aborted in beforeSend',
34
+ original: new Error('Aborted in beforeSend')
35
+ };
36
+ if (args?.reject)
37
+ throw abortError;
38
+ return {
39
+ success: false,
40
+ error: abortError,
41
+ response: args?.fallbackResponse
42
+ };
43
+ }
44
+ }
21
45
  try {
22
- const response = await Promise.resolve(typeof action === 'function' ? (action)() : action);
46
+ const response = await Promise.resolve(typeof action === 'function' ? action() : action);
23
47
  const result = { response, success: true };
24
48
  await args?.onSuccess?.(result);
25
49
  return result;
@@ -40,8 +64,29 @@ export const createAsyncHelpers = (opts) => {
40
64
  args.abort.onAbort?.();
41
65
  return args.fallbackResponse;
42
66
  }
67
+ if (args?.beforeSend) {
68
+ const beforeSendResult = await new Promise((resolve) => {
69
+ const next = () => resolve('next');
70
+ const abort = () => resolve('abort');
71
+ Promise.resolve(args.beforeSend(next, abort)).catch((err) => {
72
+ console.error('Error in beforeSend:', err);
73
+ // If beforeSend throws, treat it as abort
74
+ resolve('abort');
75
+ });
76
+ });
77
+ if (beforeSendResult === 'abort') {
78
+ if (args?.reject) {
79
+ throw {
80
+ type: 'abort',
81
+ message: 'Aborted in beforeSend',
82
+ original: new Error('Aborted in beforeSend')
83
+ };
84
+ }
85
+ return args.fallbackResponse;
86
+ }
87
+ }
43
88
  try {
44
- const response = await Promise.resolve(typeof action === 'function' ? (action)() : action);
89
+ const response = await Promise.resolve(typeof action === 'function' ? action() : action);
45
90
  await args?.onSuccess?.(response);
46
91
  return response;
47
92
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azure-net/kit",
3
- "version": "1.2.5",
3
+ "version": "1.2.7",
4
4
  "files": [
5
5
  "dist",
6
6
  "!dist/**/*.test.*",