@azure-net/kit 1.2.6 → 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 {
|
|
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,7 +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?: (abort: () => void) => void | Promise<void>;
|
|
12
|
+
beforeSend?: (next: () => void, abort: () => void) => void | Promise<void>;
|
|
13
13
|
onSuccess?: (result: AsyncActionResponse<Res, undefined, Custom>) => Promise<unknown> | unknown;
|
|
14
14
|
onError?: (result: AsyncActionResponse<never, Req, Custom>) => Promise<unknown> | unknown;
|
|
15
15
|
reject?: boolean;
|
|
@@ -20,7 +20,7 @@ export declare const createAsyncHelpers: <Custom = unknown>(opts?: {
|
|
|
20
20
|
fallbackResponse?: Res;
|
|
21
21
|
}) => Promise<AsyncActionResponse<Res, Req, Custom>>;
|
|
22
22
|
createAsyncResource: <Res, Req_1 = unknown>(action: ActionOrThunk<Res>, args?: {
|
|
23
|
-
beforeSend?: (abort: () => void) => void | Promise<void>;
|
|
23
|
+
beforeSend?: (next: () => void, abort: () => void) => void | Promise<void>;
|
|
24
24
|
onSuccess?: (result: Res) => Promise<unknown> | unknown;
|
|
25
25
|
onError?: (error: AppError<Req_1, Custom>) => Promise<unknown> | unknown;
|
|
26
26
|
reject?: boolean;
|
|
@@ -19,27 +19,28 @@ export const createAsyncHelpers = (opts) => {
|
|
|
19
19
|
};
|
|
20
20
|
}
|
|
21
21
|
if (args?.beforeSend) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
]).catch((err) => {
|
|
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') {
|
|
32
31
|
const abortError = {
|
|
33
32
|
type: 'abort',
|
|
34
33
|
message: 'Aborted in beforeSend',
|
|
35
|
-
original:
|
|
34
|
+
original: new Error('Aborted in beforeSend')
|
|
36
35
|
};
|
|
36
|
+
if (args?.reject)
|
|
37
|
+
throw abortError;
|
|
37
38
|
return {
|
|
38
39
|
success: false,
|
|
39
40
|
error: abortError,
|
|
40
41
|
response: args?.fallbackResponse
|
|
41
42
|
};
|
|
42
|
-
}
|
|
43
|
+
}
|
|
43
44
|
}
|
|
44
45
|
try {
|
|
45
46
|
const response = await Promise.resolve(typeof action === 'function' ? action() : action);
|
|
@@ -64,27 +65,25 @@ export const createAsyncHelpers = (opts) => {
|
|
|
64
65
|
return args.fallbackResponse;
|
|
65
66
|
}
|
|
66
67
|
if (args?.beforeSend) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
})
|
|
76
|
-
]).catch((err) => {
|
|
77
|
-
const abortError = {
|
|
78
|
-
type: 'abort',
|
|
79
|
-
message: 'Aborted in beforeSend',
|
|
80
|
-
original: err
|
|
81
|
-
};
|
|
82
|
-
return {
|
|
83
|
-
success: false,
|
|
84
|
-
error: abortError,
|
|
85
|
-
response: args?.fallbackResponse
|
|
86
|
-
};
|
|
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
|
+
});
|
|
87
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
|
+
}
|
|
88
87
|
}
|
|
89
88
|
try {
|
|
90
89
|
const response = await Promise.resolve(typeof action === 'function' ? action() : action);
|