nuxt-precognition 0.0.9 → 0.1.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/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 zodErrorParser: ValidationErrorParser = (error) => {
116
+ export const zodPrecognitionErrorParser: ValidationErrorParser = (error) => {
117
117
  if (error instanceof ZodError) {
118
- const errors = {} as Record<string, string[]>
119
- error.errors.forEach((e) => {
120
- const key = e.path.join('.')
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(e.message)
123
- return
122
+ errors[key].push(issue.message)
123
+ continue
124
124
  }
125
- errors[key] = [e.message]
126
- })
127
- return { errors, message: 'Validation error' }
125
+ errors[key] = [issue.message]
126
+ }
127
+ return { errors, message: error.message }
128
128
  }
129
129
  return null
130
130
  }
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 { type ModuleOptions, _default as default };
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 { type ModuleOptions, _default as default };
8
+ export { _default as default };
9
+ export type { ModuleOptions };
package/dist/module.json CHANGED
@@ -4,9 +4,9 @@
4
4
  "compatibility": {
5
5
  "nuxt": ">=3.9.0"
6
6
  },
7
- "version": "0.0.9",
7
+ "version": "0.1.2",
8
8
  "builder": {
9
- "@nuxt/module-builder": "0.8.3",
10
- "unbuild": "2.0.0"
9
+ "@nuxt/module-builder": "0.8.4",
10
+ "unbuild": "unknown"
11
11
  }
12
12
  }
package/dist/module.mjs CHANGED
@@ -48,7 +48,7 @@ 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 {
@@ -67,7 +67,11 @@ declare module 'vue' {
67
67
  interface ComponentCustomProperties extends NuxtPrecognition {}
68
68
  }
69
69
 
70
- export {}`
70
+ export {
71
+ ValidationErrorParser,
72
+ ClientStatusHandlers,
73
+ ValidationErrorsData
74
+ }`
71
75
  });
72
76
  const nitroTemplate = addTemplate({
73
77
  filename: "types/nuxt-precognition-server.d.ts",
@@ -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, H3Event } from 'h3';
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[] | undefined;
12
- statusHandlers?: Partial<Record<ErrorStatusCode, <TRequest_1 extends EventHandlerRequest>(error: Error, event: H3Event<TRequest_1>) => void | Promise<void>>> | undefined;
10
+ errorParsers?: ValidationErrorParser[];
11
+ statusHandlers?: ServerStatusHandlers;
13
12
  }) => (handler: EventHandlerObject<TRequest, TResponse>, options?: {
14
- errorParsers?: ValidationErrorParser[] | undefined;
15
- statusHandlers?: Partial<Record<ErrorStatusCode, <TRequest_1 extends EventHandlerRequest>(error: Error, event: H3Event<TRequest_1>) => void | Promise<void>>> | undefined;
16
- } | undefined) => EventHandler<TRequest, TResponse>;
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
- clientValidation?: (data: TData) => Promise<void> | void;
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> | undefined) => (init: TData | (() => TData), cb: (form: TData, precognitiveHeaders: Headers) => Promise<TResp>, options?: UseFormOptions<TData> | undefined) => TData & Form<TData, TResp>;
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
  }
@@ -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
- export { type ModuleOptions, default } from './module.js'
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
- export { type ModuleOptions, default } from './module'
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.0.9",
4
+ "version": "0.1.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",
@@ -67,8 +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": "^3.23.8"
72
+ "zod": "^4.1.11"
72
73
  },
73
74
  "packageManager": "pnpm@9.12.3+sha512.cce0f9de9c5a7c95bef944169cc5dfe8741abfb145078c0d508b868056848a87c81e626246cb60967cbd7fd29a6c062ef73ff840d96b3c86c40ac92cf4a813ee"
74
75
  }