@tanstack/react-form 0.24.3 → 0.25.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/dist/cjs/index.cjs +0 -3
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +0 -1
- package/dist/cjs/{createServerValidate.cjs → nextjs/createServerValidate.cjs} +11 -3
- package/dist/cjs/nextjs/createServerValidate.cjs.map +1 -0
- package/dist/cjs/nextjs/createServerValidate.d.cts +13 -0
- package/dist/cjs/nextjs/error.cjs +11 -0
- package/dist/cjs/nextjs/error.cjs.map +1 -0
- package/dist/cjs/nextjs/error.d.cts +10 -0
- package/dist/cjs/{nextjs-index.cjs → nextjs/index.cjs} +3 -1
- package/dist/cjs/nextjs/index.cjs.map +1 -0
- package/dist/cjs/nextjs/index.d.cts +3 -0
- package/dist/cjs/nextjs/types.d.cts +3 -0
- package/dist/cjs/start/createServerValidate.cjs +39 -0
- package/dist/cjs/start/createServerValidate.cjs.map +1 -0
- package/dist/cjs/start/createServerValidate.d.cts +13 -0
- package/dist/cjs/start/error.cjs +12 -0
- package/dist/cjs/start/error.cjs.map +1 -0
- package/dist/cjs/start/error.d.cts +12 -0
- package/dist/cjs/start/getFormData.cjs +22 -0
- package/dist/cjs/start/getFormData.cjs.map +1 -0
- package/dist/cjs/start/getFormData.d.cts +14 -0
- package/dist/cjs/start/index.cjs +10 -0
- package/dist/cjs/start/index.cjs.map +1 -0
- package/dist/cjs/start/index.d.cts +3 -0
- package/dist/cjs/start/types.d.cts +3 -0
- package/dist/cjs/start/utils.cjs +6 -0
- package/dist/cjs/start/utils.cjs.map +1 -0
- package/dist/cjs/start/utils.d.cts +1 -0
- package/dist/esm/index.d.ts +0 -1
- package/dist/esm/index.js +0 -3
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/nextjs/createServerValidate.d.ts +13 -0
- package/dist/esm/{createServerValidate.js → nextjs/createServerValidate.js} +11 -3
- package/dist/esm/nextjs/createServerValidate.js.map +1 -0
- package/dist/esm/nextjs/error.d.ts +10 -0
- package/dist/esm/nextjs/error.js +11 -0
- package/dist/esm/nextjs/error.js.map +1 -0
- package/dist/esm/nextjs/index.d.ts +3 -0
- package/dist/esm/{nextjs-index.js → nextjs/index.js} +3 -1
- package/dist/esm/nextjs/index.js.map +1 -0
- package/dist/esm/nextjs/types.d.ts +3 -0
- package/dist/esm/start/createServerValidate.d.ts +13 -0
- package/dist/esm/start/createServerValidate.js +39 -0
- package/dist/esm/start/createServerValidate.js.map +1 -0
- package/dist/esm/start/error.d.ts +12 -0
- package/dist/esm/start/error.js +12 -0
- package/dist/esm/start/error.js.map +1 -0
- package/dist/esm/start/getFormData.d.ts +14 -0
- package/dist/esm/start/getFormData.js +22 -0
- package/dist/esm/start/getFormData.js.map +1 -0
- package/dist/esm/start/index.d.ts +3 -0
- package/dist/esm/start/index.js +10 -0
- package/dist/esm/start/index.js.map +1 -0
- package/dist/esm/start/types.d.ts +3 -0
- package/dist/esm/start/utils.d.ts +1 -0
- package/dist/esm/start/utils.js +6 -0
- package/dist/esm/start/utils.js.map +1 -0
- package/package.json +26 -8
- package/src/index.ts +0 -1
- package/src/nextjs/createServerValidate.ts +66 -0
- package/src/nextjs/error.ts +18 -0
- package/src/nextjs/index.ts +4 -0
- package/src/nextjs/types.ts +6 -0
- package/src/start/createServerValidate.tsx +73 -0
- package/src/start/error.ts +21 -0
- package/src/start/getFormData.tsx +20 -0
- package/src/start/index.ts +3 -0
- package/src/start/types.ts +6 -0
- package/src/start/utils.ts +3 -0
- package/dist/cjs/createServerValidate.cjs.map +0 -1
- package/dist/cjs/createServerValidate.d.cts +0 -19
- package/dist/cjs/nextjs-index.cjs.map +0 -1
- package/dist/cjs/nextjs-index.d.cts +0 -2
- package/dist/esm/createServerValidate.d.ts +0 -19
- package/dist/esm/createServerValidate.js.map +0 -1
- package/dist/esm/nextjs-index.d.ts +0 -2
- package/dist/esm/nextjs-index.js.map +0 -1
- package/src/createServerValidate.ts +0 -72
- package/src/nextjs-index.ts +0 -3
package/dist/cjs/index.cjs
CHANGED
|
@@ -3,13 +3,10 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
3
3
|
const formCore = require("@tanstack/form-core");
|
|
4
4
|
const useForm = require("./useForm.cjs");
|
|
5
5
|
const useField = require("./useField.cjs");
|
|
6
|
-
const createServerValidate = require("./createServerValidate.cjs");
|
|
7
6
|
const useTransform = require("./useTransform.cjs");
|
|
8
7
|
exports.useForm = useForm.useForm;
|
|
9
8
|
exports.Field = useField.Field;
|
|
10
9
|
exports.useField = useField.useField;
|
|
11
|
-
exports.createServerValidate = createServerValidate.createServerValidate;
|
|
12
|
-
exports.initialFormState = createServerValidate.initialFormState;
|
|
13
10
|
exports.useTransform = useTransform.useTransform;
|
|
14
11
|
Object.keys(formCore).forEach((k) => {
|
|
15
12
|
if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k))
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;"}
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -2,5 +2,4 @@ export * from '@tanstack/form-core';
|
|
|
2
2
|
export { useForm } from './useForm.cjs';
|
|
3
3
|
export type { UseField, FieldComponent } from './useField.cjs';
|
|
4
4
|
export { useField, Field } from './useField.cjs';
|
|
5
|
-
export { createServerValidate, initialFormState } from './createServerValidate.cjs';
|
|
6
5
|
export { useTransform } from './useTransform.cjs';
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const decodeFormdata = require("decode-formdata");
|
|
4
|
+
const error = require("./error.cjs");
|
|
4
5
|
const createServerValidate = (defaultOpts) => async (formData, info) => {
|
|
5
6
|
const { validatorAdapter, onServerValidate } = defaultOpts;
|
|
6
7
|
const runValidator = (propsValue) => {
|
|
@@ -9,19 +10,26 @@ const createServerValidate = (defaultOpts) => async (formData, info) => {
|
|
|
9
10
|
}
|
|
10
11
|
return onServerValidate(propsValue);
|
|
11
12
|
};
|
|
12
|
-
const
|
|
13
|
-
const onServerError = runValidator({ value:
|
|
14
|
-
|
|
13
|
+
const values = decodeFormdata.decode(formData, info);
|
|
14
|
+
const onServerError = runValidator({ value: values });
|
|
15
|
+
if (!onServerError)
|
|
16
|
+
return;
|
|
17
|
+
const formState = {
|
|
15
18
|
errorMap: {
|
|
16
19
|
onServer: onServerError
|
|
17
20
|
},
|
|
21
|
+
values,
|
|
18
22
|
errors: onServerError ? [onServerError] : []
|
|
19
23
|
};
|
|
24
|
+
throw new error.ServerValidateError({
|
|
25
|
+
formState
|
|
26
|
+
});
|
|
20
27
|
};
|
|
21
28
|
const initialFormState = {
|
|
22
29
|
errorMap: {
|
|
23
30
|
onServer: void 0
|
|
24
31
|
},
|
|
32
|
+
values: void 0,
|
|
25
33
|
errors: []
|
|
26
34
|
};
|
|
27
35
|
exports.createServerValidate = createServerValidate;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createServerValidate.cjs","sources":["../../../src/nextjs/createServerValidate.ts"],"sourcesContent":["import { decode } from 'decode-formdata'\nimport { ServerValidateError } from './error'\nimport type {\n FormOptions,\n ValidationError,\n Validator,\n} from '@tanstack/form-core'\nimport type { ServerFormState } from './types'\n\ntype OnServerValidateFn<TFormData> = (props: {\n value: TFormData\n}) => ValidationError\n\ninterface CreateServerValidateOptions<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> extends FormOptions<TFormData, TFormValidator> {\n onServerValidate: OnServerValidateFn<TFormData>\n}\n\nexport const createServerValidate =\n <\n TFormData,\n TFormValidator extends\n | Validator<TFormData, unknown>\n | undefined = undefined,\n >(\n defaultOpts: CreateServerValidateOptions<TFormData, TFormValidator>,\n ) =>\n async (formData: FormData, info?: Parameters<typeof decode>[1]) => {\n const { validatorAdapter, onServerValidate } = defaultOpts\n\n const runValidator = (propsValue: { value: TFormData }) => {\n if (validatorAdapter && typeof onServerValidate !== 'function') {\n return validatorAdapter().validate(propsValue, onServerValidate)\n }\n\n return (onServerValidate as OnServerValidateFn<TFormData>)(propsValue)\n }\n\n const values = decode(formData, info) as never as TFormData\n\n const onServerError = runValidator({ value: values })\n\n if (!onServerError) return\n\n const formState: ServerFormState<TFormData> = {\n errorMap: {\n onServer: onServerError,\n },\n values,\n errors: onServerError ? [onServerError] : [],\n }\n\n throw new ServerValidateError({\n formState,\n })\n }\n\nexport const initialFormState: ServerFormState<any> = {\n errorMap: {\n onServer: undefined,\n },\n values: undefined,\n errors: [],\n}\n"],"names":["decode","ServerValidateError"],"mappings":";;;;AAoBO,MAAM,uBACX,CAME,gBAEF,OAAO,UAAoB,SAAwC;AAC3D,QAAA,EAAE,kBAAkB,iBAAqB,IAAA;AAEzC,QAAA,eAAe,CAAC,eAAqC;AACrD,QAAA,oBAAoB,OAAO,qBAAqB,YAAY;AAC9D,aAAO,iBAAiB,EAAE,SAAS,YAAY,gBAAgB;AAAA,IACjE;AAEA,WAAQ,iBAAmD,UAAU;AAAA,EAAA;AAGjE,QAAA,SAASA,eAAAA,OAAO,UAAU,IAAI;AAEpC,QAAM,gBAAgB,aAAa,EAAE,OAAO,OAAQ,CAAA;AAEpD,MAAI,CAAC;AAAe;AAEpB,QAAM,YAAwC;AAAA,IAC5C,UAAU;AAAA,MACR,UAAU;AAAA,IACZ;AAAA,IACA;AAAA,IACA,QAAQ,gBAAgB,CAAC,aAAa,IAAI,CAAC;AAAA,EAAA;AAG7C,QAAM,IAAIC,MAAAA,oBAAoB;AAAA,IAC5B;AAAA,EAAA,CACD;AACH;AAEK,MAAM,mBAAyC;AAAA,EACpD,UAAU;AAAA,IACR,UAAU;AAAA,EACZ;AAAA,EACA,QAAQ;AAAA,EACR,QAAQ,CAAC;AACX;;;"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { decode } from 'decode-formdata';
|
|
2
|
+
import { FormOptions, ValidationError, Validator } from '@tanstack/form-core';
|
|
3
|
+
import { ServerFormState } from './types.cjs';
|
|
4
|
+
|
|
5
|
+
type OnServerValidateFn<TFormData> = (props: {
|
|
6
|
+
value: TFormData;
|
|
7
|
+
}) => ValidationError;
|
|
8
|
+
interface CreateServerValidateOptions<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined> extends FormOptions<TFormData, TFormValidator> {
|
|
9
|
+
onServerValidate: OnServerValidateFn<TFormData>;
|
|
10
|
+
}
|
|
11
|
+
export declare const createServerValidate: <TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined>(defaultOpts: CreateServerValidateOptions<TFormData, TFormValidator>) => (formData: FormData, info?: Parameters<typeof decode>[1]) => Promise<void>;
|
|
12
|
+
export declare const initialFormState: ServerFormState<any>;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
class ServerValidateError extends Error {
|
|
4
|
+
constructor(options) {
|
|
5
|
+
super("Your form has errors. Please check the fields and try again.");
|
|
6
|
+
this.name = "ServerValidateError";
|
|
7
|
+
this.formState = options.formState;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.ServerValidateError = ServerValidateError;
|
|
11
|
+
//# sourceMappingURL=error.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.cjs","sources":["../../../src/nextjs/error.ts"],"sourcesContent":["import type { ServerFormState } from './types'\n\ninterface ServerValidateErrorState<TFormData> {\n formState: ServerFormState<TFormData>\n}\n\nexport class ServerValidateError<TFormData>\n extends Error\n implements ServerValidateErrorState<TFormData>\n{\n formState: ServerFormState<TFormData>\n\n constructor(options: ServerValidateErrorState<TFormData>) {\n super('Your form has errors. Please check the fields and try again.')\n this.name = 'ServerValidateError'\n this.formState = options.formState\n }\n}\n"],"names":[],"mappings":";;AAMO,MAAM,4BACH,MAEV;AAAA,EAGE,YAAY,SAA8C;AACxD,UAAM,8DAA8D;AACpE,SAAK,OAAO;AACZ,SAAK,YAAY,QAAQ;AAAA,EAC3B;AACF;;"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ServerFormState } from './types.cjs';
|
|
2
|
+
|
|
3
|
+
interface ServerValidateErrorState<TFormData> {
|
|
4
|
+
formState: ServerFormState<TFormData>;
|
|
5
|
+
}
|
|
6
|
+
export declare class ServerValidateError<TFormData> extends Error implements ServerValidateErrorState<TFormData> {
|
|
7
|
+
formState: ServerFormState<TFormData>;
|
|
8
|
+
constructor(options: ServerValidateErrorState<TFormData>);
|
|
9
|
+
}
|
|
10
|
+
export {};
|
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const formCore = require("@tanstack/form-core");
|
|
4
4
|
const createServerValidate = require("./createServerValidate.cjs");
|
|
5
|
+
const error = require("./error.cjs");
|
|
5
6
|
exports.createServerValidate = createServerValidate.createServerValidate;
|
|
6
7
|
exports.initialFormState = createServerValidate.initialFormState;
|
|
8
|
+
exports.ServerValidateError = error.ServerValidateError;
|
|
7
9
|
Object.keys(formCore).forEach((k) => {
|
|
8
10
|
if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k))
|
|
9
11
|
Object.defineProperty(exports, k, {
|
|
@@ -11,4 +13,4 @@ Object.keys(formCore).forEach((k) => {
|
|
|
11
13
|
get: () => formCore[k]
|
|
12
14
|
});
|
|
13
15
|
});
|
|
14
|
-
//# sourceMappingURL=
|
|
16
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const decodeFormdata = require("decode-formdata");
|
|
4
|
+
const utils = require("./utils.cjs");
|
|
5
|
+
const error = require("./error.cjs");
|
|
6
|
+
const createServerValidate = (defaultOpts) => async (ctx, formData, info) => {
|
|
7
|
+
const { validatorAdapter, onServerValidate } = defaultOpts;
|
|
8
|
+
const runValidator = (propsValue) => {
|
|
9
|
+
if (validatorAdapter && typeof onServerValidate !== "function") {
|
|
10
|
+
return validatorAdapter().validate(propsValue, onServerValidate);
|
|
11
|
+
}
|
|
12
|
+
return onServerValidate(propsValue);
|
|
13
|
+
};
|
|
14
|
+
const referer = ctx.request.headers.get("referer");
|
|
15
|
+
const data = decodeFormdata.decode(formData, info);
|
|
16
|
+
const onServerError = runValidator({ value: data });
|
|
17
|
+
if (!onServerError)
|
|
18
|
+
return;
|
|
19
|
+
const formState = {
|
|
20
|
+
errorMap: {
|
|
21
|
+
onServer: onServerError
|
|
22
|
+
},
|
|
23
|
+
values: data,
|
|
24
|
+
errors: onServerError ? [onServerError] : []
|
|
25
|
+
};
|
|
26
|
+
const cookie = await utils._tanstackInternalsCookie.serialize(formState);
|
|
27
|
+
throw new error.ServerValidateError({
|
|
28
|
+
response: new Response("ok", {
|
|
29
|
+
headers: {
|
|
30
|
+
Location: referer,
|
|
31
|
+
"Set-Cookie": cookie
|
|
32
|
+
},
|
|
33
|
+
status: 302
|
|
34
|
+
}),
|
|
35
|
+
formState
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
exports.createServerValidate = createServerValidate;
|
|
39
|
+
//# sourceMappingURL=createServerValidate.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createServerValidate.cjs","sources":["../../../src/start/createServerValidate.tsx"],"sourcesContent":["import { decode } from 'decode-formdata'\nimport { _tanstackInternalsCookie } from './utils'\nimport { ServerValidateError } from './error'\nimport type {\n FormOptions,\n ValidationError,\n Validator,\n} from '@tanstack/form-core'\nimport type { FetchFn } from '@tanstack/start'\nimport type { ServerFormState } from './types'\n\ntype Ctx = Parameters<FetchFn<FormData, unknown>>[1]\n\ntype OnServerValidateFn<TFormData> = (props: {\n value: TFormData\n}) => ValidationError\n\ninterface CreateServerValidateOptions<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> extends FormOptions<TFormData, TFormValidator> {\n onServerValidate: OnServerValidateFn<TFormData>\n}\n\nexport const createServerValidate =\n <\n TFormData,\n TFormValidator extends\n | Validator<TFormData, unknown>\n | undefined = undefined,\n >(\n defaultOpts: CreateServerValidateOptions<TFormData, TFormValidator>,\n ) =>\n async (ctx: Ctx, formData: FormData, info?: Parameters<typeof decode>[1]) => {\n const { validatorAdapter, onServerValidate } = defaultOpts\n\n const runValidator = (propsValue: { value: TFormData }) => {\n if (validatorAdapter && typeof onServerValidate !== 'function') {\n return validatorAdapter().validate(propsValue, onServerValidate)\n }\n\n return (onServerValidate as OnServerValidateFn<TFormData>)(propsValue)\n }\n\n const referer = ctx.request.headers.get('referer')!\n\n const data = decode(formData, info) as never as TFormData\n\n const onServerError = runValidator({ value: data })\n\n if (!onServerError) return\n\n const formState: ServerFormState<TFormData> = {\n errorMap: {\n onServer: onServerError,\n },\n values: data,\n errors: onServerError ? [onServerError] : [],\n }\n\n const cookie = await _tanstackInternalsCookie.serialize(formState)\n\n throw new ServerValidateError({\n response: new Response('ok', {\n headers: {\n Location: referer,\n 'Set-Cookie': cookie,\n },\n status: 302,\n }),\n formState: formState,\n })\n }\n"],"names":["decode","_tanstackInternalsCookie","ServerValidateError"],"mappings":";;;;;AAwBO,MAAM,uBACX,CAME,gBAEF,OAAO,KAAU,UAAoB,SAAwC;AACrE,QAAA,EAAE,kBAAkB,iBAAqB,IAAA;AAEzC,QAAA,eAAe,CAAC,eAAqC;AACrD,QAAA,oBAAoB,OAAO,qBAAqB,YAAY;AAC9D,aAAO,iBAAiB,EAAE,SAAS,YAAY,gBAAgB;AAAA,IACjE;AAEA,WAAQ,iBAAmD,UAAU;AAAA,EAAA;AAGvE,QAAM,UAAU,IAAI,QAAQ,QAAQ,IAAI,SAAS;AAE3C,QAAA,OAAOA,eAAAA,OAAO,UAAU,IAAI;AAElC,QAAM,gBAAgB,aAAa,EAAE,OAAO,KAAM,CAAA;AAElD,MAAI,CAAC;AAAe;AAEpB,QAAM,YAAwC;AAAA,IAC5C,UAAU;AAAA,MACR,UAAU;AAAA,IACZ;AAAA,IACA,QAAQ;AAAA,IACR,QAAQ,gBAAgB,CAAC,aAAa,IAAI,CAAC;AAAA,EAAA;AAG7C,QAAM,SAAS,MAAMC,MAAAA,yBAAyB,UAAU,SAAS;AAEjE,QAAM,IAAIC,MAAAA,oBAAoB;AAAA,IAC5B,UAAU,IAAI,SAAS,MAAM;AAAA,MAC3B,SAAS;AAAA,QACP,UAAU;AAAA,QACV,cAAc;AAAA,MAChB;AAAA,MACA,QAAQ;AAAA,IAAA,CACT;AAAA,IACD;AAAA,EAAA,CACD;AACH;;"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { decode } from 'decode-formdata';
|
|
2
|
+
import { FormOptions, ValidationError, Validator } from '@tanstack/form-core';
|
|
3
|
+
import { FetchFn } from '@tanstack/start';
|
|
4
|
+
|
|
5
|
+
type Ctx = Parameters<FetchFn<FormData, unknown>>[1];
|
|
6
|
+
type OnServerValidateFn<TFormData> = (props: {
|
|
7
|
+
value: TFormData;
|
|
8
|
+
}) => ValidationError;
|
|
9
|
+
interface CreateServerValidateOptions<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined> extends FormOptions<TFormData, TFormValidator> {
|
|
10
|
+
onServerValidate: OnServerValidateFn<TFormData>;
|
|
11
|
+
}
|
|
12
|
+
export declare const createServerValidate: <TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined>(defaultOpts: CreateServerValidateOptions<TFormData, TFormValidator>) => (ctx: Ctx, formData: FormData, info?: Parameters<typeof decode>[1]) => Promise<void>;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
class ServerValidateError extends Error {
|
|
4
|
+
constructor(options) {
|
|
5
|
+
super("Your form has errors. Please check the fields and try again.");
|
|
6
|
+
this.name = "ServerValidateError";
|
|
7
|
+
this.response = options.response;
|
|
8
|
+
this.formState = options.formState;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
exports.ServerValidateError = ServerValidateError;
|
|
12
|
+
//# sourceMappingURL=error.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.cjs","sources":["../../../src/start/error.ts"],"sourcesContent":["import type { ServerFormState } from './types'\n\ninterface ServerValidateErrorState<TFormData> {\n formState: ServerFormState<TFormData>\n response: Response\n}\n\nexport class ServerValidateError<TFormData>\n extends Error\n implements ServerValidateErrorState<TFormData>\n{\n response: Response\n formState: ServerFormState<TFormData>\n\n constructor(options: ServerValidateErrorState<TFormData>) {\n super('Your form has errors. Please check the fields and try again.')\n this.name = 'ServerValidateError'\n this.response = options.response\n this.formState = options.formState\n }\n}\n"],"names":[],"mappings":";;AAOO,MAAM,4BACH,MAEV;AAAA,EAIE,YAAY,SAA8C;AACxD,UAAM,8DAA8D;AACpE,SAAK,OAAO;AACZ,SAAK,WAAW,QAAQ;AACxB,SAAK,YAAY,QAAQ;AAAA,EAC3B;AACF;;"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ServerFormState } from './types.cjs';
|
|
2
|
+
|
|
3
|
+
interface ServerValidateErrorState<TFormData> {
|
|
4
|
+
formState: ServerFormState<TFormData>;
|
|
5
|
+
response: Response;
|
|
6
|
+
}
|
|
7
|
+
export declare class ServerValidateError<TFormData> extends Error implements ServerValidateErrorState<TFormData> {
|
|
8
|
+
response: Response;
|
|
9
|
+
formState: ServerFormState<TFormData>;
|
|
10
|
+
constructor(options: ServerValidateErrorState<TFormData>);
|
|
11
|
+
}
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const start = require("@tanstack/start");
|
|
4
|
+
const utils = require("./utils.cjs");
|
|
5
|
+
const initialFormState = {
|
|
6
|
+
errorMap: {
|
|
7
|
+
onServer: void 0
|
|
8
|
+
},
|
|
9
|
+
errors: []
|
|
10
|
+
};
|
|
11
|
+
const getFormData = start.createServerFn("GET", async (_, ctx) => {
|
|
12
|
+
const data = await utils._tanstackInternalsCookie.parse(
|
|
13
|
+
ctx.request.headers.get("Cookie")
|
|
14
|
+
);
|
|
15
|
+
ctx.request.headers.delete("Cookie");
|
|
16
|
+
if (!data)
|
|
17
|
+
return initialFormState;
|
|
18
|
+
return data;
|
|
19
|
+
});
|
|
20
|
+
exports.getFormData = getFormData;
|
|
21
|
+
exports.initialFormState = initialFormState;
|
|
22
|
+
//# sourceMappingURL=getFormData.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getFormData.cjs","sources":["../../../src/start/getFormData.tsx"],"sourcesContent":["import { createServerFn } from '@tanstack/start'\nimport { _tanstackInternalsCookie } from './utils'\nimport type { ServerFormState } from './types'\n\nexport const initialFormState = {\n errorMap: {\n onServer: undefined,\n },\n errors: [],\n}\n\nexport const getFormData = createServerFn('GET', async (_, ctx) => {\n const data = (await _tanstackInternalsCookie.parse(\n ctx.request.headers.get('Cookie'),\n )) as undefined | ServerFormState<any>\n // Delete the cookie before it hits the client again¸\n ctx.request.headers.delete('Cookie')\n if (!data) return initialFormState\n return data\n})\n"],"names":["createServerFn","_tanstackInternalsCookie"],"mappings":";;;;AAIO,MAAM,mBAAmB;AAAA,EAC9B,UAAU;AAAA,IACR,UAAU;AAAA,EACZ;AAAA,EACA,QAAQ,CAAC;AACX;AAEO,MAAM,cAAcA,MAAAA,eAAe,OAAO,OAAO,GAAG,QAAQ;AAC3D,QAAA,OAAQ,MAAMC,MAAAA,yBAAyB;AAAA,IAC3C,IAAI,QAAQ,QAAQ,IAAI,QAAQ;AAAA,EAAA;AAG9B,MAAA,QAAQ,QAAQ,OAAO,QAAQ;AACnC,MAAI,CAAC;AAAa,WAAA;AACX,SAAA;AACT,CAAC;;;"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ServerFormState } from './types.cjs';
|
|
2
|
+
|
|
3
|
+
export declare const initialFormState: {
|
|
4
|
+
errorMap: {
|
|
5
|
+
onServer: undefined;
|
|
6
|
+
};
|
|
7
|
+
errors: never[];
|
|
8
|
+
};
|
|
9
|
+
export declare const getFormData: import('@tanstack/start').Fetcher<undefined, {
|
|
10
|
+
errorMap: {
|
|
11
|
+
onServer: undefined;
|
|
12
|
+
};
|
|
13
|
+
errors: never[];
|
|
14
|
+
} | ServerFormState<any>>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const createServerValidate = require("./createServerValidate.cjs");
|
|
4
|
+
const getFormData = require("./getFormData.cjs");
|
|
5
|
+
const error = require("./error.cjs");
|
|
6
|
+
exports.createServerValidate = createServerValidate.createServerValidate;
|
|
7
|
+
exports.getFormData = getFormData.getFormData;
|
|
8
|
+
exports.initialFormState = getFormData.initialFormState;
|
|
9
|
+
exports.ServerValidateError = error.ServerValidateError;
|
|
10
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const node = require("@remix-run/node");
|
|
4
|
+
const _tanstackInternalsCookie = node.createCookie("_tanstack_form_internals");
|
|
5
|
+
exports._tanstackInternalsCookie = _tanstackInternalsCookie;
|
|
6
|
+
//# sourceMappingURL=utils.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.cjs","sources":["../../../src/start/utils.ts"],"sourcesContent":["import { createCookie } from '@remix-run/node'\n\nexport const _tanstackInternalsCookie = createCookie('_tanstack_form_internals')\n"],"names":["createCookie"],"mappings":";;;AAEa,MAAA,2BAA2BA,kBAAa,0BAA0B;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const _tanstackInternalsCookie: import('@remix-run/node').Cookie;
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -2,5 +2,4 @@ export * from '@tanstack/form-core';
|
|
|
2
2
|
export { useForm } from './useForm.js';
|
|
3
3
|
export type { UseField, FieldComponent } from './useField.js';
|
|
4
4
|
export { useField, Field } from './useField.js';
|
|
5
|
-
export { createServerValidate, initialFormState } from './createServerValidate.js';
|
|
6
5
|
export { useTransform } from './useTransform.js';
|
package/dist/esm/index.js
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
export * from "@tanstack/form-core";
|
|
2
2
|
import { useForm } from "./useForm.js";
|
|
3
3
|
import { Field, useField } from "./useField.js";
|
|
4
|
-
import { createServerValidate, initialFormState } from "./createServerValidate.js";
|
|
5
4
|
import { useTransform } from "./useTransform.js";
|
|
6
5
|
export {
|
|
7
6
|
Field,
|
|
8
|
-
createServerValidate,
|
|
9
|
-
initialFormState,
|
|
10
7
|
useField,
|
|
11
8
|
useForm,
|
|
12
9
|
useTransform
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { decode } from 'decode-formdata';
|
|
2
|
+
import { FormOptions, ValidationError, Validator } from '@tanstack/form-core';
|
|
3
|
+
import { ServerFormState } from './types.js';
|
|
4
|
+
|
|
5
|
+
type OnServerValidateFn<TFormData> = (props: {
|
|
6
|
+
value: TFormData;
|
|
7
|
+
}) => ValidationError;
|
|
8
|
+
interface CreateServerValidateOptions<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined> extends FormOptions<TFormData, TFormValidator> {
|
|
9
|
+
onServerValidate: OnServerValidateFn<TFormData>;
|
|
10
|
+
}
|
|
11
|
+
export declare const createServerValidate: <TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined>(defaultOpts: CreateServerValidateOptions<TFormData, TFormValidator>) => (formData: FormData, info?: Parameters<typeof decode>[1]) => Promise<void>;
|
|
12
|
+
export declare const initialFormState: ServerFormState<any>;
|
|
13
|
+
export {};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { decode } from "decode-formdata";
|
|
2
|
+
import { ServerValidateError } from "./error.js";
|
|
2
3
|
const createServerValidate = (defaultOpts) => async (formData, info) => {
|
|
3
4
|
const { validatorAdapter, onServerValidate } = defaultOpts;
|
|
4
5
|
const runValidator = (propsValue) => {
|
|
@@ -7,19 +8,26 @@ const createServerValidate = (defaultOpts) => async (formData, info) => {
|
|
|
7
8
|
}
|
|
8
9
|
return onServerValidate(propsValue);
|
|
9
10
|
};
|
|
10
|
-
const
|
|
11
|
-
const onServerError = runValidator({ value:
|
|
12
|
-
|
|
11
|
+
const values = decode(formData, info);
|
|
12
|
+
const onServerError = runValidator({ value: values });
|
|
13
|
+
if (!onServerError)
|
|
14
|
+
return;
|
|
15
|
+
const formState = {
|
|
13
16
|
errorMap: {
|
|
14
17
|
onServer: onServerError
|
|
15
18
|
},
|
|
19
|
+
values,
|
|
16
20
|
errors: onServerError ? [onServerError] : []
|
|
17
21
|
};
|
|
22
|
+
throw new ServerValidateError({
|
|
23
|
+
formState
|
|
24
|
+
});
|
|
18
25
|
};
|
|
19
26
|
const initialFormState = {
|
|
20
27
|
errorMap: {
|
|
21
28
|
onServer: void 0
|
|
22
29
|
},
|
|
30
|
+
values: void 0,
|
|
23
31
|
errors: []
|
|
24
32
|
};
|
|
25
33
|
export {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createServerValidate.js","sources":["../../../src/nextjs/createServerValidate.ts"],"sourcesContent":["import { decode } from 'decode-formdata'\nimport { ServerValidateError } from './error'\nimport type {\n FormOptions,\n ValidationError,\n Validator,\n} from '@tanstack/form-core'\nimport type { ServerFormState } from './types'\n\ntype OnServerValidateFn<TFormData> = (props: {\n value: TFormData\n}) => ValidationError\n\ninterface CreateServerValidateOptions<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> extends FormOptions<TFormData, TFormValidator> {\n onServerValidate: OnServerValidateFn<TFormData>\n}\n\nexport const createServerValidate =\n <\n TFormData,\n TFormValidator extends\n | Validator<TFormData, unknown>\n | undefined = undefined,\n >(\n defaultOpts: CreateServerValidateOptions<TFormData, TFormValidator>,\n ) =>\n async (formData: FormData, info?: Parameters<typeof decode>[1]) => {\n const { validatorAdapter, onServerValidate } = defaultOpts\n\n const runValidator = (propsValue: { value: TFormData }) => {\n if (validatorAdapter && typeof onServerValidate !== 'function') {\n return validatorAdapter().validate(propsValue, onServerValidate)\n }\n\n return (onServerValidate as OnServerValidateFn<TFormData>)(propsValue)\n }\n\n const values = decode(formData, info) as never as TFormData\n\n const onServerError = runValidator({ value: values })\n\n if (!onServerError) return\n\n const formState: ServerFormState<TFormData> = {\n errorMap: {\n onServer: onServerError,\n },\n values,\n errors: onServerError ? [onServerError] : [],\n }\n\n throw new ServerValidateError({\n formState,\n })\n }\n\nexport const initialFormState: ServerFormState<any> = {\n errorMap: {\n onServer: undefined,\n },\n values: undefined,\n errors: [],\n}\n"],"names":[],"mappings":";;AAoBO,MAAM,uBACX,CAME,gBAEF,OAAO,UAAoB,SAAwC;AAC3D,QAAA,EAAE,kBAAkB,iBAAqB,IAAA;AAEzC,QAAA,eAAe,CAAC,eAAqC;AACrD,QAAA,oBAAoB,OAAO,qBAAqB,YAAY;AAC9D,aAAO,iBAAiB,EAAE,SAAS,YAAY,gBAAgB;AAAA,IACjE;AAEA,WAAQ,iBAAmD,UAAU;AAAA,EAAA;AAGjE,QAAA,SAAS,OAAO,UAAU,IAAI;AAEpC,QAAM,gBAAgB,aAAa,EAAE,OAAO,OAAQ,CAAA;AAEpD,MAAI,CAAC;AAAe;AAEpB,QAAM,YAAwC;AAAA,IAC5C,UAAU;AAAA,MACR,UAAU;AAAA,IACZ;AAAA,IACA;AAAA,IACA,QAAQ,gBAAgB,CAAC,aAAa,IAAI,CAAC;AAAA,EAAA;AAG7C,QAAM,IAAI,oBAAoB;AAAA,IAC5B;AAAA,EAAA,CACD;AACH;AAEK,MAAM,mBAAyC;AAAA,EACpD,UAAU;AAAA,IACR,UAAU;AAAA,EACZ;AAAA,EACA,QAAQ;AAAA,EACR,QAAQ,CAAC;AACX;"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ServerFormState } from './types.js';
|
|
2
|
+
|
|
3
|
+
interface ServerValidateErrorState<TFormData> {
|
|
4
|
+
formState: ServerFormState<TFormData>;
|
|
5
|
+
}
|
|
6
|
+
export declare class ServerValidateError<TFormData> extends Error implements ServerValidateErrorState<TFormData> {
|
|
7
|
+
formState: ServerFormState<TFormData>;
|
|
8
|
+
constructor(options: ServerValidateErrorState<TFormData>);
|
|
9
|
+
}
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
class ServerValidateError extends Error {
|
|
2
|
+
constructor(options) {
|
|
3
|
+
super("Your form has errors. Please check the fields and try again.");
|
|
4
|
+
this.name = "ServerValidateError";
|
|
5
|
+
this.formState = options.formState;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export {
|
|
9
|
+
ServerValidateError
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.js","sources":["../../../src/nextjs/error.ts"],"sourcesContent":["import type { ServerFormState } from './types'\n\ninterface ServerValidateErrorState<TFormData> {\n formState: ServerFormState<TFormData>\n}\n\nexport class ServerValidateError<TFormData>\n extends Error\n implements ServerValidateErrorState<TFormData>\n{\n formState: ServerFormState<TFormData>\n\n constructor(options: ServerValidateErrorState<TFormData>) {\n super('Your form has errors. Please check the fields and try again.')\n this.name = 'ServerValidateError'\n this.formState = options.formState\n }\n}\n"],"names":[],"mappings":"AAMO,MAAM,4BACH,MAEV;AAAA,EAGE,YAAY,SAA8C;AACxD,UAAM,8DAA8D;AACpE,SAAK,OAAO;AACZ,SAAK,YAAY,QAAQ;AAAA,EAC3B;AACF;"}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
export * from "@tanstack/form-core";
|
|
2
2
|
import { createServerValidate, initialFormState } from "./createServerValidate.js";
|
|
3
|
+
import { ServerValidateError } from "./error.js";
|
|
3
4
|
export {
|
|
5
|
+
ServerValidateError,
|
|
4
6
|
createServerValidate,
|
|
5
7
|
initialFormState
|
|
6
8
|
};
|
|
7
|
-
//# sourceMappingURL=
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { decode } from 'decode-formdata';
|
|
2
|
+
import { FormOptions, ValidationError, Validator } from '@tanstack/form-core';
|
|
3
|
+
import { FetchFn } from '@tanstack/start';
|
|
4
|
+
|
|
5
|
+
type Ctx = Parameters<FetchFn<FormData, unknown>>[1];
|
|
6
|
+
type OnServerValidateFn<TFormData> = (props: {
|
|
7
|
+
value: TFormData;
|
|
8
|
+
}) => ValidationError;
|
|
9
|
+
interface CreateServerValidateOptions<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined> extends FormOptions<TFormData, TFormValidator> {
|
|
10
|
+
onServerValidate: OnServerValidateFn<TFormData>;
|
|
11
|
+
}
|
|
12
|
+
export declare const createServerValidate: <TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined>(defaultOpts: CreateServerValidateOptions<TFormData, TFormValidator>) => (ctx: Ctx, formData: FormData, info?: Parameters<typeof decode>[1]) => Promise<void>;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { decode } from "decode-formdata";
|
|
2
|
+
import { _tanstackInternalsCookie } from "./utils.js";
|
|
3
|
+
import { ServerValidateError } from "./error.js";
|
|
4
|
+
const createServerValidate = (defaultOpts) => async (ctx, formData, info) => {
|
|
5
|
+
const { validatorAdapter, onServerValidate } = defaultOpts;
|
|
6
|
+
const runValidator = (propsValue) => {
|
|
7
|
+
if (validatorAdapter && typeof onServerValidate !== "function") {
|
|
8
|
+
return validatorAdapter().validate(propsValue, onServerValidate);
|
|
9
|
+
}
|
|
10
|
+
return onServerValidate(propsValue);
|
|
11
|
+
};
|
|
12
|
+
const referer = ctx.request.headers.get("referer");
|
|
13
|
+
const data = decode(formData, info);
|
|
14
|
+
const onServerError = runValidator({ value: data });
|
|
15
|
+
if (!onServerError)
|
|
16
|
+
return;
|
|
17
|
+
const formState = {
|
|
18
|
+
errorMap: {
|
|
19
|
+
onServer: onServerError
|
|
20
|
+
},
|
|
21
|
+
values: data,
|
|
22
|
+
errors: onServerError ? [onServerError] : []
|
|
23
|
+
};
|
|
24
|
+
const cookie = await _tanstackInternalsCookie.serialize(formState);
|
|
25
|
+
throw new ServerValidateError({
|
|
26
|
+
response: new Response("ok", {
|
|
27
|
+
headers: {
|
|
28
|
+
Location: referer,
|
|
29
|
+
"Set-Cookie": cookie
|
|
30
|
+
},
|
|
31
|
+
status: 302
|
|
32
|
+
}),
|
|
33
|
+
formState
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
export {
|
|
37
|
+
createServerValidate
|
|
38
|
+
};
|
|
39
|
+
//# sourceMappingURL=createServerValidate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createServerValidate.js","sources":["../../../src/start/createServerValidate.tsx"],"sourcesContent":["import { decode } from 'decode-formdata'\nimport { _tanstackInternalsCookie } from './utils'\nimport { ServerValidateError } from './error'\nimport type {\n FormOptions,\n ValidationError,\n Validator,\n} from '@tanstack/form-core'\nimport type { FetchFn } from '@tanstack/start'\nimport type { ServerFormState } from './types'\n\ntype Ctx = Parameters<FetchFn<FormData, unknown>>[1]\n\ntype OnServerValidateFn<TFormData> = (props: {\n value: TFormData\n}) => ValidationError\n\ninterface CreateServerValidateOptions<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> extends FormOptions<TFormData, TFormValidator> {\n onServerValidate: OnServerValidateFn<TFormData>\n}\n\nexport const createServerValidate =\n <\n TFormData,\n TFormValidator extends\n | Validator<TFormData, unknown>\n | undefined = undefined,\n >(\n defaultOpts: CreateServerValidateOptions<TFormData, TFormValidator>,\n ) =>\n async (ctx: Ctx, formData: FormData, info?: Parameters<typeof decode>[1]) => {\n const { validatorAdapter, onServerValidate } = defaultOpts\n\n const runValidator = (propsValue: { value: TFormData }) => {\n if (validatorAdapter && typeof onServerValidate !== 'function') {\n return validatorAdapter().validate(propsValue, onServerValidate)\n }\n\n return (onServerValidate as OnServerValidateFn<TFormData>)(propsValue)\n }\n\n const referer = ctx.request.headers.get('referer')!\n\n const data = decode(formData, info) as never as TFormData\n\n const onServerError = runValidator({ value: data })\n\n if (!onServerError) return\n\n const formState: ServerFormState<TFormData> = {\n errorMap: {\n onServer: onServerError,\n },\n values: data,\n errors: onServerError ? [onServerError] : [],\n }\n\n const cookie = await _tanstackInternalsCookie.serialize(formState)\n\n throw new ServerValidateError({\n response: new Response('ok', {\n headers: {\n Location: referer,\n 'Set-Cookie': cookie,\n },\n status: 302,\n }),\n formState: formState,\n })\n }\n"],"names":[],"mappings":";;;AAwBO,MAAM,uBACX,CAME,gBAEF,OAAO,KAAU,UAAoB,SAAwC;AACrE,QAAA,EAAE,kBAAkB,iBAAqB,IAAA;AAEzC,QAAA,eAAe,CAAC,eAAqC;AACrD,QAAA,oBAAoB,OAAO,qBAAqB,YAAY;AAC9D,aAAO,iBAAiB,EAAE,SAAS,YAAY,gBAAgB;AAAA,IACjE;AAEA,WAAQ,iBAAmD,UAAU;AAAA,EAAA;AAGvE,QAAM,UAAU,IAAI,QAAQ,QAAQ,IAAI,SAAS;AAE3C,QAAA,OAAO,OAAO,UAAU,IAAI;AAElC,QAAM,gBAAgB,aAAa,EAAE,OAAO,KAAM,CAAA;AAElD,MAAI,CAAC;AAAe;AAEpB,QAAM,YAAwC;AAAA,IAC5C,UAAU;AAAA,MACR,UAAU;AAAA,IACZ;AAAA,IACA,QAAQ;AAAA,IACR,QAAQ,gBAAgB,CAAC,aAAa,IAAI,CAAC;AAAA,EAAA;AAG7C,QAAM,SAAS,MAAM,yBAAyB,UAAU,SAAS;AAEjE,QAAM,IAAI,oBAAoB;AAAA,IAC5B,UAAU,IAAI,SAAS,MAAM;AAAA,MAC3B,SAAS;AAAA,QACP,UAAU;AAAA,QACV,cAAc;AAAA,MAChB;AAAA,MACA,QAAQ;AAAA,IAAA,CACT;AAAA,IACD;AAAA,EAAA,CACD;AACH;"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ServerFormState } from './types.js';
|
|
2
|
+
|
|
3
|
+
interface ServerValidateErrorState<TFormData> {
|
|
4
|
+
formState: ServerFormState<TFormData>;
|
|
5
|
+
response: Response;
|
|
6
|
+
}
|
|
7
|
+
export declare class ServerValidateError<TFormData> extends Error implements ServerValidateErrorState<TFormData> {
|
|
8
|
+
response: Response;
|
|
9
|
+
formState: ServerFormState<TFormData>;
|
|
10
|
+
constructor(options: ServerValidateErrorState<TFormData>);
|
|
11
|
+
}
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
class ServerValidateError extends Error {
|
|
2
|
+
constructor(options) {
|
|
3
|
+
super("Your form has errors. Please check the fields and try again.");
|
|
4
|
+
this.name = "ServerValidateError";
|
|
5
|
+
this.response = options.response;
|
|
6
|
+
this.formState = options.formState;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export {
|
|
10
|
+
ServerValidateError
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.js","sources":["../../../src/start/error.ts"],"sourcesContent":["import type { ServerFormState } from './types'\n\ninterface ServerValidateErrorState<TFormData> {\n formState: ServerFormState<TFormData>\n response: Response\n}\n\nexport class ServerValidateError<TFormData>\n extends Error\n implements ServerValidateErrorState<TFormData>\n{\n response: Response\n formState: ServerFormState<TFormData>\n\n constructor(options: ServerValidateErrorState<TFormData>) {\n super('Your form has errors. Please check the fields and try again.')\n this.name = 'ServerValidateError'\n this.response = options.response\n this.formState = options.formState\n }\n}\n"],"names":[],"mappings":"AAOO,MAAM,4BACH,MAEV;AAAA,EAIE,YAAY,SAA8C;AACxD,UAAM,8DAA8D;AACpE,SAAK,OAAO;AACZ,SAAK,WAAW,QAAQ;AACxB,SAAK,YAAY,QAAQ;AAAA,EAC3B;AACF;"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ServerFormState } from './types.js';
|
|
2
|
+
|
|
3
|
+
export declare const initialFormState: {
|
|
4
|
+
errorMap: {
|
|
5
|
+
onServer: undefined;
|
|
6
|
+
};
|
|
7
|
+
errors: never[];
|
|
8
|
+
};
|
|
9
|
+
export declare const getFormData: import('@tanstack/start').Fetcher<undefined, {
|
|
10
|
+
errorMap: {
|
|
11
|
+
onServer: undefined;
|
|
12
|
+
};
|
|
13
|
+
errors: never[];
|
|
14
|
+
} | ServerFormState<any>>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { createServerFn } from "@tanstack/start";
|
|
2
|
+
import { _tanstackInternalsCookie } from "./utils.js";
|
|
3
|
+
const initialFormState = {
|
|
4
|
+
errorMap: {
|
|
5
|
+
onServer: void 0
|
|
6
|
+
},
|
|
7
|
+
errors: []
|
|
8
|
+
};
|
|
9
|
+
const getFormData = createServerFn("GET", async (_, ctx) => {
|
|
10
|
+
const data = await _tanstackInternalsCookie.parse(
|
|
11
|
+
ctx.request.headers.get("Cookie")
|
|
12
|
+
);
|
|
13
|
+
ctx.request.headers.delete("Cookie");
|
|
14
|
+
if (!data)
|
|
15
|
+
return initialFormState;
|
|
16
|
+
return data;
|
|
17
|
+
});
|
|
18
|
+
export {
|
|
19
|
+
getFormData,
|
|
20
|
+
initialFormState
|
|
21
|
+
};
|
|
22
|
+
//# sourceMappingURL=getFormData.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getFormData.js","sources":["../../../src/start/getFormData.tsx"],"sourcesContent":["import { createServerFn } from '@tanstack/start'\nimport { _tanstackInternalsCookie } from './utils'\nimport type { ServerFormState } from './types'\n\nexport const initialFormState = {\n errorMap: {\n onServer: undefined,\n },\n errors: [],\n}\n\nexport const getFormData = createServerFn('GET', async (_, ctx) => {\n const data = (await _tanstackInternalsCookie.parse(\n ctx.request.headers.get('Cookie'),\n )) as undefined | ServerFormState<any>\n // Delete the cookie before it hits the client again¸\n ctx.request.headers.delete('Cookie')\n if (!data) return initialFormState\n return data\n})\n"],"names":[],"mappings":";;AAIO,MAAM,mBAAmB;AAAA,EAC9B,UAAU;AAAA,IACR,UAAU;AAAA,EACZ;AAAA,EACA,QAAQ,CAAC;AACX;AAEO,MAAM,cAAc,eAAe,OAAO,OAAO,GAAG,QAAQ;AAC3D,QAAA,OAAQ,MAAM,yBAAyB;AAAA,IAC3C,IAAI,QAAQ,QAAQ,IAAI,QAAQ;AAAA,EAAA;AAG9B,MAAA,QAAQ,QAAQ,OAAO,QAAQ;AACnC,MAAI,CAAC;AAAa,WAAA;AACX,SAAA;AACT,CAAC;"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { createServerValidate } from "./createServerValidate.js";
|
|
2
|
+
import { getFormData, initialFormState } from "./getFormData.js";
|
|
3
|
+
import { ServerValidateError } from "./error.js";
|
|
4
|
+
export {
|
|
5
|
+
ServerValidateError,
|
|
6
|
+
createServerValidate,
|
|
7
|
+
getFormData,
|
|
8
|
+
initialFormState
|
|
9
|
+
};
|
|
10
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const _tanstackInternalsCookie: import('@remix-run/node').Cookie;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sources":["../../../src/start/utils.ts"],"sourcesContent":["import { createCookie } from '@remix-run/node'\n\nexport const _tanstackInternalsCookie = createCookie('_tanstack_form_internals')\n"],"names":[],"mappings":";AAEa,MAAA,2BAA2B,aAAa,0BAA0B;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/react-form",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.25.1",
|
|
4
4
|
"description": "Powerful, type-safe forms for React.",
|
|
5
5
|
"author": "tannerlinsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -36,31 +36,49 @@
|
|
|
36
36
|
},
|
|
37
37
|
"./nextjs": {
|
|
38
38
|
"import": {
|
|
39
|
-
"types": "./dist/esm/nextjs
|
|
40
|
-
"default": "./dist/esm/nextjs
|
|
39
|
+
"types": "./dist/esm/nextjs/index.d.ts",
|
|
40
|
+
"default": "./dist/esm/nextjs/index.js"
|
|
41
41
|
},
|
|
42
42
|
"require": {
|
|
43
|
-
"types": "./dist/cjs/nextjs
|
|
44
|
-
"default": "./dist/cjs/nextjs
|
|
43
|
+
"types": "./dist/cjs/nextjs/index.d.cts",
|
|
44
|
+
"default": "./dist/cjs/nextjs/index.cjs"
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
"./start": {
|
|
48
|
+
"import": {
|
|
49
|
+
"types": "./dist/esm/start/index.d.ts",
|
|
50
|
+
"default": "./dist/esm/start/index.js"
|
|
51
|
+
},
|
|
52
|
+
"require": {
|
|
53
|
+
"types": "./dist/cjs/start/index.d.cts",
|
|
54
|
+
"default": "./dist/cjs/start/index.cjs"
|
|
45
55
|
}
|
|
46
56
|
},
|
|
47
57
|
"./package.json": "./package.json"
|
|
48
58
|
},
|
|
49
59
|
"devDependencies": {
|
|
60
|
+
"@tanstack/start": "^1.42.1",
|
|
50
61
|
"@types/react": "^18.3.3",
|
|
51
62
|
"@types/react-dom": "^18.3.0",
|
|
52
63
|
"@vitejs/plugin-react": "^4.3.0",
|
|
53
64
|
"react": "^18.3.1",
|
|
54
65
|
"react-dom": "^18.3.1",
|
|
55
|
-
"vite": "^5.
|
|
66
|
+
"vite": "^5.1.4"
|
|
56
67
|
},
|
|
57
68
|
"dependencies": {
|
|
58
69
|
"@tanstack/react-store": "^0.5.0",
|
|
59
70
|
"decode-formdata": "^0.7.5",
|
|
60
|
-
"@
|
|
71
|
+
"@remix-run/node": "^2.9.2",
|
|
72
|
+
"@tanstack/form-core": "0.25.1"
|
|
61
73
|
},
|
|
62
74
|
"peerDependencies": {
|
|
63
|
-
"
|
|
75
|
+
"@tanstack/start": "^1.40.1",
|
|
76
|
+
"react": "^17.0.0 || ^18.0.0 || ^19.0.0"
|
|
77
|
+
},
|
|
78
|
+
"peerDependenciesMeta": {
|
|
79
|
+
"@tanstack/start": {
|
|
80
|
+
"optional": true
|
|
81
|
+
}
|
|
64
82
|
},
|
|
65
83
|
"scripts": {}
|
|
66
84
|
}
|
package/src/index.ts
CHANGED
|
@@ -5,5 +5,4 @@ export { useForm } from './useForm'
|
|
|
5
5
|
export type { UseField, FieldComponent } from './useField'
|
|
6
6
|
export { useField, Field } from './useField'
|
|
7
7
|
|
|
8
|
-
export { createServerValidate, initialFormState } from './createServerValidate'
|
|
9
8
|
export { useTransform } from './useTransform'
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { decode } from 'decode-formdata'
|
|
2
|
+
import { ServerValidateError } from './error'
|
|
3
|
+
import type {
|
|
4
|
+
FormOptions,
|
|
5
|
+
ValidationError,
|
|
6
|
+
Validator,
|
|
7
|
+
} from '@tanstack/form-core'
|
|
8
|
+
import type { ServerFormState } from './types'
|
|
9
|
+
|
|
10
|
+
type OnServerValidateFn<TFormData> = (props: {
|
|
11
|
+
value: TFormData
|
|
12
|
+
}) => ValidationError
|
|
13
|
+
|
|
14
|
+
interface CreateServerValidateOptions<
|
|
15
|
+
TFormData,
|
|
16
|
+
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
|
|
17
|
+
> extends FormOptions<TFormData, TFormValidator> {
|
|
18
|
+
onServerValidate: OnServerValidateFn<TFormData>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const createServerValidate =
|
|
22
|
+
<
|
|
23
|
+
TFormData,
|
|
24
|
+
TFormValidator extends
|
|
25
|
+
| Validator<TFormData, unknown>
|
|
26
|
+
| undefined = undefined,
|
|
27
|
+
>(
|
|
28
|
+
defaultOpts: CreateServerValidateOptions<TFormData, TFormValidator>,
|
|
29
|
+
) =>
|
|
30
|
+
async (formData: FormData, info?: Parameters<typeof decode>[1]) => {
|
|
31
|
+
const { validatorAdapter, onServerValidate } = defaultOpts
|
|
32
|
+
|
|
33
|
+
const runValidator = (propsValue: { value: TFormData }) => {
|
|
34
|
+
if (validatorAdapter && typeof onServerValidate !== 'function') {
|
|
35
|
+
return validatorAdapter().validate(propsValue, onServerValidate)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return (onServerValidate as OnServerValidateFn<TFormData>)(propsValue)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const values = decode(formData, info) as never as TFormData
|
|
42
|
+
|
|
43
|
+
const onServerError = runValidator({ value: values })
|
|
44
|
+
|
|
45
|
+
if (!onServerError) return
|
|
46
|
+
|
|
47
|
+
const formState: ServerFormState<TFormData> = {
|
|
48
|
+
errorMap: {
|
|
49
|
+
onServer: onServerError,
|
|
50
|
+
},
|
|
51
|
+
values,
|
|
52
|
+
errors: onServerError ? [onServerError] : [],
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
throw new ServerValidateError({
|
|
56
|
+
formState,
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export const initialFormState: ServerFormState<any> = {
|
|
61
|
+
errorMap: {
|
|
62
|
+
onServer: undefined,
|
|
63
|
+
},
|
|
64
|
+
values: undefined,
|
|
65
|
+
errors: [],
|
|
66
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ServerFormState } from './types'
|
|
2
|
+
|
|
3
|
+
interface ServerValidateErrorState<TFormData> {
|
|
4
|
+
formState: ServerFormState<TFormData>
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export class ServerValidateError<TFormData>
|
|
8
|
+
extends Error
|
|
9
|
+
implements ServerValidateErrorState<TFormData>
|
|
10
|
+
{
|
|
11
|
+
formState: ServerFormState<TFormData>
|
|
12
|
+
|
|
13
|
+
constructor(options: ServerValidateErrorState<TFormData>) {
|
|
14
|
+
super('Your form has errors. Please check the fields and try again.')
|
|
15
|
+
this.name = 'ServerValidateError'
|
|
16
|
+
this.formState = options.formState
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { decode } from 'decode-formdata'
|
|
2
|
+
import { _tanstackInternalsCookie } from './utils'
|
|
3
|
+
import { ServerValidateError } from './error'
|
|
4
|
+
import type {
|
|
5
|
+
FormOptions,
|
|
6
|
+
ValidationError,
|
|
7
|
+
Validator,
|
|
8
|
+
} from '@tanstack/form-core'
|
|
9
|
+
import type { FetchFn } from '@tanstack/start'
|
|
10
|
+
import type { ServerFormState } from './types'
|
|
11
|
+
|
|
12
|
+
type Ctx = Parameters<FetchFn<FormData, unknown>>[1]
|
|
13
|
+
|
|
14
|
+
type OnServerValidateFn<TFormData> = (props: {
|
|
15
|
+
value: TFormData
|
|
16
|
+
}) => ValidationError
|
|
17
|
+
|
|
18
|
+
interface CreateServerValidateOptions<
|
|
19
|
+
TFormData,
|
|
20
|
+
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
|
|
21
|
+
> extends FormOptions<TFormData, TFormValidator> {
|
|
22
|
+
onServerValidate: OnServerValidateFn<TFormData>
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const createServerValidate =
|
|
26
|
+
<
|
|
27
|
+
TFormData,
|
|
28
|
+
TFormValidator extends
|
|
29
|
+
| Validator<TFormData, unknown>
|
|
30
|
+
| undefined = undefined,
|
|
31
|
+
>(
|
|
32
|
+
defaultOpts: CreateServerValidateOptions<TFormData, TFormValidator>,
|
|
33
|
+
) =>
|
|
34
|
+
async (ctx: Ctx, formData: FormData, info?: Parameters<typeof decode>[1]) => {
|
|
35
|
+
const { validatorAdapter, onServerValidate } = defaultOpts
|
|
36
|
+
|
|
37
|
+
const runValidator = (propsValue: { value: TFormData }) => {
|
|
38
|
+
if (validatorAdapter && typeof onServerValidate !== 'function') {
|
|
39
|
+
return validatorAdapter().validate(propsValue, onServerValidate)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return (onServerValidate as OnServerValidateFn<TFormData>)(propsValue)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const referer = ctx.request.headers.get('referer')!
|
|
46
|
+
|
|
47
|
+
const data = decode(formData, info) as never as TFormData
|
|
48
|
+
|
|
49
|
+
const onServerError = runValidator({ value: data })
|
|
50
|
+
|
|
51
|
+
if (!onServerError) return
|
|
52
|
+
|
|
53
|
+
const formState: ServerFormState<TFormData> = {
|
|
54
|
+
errorMap: {
|
|
55
|
+
onServer: onServerError,
|
|
56
|
+
},
|
|
57
|
+
values: data,
|
|
58
|
+
errors: onServerError ? [onServerError] : [],
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const cookie = await _tanstackInternalsCookie.serialize(formState)
|
|
62
|
+
|
|
63
|
+
throw new ServerValidateError({
|
|
64
|
+
response: new Response('ok', {
|
|
65
|
+
headers: {
|
|
66
|
+
Location: referer,
|
|
67
|
+
'Set-Cookie': cookie,
|
|
68
|
+
},
|
|
69
|
+
status: 302,
|
|
70
|
+
}),
|
|
71
|
+
formState: formState,
|
|
72
|
+
})
|
|
73
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ServerFormState } from './types'
|
|
2
|
+
|
|
3
|
+
interface ServerValidateErrorState<TFormData> {
|
|
4
|
+
formState: ServerFormState<TFormData>
|
|
5
|
+
response: Response
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export class ServerValidateError<TFormData>
|
|
9
|
+
extends Error
|
|
10
|
+
implements ServerValidateErrorState<TFormData>
|
|
11
|
+
{
|
|
12
|
+
response: Response
|
|
13
|
+
formState: ServerFormState<TFormData>
|
|
14
|
+
|
|
15
|
+
constructor(options: ServerValidateErrorState<TFormData>) {
|
|
16
|
+
super('Your form has errors. Please check the fields and try again.')
|
|
17
|
+
this.name = 'ServerValidateError'
|
|
18
|
+
this.response = options.response
|
|
19
|
+
this.formState = options.formState
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { createServerFn } from '@tanstack/start'
|
|
2
|
+
import { _tanstackInternalsCookie } from './utils'
|
|
3
|
+
import type { ServerFormState } from './types'
|
|
4
|
+
|
|
5
|
+
export const initialFormState = {
|
|
6
|
+
errorMap: {
|
|
7
|
+
onServer: undefined,
|
|
8
|
+
},
|
|
9
|
+
errors: [],
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const getFormData = createServerFn('GET', async (_, ctx) => {
|
|
13
|
+
const data = (await _tanstackInternalsCookie.parse(
|
|
14
|
+
ctx.request.headers.get('Cookie'),
|
|
15
|
+
)) as undefined | ServerFormState<any>
|
|
16
|
+
// Delete the cookie before it hits the client again¸
|
|
17
|
+
ctx.request.headers.delete('Cookie')
|
|
18
|
+
if (!data) return initialFormState
|
|
19
|
+
return data
|
|
20
|
+
})
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createServerValidate.cjs","sources":["../../src/createServerValidate.ts"],"sourcesContent":["import { decode } from 'decode-formdata'\nimport type {\n FormApi,\n FormOptions,\n ValidationError,\n Validator,\n} from '@tanstack/form-core'\n\ntype OnServerValidateFn<TFormData> = (props: {\n value: TFormData\n}) => ValidationError\n\ntype OnServerValidateOrFn<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> = TFormValidator extends Validator<TFormData, infer FFN>\n ? FFN | OnServerValidateFn<TFormData>\n : OnServerValidateFn<TFormData>\n\ninterface ServerFormOptions<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> extends FormOptions<TFormData, TFormValidator> {\n onServerValidate?: OnServerValidateOrFn<TFormData, TFormValidator>\n}\n\ntype ValidateFormData<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> = (\n formData: FormData,\n info?: Parameters<typeof decode>[1],\n) => Promise<Partial<FormApi<TFormData, TFormValidator>['state']>>\n\nexport const createServerValidate = <\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n>(\n defaultOpts: ServerFormOptions<TFormData, TFormValidator>,\n) =>\n (async (\n formData: FormData,\n info?: Parameters<typeof decode>[1],\n ): Promise<Partial<FormApi<TFormData, TFormValidator>['state']>> => {\n const { validatorAdapter, onServerValidate } = defaultOpts\n\n const runValidator = (propsValue: { value: TFormData }) => {\n if (validatorAdapter && typeof onServerValidate !== 'function') {\n return validatorAdapter().validate(propsValue, onServerValidate)\n }\n\n return (onServerValidate as OnServerValidateFn<TFormData>)(propsValue)\n }\n\n const data = decode(formData, info) as never as TFormData\n\n const onServerError = runValidator({ value: data })\n\n return {\n errorMap: {\n onServer: onServerError,\n },\n errors: onServerError ? [onServerError] : [],\n }\n }) as ValidateFormData<TFormData, TFormValidator>\n\nexport const initialFormState = {\n errorMap: {\n onServer: undefined,\n },\n errors: [],\n}\n"],"names":["decode"],"mappings":";;;AAkCO,MAAM,uBAAuB,CAIlC,gBAEC,OACC,UACA,SACkE;AAC5D,QAAA,EAAE,kBAAkB,iBAAqB,IAAA;AAEzC,QAAA,eAAe,CAAC,eAAqC;AACrD,QAAA,oBAAoB,OAAO,qBAAqB,YAAY;AAC9D,aAAO,iBAAiB,EAAE,SAAS,YAAY,gBAAgB;AAAA,IACjE;AAEA,WAAQ,iBAAmD,UAAU;AAAA,EAAA;AAGjE,QAAA,OAAOA,eAAAA,OAAO,UAAU,IAAI;AAElC,QAAM,gBAAgB,aAAa,EAAE,OAAO,KAAM,CAAA;AAE3C,SAAA;AAAA,IACL,UAAU;AAAA,MACR,UAAU;AAAA,IACZ;AAAA,IACA,QAAQ,gBAAgB,CAAC,aAAa,IAAI,CAAC;AAAA,EAAA;AAE/C;AAEK,MAAM,mBAAmB;AAAA,EAC9B,UAAU;AAAA,IACR,UAAU;AAAA,EACZ;AAAA,EACA,QAAQ,CAAC;AACX;;;"}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { decode } from 'decode-formdata';
|
|
2
|
-
import { FormApi, FormOptions, ValidationError, Validator } from '@tanstack/form-core';
|
|
3
|
-
|
|
4
|
-
type OnServerValidateFn<TFormData> = (props: {
|
|
5
|
-
value: TFormData;
|
|
6
|
-
}) => ValidationError;
|
|
7
|
-
type OnServerValidateOrFn<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined> = TFormValidator extends Validator<TFormData, infer FFN> ? FFN | OnServerValidateFn<TFormData> : OnServerValidateFn<TFormData>;
|
|
8
|
-
interface ServerFormOptions<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined> extends FormOptions<TFormData, TFormValidator> {
|
|
9
|
-
onServerValidate?: OnServerValidateOrFn<TFormData, TFormValidator>;
|
|
10
|
-
}
|
|
11
|
-
type ValidateFormData<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined> = (formData: FormData, info?: Parameters<typeof decode>[1]) => Promise<Partial<FormApi<TFormData, TFormValidator>['state']>>;
|
|
12
|
-
export declare const createServerValidate: <TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined>(defaultOpts: ServerFormOptions<TFormData, TFormValidator>) => ValidateFormData<TFormData, TFormValidator>;
|
|
13
|
-
export declare const initialFormState: {
|
|
14
|
-
errorMap: {
|
|
15
|
-
onServer: undefined;
|
|
16
|
-
};
|
|
17
|
-
errors: never[];
|
|
18
|
-
};
|
|
19
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"nextjs-index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;"}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { decode } from 'decode-formdata';
|
|
2
|
-
import { FormApi, FormOptions, ValidationError, Validator } from '@tanstack/form-core';
|
|
3
|
-
|
|
4
|
-
type OnServerValidateFn<TFormData> = (props: {
|
|
5
|
-
value: TFormData;
|
|
6
|
-
}) => ValidationError;
|
|
7
|
-
type OnServerValidateOrFn<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined> = TFormValidator extends Validator<TFormData, infer FFN> ? FFN | OnServerValidateFn<TFormData> : OnServerValidateFn<TFormData>;
|
|
8
|
-
interface ServerFormOptions<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined> extends FormOptions<TFormData, TFormValidator> {
|
|
9
|
-
onServerValidate?: OnServerValidateOrFn<TFormData, TFormValidator>;
|
|
10
|
-
}
|
|
11
|
-
type ValidateFormData<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined> = (formData: FormData, info?: Parameters<typeof decode>[1]) => Promise<Partial<FormApi<TFormData, TFormValidator>['state']>>;
|
|
12
|
-
export declare const createServerValidate: <TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined>(defaultOpts: ServerFormOptions<TFormData, TFormValidator>) => ValidateFormData<TFormData, TFormValidator>;
|
|
13
|
-
export declare const initialFormState: {
|
|
14
|
-
errorMap: {
|
|
15
|
-
onServer: undefined;
|
|
16
|
-
};
|
|
17
|
-
errors: never[];
|
|
18
|
-
};
|
|
19
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createServerValidate.js","sources":["../../src/createServerValidate.ts"],"sourcesContent":["import { decode } from 'decode-formdata'\nimport type {\n FormApi,\n FormOptions,\n ValidationError,\n Validator,\n} from '@tanstack/form-core'\n\ntype OnServerValidateFn<TFormData> = (props: {\n value: TFormData\n}) => ValidationError\n\ntype OnServerValidateOrFn<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> = TFormValidator extends Validator<TFormData, infer FFN>\n ? FFN | OnServerValidateFn<TFormData>\n : OnServerValidateFn<TFormData>\n\ninterface ServerFormOptions<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> extends FormOptions<TFormData, TFormValidator> {\n onServerValidate?: OnServerValidateOrFn<TFormData, TFormValidator>\n}\n\ntype ValidateFormData<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> = (\n formData: FormData,\n info?: Parameters<typeof decode>[1],\n) => Promise<Partial<FormApi<TFormData, TFormValidator>['state']>>\n\nexport const createServerValidate = <\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n>(\n defaultOpts: ServerFormOptions<TFormData, TFormValidator>,\n) =>\n (async (\n formData: FormData,\n info?: Parameters<typeof decode>[1],\n ): Promise<Partial<FormApi<TFormData, TFormValidator>['state']>> => {\n const { validatorAdapter, onServerValidate } = defaultOpts\n\n const runValidator = (propsValue: { value: TFormData }) => {\n if (validatorAdapter && typeof onServerValidate !== 'function') {\n return validatorAdapter().validate(propsValue, onServerValidate)\n }\n\n return (onServerValidate as OnServerValidateFn<TFormData>)(propsValue)\n }\n\n const data = decode(formData, info) as never as TFormData\n\n const onServerError = runValidator({ value: data })\n\n return {\n errorMap: {\n onServer: onServerError,\n },\n errors: onServerError ? [onServerError] : [],\n }\n }) as ValidateFormData<TFormData, TFormValidator>\n\nexport const initialFormState = {\n errorMap: {\n onServer: undefined,\n },\n errors: [],\n}\n"],"names":[],"mappings":";AAkCO,MAAM,uBAAuB,CAIlC,gBAEC,OACC,UACA,SACkE;AAC5D,QAAA,EAAE,kBAAkB,iBAAqB,IAAA;AAEzC,QAAA,eAAe,CAAC,eAAqC;AACrD,QAAA,oBAAoB,OAAO,qBAAqB,YAAY;AAC9D,aAAO,iBAAiB,EAAE,SAAS,YAAY,gBAAgB;AAAA,IACjE;AAEA,WAAQ,iBAAmD,UAAU;AAAA,EAAA;AAGjE,QAAA,OAAO,OAAO,UAAU,IAAI;AAElC,QAAM,gBAAgB,aAAa,EAAE,OAAO,KAAM,CAAA;AAE3C,SAAA;AAAA,IACL,UAAU;AAAA,MACR,UAAU;AAAA,IACZ;AAAA,IACA,QAAQ,gBAAgB,CAAC,aAAa,IAAI,CAAC;AAAA,EAAA;AAE/C;AAEK,MAAM,mBAAmB;AAAA,EAC9B,UAAU;AAAA,IACR,UAAU;AAAA,EACZ;AAAA,EACA,QAAQ,CAAC;AACX;"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"nextjs-index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;"}
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { decode } from 'decode-formdata'
|
|
2
|
-
import type {
|
|
3
|
-
FormApi,
|
|
4
|
-
FormOptions,
|
|
5
|
-
ValidationError,
|
|
6
|
-
Validator,
|
|
7
|
-
} from '@tanstack/form-core'
|
|
8
|
-
|
|
9
|
-
type OnServerValidateFn<TFormData> = (props: {
|
|
10
|
-
value: TFormData
|
|
11
|
-
}) => ValidationError
|
|
12
|
-
|
|
13
|
-
type OnServerValidateOrFn<
|
|
14
|
-
TFormData,
|
|
15
|
-
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
|
|
16
|
-
> = TFormValidator extends Validator<TFormData, infer FFN>
|
|
17
|
-
? FFN | OnServerValidateFn<TFormData>
|
|
18
|
-
: OnServerValidateFn<TFormData>
|
|
19
|
-
|
|
20
|
-
interface ServerFormOptions<
|
|
21
|
-
TFormData,
|
|
22
|
-
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
|
|
23
|
-
> extends FormOptions<TFormData, TFormValidator> {
|
|
24
|
-
onServerValidate?: OnServerValidateOrFn<TFormData, TFormValidator>
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
type ValidateFormData<
|
|
28
|
-
TFormData,
|
|
29
|
-
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
|
|
30
|
-
> = (
|
|
31
|
-
formData: FormData,
|
|
32
|
-
info?: Parameters<typeof decode>[1],
|
|
33
|
-
) => Promise<Partial<FormApi<TFormData, TFormValidator>['state']>>
|
|
34
|
-
|
|
35
|
-
export const createServerValidate = <
|
|
36
|
-
TFormData,
|
|
37
|
-
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
|
|
38
|
-
>(
|
|
39
|
-
defaultOpts: ServerFormOptions<TFormData, TFormValidator>,
|
|
40
|
-
) =>
|
|
41
|
-
(async (
|
|
42
|
-
formData: FormData,
|
|
43
|
-
info?: Parameters<typeof decode>[1],
|
|
44
|
-
): Promise<Partial<FormApi<TFormData, TFormValidator>['state']>> => {
|
|
45
|
-
const { validatorAdapter, onServerValidate } = defaultOpts
|
|
46
|
-
|
|
47
|
-
const runValidator = (propsValue: { value: TFormData }) => {
|
|
48
|
-
if (validatorAdapter && typeof onServerValidate !== 'function') {
|
|
49
|
-
return validatorAdapter().validate(propsValue, onServerValidate)
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return (onServerValidate as OnServerValidateFn<TFormData>)(propsValue)
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const data = decode(formData, info) as never as TFormData
|
|
56
|
-
|
|
57
|
-
const onServerError = runValidator({ value: data })
|
|
58
|
-
|
|
59
|
-
return {
|
|
60
|
-
errorMap: {
|
|
61
|
-
onServer: onServerError,
|
|
62
|
-
},
|
|
63
|
-
errors: onServerError ? [onServerError] : [],
|
|
64
|
-
}
|
|
65
|
-
}) as ValidateFormData<TFormData, TFormValidator>
|
|
66
|
-
|
|
67
|
-
export const initialFormState = {
|
|
68
|
-
errorMap: {
|
|
69
|
-
onServer: undefined,
|
|
70
|
-
},
|
|
71
|
-
errors: [],
|
|
72
|
-
}
|
package/src/nextjs-index.ts
DELETED