import type { TaintedFields, SuperFormValidated, SuperValidated } from '../superValidate.js'; import type { ActionResult, BeforeNavigate, Page, SubmitFunction, Transport } from '@sveltejs/kit'; import { type Readable, type Writable, type Updater } from 'svelte/store'; import { type FormPathType, type FormPath, type FormPathLeaves } from '../stringPath.js'; import { enhance as kitEnhance } from '$app/forms'; import type { ValidationErrors } from '../superValidate.js'; import type { IsAny, MaybePromise } from '../utils.js'; import type { ClientValidationAdapter, ValidationAdapter } from '../adapters/adapters.js'; import type { InputConstraints } from '../jsonSchema/constraints.js'; import { type ProxyOptions } from './proxies.js'; export type SuperFormEvents, M> = Pick, 'onError' | 'onResult' | 'onSubmit' | 'onUpdate' | 'onUpdated'>; export type SuperFormEventList, M> = { [Property in keyof SuperFormEvents]-?: NonNullable[Property]>[]; }; type FilterType = { [K in keyof NonNullable as NonNullable[K]> extends Check ? never : K]: NonNullable[K]; }; /** * Helper type for making ActionResult data strongly typed in onUpdate. * @example const action : FormResult = result.data; */ export type FormResult | null | undefined> = FilterType, any, Record>>; export type TaintOption = boolean | 'untaint' | 'untaint-all' | 'untaint-form'; type ValidatorsOption> = ValidationAdapter, Record> | false | 'clear'; export type FormOptions = Record, M = any, In extends Record = T> = Partial<{ id: string; /** * If `false`, the form won't react to page state updates, except for page invalidations. * If `'never'`, not even page invalidations will affect the form. * @default true */ applyAction: boolean | 'never'; invalidateAll: boolean | 'force' | 'pessimistic'; resetForm: boolean | (() => boolean); scrollToError: 'auto' | 'smooth' | 'off' | boolean | ScrollIntoViewOptions; autoFocusOnError: boolean | 'detect'; errorSelector: string; selectErrorText: boolean; stickyNavbar: string; taintedMessage: string | boolean | null | ((nav: BeforeNavigate) => MaybePromise); /** * Enable single page application (SPA) mode. * **The string and failStatus options are deprecated** and will be removed in the next major release. * @see https://superforms.rocks/concepts/spa */ SPA: true | /** @deprecated */ string | /** @deprecated */ { failStatus?: number; }; onSubmit: (input: Parameters[0] & { /** * If dataType: 'json' is set, send this data instead of $form when posting, * and client-side validation for $form passes. * @param data An object that can be serialized with devalue. */ jsonData: (data: Record) => void; /** * Override client validation temporarily for this form submission. */ validators: (validators: Exclude, 'clear'>) => void; /** * Use a custom fetch or XMLHttpRequest implementation for this form submission. It must return an ActionResult in the response body. * If the request is using a XMLHttpRequest, the promise must be resolved when the request has been completed, not before. */ customRequest: (validators: (input: Parameters[0]) => Promise) => void; }) => MaybePromise; onResult: (event: { result: ActionResult; /** * @deprecated Use formElement instead */ formEl: HTMLFormElement; formElement: HTMLFormElement; cancel: () => void; }) => MaybePromise; onUpdate: (event: { form: SuperValidated; /** * @deprecated Use formElement instead */ formEl: HTMLFormElement; formElement: HTMLFormElement; cancel: () => void; result: Required>; }) => MaybePromise; onUpdated: (event: { form: Readonly>; }) => MaybePromise; onError: 'apply' | ((event: { result: { type: 'error'; status?: number; error: App.Error | Error | { message: string; }; }; }) => MaybePromise); onChange: (event: ChangeEvent) => void; dataType: 'form' | 'json'; jsonChunkSize: number; validators: ClientValidationAdapter, Record> | ValidatorsOption; validationMethod: 'auto' | 'oninput' | 'onblur' | 'onsubmit' | 'submit-only'; customValidity: boolean; clearOnSubmit: 'errors' | 'message' | 'errors-and-message' | 'none'; delayMs: number; timeoutMs: number; multipleSubmits: 'prevent' | 'allow' | 'abort'; syncFlashMessage?: boolean; /** * @deprecated SvelteKit has moved to $app/state instead of $app/stores, making it hard to support both. Use the flash library directly (setFlash or redirect) instead of integrating it with Superforms. */ flashMessage: { module: { getFlash(page: Readable): Writable; updateFlash(page: Readable, update?: () => Promise): Promise; }; onError?: (event: { result: { type: 'error'; status?: number; error: App.Error | Error | { message: string; }; }; flashMessage: Writable; }) => MaybePromise; cookiePath?: string; cookieName?: string; }; warnings: { duplicateId?: boolean; }; transport: IsAny extends true ? never : Transport; /** * Version 1 compatibilty mode if true. * Sets resetForm = false and taintedMessage = true. * Add define: { SUPERFORMS_LEGACY: true } to vite.config.ts to enable globally. */ legacy: boolean; }>; export type SuperFormSnapshot, M = App.Superforms.Message extends never ? any : App.Superforms.Message, In extends Record = T> = SuperFormValidated & { tainted: TaintedFields | undefined; }; export type SuperFormData> = { subscribe: Readable['subscribe']; set(this: void, value: T, options?: { taint?: TaintOption; }): void; update(this: void, updater: Updater, options?: { taint?: TaintOption; }): void; }; export type SuperFormErrors> = { subscribe: Writable>['subscribe']; set(this: void, value: ValidationErrors, options?: { force?: boolean; }): void; update(this: void, updater: Updater>, options?: { force?: boolean; }): void; clear: () => void; }; type ResetOptions> = { keepMessage?: boolean; data?: Partial; newState?: Partial; id?: string; }; type Capture, M = App.Superforms.Message extends never ? any : App.Superforms.Message> = [T] extends [T] ? () => SuperFormSnapshot : never; type Restore, M = App.Superforms.Message extends never ? any : App.Superforms.Message> = (snapshot: SuperFormSnapshot) => void; export type SuperForm, M = App.Superforms.Message extends never ? any : App.Superforms.Message> = { form: SuperFormData; formId: Writable; errors: SuperFormErrors; constraints: Writable>; message: Writable; tainted: Writable | undefined>; submitting: Readable; delayed: Readable; timeout: Readable; /** * @deprecated posted is inconsistent between server and client validation, and SPA mode. Will be removed in v3. Use a status message or return your own data in the form action to handle form post status. */ posted: Readable; allErrors: Readable<{ path: string; messages: string[]; }[]>; options: T extends T ? FormOptions : never; enhance: (el: HTMLFormElement, events?: SuperFormEvents) => ReturnType; isTainted: (path?: FormPath | Record | boolean | undefined) => boolean; reset: (options?: ResetOptions) => void; submit: (submitter?: HTMLElement | Event | EventTarget | null) => void; capture: Capture; restore: T extends T ? Restore : never; validate: = T, Path extends FormPathLeaves = FormPathLeaves, In extends Record = Record>(path: Path, opts?: ValidateOptions, Out, In>) => Promise; validateForm: = T, In extends Record = Record>(opts?: { update?: boolean; schema?: ValidationAdapter; focusOnError?: boolean; }) => Promise>; }; export type ValidateOptions, In extends Record> = Partial<{ value: Value; update: boolean | 'errors' | 'value'; taint: TaintOption; errors: string | string[]; schema: ValidationAdapter; }>; export type ChangeEvent> = { path: FormPath; paths: FormPath[]; formElement: HTMLFormElement; target: Element; set: >(path: Path, value: FormPathType, options?: ProxyOptions) => void; get: >(path: Path) => FormPathType; } | { target: undefined; paths: FormPath[]; set: >(path: Path, value: FormPathType, options?: ProxyOptions) => void; get: >(path: Path) => FormPathType; }; /** * Initializes a SvelteKit form, for convenient handling of values, errors and sumbitting data. * @param {SuperValidated} form Usually data.form from PageData or defaults, but can also be an object with default values, but then constraints won't be available. * @param {FormOptions} formOptions Configuration for the form. * @returns {SuperForm} A SuperForm object that can be used in a Svelte component. * @DCI-context */ export declare function superForm = Record, M = App.Superforms.Message extends never ? any : App.Superforms.Message, In extends Record = T>(form: SuperValidated | T, formOptions?: FormOptions): SuperForm; export {};