@azure-net/kit 1.2.5 → 1.2.6
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.
|
@@ -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?: (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?: (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,31 @@ export const createAsyncHelpers = (opts) => {
|
|
|
18
18
|
response: args?.fallbackResponse
|
|
19
19
|
};
|
|
20
20
|
}
|
|
21
|
+
if (args?.beforeSend) {
|
|
22
|
+
let aborted = false;
|
|
23
|
+
await Promise.race([
|
|
24
|
+
Promise.resolve(args.beforeSend(() => {
|
|
25
|
+
aborted = true;
|
|
26
|
+
})),
|
|
27
|
+
new Promise((_, reject) => {
|
|
28
|
+
if (aborted)
|
|
29
|
+
reject(new Error('Aborted in beforeSend'));
|
|
30
|
+
})
|
|
31
|
+
]).catch((err) => {
|
|
32
|
+
const abortError = {
|
|
33
|
+
type: 'abort',
|
|
34
|
+
message: 'Aborted in beforeSend',
|
|
35
|
+
original: err
|
|
36
|
+
};
|
|
37
|
+
return {
|
|
38
|
+
success: false,
|
|
39
|
+
error: abortError,
|
|
40
|
+
response: args?.fallbackResponse
|
|
41
|
+
};
|
|
42
|
+
});
|
|
43
|
+
}
|
|
21
44
|
try {
|
|
22
|
-
const response = await Promise.resolve(typeof action === 'function' ?
|
|
45
|
+
const response = await Promise.resolve(typeof action === 'function' ? action() : action);
|
|
23
46
|
const result = { response, success: true };
|
|
24
47
|
await args?.onSuccess?.(result);
|
|
25
48
|
return result;
|
|
@@ -40,8 +63,31 @@ export const createAsyncHelpers = (opts) => {
|
|
|
40
63
|
args.abort.onAbort?.();
|
|
41
64
|
return args.fallbackResponse;
|
|
42
65
|
}
|
|
66
|
+
if (args?.beforeSend) {
|
|
67
|
+
let aborted = false;
|
|
68
|
+
await Promise.race([
|
|
69
|
+
Promise.resolve(args.beforeSend(() => {
|
|
70
|
+
aborted = true;
|
|
71
|
+
})),
|
|
72
|
+
new Promise((_, reject) => {
|
|
73
|
+
if (aborted)
|
|
74
|
+
reject(new Error('Aborted in beforeSend'));
|
|
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
|
+
};
|
|
87
|
+
});
|
|
88
|
+
}
|
|
43
89
|
try {
|
|
44
|
-
const response = await Promise.resolve(typeof action === 'function' ?
|
|
90
|
+
const response = await Promise.resolve(typeof action === 'function' ? action() : action);
|
|
45
91
|
await args?.onSuccess?.(response);
|
|
46
92
|
return response;
|
|
47
93
|
}
|