xanv 1.1.27 → 1.1.29
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/XVType.cjs +13 -2
- package/XVType.cjs.map +1 -1
- package/XVType.d.ts +1 -0
- package/XVType.js +13 -2
- package/XVType.js.map +1 -1
- package/package.json +1 -1
- package/types/FunctionComponent.cjs +6 -0
- package/types/FunctionComponent.cjs.map +1 -1
- package/types/FunctionComponent.d.ts +1 -1
- package/types/FunctionComponent.js +6 -0
- package/types/FunctionComponent.js.map +1 -1
package/XVType.cjs
CHANGED
|
@@ -32,8 +32,12 @@ class XVType {
|
|
|
32
32
|
}
|
|
33
33
|
parse(value) {
|
|
34
34
|
// default
|
|
35
|
-
if (this.meta.default !== undefined &&
|
|
36
|
-
value
|
|
35
|
+
if (this.meta.default !== undefined &&
|
|
36
|
+
(value === undefined || value === null)) {
|
|
37
|
+
value =
|
|
38
|
+
typeof this.meta.default === "function"
|
|
39
|
+
? this.meta.default()
|
|
40
|
+
: this.meta.default;
|
|
37
41
|
}
|
|
38
42
|
// optional / nullable
|
|
39
43
|
if (this.meta.optional && value === undefined)
|
|
@@ -52,6 +56,13 @@ class XVType {
|
|
|
52
56
|
}
|
|
53
57
|
return result;
|
|
54
58
|
}
|
|
59
|
+
getDefault() {
|
|
60
|
+
if (this.meta.default === undefined)
|
|
61
|
+
return undefined;
|
|
62
|
+
return typeof this.meta.default === "function"
|
|
63
|
+
? this.meta.default()
|
|
64
|
+
: this.meta.default;
|
|
65
|
+
}
|
|
55
66
|
}
|
|
56
67
|
|
|
57
68
|
module.exports = XVType;
|
package/XVType.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"XVType.cjs","sources":["../src/XVType.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"XVType.cjs","sources":["../src/XVType.ts"],"sourcesContent":["import {\n XVCheckCallback,\n XVDefault,\n XVDefaultValue,\n XVMeta,\n XVNullable,\n XVOptional,\n XVTransformCallback,\n} from \"./types\";\n\nabstract class XVType<T> {\n readonly _type!: T;\n protected checks: XVCheckCallback<T>[] = [];\n protected transforms: XVTransformCallback<T>[] = [];\n readonly meta: XVMeta<T> = {};\n protected abstract check(value: unknown): T;\n\n protected set(\n method: string,\n check: XVCheckCallback<T>,\n args: any = true,\n ): this {\n if (!(method in this)) {\n throw new Error(\n `Method ${method} does not exist on ${this.constructor.name}`,\n );\n }\n this.checks.push(check);\n this.meta[method] = args;\n return this;\n }\n\n clone(): this {\n return Object.assign(Object.create(Object.getPrototypeOf(this)), this);\n }\n\n optional(): XVOptional<this> {\n return this.set(\"optional\", () => {}) as any;\n }\n\n nullable(): XVNullable<this> {\n return this.set(\"nullable\", () => {}) as any;\n }\n\n default(value: XVDefaultValue<T>): XVDefault<this, T> {\n return this.set(\"default\", () => {}, value) as any;\n }\n\n transform(cb: XVTransformCallback<T>) {\n this.transforms.push(cb);\n return this;\n }\n\n parse(value: unknown): T | undefined | null {\n // default\n if (\n this.meta.default !== undefined &&\n (value === undefined || value === null)\n ) {\n value =\n typeof this.meta.default === \"function\"\n ? (this.meta.default as () => T)()\n : this.meta.default;\n }\n\n // optional / nullable\n if (this.meta.optional && value === undefined) return undefined;\n if (this.meta.nullable && value === null) return null;\n\n // run internal check\n let result = this.check(value);\n\n // run user checks\n for (const check of this.checks) {\n check(result);\n }\n\n // run transforms\n for (const transform of this.transforms) {\n result = transform(result);\n }\n\n return result;\n }\n\n getDefault(): T | undefined {\n if (this.meta.default === undefined) return undefined;\n return typeof this.meta.default === \"function\"\n ? (this.meta.default as () => T)()\n : this.meta.default;\n }\n}\n\nexport default XVType;\n"],"names":[],"mappings":";;AAUA,MAAe,MAAM,CAAA;AAArB,IAAA,WAAA,GAAA;QAEY,IAAA,CAAA,MAAM,GAAyB,EAAE;QACjC,IAAA,CAAA,UAAU,GAA6B,EAAE;QAC1C,IAAA,CAAA,IAAI,GAAc,EAAE;IA6E/B;AA1EY,IAAA,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AG,IAAI,CAAC,IAAI,CAAC,OAAmB;AAChC,sBAAE,IAAI,CAAC,IAAI,CAAC,OAAO;QACzB;;QAGA,IAAI,IAAI,CAAC,IAAI,CAAC,QAAQ,IAAI,KAAK,KAAK,SAAS;AAAE,YAAA,OAAO,SAAS;QAC/D,IAAI,IAAI,CAAC,IAAI,CAAC,QAAQ,IAAI,KAAK,KAAK,IAAI;AAAE,YAAA,OAAO,IAAI;;QAGrD,IAAI,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC;;AAG9B,QAAA,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,MAAM,EAAE;YAC/B,KAAK,CAAC,MAAM,CAAC;QACf;;AAGA,QAAA,KAAK,MAAM,SAAS,IAAI,IAAI,CAAC,UAAU,EAAE;AACvC,YAAA,MAAM,GAAG,SAAS,CAAC,MAAM,CAAC;QAC5B;AAEA,QAAA,OAAO,MAAM;IACf;IAEA,UAAU,GAAA;AACR,QAAA,IAAI,IAAI,CAAC,IAAI,CAAC,OAAO,KAAK,SAAS;AAAE,YAAA,OAAO,SAAS;AACrD,QAAA,OAAO,OAAO,IAAI,CAAC,IAAI,CAAC,OAAO,KAAK;AAClC,cAAG,IAAI,CAAC,IAAI,CAAC,OAAmB;AAChC,cAAE,IAAI,CAAC,IAAI,CAAC,OAAO;IACvB;AACD;;;;"}
|
package/XVType.d.ts
CHANGED
package/XVType.js
CHANGED
|
@@ -30,8 +30,12 @@ class XVType {
|
|
|
30
30
|
}
|
|
31
31
|
parse(value) {
|
|
32
32
|
// default
|
|
33
|
-
if (this.meta.default !== undefined &&
|
|
34
|
-
value
|
|
33
|
+
if (this.meta.default !== undefined &&
|
|
34
|
+
(value === undefined || value === null)) {
|
|
35
|
+
value =
|
|
36
|
+
typeof this.meta.default === "function"
|
|
37
|
+
? this.meta.default()
|
|
38
|
+
: this.meta.default;
|
|
35
39
|
}
|
|
36
40
|
// optional / nullable
|
|
37
41
|
if (this.meta.optional && value === undefined)
|
|
@@ -50,6 +54,13 @@ class XVType {
|
|
|
50
54
|
}
|
|
51
55
|
return result;
|
|
52
56
|
}
|
|
57
|
+
getDefault() {
|
|
58
|
+
if (this.meta.default === undefined)
|
|
59
|
+
return undefined;
|
|
60
|
+
return typeof this.meta.default === "function"
|
|
61
|
+
? this.meta.default()
|
|
62
|
+
: this.meta.default;
|
|
63
|
+
}
|
|
53
64
|
}
|
|
54
65
|
|
|
55
66
|
export { XVType as default };
|
package/XVType.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"XVType.js","sources":["../src/XVType.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"XVType.js","sources":["../src/XVType.ts"],"sourcesContent":["import {\n XVCheckCallback,\n XVDefault,\n XVDefaultValue,\n XVMeta,\n XVNullable,\n XVOptional,\n XVTransformCallback,\n} from \"./types\";\n\nabstract class XVType<T> {\n readonly _type!: T;\n protected checks: XVCheckCallback<T>[] = [];\n protected transforms: XVTransformCallback<T>[] = [];\n readonly meta: XVMeta<T> = {};\n protected abstract check(value: unknown): T;\n\n protected set(\n method: string,\n check: XVCheckCallback<T>,\n args: any = true,\n ): this {\n if (!(method in this)) {\n throw new Error(\n `Method ${method} does not exist on ${this.constructor.name}`,\n );\n }\n this.checks.push(check);\n this.meta[method] = args;\n return this;\n }\n\n clone(): this {\n return Object.assign(Object.create(Object.getPrototypeOf(this)), this);\n }\n\n optional(): XVOptional<this> {\n return this.set(\"optional\", () => {}) as any;\n }\n\n nullable(): XVNullable<this> {\n return this.set(\"nullable\", () => {}) as any;\n }\n\n default(value: XVDefaultValue<T>): XVDefault<this, T> {\n return this.set(\"default\", () => {}, value) as any;\n }\n\n transform(cb: XVTransformCallback<T>) {\n this.transforms.push(cb);\n return this;\n }\n\n parse(value: unknown): T | undefined | null {\n // default\n if (\n this.meta.default !== undefined &&\n (value === undefined || value === null)\n ) {\n value =\n typeof this.meta.default === \"function\"\n ? (this.meta.default as () => T)()\n : this.meta.default;\n }\n\n // optional / nullable\n if (this.meta.optional && value === undefined) return undefined;\n if (this.meta.nullable && value === null) return null;\n\n // run internal check\n let result = this.check(value);\n\n // run user checks\n for (const check of this.checks) {\n check(result);\n }\n\n // run transforms\n for (const transform of this.transforms) {\n result = transform(result);\n }\n\n return result;\n }\n\n getDefault(): T | undefined {\n if (this.meta.default === undefined) return undefined;\n return typeof this.meta.default === \"function\"\n ? (this.meta.default as () => T)()\n : this.meta.default;\n }\n}\n\nexport default XVType;\n"],"names":[],"mappings":"AAUA,MAAe,MAAM,CAAA;AAArB,IAAA,WAAA,GAAA;QAEY,IAAA,CAAA,MAAM,GAAyB,EAAE;QACjC,IAAA,CAAA,UAAU,GAA6B,EAAE;QAC1C,IAAA,CAAA,IAAI,GAAc,EAAE;IA6E/B;AA1EY,IAAA,GAAG,CACX,MAAc,EACd,KAAyB,EACzB,OAAY,IAAI,EAAA;AAEhB,QAAA,IAAI,EAAE,MAAM,IAAI,IAAI,CAAC,EAAE;AACrB,YAAA,MAAM,IAAI,KAAK,CACb,CAAA,OAAA,EAAU,MAAM,CAAA,mBAAA,EAAsB,IAAI,CAAC,WAAW,CAAC,IAAI,CAAA,CAAE,CAC9D;QACH;AACA,QAAA,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC;AACvB,QAAA,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,IAAI;AACxB,QAAA,OAAO,IAAI;IACb;IAEA,KAAK,GAAA;AACH,QAAA,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,EAAE,IAAI,CAAC;IACxE;IAEA,QAAQ,GAAA;QACN,OAAO,IAAI,CAAC,GAAG,CAAC,UAAU,EAAE,MAAK,EAAE,CAAC,CAAQ;IAC9C;IAEA,QAAQ,GAAA;QACN,OAAO,IAAI,CAAC,GAAG,CAAC,UAAU,EAAE,MAAK,EAAE,CAAC,CAAQ;I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,KAAK;AAClC,cAAG,IAAI,CAAC,IAAI,CAAC,OAAmB;AAChC,cAAE,IAAI,CAAC,IAAI,CAAC,OAAO;IACvB;AACD;;;;"}
|
package/package.json
CHANGED
|
@@ -16,6 +16,12 @@ class XVFunctionComponent extends XVType {
|
|
|
16
16
|
}
|
|
17
17
|
parse(fn) {
|
|
18
18
|
const checked = super.parse(fn);
|
|
19
|
+
if (this.meta.optional && !checked) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (this.meta.nullable && !checked) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
19
25
|
return ((inputProps) => {
|
|
20
26
|
const parsed = {};
|
|
21
27
|
for (const key in this.props) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FunctionComponent.cjs","sources":["../../src/types/FunctionComponent.ts"],"sourcesContent":["import { Infer } from \"../types\";\nimport XVType from \"../XVType\";\n\n/**\n * Function component type (sync + async)\n */\ntype FuncComponent<P, Async extends boolean = false> = Async extends true\n ? (props: P) => Promise<any>\n : (props: P) => any;\n\n/**\n * Convert schema props → runtime props\n */\ntype InferProps<P extends Record<string, XVType<any>>> = {\n [K in keyof P]: Infer<P[K]>;\n};\n\nclass XVFunctionComponent<\n P extends Record<string, XVType<any>>,\n Async extends boolean = false,\n> extends XVType<FuncComponent<InferProps<P>, Async>> {\n readonly props: P;\n readonly is_async: Async;\n\n constructor(props: P, async?: Async) {\n super();\n this.props = props;\n this.is_async = (async ?? false) as Async;\n }\n\n protected check(value: unknown): any {\n if (typeof value !== \"function\") {\n throw new Error(\"Component must be a function\");\n }\n return value;\n }\n\n parse(fn: unknown) {\n const checked = super.parse(fn);\n\n return ((inputProps: InferProps<P>) => {\n const parsed: InferProps<P> = {} as InferProps<P>;\n\n for (const key in this.props) {\n parsed[key] = this.props[key].parse((inputProps as any)[key]);\n }\n\n const result = (checked as any)(parsed);\n\n if (this.is_async) {\n return Promise.resolve(result);\n }\n\n return result;\n }) as FuncComponent<InferProps<P>, Async>;\n }\n}\n\nexport default XVFunctionComponent;\n"],"names":[],"mappings":";;;;AAiBA,MAAM,mBAGJ,SAAQ,MAA2C,CAAA;IAInD,WAAA,CAAY,KAAQ,EAAE,KAAa,EAAA;AACjC,QAAA,KAAK,EAAE;AACP,QAAA,IAAI,CAAC,KAAK,GAAG,KAAK;AAClB,QAAA,IAAI,CAAC,QAAQ,IAAI,KAAK,KAAA,IAAA,IAAL,KAAK,KAAA,MAAA,GAAL,KAAK,GAAI,KAAK,CAAU;IAC3C;AAEU,IAAA,KAAK,CAAC,KAAc,EAAA;AAC5B,QAAA,IAAI,OAAO,KAAK,KAAK,UAAU,EAAE;AAC/B,YAAA,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC;QACjD;AACA,QAAA,OAAO,KAAK;IACd;AAEA,IAAA,KAAK,CAAC,EAAW,EAAA;QACf,MAAM,OAAO,GAAG,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"FunctionComponent.cjs","sources":["../../src/types/FunctionComponent.ts"],"sourcesContent":["import { Infer } from \"../types\";\nimport XVType from \"../XVType\";\n\n/**\n * Function component type (sync + async)\n */\ntype FuncComponent<P, Async extends boolean = false> = Async extends true\n ? (props: P) => Promise<any>\n : (props: P) => any;\n\n/**\n * Convert schema props → runtime props\n */\ntype InferProps<P extends Record<string, XVType<any>>> = {\n [K in keyof P]: Infer<P[K]>;\n};\n\nclass XVFunctionComponent<\n P extends Record<string, XVType<any>>,\n Async extends boolean = false,\n> extends XVType<FuncComponent<InferProps<P>, Async>> {\n readonly props: P;\n readonly is_async: Async;\n\n constructor(props: P, async?: Async) {\n super();\n this.props = props;\n this.is_async = (async ?? false) as Async;\n }\n\n protected check(value: unknown): any {\n if (typeof value !== \"function\") {\n throw new Error(\"Component must be a function\");\n }\n return value;\n }\n\n parse(fn: unknown) {\n const checked = super.parse(fn);\n\n if (this.meta.optional && !checked) {\n return;\n }\n if (this.meta.nullable && !checked) {\n return null;\n }\n\n return ((inputProps: InferProps<P>) => {\n const parsed: InferProps<P> = {} as InferProps<P>;\n\n for (const key in this.props) {\n parsed[key] = this.props[key].parse((inputProps as any)[key]);\n }\n\n const result = (checked as any)(parsed);\n\n if (this.is_async) {\n return Promise.resolve(result);\n }\n\n return result;\n }) as FuncComponent<InferProps<P>, Async>;\n }\n}\n\nexport default XVFunctionComponent;\n"],"names":[],"mappings":";;;;AAiBA,MAAM,mBAGJ,SAAQ,MAA2C,CAAA;IAInD,WAAA,CAAY,KAAQ,EAAE,KAAa,EAAA;AACjC,QAAA,KAAK,EAAE;AACP,QAAA,IAAI,CAAC,KAAK,GAAG,KAAK;AAClB,QAAA,IAAI,CAAC,QAAQ,IAAI,KAAK,KAAA,IAAA,IAAL,KAAK,KAAA,MAAA,GAAL,KAAK,GAAI,KAAK,CAAU;IAC3C;AAEU,IAAA,KAAK,CAAC,KAAc,EAAA;AAC5B,QAAA,IAAI,OAAO,KAAK,KAAK,UAAU,EAAE;AAC/B,YAAA,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC;QACjD;AACA,QAAA,OAAO,KAAK;IACd;AAEA,IAAA,KAAK,CAAC,EAAW,EAAA;QACf,MAAM,OAAO,GAAG,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;QAE/B,IAAI,IAAI,CAAC,IAAI,CAAC,QAAQ,IAAI,CAAC,OAAO,EAAE;YAClC;QACF;QACA,IAAI,IAAI,CAAC,IAAI,CAAC,QAAQ,IAAI,CAAC,OAAO,EAAE;AAClC,YAAA,OAAO,IAAI;QACb;AAEA,QAAA,QAAQ,CAAC,UAAyB,KAAI;YACpC,MAAM,MAAM,GAAkB,EAAmB;AAEjD,YAAA,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,KAAK,EAAE;AAC5B,gBAAA,MAAM,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,KAAK,CAAE,UAAkB,CAAC,GAAG,CAAC,CAAC;YAC/D;AAEA,YAAA,MAAM,MAAM,GAAI,OAAe,CAAC,MAAM,CAAC;AAEvC,YAAA,IAAI,IAAI,CAAC,QAAQ,EAAE;AACjB,gBAAA,OAAO,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC;YAChC;AAEA,YAAA,OAAO,MAAM;AACf,QAAA,CAAC;IACH;AACD;;;;"}
|
|
@@ -16,7 +16,7 @@ declare class XVFunctionComponent<P extends Record<string, XVType<any>>, Async e
|
|
|
16
16
|
readonly is_async: Async;
|
|
17
17
|
constructor(props: P, async?: Async);
|
|
18
18
|
protected check(value: unknown): any;
|
|
19
|
-
parse(fn: unknown): FuncComponent<InferProps<P>, Async
|
|
19
|
+
parse(fn: unknown): FuncComponent<InferProps<P>, Async> | null | undefined;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
export { XVFunctionComponent as default };
|
|
@@ -14,6 +14,12 @@ class XVFunctionComponent extends XVType {
|
|
|
14
14
|
}
|
|
15
15
|
parse(fn) {
|
|
16
16
|
const checked = super.parse(fn);
|
|
17
|
+
if (this.meta.optional && !checked) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
if (this.meta.nullable && !checked) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
17
23
|
return ((inputProps) => {
|
|
18
24
|
const parsed = {};
|
|
19
25
|
for (const key in this.props) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FunctionComponent.js","sources":["../../src/types/FunctionComponent.ts"],"sourcesContent":["import { Infer } from \"../types\";\nimport XVType from \"../XVType\";\n\n/**\n * Function component type (sync + async)\n */\ntype FuncComponent<P, Async extends boolean = false> = Async extends true\n ? (props: P) => Promise<any>\n : (props: P) => any;\n\n/**\n * Convert schema props → runtime props\n */\ntype InferProps<P extends Record<string, XVType<any>>> = {\n [K in keyof P]: Infer<P[K]>;\n};\n\nclass XVFunctionComponent<\n P extends Record<string, XVType<any>>,\n Async extends boolean = false,\n> extends XVType<FuncComponent<InferProps<P>, Async>> {\n readonly props: P;\n readonly is_async: Async;\n\n constructor(props: P, async?: Async) {\n super();\n this.props = props;\n this.is_async = (async ?? false) as Async;\n }\n\n protected check(value: unknown): any {\n if (typeof value !== \"function\") {\n throw new Error(\"Component must be a function\");\n }\n return value;\n }\n\n parse(fn: unknown) {\n const checked = super.parse(fn);\n\n return ((inputProps: InferProps<P>) => {\n const parsed: InferProps<P> = {} as InferProps<P>;\n\n for (const key in this.props) {\n parsed[key] = this.props[key].parse((inputProps as any)[key]);\n }\n\n const result = (checked as any)(parsed);\n\n if (this.is_async) {\n return Promise.resolve(result);\n }\n\n return result;\n }) as FuncComponent<InferProps<P>, Async>;\n }\n}\n\nexport default XVFunctionComponent;\n"],"names":[],"mappings":";;AAiBA,MAAM,mBAGJ,SAAQ,MAA2C,CAAA;IAInD,WAAA,CAAY,KAAQ,EAAE,KAAa,EAAA;AACjC,QAAA,KAAK,EAAE;AACP,QAAA,IAAI,CAAC,KAAK,GAAG,KAAK;AAClB,QAAA,IAAI,CAAC,QAAQ,IAAI,KAAK,KAAA,IAAA,IAAL,KAAK,KAAA,MAAA,GAAL,KAAK,GAAI,KAAK,CAAU;IAC3C;AAEU,IAAA,KAAK,CAAC,KAAc,EAAA;AAC5B,QAAA,IAAI,OAAO,KAAK,KAAK,UAAU,EAAE;AAC/B,YAAA,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC;QACjD;AACA,QAAA,OAAO,KAAK;IACd;AAEA,IAAA,KAAK,CAAC,EAAW,EAAA;QACf,MAAM,OAAO,GAAG,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"FunctionComponent.js","sources":["../../src/types/FunctionComponent.ts"],"sourcesContent":["import { Infer } from \"../types\";\nimport XVType from \"../XVType\";\n\n/**\n * Function component type (sync + async)\n */\ntype FuncComponent<P, Async extends boolean = false> = Async extends true\n ? (props: P) => Promise<any>\n : (props: P) => any;\n\n/**\n * Convert schema props → runtime props\n */\ntype InferProps<P extends Record<string, XVType<any>>> = {\n [K in keyof P]: Infer<P[K]>;\n};\n\nclass XVFunctionComponent<\n P extends Record<string, XVType<any>>,\n Async extends boolean = false,\n> extends XVType<FuncComponent<InferProps<P>, Async>> {\n readonly props: P;\n readonly is_async: Async;\n\n constructor(props: P, async?: Async) {\n super();\n this.props = props;\n this.is_async = (async ?? false) as Async;\n }\n\n protected check(value: unknown): any {\n if (typeof value !== \"function\") {\n throw new Error(\"Component must be a function\");\n }\n return value;\n }\n\n parse(fn: unknown) {\n const checked = super.parse(fn);\n\n if (this.meta.optional && !checked) {\n return;\n }\n if (this.meta.nullable && !checked) {\n return null;\n }\n\n return ((inputProps: InferProps<P>) => {\n const parsed: InferProps<P> = {} as InferProps<P>;\n\n for (const key in this.props) {\n parsed[key] = this.props[key].parse((inputProps as any)[key]);\n }\n\n const result = (checked as any)(parsed);\n\n if (this.is_async) {\n return Promise.resolve(result);\n }\n\n return result;\n }) as FuncComponent<InferProps<P>, Async>;\n }\n}\n\nexport default XVFunctionComponent;\n"],"names":[],"mappings":";;AAiBA,MAAM,mBAGJ,SAAQ,MAA2C,CAAA;IAInD,WAAA,CAAY,KAAQ,EAAE,KAAa,EAAA;AACjC,QAAA,KAAK,EAAE;AACP,QAAA,IAAI,CAAC,KAAK,GAAG,KAAK;AAClB,QAAA,IAAI,CAAC,QAAQ,IAAI,KAAK,KAAA,IAAA,IAAL,KAAK,KAAA,MAAA,GAAL,KAAK,GAAI,KAAK,CAAU;IAC3C;AAEU,IAAA,KAAK,CAAC,KAAc,EAAA;AAC5B,QAAA,IAAI,OAAO,KAAK,KAAK,UAAU,EAAE;AAC/B,YAAA,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC;QACjD;AACA,QAAA,OAAO,KAAK;IACd;AAEA,IAAA,KAAK,CAAC,EAAW,EAAA;QACf,MAAM,OAAO,GAAG,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;QAE/B,IAAI,IAAI,CAAC,IAAI,CAAC,QA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