nuxt-precognition 0.1.7 → 0.3.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 +8 -4
- package/dist/runtime/useForm.d.ts +2 -2
- package/dist/runtime/useForm.js +34 -14
- package/package.json +12 -4
package/dist/module.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
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> {
|
|
4
|
-
headers?:
|
|
3
|
+
export interface SubmitOptions<TData extends object, TResp> {
|
|
4
|
+
headers?: HeadersInit;
|
|
5
5
|
/**
|
|
6
6
|
* callback triggered when error is captured
|
|
7
7
|
* @param error
|
|
@@ -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?: HeadersInit;
|
|
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;
|
|
@@ -56,7 +60,7 @@ export interface UseFormOptions<TData extends object, TResp> {
|
|
|
56
60
|
/**
|
|
57
61
|
* Headers to be sent along with the precognition request (e.g. for authentication purposes).
|
|
58
62
|
*/
|
|
59
|
-
validationHeaders?:
|
|
63
|
+
validationHeaders?: HeadersInit;
|
|
60
64
|
/**
|
|
61
65
|
* Timeout in milliseconds for the validation request.
|
|
62
66
|
*/
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Form, UseFormOptions } from './types/form.js';
|
|
2
|
-
export declare function useForm<TData extends object, TResp>(init: TData | (() => TData), cb: (form: TData, precognitiveHeaders:
|
|
2
|
+
export declare function useForm<TData extends object, TResp>(init: TData | (() => TData), cb: (form: TData, precognitiveHeaders: Record<string, string>) => Promise<TResp>, options?: UseFormOptions<TData, TResp>): TData & Form<TData, TResp>;
|
|
3
3
|
export declare namespace useForm {
|
|
4
|
-
var create: <TData extends object, TResp>(_options?: UseFormOptions<TData, TResp>) => (init: TData | (() => TData), cb: (form: TData, precognitiveHeaders:
|
|
4
|
+
var create: <TData extends object, TResp>(_options?: UseFormOptions<TData, TResp>) => (init: TData | (() => TData), cb: (form: TData, precognitiveHeaders: Record<string, string>) => Promise<TResp>, options?: UseFormOptions<TData, TResp>) => TData & Form<TData, TResp>;
|
|
5
5
|
}
|
package/dist/runtime/useForm.js
CHANGED
|
@@ -7,7 +7,29 @@ export function useForm(init, cb, options) {
|
|
|
7
7
|
function getInitialData() {
|
|
8
8
|
return cloneDeep(toRaw(resolveDynamicObject(init)));
|
|
9
9
|
}
|
|
10
|
-
const validator = makeValidator(
|
|
10
|
+
const validator = makeValidator(
|
|
11
|
+
(data, headers) => cb(data, Object.fromEntries(headers.entries())),
|
|
12
|
+
options
|
|
13
|
+
);
|
|
14
|
+
async function handleSubmit(o) {
|
|
15
|
+
if (form.processing)
|
|
16
|
+
return;
|
|
17
|
+
const data = form.data();
|
|
18
|
+
try {
|
|
19
|
+
const onBefore = o?.onBefore ? await o.onBefore(data) : true;
|
|
20
|
+
if (!onBefore)
|
|
21
|
+
return;
|
|
22
|
+
if (o?.onStart)
|
|
23
|
+
await o.onStart(data);
|
|
24
|
+
const resp = await form.asyncSubmit({ headers: o?.headers });
|
|
25
|
+
if (o?.onSuccess)
|
|
26
|
+
await o.onSuccess(resp, data);
|
|
27
|
+
} catch (error) {
|
|
28
|
+
const err = error instanceof Error ? error : new Error("Invalid form");
|
|
29
|
+
if (o?.onError)
|
|
30
|
+
await o.onError(err, data);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
11
33
|
const form = reactive({
|
|
12
34
|
...getInitialData(),
|
|
13
35
|
data: () => {
|
|
@@ -42,33 +64,32 @@ export function useForm(init, cb, options) {
|
|
|
42
64
|
form.validatedKeys = [];
|
|
43
65
|
return form;
|
|
44
66
|
},
|
|
45
|
-
|
|
67
|
+
submit(o) {
|
|
68
|
+
void handleSubmit(o);
|
|
69
|
+
},
|
|
70
|
+
async asyncSubmit(o) {
|
|
46
71
|
if (form.processing)
|
|
47
|
-
|
|
72
|
+
throw new Error("Form is already processing.");
|
|
48
73
|
const data = form.data();
|
|
49
74
|
try {
|
|
50
|
-
const onBefore = o?.onBefore ? await o.onBefore(data) : true;
|
|
51
|
-
if (!onBefore)
|
|
52
|
-
return;
|
|
53
75
|
form.processing = true;
|
|
54
76
|
form.error = null;
|
|
55
|
-
if (o?.onStart)
|
|
56
|
-
await o.onStart(data);
|
|
57
77
|
if (options?.clientValidation) {
|
|
58
78
|
form.validating = true;
|
|
59
79
|
await options.clientValidation(data);
|
|
60
80
|
form.validating = false;
|
|
61
81
|
}
|
|
62
|
-
const
|
|
82
|
+
const h = new Headers(o?.headers);
|
|
83
|
+
const resp = await cb(data, Object.fromEntries(h.entries()));
|
|
63
84
|
if (validator.responseParsers.length)
|
|
64
85
|
validator.responseParsers.forEach((p) => p(resp));
|
|
65
|
-
|
|
66
|
-
await o.onSuccess(resp, data);
|
|
86
|
+
return resp;
|
|
67
87
|
} catch (error) {
|
|
68
88
|
const err = error instanceof Error ? error : new Error("Invalid form");
|
|
69
89
|
const statusHandler = hasResponse(err) ? validator.statusHandlers[`${err.response.status}`] : void 0;
|
|
70
90
|
if (statusHandler) {
|
|
71
|
-
|
|
91
|
+
await statusHandler(err, form);
|
|
92
|
+
throw err;
|
|
72
93
|
}
|
|
73
94
|
form.error = err;
|
|
74
95
|
const errorsData = resolveValidationErrorData(err, validator.errorParsers);
|
|
@@ -76,8 +97,7 @@ export function useForm(init, cb, options) {
|
|
|
76
97
|
form.setErrors(errorsData);
|
|
77
98
|
form.touch();
|
|
78
99
|
}
|
|
79
|
-
|
|
80
|
-
return o.onError(err, data);
|
|
100
|
+
throw err;
|
|
81
101
|
} finally {
|
|
82
102
|
form.processing = false;
|
|
83
103
|
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.3.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",
|