nuxt-precognition 0.3.1 → 0.3.2
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.
- package/dist/module.json +1 -1
- package/dist/runtime/useForm.js +0 -2
- package/dist/runtime/validator.js +14 -1
- package/package.json +1 -1
package/dist/module.json
CHANGED
package/dist/runtime/useForm.js
CHANGED
|
@@ -17,8 +17,10 @@ export function makeValidator(cb, options) {
|
|
|
17
17
|
const responseParsers = errorParsers.filter((p) => typeof p === "object").map((p) => p.responseParser);
|
|
18
18
|
const statusHandlers = { ...$precognition.statusHandlers, ...options?.statusHandlers };
|
|
19
19
|
const pendingKeys = /* @__PURE__ */ new Set();
|
|
20
|
+
let latestValidationId = 0;
|
|
20
21
|
const debouncedValidate = debounce(
|
|
21
22
|
async function(form) {
|
|
23
|
+
const validationId = ++latestValidationId;
|
|
22
24
|
const keys = Array.from(pendingKeys);
|
|
23
25
|
pendingKeys.clear();
|
|
24
26
|
const headers = requestPrecognitiveHeaders(validateOptions.validationHeaders, keys);
|
|
@@ -31,13 +33,19 @@ export function makeValidator(cb, options) {
|
|
|
31
33
|
form.error = null;
|
|
32
34
|
const onValidationStart = validateOptions.onValidationStart ?? (() => Promise.resolve());
|
|
33
35
|
await onValidationStart(data);
|
|
36
|
+
if (validationId !== latestValidationId)
|
|
37
|
+
return;
|
|
34
38
|
const clientValidation = validateOptions?.clientValidation ?? (() => Promise.resolve());
|
|
35
39
|
await clientValidation(data);
|
|
40
|
+
if (validationId !== latestValidationId)
|
|
41
|
+
return;
|
|
36
42
|
const backendValidation = validateOptions.backendValidation ? cb : () => Promise.resolve(void 0);
|
|
37
43
|
const resp = await backendValidation(
|
|
38
44
|
validateOptions.validateFiles ? data : withoutFiles(data),
|
|
39
45
|
headers
|
|
40
46
|
);
|
|
47
|
+
if (validationId !== latestValidationId)
|
|
48
|
+
return;
|
|
41
49
|
if (responseParsers.length)
|
|
42
50
|
responseParsers.forEach((p) => typeof resp === "undefined" ? void 0 : p(resp));
|
|
43
51
|
form.forgetErrors(...keys);
|
|
@@ -45,6 +53,8 @@ export function makeValidator(cb, options) {
|
|
|
45
53
|
const onSuccess = validateOptions?.onValidationSuccess ?? (() => void 0);
|
|
46
54
|
return onSuccess(data);
|
|
47
55
|
} catch (error) {
|
|
56
|
+
if (validationId !== latestValidationId)
|
|
57
|
+
return;
|
|
48
58
|
const err = error instanceof Error ? error : new Error("An error occurred while validating the form. Please try again.");
|
|
49
59
|
const statusHandler = hasResponse(err) && statusHandlers ? statusHandlers[`${err.response.status}`] : void 0;
|
|
50
60
|
if (statusHandler)
|
|
@@ -58,7 +68,8 @@ export function makeValidator(cb, options) {
|
|
|
58
68
|
const onError = validateOptions?.onValidationError ?? (() => void 0);
|
|
59
69
|
return onError(err, data, keys);
|
|
60
70
|
} finally {
|
|
61
|
-
|
|
71
|
+
if (validationId === latestValidationId)
|
|
72
|
+
form.validating = false;
|
|
62
73
|
}
|
|
63
74
|
},
|
|
64
75
|
validateOptions.validationTimeout,
|
|
@@ -71,6 +82,7 @@ export function makeValidator(cb, options) {
|
|
|
71
82
|
},
|
|
72
83
|
{
|
|
73
84
|
cancel: () => {
|
|
85
|
+
latestValidationId++;
|
|
74
86
|
pendingKeys.clear();
|
|
75
87
|
debouncedValidate.cancel();
|
|
76
88
|
},
|
|
@@ -78,6 +90,7 @@ export function makeValidator(cb, options) {
|
|
|
78
90
|
}
|
|
79
91
|
);
|
|
80
92
|
function reset() {
|
|
93
|
+
latestValidationId++;
|
|
81
94
|
validate.cancel();
|
|
82
95
|
}
|
|
83
96
|
function setValidateFiles(value) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-precognition",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.3.
|
|
4
|
+
"version": "0.3.2",
|
|
5
5
|
"description": "Nuxt module implementing Precognition protocol (inspired by official Laravel Precognition) for backend precognitive validation.",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "sot1986",
|