@azure-net/kit 1.2.4 → 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,6 +18,29 @@ 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
45
|
const response = await Promise.resolve(typeof action === 'function' ? action() : action);
|
|
23
46
|
const result = { response, success: true };
|
|
@@ -40,6 +63,29 @@ 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
90
|
const response = await Promise.resolve(typeof action === 'function' ? action() : action);
|
|
45
91
|
await args?.onSuccess?.(response);
|
|
@@ -31,13 +31,18 @@ interface SchemaBuilder<SchemaData, Rules = unknown, TransformResult = SchemaDat
|
|
|
31
31
|
with<M extends Record<string, unknown>>(callback: () => M): SchemaBuilder<SchemaData, Rules, TransformResult, M>;
|
|
32
32
|
create(): Schema<SchemaData, TransformResult, CustomMethods>;
|
|
33
33
|
}
|
|
34
|
-
interface SchemaInstance<TransformResult> {
|
|
35
|
-
json(): TransformResult;
|
|
36
|
-
formData(): FormData;
|
|
37
|
-
validated():
|
|
34
|
+
interface SchemaInstance<TransformResult, SchemaData> {
|
|
35
|
+
json(validate?: boolean): TransformResult;
|
|
36
|
+
formData(validate?: boolean): FormData;
|
|
37
|
+
validated(): {
|
|
38
|
+
valid: boolean;
|
|
39
|
+
errors: RequestErrors<SchemaData>;
|
|
40
|
+
json(): TransformResult;
|
|
41
|
+
formData(): FormData;
|
|
42
|
+
};
|
|
38
43
|
}
|
|
39
44
|
export type Schema<SchemaData, TransformResult, CustomMethods> = CustomMethods & {
|
|
40
|
-
from(data: Partial<SchemaData> | FormData): SchemaInstance<TransformResult>;
|
|
45
|
+
from(data: Partial<SchemaData> | FormData): SchemaInstance<TransformResult, SchemaData>;
|
|
41
46
|
getSchemaError(e: unknown): RequestErrors<SchemaData> | undefined;
|
|
42
47
|
};
|
|
43
48
|
export declare const createSchemaFactory: <Rules extends Record<string, unknown>>(rules: Rules) => <SchemaData>() => SchemaBuilder<SchemaData, Rules>;
|
|
@@ -70,38 +70,44 @@ class SchemaBuilderImpl {
|
|
|
70
70
|
throw Error('Data to validate is not an object');
|
|
71
71
|
}
|
|
72
72
|
const validated = () => {
|
|
73
|
-
if (!rules)
|
|
74
|
-
return true;
|
|
75
73
|
_isValid = true;
|
|
76
74
|
_errors = {};
|
|
77
|
-
const definedSchema = rules(rulesFactory);
|
|
78
|
-
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
75
|
+
const definedSchema = rules?.(rulesFactory);
|
|
76
|
+
if (definedSchema && typeof definedSchema === 'object' && Object.keys(definedSchema).length) {
|
|
77
|
+
for (const key in definedSchema) {
|
|
78
|
+
const fieldRules = definedSchema[key] ?? [];
|
|
79
|
+
const value = getByPath(key, _preparedData);
|
|
80
|
+
for (const rule of fieldRules) {
|
|
81
|
+
const failMessage = rule({
|
|
82
|
+
val: value,
|
|
83
|
+
listValues: _preparedData,
|
|
84
|
+
key: key
|
|
85
|
+
});
|
|
86
|
+
if (failMessage) {
|
|
87
|
+
setByPath(_errors, key, failMessage);
|
|
88
|
+
_isValid = false;
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
91
|
}
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
|
-
|
|
94
|
+
return {
|
|
95
|
+
valid: _isValid,
|
|
96
|
+
errors: _errors,
|
|
97
|
+
json: () => json(false),
|
|
98
|
+
formData: () => formData(false)
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
const json = (validate = true) => {
|
|
102
|
+
if (validate && !validated().valid) {
|
|
95
103
|
throw new SchemaFail(_errors);
|
|
96
104
|
}
|
|
97
|
-
return true;
|
|
98
|
-
};
|
|
99
|
-
const json = () => {
|
|
100
|
-
validated();
|
|
101
105
|
return transform(_preparedData);
|
|
102
106
|
};
|
|
103
|
-
const formData = () => {
|
|
104
|
-
validated()
|
|
107
|
+
const formData = (validate = true) => {
|
|
108
|
+
if (validate && !validated().valid) {
|
|
109
|
+
throw new SchemaFail(_errors);
|
|
110
|
+
}
|
|
105
111
|
return FormDataUtil.fromObject(transform(_preparedData));
|
|
106
112
|
};
|
|
107
113
|
return {
|