nuxt-precognition 0.1.6 → 0.2.0
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/types/form.d.ts +6 -2
- package/dist/runtime/types/utils.d.ts +1 -1
- package/dist/runtime/useForm.js +28 -12
- package/package.json +12 -4
package/dist/module.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ValidationErrorParser, ValidationErrorsData } from './core.js';
|
|
2
2
|
import type { NestedKeyOf, ErrorStatusCode } from './utils.js';
|
|
3
|
-
interface SubmitOptions<TData extends object, TResp> {
|
|
3
|
+
export interface SubmitOptions<TData extends object, TResp> {
|
|
4
4
|
headers?: Headers;
|
|
5
5
|
/**
|
|
6
6
|
* callback triggered when error is captured
|
|
@@ -29,6 +29,9 @@ interface SubmitOptions<TData extends object, TResp> {
|
|
|
29
29
|
*/
|
|
30
30
|
onSuccess?: (resp: TResp, data: TData) => Promise<void> | void;
|
|
31
31
|
}
|
|
32
|
+
export interface AsyncSubmitOptions {
|
|
33
|
+
headers?: Headers;
|
|
34
|
+
}
|
|
32
35
|
export interface Form<TData extends object, Tresp> {
|
|
33
36
|
data: () => TData;
|
|
34
37
|
setData: (data: TData) => TData & Form<TData, Tresp>;
|
|
@@ -40,7 +43,8 @@ export interface Form<TData extends object, Tresp> {
|
|
|
40
43
|
disabled: () => boolean;
|
|
41
44
|
validate: (...keys: NestedKeyOf<TData>[]) => TData & Form<TData, Tresp>;
|
|
42
45
|
reset: () => TData & Form<TData, Tresp>;
|
|
43
|
-
submit: (options?: SubmitOptions<TData, Tresp>) =>
|
|
46
|
+
submit: (options?: SubmitOptions<TData, Tresp>) => void;
|
|
47
|
+
asyncSubmit: (options?: AsyncSubmitOptions) => Promise<Tresp>;
|
|
44
48
|
valid: (...keys: (NestedKeyOf<TData>)[]) => boolean;
|
|
45
49
|
invalid: (...keys: (NestedKeyOf<TData>)[]) => boolean;
|
|
46
50
|
touched: (...keys: (NestedKeyOf<TData>)[]) => boolean;
|
|
@@ -3,7 +3,7 @@ type ObjectKeys<T extends object> = T extends unknown[] ? ArrayKeys<T> : keyof T
|
|
|
3
3
|
interface HasConstructor {
|
|
4
4
|
new (...args: unknown[]): unknown;
|
|
5
5
|
}
|
|
6
|
-
export type NestedKeyOf<T> = T extends Partial<Record<infer Key, unknown>> ? T extends HasConstructor ? never : T extends CallableFunction ? never : Key extends string | number ? (ObjectKeys<T> | (T[Key] extends object ? `${ObjectKeys<Pick<T, Key>>}.${NestedKeyOf<T[Key]>}` : T extends unknown[] ? T extends [unknown, ...unknown[]] ? never : T[number] extends object ? `${number}.${NestedKeyOf<T[number]>}` : never : never)) : never : never;
|
|
6
|
+
export type NestedKeyOf<T> = T extends Partial<Record<infer Key, unknown>> ? T extends HasConstructor ? never : T extends CallableFunction ? never : Key extends string | number ? (ObjectKeys<T> | (T[Key] extends object | null | undefined ? `${ObjectKeys<Pick<T, Key>>}.${NestedKeyOf<T[Key]>}` : T extends unknown[] ? T extends [unknown, ...unknown[]] ? never : T[number] extends object ? `${number}.${NestedKeyOf<T[number]>}` : never : never)) : never : never;
|
|
7
7
|
type SingleDigit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
|
|
8
8
|
export type ErrorStatusCode = `4${SingleDigit}${SingleDigit}` | `5${SingleDigit}${SingleDigit}`;
|
|
9
9
|
export {};
|
package/dist/runtime/useForm.js
CHANGED
|
@@ -8,6 +8,25 @@ export function useForm(init, cb, options) {
|
|
|
8
8
|
return cloneDeep(toRaw(resolveDynamicObject(init)));
|
|
9
9
|
}
|
|
10
10
|
const validator = makeValidator(cb, options);
|
|
11
|
+
async function handleSubmit(o) {
|
|
12
|
+
if (form.processing)
|
|
13
|
+
return;
|
|
14
|
+
const data = form.data();
|
|
15
|
+
try {
|
|
16
|
+
const onBefore = o?.onBefore ? await o.onBefore(data) : true;
|
|
17
|
+
if (!onBefore)
|
|
18
|
+
return;
|
|
19
|
+
if (o?.onStart)
|
|
20
|
+
await o.onStart(data);
|
|
21
|
+
const resp = await form.asyncSubmit({ headers: o?.headers });
|
|
22
|
+
if (o?.onSuccess)
|
|
23
|
+
await o.onSuccess(resp, data);
|
|
24
|
+
} catch (error) {
|
|
25
|
+
const err = error instanceof Error ? error : new Error("Invalid form");
|
|
26
|
+
if (o?.onError)
|
|
27
|
+
await o.onError(err, data);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
11
30
|
const form = reactive({
|
|
12
31
|
...getInitialData(),
|
|
13
32
|
data: () => {
|
|
@@ -42,18 +61,16 @@ export function useForm(init, cb, options) {
|
|
|
42
61
|
form.validatedKeys = [];
|
|
43
62
|
return form;
|
|
44
63
|
},
|
|
45
|
-
|
|
64
|
+
submit(o) {
|
|
65
|
+
void handleSubmit(o);
|
|
66
|
+
},
|
|
67
|
+
async asyncSubmit(o) {
|
|
46
68
|
if (form.processing)
|
|
47
|
-
|
|
69
|
+
throw new Error("Form is already processing.");
|
|
48
70
|
const data = form.data();
|
|
49
71
|
try {
|
|
50
|
-
const onBefore = o?.onBefore ? await o.onBefore(data) : true;
|
|
51
|
-
if (!onBefore)
|
|
52
|
-
return;
|
|
53
72
|
form.processing = true;
|
|
54
73
|
form.error = null;
|
|
55
|
-
if (o?.onStart)
|
|
56
|
-
await o.onStart(data);
|
|
57
74
|
if (options?.clientValidation) {
|
|
58
75
|
form.validating = true;
|
|
59
76
|
await options.clientValidation(data);
|
|
@@ -62,13 +79,13 @@ export function useForm(init, cb, options) {
|
|
|
62
79
|
const resp = await cb(data, o?.headers ?? new Headers());
|
|
63
80
|
if (validator.responseParsers.length)
|
|
64
81
|
validator.responseParsers.forEach((p) => p(resp));
|
|
65
|
-
|
|
66
|
-
await o.onSuccess(resp, data);
|
|
82
|
+
return resp;
|
|
67
83
|
} catch (error) {
|
|
68
84
|
const err = error instanceof Error ? error : new Error("Invalid form");
|
|
69
85
|
const statusHandler = hasResponse(err) ? validator.statusHandlers[`${err.response.status}`] : void 0;
|
|
70
86
|
if (statusHandler) {
|
|
71
|
-
|
|
87
|
+
await statusHandler(err, form);
|
|
88
|
+
throw err;
|
|
72
89
|
}
|
|
73
90
|
form.error = err;
|
|
74
91
|
const errorsData = resolveValidationErrorData(err, validator.errorParsers);
|
|
@@ -76,8 +93,7 @@ export function useForm(init, cb, options) {
|
|
|
76
93
|
form.setErrors(errorsData);
|
|
77
94
|
form.touch();
|
|
78
95
|
}
|
|
79
|
-
|
|
80
|
-
return o.onError(err, data);
|
|
96
|
+
throw err;
|
|
81
97
|
} finally {
|
|
82
98
|
form.processing = false;
|
|
83
99
|
form.validating = false;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-precognition",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.2.0",
|
|
5
5
|
"description": "Nuxt module implementing Precognition protocol (inspired by official Laravel Precognition) for backend precognitive validation.",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "sot1986",
|
|
@@ -30,20 +30,24 @@
|
|
|
30
30
|
},
|
|
31
31
|
"main": "./dist/module.cjs",
|
|
32
32
|
"types": "./dist/types.d.ts",
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
33
36
|
"files": [
|
|
34
37
|
"dist"
|
|
35
38
|
],
|
|
36
39
|
"scripts": {
|
|
37
40
|
"prepack": "nuxt-module-build build",
|
|
41
|
+
"build": "nuxt-module-build build",
|
|
38
42
|
"dev": "nuxi dev playground",
|
|
39
43
|
"dev:build": "nuxi build playground",
|
|
40
44
|
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
|
|
41
|
-
"release": "npm run lint && npm run test && npm run prepack && changelogen --release && npm publish && git push --follow-tags",
|
|
42
|
-
"release:locally": "npm run lint && npm run test && npm run prepack && npm pack && mv ./nuxt-precognition-*.tgz ../Released",
|
|
43
45
|
"lint": "eslint .",
|
|
44
46
|
"lint:fix": "eslint . --fix",
|
|
45
47
|
"test": "vitest run",
|
|
46
|
-
"test:
|
|
48
|
+
"test:unit": "vitest run test/unit",
|
|
49
|
+
"test:watch": "vitest watch",
|
|
50
|
+
"semantic-release": "semantic-release"
|
|
47
51
|
},
|
|
48
52
|
"dependencies": {
|
|
49
53
|
"@nuxt/kit": "^3.11.2",
|
|
@@ -56,6 +60,9 @@
|
|
|
56
60
|
"@nuxt/module-builder": "^0.8.3",
|
|
57
61
|
"@nuxt/schema": "^3.11.2",
|
|
58
62
|
"@nuxt/test-utils": "^3.12.1",
|
|
63
|
+
"@semantic-release/changelog": "^7.0.0",
|
|
64
|
+
"@semantic-release/git": "^11.0.1",
|
|
65
|
+
"@semantic-release/github": "^12.0.9",
|
|
59
66
|
"@tailwindcss/forms": "^0.5.7",
|
|
60
67
|
"@types/lodash": "^4.17.21",
|
|
61
68
|
"@types/lodash-es": "^4.17.12",
|
|
@@ -67,6 +74,7 @@
|
|
|
67
74
|
"nuxt": "^3.11.2",
|
|
68
75
|
"playwright-core": "^1.44.0",
|
|
69
76
|
"postcss": "^8.4.38",
|
|
77
|
+
"semantic-release": "^25.0.8",
|
|
70
78
|
"tailwindcss": "^3.4.3",
|
|
71
79
|
"typescript": "^5.4.5",
|
|
72
80
|
"vitest": "^1.6.0",
|