nuxt-precognition 0.0.8 → 0.1.1
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/README.md +10 -10
- package/dist/module.d.mts +3 -2
- package/dist/module.d.ts +3 -2
- package/dist/module.json +3 -3
- package/dist/module.mjs +8 -3
- package/dist/runtime/plugin.d.ts +20 -0
- package/dist/runtime/server/definePrecognitiveEventHandler.d.ts +6 -7
- package/dist/runtime/types/form.d.ts +45 -1
- package/dist/runtime/useForm.d.ts +1 -1
- package/dist/runtime/useForm.js +5 -0
- package/dist/types.d.mts +7 -1
- package/dist/types.d.ts +7 -1
- package/package.json +5 -3
- package/dist/runtime/playWithTypes.d.ts +0 -2
- package/dist/runtime/playWithTypes.js +0 -41
package/README.md
CHANGED
|
@@ -113,18 +113,18 @@ type ValidationErrorParser = (error: Error) => ValidationErrorsData | undefined
|
|
|
113
113
|
// app/utils/precognition.ts or shared/utils/precognition.ts
|
|
114
114
|
import { ZodError } from 'zod'
|
|
115
115
|
|
|
116
|
-
export const
|
|
116
|
+
export const zodPrecognitionErrorParser: ValidationErrorParser = (error) => {
|
|
117
117
|
if (error instanceof ZodError) {
|
|
118
|
-
const errors
|
|
119
|
-
error.
|
|
120
|
-
const key =
|
|
118
|
+
const errors: Record<string, string[]> = {}
|
|
119
|
+
for (const issue of error.issues) {
|
|
120
|
+
const key = issue.path.join('.')
|
|
121
121
|
if (key in errors) {
|
|
122
|
-
errors[key].push(
|
|
123
|
-
|
|
122
|
+
errors[key].push(issue.message)
|
|
123
|
+
continue
|
|
124
124
|
}
|
|
125
|
-
errors[key] = [
|
|
126
|
-
}
|
|
127
|
-
return { errors, message:
|
|
125
|
+
errors[key] = [issue.message]
|
|
126
|
+
}
|
|
127
|
+
return { errors, message: error.message }
|
|
128
128
|
}
|
|
129
129
|
return null
|
|
130
130
|
}
|
|
@@ -182,7 +182,7 @@ function reset() {
|
|
|
182
182
|
}
|
|
183
183
|
```
|
|
184
184
|
|
|
185
|
-
```
|
|
185
|
+
```html
|
|
186
186
|
<form
|
|
187
187
|
@submit.prevent="login"
|
|
188
188
|
@reset.prevent="reset"
|
package/dist/module.d.mts
CHANGED
|
@@ -3,6 +3,7 @@ import { Config } from '../dist/runtime/types/config.js';
|
|
|
3
3
|
|
|
4
4
|
interface ModuleOptions extends Config {
|
|
5
5
|
}
|
|
6
|
-
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
|
|
6
|
+
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
|
|
7
7
|
|
|
8
|
-
export {
|
|
8
|
+
export { _default as default };
|
|
9
|
+
export type { ModuleOptions };
|
package/dist/module.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { Config } from '../dist/runtime/types/config.js';
|
|
|
3
3
|
|
|
4
4
|
interface ModuleOptions extends Config {
|
|
5
5
|
}
|
|
6
|
-
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
|
|
6
|
+
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
|
|
7
7
|
|
|
8
|
-
export {
|
|
8
|
+
export { _default as default };
|
|
9
|
+
export type { ModuleOptions };
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -48,13 +48,14 @@ const module = defineNuxtModule({
|
|
|
48
48
|
addTypeTemplate({
|
|
49
49
|
filename: "types/nuxt-precognition.d.ts",
|
|
50
50
|
getContents: () => `// Generated by nuxt-precognition
|
|
51
|
-
import type {ValidationErrorParser} from '${resolver.resolve("./runtime/types/core")}'
|
|
51
|
+
import type {ValidationErrorParser, ValidationErrorsData} from '${resolver.resolve("./runtime/types/core")}'
|
|
52
52
|
import type {ClientStatusHandlers} from '${resolver.resolve("./runtime/types/form")}'
|
|
53
53
|
|
|
54
54
|
interface NuxtPrecognition {
|
|
55
55
|
$precognition: {
|
|
56
56
|
errorParsers: ValidationErrorParser[],
|
|
57
|
-
statusHandlers: ClientStatusHandlers
|
|
57
|
+
statusHandlers: ClientStatusHandlers,
|
|
58
|
+
assertSuccessfulPrecognitiveResponses: (ctx: { options: { headers?: HeadersInit }, response: Response }) => void
|
|
58
59
|
}
|
|
59
60
|
}
|
|
60
61
|
|
|
@@ -66,7 +67,11 @@ declare module 'vue' {
|
|
|
66
67
|
interface ComponentCustomProperties extends NuxtPrecognition {}
|
|
67
68
|
}
|
|
68
69
|
|
|
69
|
-
export {
|
|
70
|
+
export {
|
|
71
|
+
ValidationErrorParser,
|
|
72
|
+
ClientStatusHandlers,
|
|
73
|
+
ValidationErrorsData
|
|
74
|
+
}`
|
|
70
75
|
});
|
|
71
76
|
const nitroTemplate = addTemplate({
|
|
72
77
|
filename: "types/nuxt-precognition-server.d.ts",
|
package/dist/runtime/plugin.d.ts
CHANGED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ValidationErrorParser } from './types/core.js';
|
|
2
|
+
import { assertSuccessfulPrecognitiveResponses } from './core.js';
|
|
3
|
+
declare const _default: import("nuxt/app").Plugin<{
|
|
4
|
+
precognition: {
|
|
5
|
+
errorParsers: ValidationErrorParser[];
|
|
6
|
+
statusHandlers: Partial<Record<import("./types/utils").ErrorStatusCode, <TData extends object, TResp>(error: Error & {
|
|
7
|
+
response: Response;
|
|
8
|
+
}, form: TData & import("./types/form").Form<TData, TResp>) => void | Promise<void>>>;
|
|
9
|
+
assertSuccessfulPrecognitiveResponses: typeof assertSuccessfulPrecognitiveResponses;
|
|
10
|
+
};
|
|
11
|
+
}> & import("nuxt/app").ObjectPlugin<{
|
|
12
|
+
precognition: {
|
|
13
|
+
errorParsers: ValidationErrorParser[];
|
|
14
|
+
statusHandlers: Partial<Record<import("./types/utils").ErrorStatusCode, <TData extends object, TResp>(error: Error & {
|
|
15
|
+
response: Response;
|
|
16
|
+
}, form: TData & import("./types/form").Form<TData, TResp>) => void | Promise<void>>>;
|
|
17
|
+
assertSuccessfulPrecognitiveResponses: typeof assertSuccessfulPrecognitiveResponses;
|
|
18
|
+
};
|
|
19
|
+
}>;
|
|
20
|
+
export default _default;
|
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
import type { EventHandler, EventHandlerObject, EventHandlerRequest
|
|
1
|
+
import type { EventHandler, EventHandlerObject, EventHandlerRequest } from 'h3';
|
|
2
2
|
import type { ValidationErrorParser } from '../types/core.js';
|
|
3
3
|
import type { ServerStatusHandlers } from '../types/eventHandler.js';
|
|
4
|
-
import type { ErrorStatusCode } from '../types/utils.js';
|
|
5
4
|
declare function definePrecognitiveEventHandler<TRequest extends EventHandlerRequest, TResponse>(handler: EventHandlerObject<TRequest, TResponse>, options?: {
|
|
6
5
|
errorParsers?: ValidationErrorParser[];
|
|
7
6
|
statusHandlers?: ServerStatusHandlers;
|
|
8
7
|
}): EventHandler<TRequest, TResponse>;
|
|
9
8
|
declare namespace definePrecognitiveEventHandler {
|
|
10
9
|
var create: <TRequest extends EventHandlerRequest, TResponse>(options: {
|
|
11
|
-
errorParsers?: ValidationErrorParser[]
|
|
12
|
-
statusHandlers?:
|
|
10
|
+
errorParsers?: ValidationErrorParser[];
|
|
11
|
+
statusHandlers?: ServerStatusHandlers;
|
|
13
12
|
}) => (handler: EventHandlerObject<TRequest, TResponse>, options?: {
|
|
14
|
-
errorParsers?: ValidationErrorParser[]
|
|
15
|
-
statusHandlers?:
|
|
16
|
-
}
|
|
13
|
+
errorParsers?: ValidationErrorParser[];
|
|
14
|
+
statusHandlers?: ServerStatusHandlers;
|
|
15
|
+
}) => EventHandler<TRequest, TResponse>;
|
|
17
16
|
}
|
|
18
17
|
export { definePrecognitiveEventHandler };
|
|
@@ -31,16 +31,60 @@ export type ClientStatusHandlers = Partial<Record<ErrorStatusCode, <TData extend
|
|
|
31
31
|
response: Response;
|
|
32
32
|
}, form: TData & Form<TData, TResp>) => void | Promise<void>>>;
|
|
33
33
|
export interface UseFormOptions<TData extends object> {
|
|
34
|
+
/**
|
|
35
|
+
* Headers to be sent along with the precognition request (e.g. for authentication purposes).
|
|
36
|
+
*/
|
|
34
37
|
validationHeaders?: Headers;
|
|
38
|
+
/**
|
|
39
|
+
* Timeout in milliseconds for the validation request.
|
|
40
|
+
*/
|
|
35
41
|
validationTimeout?: number;
|
|
42
|
+
/**
|
|
43
|
+
* Whether to perform backend validation using a precognition request.
|
|
44
|
+
*/
|
|
36
45
|
backendValidation?: boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Whether to send files along with the precognition request during precognitive validation.
|
|
48
|
+
*/
|
|
37
49
|
validateFiles?: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Called before the precognitive validation request is sent. If it returns `false`, the request is not sent.
|
|
52
|
+
* @param data The form data
|
|
53
|
+
* @returns Whether to proceed with the validation request
|
|
54
|
+
*/
|
|
38
55
|
onBeforeValidation?: (data: TData) => boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Called when the precognitive validation request is started.
|
|
58
|
+
* @param data The form data
|
|
59
|
+
*/
|
|
39
60
|
onValidationStart?: (data: TData) => Promise<void> | void;
|
|
40
|
-
|
|
61
|
+
/**
|
|
62
|
+
* If present, this function is called to perform client-side validation before sending the precognitive validation request.
|
|
63
|
+
* It is called after `onBeforeValidation` and `onValidationStart`.
|
|
64
|
+
* If it throws an error, the precognitive validation request is not sent, and the error is handled as a validation error.
|
|
65
|
+
* @param data The form data
|
|
66
|
+
* @returns A promise that resolves when client-side validation is complete
|
|
67
|
+
*/
|
|
68
|
+
clientValidation?: (data: TData) => Promise<unknown> | unknown;
|
|
69
|
+
/**
|
|
70
|
+
* Called when the precognitive validation request returns a validation error (HTTP 422) or any other error.
|
|
71
|
+
* @param error The error object
|
|
72
|
+
* @param data The form data
|
|
73
|
+
* @param keys The keys that failed validation
|
|
74
|
+
*/
|
|
41
75
|
onValidationError?: (error: Error, data: TData, keys: NestedKeyOf<TData>[]) => Promise<void> | void;
|
|
76
|
+
/**
|
|
77
|
+
* Called when the precognitive validation request is successful (HTTP 204).
|
|
78
|
+
* @param data The form data
|
|
79
|
+
*/
|
|
42
80
|
onValidationSuccess?: (data: TData) => Promise<void> | void;
|
|
81
|
+
/**
|
|
82
|
+
* Any custom error parser that should specifically be used for this form instance when handling validation errors.
|
|
83
|
+
*/
|
|
43
84
|
clientErrorParsers?: ValidationErrorParser[];
|
|
85
|
+
/**
|
|
86
|
+
* Handlers for specific client status codes.
|
|
87
|
+
*/
|
|
44
88
|
statusHandlers?: ClientStatusHandlers;
|
|
45
89
|
}
|
|
46
90
|
export {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Form, UseFormOptions } from './types/form.js';
|
|
2
2
|
export declare function useForm<TData extends object, TResp>(init: TData | (() => TData), cb: (form: TData, precognitiveHeaders: Headers) => Promise<TResp>, options?: UseFormOptions<TData>): TData & Form<TData, TResp>;
|
|
3
3
|
export declare namespace useForm {
|
|
4
|
-
var create: <TData extends object, TResp>(_options?: UseFormOptions<TData>
|
|
4
|
+
var create: <TData extends object, TResp>(_options?: UseFormOptions<TData>) => (init: TData | (() => TData), cb: (form: TData, precognitiveHeaders: Headers) => Promise<TResp>, options?: UseFormOptions<TData>) => TData & Form<TData, TResp>;
|
|
5
5
|
}
|
package/dist/runtime/useForm.js
CHANGED
|
@@ -54,6 +54,11 @@ export function useForm(init, cb, options) {
|
|
|
54
54
|
form.error = null;
|
|
55
55
|
if (o?.onStart)
|
|
56
56
|
await o.onStart(data);
|
|
57
|
+
if (options?.clientValidation) {
|
|
58
|
+
form.validating = true;
|
|
59
|
+
await options.clientValidation(data);
|
|
60
|
+
form.validating = false;
|
|
61
|
+
}
|
|
57
62
|
const resp = await cb(data, o?.headers ?? new Headers());
|
|
58
63
|
if (o?.onSuccess)
|
|
59
64
|
await o.onSuccess(resp, data);
|
package/dist/types.d.mts
CHANGED
|
@@ -1 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
import type { NuxtModule } from '@nuxt/schema'
|
|
2
|
+
|
|
3
|
+
import type { default as Module } from './module.js'
|
|
4
|
+
|
|
5
|
+
export type ModuleOptions = typeof Module extends NuxtModule<infer O> ? Partial<O> : Record<string, any>
|
|
6
|
+
|
|
7
|
+
export { default } from './module.js'
|
package/dist/types.d.ts
CHANGED
|
@@ -1 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
import type { NuxtModule } from '@nuxt/schema'
|
|
2
|
+
|
|
3
|
+
import type { default as Module } from './module'
|
|
4
|
+
|
|
5
|
+
export type ModuleOptions = typeof Module extends NuxtModule<infer O> ? Partial<O> : Record<string, any>
|
|
6
|
+
|
|
7
|
+
export { default } from './module'
|
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.1.1",
|
|
5
5
|
"description": "Nuxt module implementing Precognition protocol (inspired by official Laravel Precognition) for backend precognitive validation.",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "sot1986",
|
|
@@ -67,7 +67,9 @@
|
|
|
67
67
|
"tailwindcss": "^3.4.3",
|
|
68
68
|
"typescript": "^5.4.5",
|
|
69
69
|
"vitest": "^1.6.0",
|
|
70
|
+
"vue": "^3.5.22",
|
|
70
71
|
"vue-tsc": "^2.0.17",
|
|
71
|
-
"zod": "^
|
|
72
|
-
}
|
|
72
|
+
"zod": "^4.1.11"
|
|
73
|
+
},
|
|
74
|
+
"packageManager": "pnpm@9.12.3+sha512.cce0f9de9c5a7c95bef944169cc5dfe8741abfb145078c0d508b868056848a87c81e626246cb60967cbd7fd29a6c062ef73ff840d96b3c86c40ac92cf4a813ee"
|
|
73
75
|
}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
const model = {
|
|
2
|
-
id: "1",
|
|
3
|
-
name: "John Doe",
|
|
4
|
-
age: 30,
|
|
5
|
-
address: {
|
|
6
|
-
street: "123 Main St",
|
|
7
|
-
city: "Anytown"
|
|
8
|
-
},
|
|
9
|
-
tags: ["developer", "blogger"],
|
|
10
|
-
websites: [
|
|
11
|
-
{
|
|
12
|
-
url: "https://example.com",
|
|
13
|
-
description: "My personal blog",
|
|
14
|
-
followers: 1e3
|
|
15
|
-
}
|
|
16
|
-
],
|
|
17
|
-
favorites: [
|
|
18
|
-
{ site: "https://favorite.com" },
|
|
19
|
-
{ site: "https://another-favorite.com" }
|
|
20
|
-
]
|
|
21
|
-
};
|
|
22
|
-
export function getKeyValue(obj, key) {
|
|
23
|
-
const firstKey = key.split(".")[0];
|
|
24
|
-
if (key === firstKey) {
|
|
25
|
-
return obj[firstKey];
|
|
26
|
-
}
|
|
27
|
-
if (typeof firstKey !== "string") {
|
|
28
|
-
throw new TypeError(`Key must be a string, received: ${typeof firstKey}`);
|
|
29
|
-
}
|
|
30
|
-
if (!(firstKey in obj)) {
|
|
31
|
-
throw new Error(`Key "${firstKey}" does not exist in the object.`);
|
|
32
|
-
}
|
|
33
|
-
if (typeof obj[firstKey] !== "object") {
|
|
34
|
-
throw new TypeError(`Value at key "${firstKey}" is not an object or is null.`);
|
|
35
|
-
}
|
|
36
|
-
if (!obj[firstKey]) {
|
|
37
|
-
return null;
|
|
38
|
-
}
|
|
39
|
-
const restKey = key.slice(firstKey.length + 1);
|
|
40
|
-
return getKeyValue(obj[firstKey], restKey);
|
|
41
|
-
}
|