@embeddables/forms 0.0.5 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +116 -33
- package/dist/{form-9Tp91g6a.js → form-7wmC3G_q.js} +652 -221
- package/dist/form-7wmC3G_q.js.map +1 -0
- package/dist/{form-DzfCc5X2.cjs → form-CdIfHqNc.cjs} +675 -220
- package/dist/form-CdIfHqNc.cjs.map +1 -0
- package/dist/{index-qfBhdk5L.d.ts → form-x4EJcR9A.d.ts} +114 -122
- package/dist/form-x4EJcR9A.d.ts.map +1 -0
- package/dist/index-CmcgIxTH.d.ts +78 -0
- package/dist/index-CmcgIxTH.d.ts.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.js +1 -1
- package/dist/react.cjs +136 -15
- package/dist/react.cjs.map +1 -1
- package/dist/react.d.ts +49 -7
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +136 -16
- package/dist/react.js.map +1 -1
- package/dist/server.cjs +50 -0
- package/dist/server.cjs.map +1 -0
- package/dist/server.d.ts +13 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +49 -0
- package/dist/server.js.map +1 -0
- package/package.json +8 -3
- package/dist/form-9Tp91g6a.js.map +0 -1
- package/dist/form-DzfCc5X2.cjs.map +0 -1
- package/dist/index-qfBhdk5L.d.ts.map +0 -1
package/dist/react.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react.cjs","names":["useRef","useCallback","useSyncExternalStore","useEmbeddablesModule","useRef","useState","useCallback","initForms"],"sources":["../src/react/use-form-store.ts","../src/react/forms-registry.ts","../src/react/use-forms.ts","../src/react/use-form.ts","../src/react/use-form-errors.ts","../src/react/use-form-field.ts","../src/react/resolve-core-analytics.ts","../src/react/register-forms-client.ts","../src/react/forms-module.ts"],"sourcesContent":["import { useCallback, useRef, useSyncExternalStore } from 'react'\n\nimport type { FormSchema, FormValues } from '../core/config.js'\nimport type { FieldErrors, FormInstance } from '../core/form.js'\n\nexport type FormSnapshot<TSchema extends FormSchema> = Readonly<{\n values: Partial<FormValues<TSchema>>\n errors: FieldErrors<TSchema>\n}>\n\nconst EMPTY_SNAPSHOT: FormSnapshot<FormSchema> = Object.freeze({\n values: {},\n errors: {},\n})\n\nfunction createSnapshot<TSchema extends FormSchema>(\n form: FormInstance<TSchema>,\n): FormSnapshot<TSchema> {\n return Object.freeze({\n values: form.getAll(),\n errors: form.errors(),\n })\n}\n\ninterface CachedSnapshot<TSchema extends FormSchema> {\n readonly form: FormInstance<TSchema>\n readonly snapshot: FormSnapshot<TSchema>\n}\n\nexport function useFormSnapshot<TSchema extends FormSchema>(\n form: FormInstance<TSchema> | null,\n): FormSnapshot<TSchema> {\n const cachedRef = useRef<CachedSnapshot<TSchema> | null>(null)\n const emptyRef = useRef<FormSnapshot<TSchema>>(EMPTY_SNAPSHOT as FormSnapshot<TSchema>)\n\n const subscribe = useCallback(\n (listener: () => void) => {\n if (form === null) return () => undefined\n return form.subscribe(() => {\n cachedRef.current = { form, snapshot: createSnapshot(form) }\n listener()\n })\n },\n [form],\n )\n\n const getSnapshot = useCallback((): FormSnapshot<TSchema> => {\n if (form === null) {\n // * Otherwise a later A -> null -> mutate A -> A sequence would resurrect\n // * a snapshot cached before the detach, missing the mutation in between.\n cachedRef.current = null\n return emptyRef.current\n }\n // * A cached snapshot from a previous form instance must not survive a\n // * form change — recompute so switching forms without a mutation still\n // * returns the new form's values and errors.\n if (cachedRef.current === null || cachedRef.current.form !== form) {\n cachedRef.current = { form, snapshot: createSnapshot(form) }\n }\n return cachedRef.current.snapshot\n }, [form])\n\n return useSyncExternalStore(subscribe, getSnapshot, getSnapshot)\n}\n","import type { FormSchemaMap, FormsClient } from '../core/form.js'\n\nexport const FORMS_MODULE_KEY = 'forms'\n\n/**\n * Idempotence guard for `registerFormsClient` only — React reads the client\n * from provider context, not from here.\n */\nexport const formsByCore = new WeakMap<object, FormsClient<FormSchemaMap>>()\n\nexport function getRegisteredFormsClient(core: object): FormsClient<FormSchemaMap> | undefined {\n return formsByCore.get(core)\n}\n","import { useEmbeddablesModule } from '@embeddables/core/react'\n\nimport { FORMS_MODULE_KEY } from './forms-registry.js'\n\nimport type { FormSchemaMap, FormsClient, InitFormsOptions } from '../core/form.js'\n\nexport type FormsReactOptions<TSchemas extends FormSchemaMap = FormSchemaMap> = Omit<\n InitFormsOptions<TSchemas>,\n 'core'\n>\n\n/** @internal Product hooks read the module client from provider context. */\nexport function useRegisteredFormsClient<\n TSchemas extends FormSchemaMap = FormSchemaMap,\n>(): FormsClient<TSchemas> | null {\n return useEmbeddablesModule<FormsClient<TSchemas>>({ key: FORMS_MODULE_KEY })\n}\n","import { useRef } from 'react'\n\nimport { useFormSnapshot } from './use-form-store.js'\nimport { useRegisteredFormsClient } from './use-forms.js'\n\nimport type { CustomValidationsFor, FormInstance, FormSchemaMap } from '../core/form.js'\nimport type { RegisteredSchemas } from './schema-registry.js'\n\n/**\n * Reactive binding for one declared form.\n *\n * Takes no type arguments: `TFormId` is inferred from `formId`, and the schema\n * map comes from the registry `em build` augments. Pass both explicitly\n * (`useForm<'signup', OtherSchemas>`) only to work against a map other than the\n * registered one.\n */\nexport function useForm<\n TFormId extends keyof TSchemas & string,\n TSchemas extends FormSchemaMap = RegisteredSchemas,\n>({\n formId,\n customValidations,\n}: {\n formId: TFormId\n customValidations?: CustomValidationsFor<TSchemas[TFormId]>\n}): {\n form: FormInstance<TSchemas[TFormId]> | null\n values: ReturnType<typeof useFormSnapshot<TSchemas[TFormId]>>['values']\n errors: ReturnType<typeof useFormSnapshot<TSchemas[TFormId]>>['errors']\n} {\n const client = useRegisteredFormsClient<TSchemas>()\n // * Held in a ref because only the first call for a form id builds the\n // * instance; a later render passing new validators must not look like a change.\n const customValidationsRef = useRef(customValidations)\n customValidationsRef.current = customValidations\n\n // * Safe to call on every render: the client returns one instance per form id.\n const form =\n client === null\n ? null\n : client.getForm({ formId, customValidations: customValidationsRef.current })\n const { values, errors } = useFormSnapshot(form)\n\n return { form, values, errors }\n}\n","import { useFormSnapshot } from './use-form-store.js'\n\nimport type { FormSchema } from '../core/config.js'\nimport type { FieldErrors, FormInstance } from '../core/form.js'\n\nexport function useFormErrors<TSchema extends FormSchema>(\n form: FormInstance<TSchema> | null,\n): FieldErrors<TSchema> {\n const { errors } = useFormSnapshot(form)\n return errors\n}\n","import { useCallback, useEffect, useState } from 'react'\n\nimport { useFormSnapshot } from './use-form-store.js'\n\nimport type { FormFieldKey, FormSchema, FormValues } from '../core/config.js'\nimport type { FieldErrors, FormInstance, SetResult } from '../core/form.js'\n\nconst noopSetValue = async (): Promise<SetResult<FormSchema>> => ({\n ok: false,\n errors: {},\n})\n\nexport function useFormField<const TSchema extends FormSchema, K extends FormFieldKey<TSchema>>({\n form,\n key,\n}: {\n form: FormInstance<TSchema> | null\n key: K\n}): {\n value: FormValues<TSchema>[K] | undefined\n error: readonly string[] | undefined\n setValue: (value: FormValues<TSchema>[K]) => void\n onBlur: () => Promise<SetResult<TSchema>>\n} {\n const { values, errors } = useFormSnapshot(form)\n const committed = values[key]\n const [draft, setDraft] = useState<FormValues<TSchema>[K] | undefined>(committed)\n\n useEffect(() => {\n setDraft(committed)\n }, [form, key, committed])\n\n const setValue = useCallback((value: FormValues<TSchema>[K]) => {\n setDraft(value)\n }, [])\n\n const onBlur = useCallback(async (): Promise<SetResult<TSchema>> => {\n if (form === null) return noopSetValue()\n if (draft === committed) {\n return { ok: true, errors: {} as FieldErrors<TSchema> }\n }\n return form.set({ [key]: draft } as Partial<FormValues<TSchema>>)\n }, [committed, draft, form, key])\n\n if (form === null) {\n return {\n value: undefined,\n error: undefined,\n setValue: () => undefined,\n onBlur: async () => noopSetValue(),\n }\n }\n\n return {\n value: draft,\n error: errors[key],\n setValue,\n onBlur,\n }\n}\n","import type { AnalyticsInstance } from '@embeddables/core'\nimport type { EmbeddablesInstance } from '@embeddables/core'\n\nexport function resolveCoreAnalyticsInstance(\n core: EmbeddablesInstance,\n): AnalyticsInstance | undefined {\n if (typeof core.getAnalyticsInstance !== 'function') return undefined\n return core.getAnalyticsInstance() ?? undefined\n}\n","import { initForms } from '../core/form.js'\nimport { formsByCore } from './forms-registry.js'\nimport { resolveCoreAnalyticsInstance } from './resolve-core-analytics.js'\n\nimport type { FormSchemaMap, FormsClient, InitFormsOptions } from '../core/form.js'\n\nexport function registerFormsClient<const TSchemas extends FormSchemaMap>({\n core,\n ...options\n}: InitFormsOptions<TSchemas>): FormsClient<TSchemas> {\n const existing = formsByCore.get(core) as FormsClient<TSchemas> | undefined\n if (existing !== undefined) return existing\n\n const analyticsInstance = options.analyticsInstance ?? resolveCoreAnalyticsInstance(core)\n\n const client = initForms({ ...options, core, analyticsInstance })\n formsByCore.set(core, client as FormsClient<FormSchemaMap>)\n return client\n}\n","import type { EmbeddablesReactModule } from '@embeddables/core/react'\n\nimport { FORMS_MODULE_KEY } from './forms-registry.js'\nimport { registerFormsClient } from './register-forms-client.js'\n\nimport type { FormSchemaMap, FormsClient } from '../core/form.js'\nimport type { FormsReactOptions } from './use-forms.js'\n\nexport type { FormsReactOptions } from './use-forms.js'\n\nexport function forms<const TSchemas extends FormSchemaMap>(\n options: FormsReactOptions<TSchemas>,\n): EmbeddablesReactModule<FormsClient<TSchemas>> {\n return {\n key: FORMS_MODULE_KEY,\n init: (core) => registerFormsClient({ core, ...options }),\n }\n}\n"],"mappings":";;;;;AAUA,MAAM,iBAA2C,OAAO,OAAO;CAC7D,QAAQ,CAAC;CACT,QAAQ,CAAC;AACX,CAAC;AAED,SAAS,eACP,MACuB;CACvB,OAAO,OAAO,OAAO;EACnB,QAAQ,KAAK,OAAO;EACpB,QAAQ,KAAK,OAAO;CACtB,CAAC;AACH;AAOA,SAAgB,gBACd,MACuB;CACvB,MAAM,aAAA,GAAYA,MAAAA,OAAAA,CAAuC,IAAI;CAC7D,MAAM,YAAA,GAAWA,MAAAA,OAAAA,CAA8B,cAAuC;CAEt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C;CAC1D,OAAO;AACT;;;ACRA,SAAgB,MACd,SAC+C;CAC/C,OAAO;EACL,KAAK;EACL,OAAO,SAAS,oBAAoB;GAAE;GAAM,GAAG;EAAQ,CAAC;CAC1D;AACF"}
|
|
1
|
+
{"version":3,"file":"react.cjs","names":["useRef","useCallback","useSyncExternalStore","useEmbeddablesModule","useState","useCallback","useState","useRef","useCallback","FormsError","initForms","FormsError"],"sources":["../src/react/use-form-store.ts","../src/react/forms-registry.ts","../src/react/use-forms.ts","../src/react/use-form.ts","../src/react/use-form-errors.ts","../src/react/use-form-field.ts","../src/react/use-form-file-upload.ts","../src/react/resolve-core-analytics.ts","../src/react/register-forms-client.ts","../src/react/forms-module.ts"],"sourcesContent":["import { useCallback, useRef, useSyncExternalStore } from 'react'\n\nimport type { FormSchema, FormValues } from '../core/config.js'\nimport type { FieldErrors, FormInstance } from '../core/form.js'\n\nexport type FormSnapshot<TSchema extends FormSchema> = Readonly<{\n values: Partial<FormValues<TSchema>>\n errors: FieldErrors<TSchema>\n}>\n\nconst EMPTY_SNAPSHOT: FormSnapshot<FormSchema> = Object.freeze({\n values: {},\n errors: {},\n})\n\nfunction createSnapshot<TSchema extends FormSchema>(\n form: FormInstance<TSchema>,\n): FormSnapshot<TSchema> {\n return Object.freeze({\n values: form.getAll(),\n errors: form.errors(),\n })\n}\n\ninterface CachedSnapshot<TSchema extends FormSchema> {\n readonly form: FormInstance<TSchema>\n readonly snapshot: FormSnapshot<TSchema>\n}\n\nexport function useFormSnapshot<TSchema extends FormSchema>(\n form: FormInstance<TSchema> | null,\n): FormSnapshot<TSchema> {\n const cachedRef = useRef<CachedSnapshot<TSchema> | null>(null)\n const emptyRef = useRef<FormSnapshot<TSchema>>(EMPTY_SNAPSHOT as FormSnapshot<TSchema>)\n\n const subscribe = useCallback(\n (listener: () => void) => {\n if (form === null) return () => undefined\n return form.subscribe(() => {\n cachedRef.current = { form, snapshot: createSnapshot(form) }\n listener()\n })\n },\n [form],\n )\n\n const getSnapshot = useCallback((): FormSnapshot<TSchema> => {\n if (form === null) {\n // * Otherwise a later A -> null -> mutate A -> A sequence would resurrect\n // * a snapshot cached before the detach, missing the mutation in between.\n cachedRef.current = null\n return emptyRef.current\n }\n // * A cached snapshot from a previous form instance must not survive a\n // * form change — recompute so switching forms without a mutation still\n // * returns the new form's values and errors.\n if (cachedRef.current === null || cachedRef.current.form !== form) {\n cachedRef.current = { form, snapshot: createSnapshot(form) }\n }\n return cachedRef.current.snapshot\n }, [form])\n\n return useSyncExternalStore(subscribe, getSnapshot, getSnapshot)\n}\n","import type { FormSchemaMap, FormsClient } from '../core/form.js'\n\nexport const FORMS_MODULE_KEY = 'forms'\n\n/**\n * Idempotence guard for `registerFormsClient` only — React reads the client\n * from provider context, not from here.\n */\nexport const formsByCore = new WeakMap<object, FormsClient<FormSchemaMap>>()\n\nexport function getRegisteredFormsClient(core: object): FormsClient<FormSchemaMap> | undefined {\n return formsByCore.get(core)\n}\n","import { useEmbeddablesModule } from '@embeddables/core/react'\n\nimport { FORMS_MODULE_KEY } from './forms-registry.js'\n\nimport type { FormSchemaMap, FormsClient, InitFormsOptions } from '../core/form.js'\n\nexport type FormsReactOptions<TSchemas extends FormSchemaMap = FormSchemaMap> = Omit<\n InitFormsOptions<TSchemas>,\n 'core'\n>\n\n/** @internal Product hooks read the module client from provider context. */\nexport function useRegisteredFormsClient<\n TSchemas extends FormSchemaMap = FormSchemaMap,\n>(): FormsClient<TSchemas> | null {\n return useEmbeddablesModule<FormsClient<TSchemas>>({ key: FORMS_MODULE_KEY })\n}\n","import { useFormSnapshot } from './use-form-store.js'\nimport { useRegisteredFormsClient } from './use-forms.js'\n\nimport type { FormInstance, FormSchemaMap } from '../core/form.js'\nimport type { RegisteredSchemas } from './schema-registry.js'\n\n/**\n * Reactive binding for one declared form.\n *\n * Takes no type arguments: `TFormId` is inferred from `formId`, and the schema\n * map comes from the registry `em build` augments. Pass both explicitly\n * (`useForm<'signup', OtherSchemas>`) only to work against a map other than the\n * registered one.\n */\nexport function useForm<\n TFormId extends keyof TSchemas & string,\n TSchemas extends FormSchemaMap = RegisteredSchemas,\n>({\n formId,\n}: {\n formId: TFormId\n}): {\n form: FormInstance<TSchemas[TFormId]> | null\n values: ReturnType<typeof useFormSnapshot<TSchemas[TFormId]>>['values']\n errors: ReturnType<typeof useFormSnapshot<TSchemas[TFormId]>>['errors']\n} {\n const client = useRegisteredFormsClient<TSchemas>()\n\n // * Safe to call on every render: the client returns one instance per form id.\n const form = client === null ? null : client.getForm({ formId })\n const { values, errors } = useFormSnapshot(form)\n\n return { form, values, errors }\n}\n","import { useFormSnapshot } from './use-form-store.js'\n\nimport type { FormSchema } from '../core/config.js'\nimport type { FieldErrors, FormInstance } from '../core/form.js'\n\nexport function useFormErrors<TSchema extends FormSchema>(\n form: FormInstance<TSchema> | null,\n): FieldErrors<TSchema> {\n const { errors } = useFormSnapshot(form)\n return errors\n}\n","import { useCallback, useEffect, useState } from 'react'\n\nimport { useFormSnapshot } from './use-form-store.js'\n\nimport type { FormFieldKey, FormSchema, FormValues } from '../core/config.js'\nimport type { FieldErrors, FormInstance, SetResult } from '../core/form.js'\n\nconst noopSetValue = async (): Promise<SetResult<FormSchema>> => ({\n ok: false,\n errors: {},\n})\n\nexport function useFormField<const TSchema extends FormSchema, K extends FormFieldKey<TSchema>>({\n form,\n key,\n}: {\n form: FormInstance<TSchema> | null\n key: K\n}): {\n value: FormValues<TSchema>[K] | undefined\n error: readonly string[] | undefined\n setValue: (value: FormValues<TSchema>[K] | undefined) => void\n commit: (value: FormValues<TSchema>[K] | undefined) => Promise<SetResult<TSchema>>\n onBlur: () => Promise<SetResult<TSchema>>\n} {\n const { values, errors } = useFormSnapshot(form)\n const committed = values[key]\n const [draft, setDraft] = useState<FormValues<TSchema>[K] | undefined>(committed)\n\n useEffect(() => {\n setDraft(committed)\n }, [form, key, committed])\n\n const setValue = useCallback((value: FormValues<TSchema>[K] | undefined) => {\n setDraft(value)\n }, [])\n\n // * Takes the value as an argument rather than reading `draft`, so it is\n // * correct when called in the same tick as the change that produced it.\n // * `onBlur` cannot be: it compares the `draft`/`committed` pair captured in\n // * the current render, which a same-tick `setValue` has not updated yet.\n const commit = useCallback(\n (value: FormValues<TSchema>[K] | undefined): Promise<SetResult<TSchema>> => {\n if (form === null) return noopSetValue()\n // * Keeps the control responsive while `set` runs, and keeps a rejected\n // * value on screen next to its error instead of snapping back.\n setDraft(value)\n return form.set({ [key]: value } as Partial<FormValues<TSchema>>)\n },\n [form, key],\n )\n\n const onBlur = useCallback(async (): Promise<SetResult<TSchema>> => {\n if (form === null) return noopSetValue()\n if (draft === committed) {\n return { ok: true, errors: {} as FieldErrors<TSchema> }\n }\n return form.set({ [key]: draft } as Partial<FormValues<TSchema>>)\n }, [committed, draft, form, key])\n\n if (form === null) {\n return {\n value: undefined,\n error: undefined,\n setValue: () => undefined,\n commit: async () => noopSetValue(),\n onBlur: async () => noopSetValue(),\n }\n }\n\n return {\n value: draft,\n error: errors[key],\n setValue,\n commit,\n onBlur,\n }\n}\n","import { useCallback, useEffect, useRef, useState } from 'react'\n\nimport { FormsError } from '../errors.js'\nimport { useFormSnapshot } from './use-form-store.js'\n\nimport type { FormSchema, FormValues } from '../core/config.js'\nimport type { FieldErrors, FormInstance, SetResult } from '../core/form.js'\nimport type { ChangeEvent } from 'react'\n\n/** Keys declared with `type: file` on a schema. */\nexport type FormFileFieldKey<TSchema extends FormSchema> = Extract<\n TSchema['fields'][number],\n { type: 'file' }\n>['key']\n\nconst noopSetResult = async <TSchema extends FormSchema>(): Promise<SetResult<TSchema>> => ({\n ok: false,\n errors: {} as FieldErrors<TSchema>,\n})\n\n/**\n * Reactive binding for a `type: file` field: uploads through the public API,\n * commits the returned `FormFileRef`, and exposes a native `<input type=\"file\">`\n * handler.\n */\nexport function useFormFileUpload<\n const TSchema extends FormSchema,\n K extends FormFileFieldKey<TSchema>,\n>({\n form,\n key,\n}: {\n form: FormInstance<TSchema> | null\n key: K\n}): {\n value: FormValues<TSchema>[K] | undefined\n error: readonly string[] | undefined\n isLoading: boolean\n uploadError: string | undefined\n upload: (file: File) => Promise<SetResult<TSchema>>\n clear: () => Promise<SetResult<TSchema>>\n inputProps: {\n type: 'file'\n disabled: boolean\n onChange: (event: ChangeEvent<HTMLInputElement>) => void\n }\n} {\n const { values, errors } = useFormSnapshot(form)\n const [isLoading, setIsLoading] = useState(false)\n const [uploadError, setUploadError] = useState<string | undefined>(undefined)\n const uploadGenerationRef = useRef(0)\n\n useEffect(() => {\n return () => {\n uploadGenerationRef.current += 1\n setIsLoading(false)\n setUploadError(undefined)\n }\n }, [form, key])\n\n const upload = useCallback(\n async (file: File): Promise<SetResult<TSchema>> => {\n if (form === null) return noopSetResult<TSchema>()\n\n const generation = ++uploadGenerationRef.current\n setIsLoading(true)\n setUploadError(undefined)\n try {\n const ref = await form.uploadFile({ key, file })\n if (generation !== uploadGenerationRef.current) {\n return { ok: false, errors: {} as FieldErrors<TSchema> }\n }\n return await form.set({ [key]: ref } as Partial<FormValues<TSchema>>)\n } catch (error) {\n if (generation !== uploadGenerationRef.current) {\n return { ok: false, errors: {} as FieldErrors<TSchema> }\n }\n const message = error instanceof FormsError ? error.message : 'File upload failed.'\n setUploadError(message)\n return { ok: false, errors: {} as FieldErrors<TSchema> }\n } finally {\n if (generation === uploadGenerationRef.current) {\n setIsLoading(false)\n }\n }\n },\n [form, key],\n )\n\n const clear = useCallback(async (): Promise<SetResult<TSchema>> => {\n if (form === null) return noopSetResult<TSchema>()\n uploadGenerationRef.current += 1\n setIsLoading(false)\n setUploadError(undefined)\n return form.set({ [key]: null } as Partial<FormValues<TSchema>>)\n }, [form, key])\n\n const onChange = useCallback(\n (event: ChangeEvent<HTMLInputElement>) => {\n const file = event.target.files?.[0]\n if (file === undefined) return\n void upload(file)\n // * Lets the user pick the same file again after a failed upload.\n event.target.value = ''\n },\n [upload],\n )\n\n if (form === null) {\n return {\n value: undefined,\n error: undefined,\n isLoading: false,\n uploadError: undefined,\n upload: async () => noopSetResult<TSchema>(),\n clear: async () => noopSetResult<TSchema>(),\n inputProps: {\n type: 'file',\n disabled: true,\n onChange: () => undefined,\n },\n }\n }\n\n return {\n value: values[key],\n error: errors[key],\n isLoading,\n uploadError,\n upload,\n clear,\n inputProps: {\n type: 'file',\n disabled: isLoading,\n onChange,\n },\n }\n}\n","import type { AnalyticsInstance } from '@embeddables/core'\nimport type { EmbeddablesInstance } from '@embeddables/core'\n\nexport function resolveCoreAnalyticsInstance(\n core: EmbeddablesInstance,\n): AnalyticsInstance | undefined {\n if (typeof core.getAnalyticsInstance !== 'function') return undefined\n return core.getAnalyticsInstance() ?? undefined\n}\n","import { initForms } from '../core/form.js'\nimport { formsByCore } from './forms-registry.js'\nimport { resolveCoreAnalyticsInstance } from './resolve-core-analytics.js'\n\nimport type { FormsClient, InitFormsOptions } from '../core/form.js'\nimport type { RegisteredSchemas } from './schema-registry.js'\n\ninterface RegisterFormsModuleClientOptions extends InitFormsOptions<RegisteredSchemas> {\n /**\n * Set when `excludeAnalyticsInModules` names Forms. The exclusion is absolute:\n * it suppresses the Core-instance fallback as well as the injected client.\n */\n disableAnalyticsFallback?: boolean\n}\n\nfunction register({\n disableAnalyticsFallback,\n ...options\n}: RegisterFormsModuleClientOptions): FormsClient<RegisteredSchemas> {\n const { core } = options\n const existing = formsByCore.get(core)\n if (existing !== undefined) return existing\n\n const analyticsInstance = disableAnalyticsFallback\n ? undefined\n : (options.analyticsInstance ?? resolveCoreAnalyticsInstance(core))\n\n const client = initForms<RegisteredSchemas>({ ...options, core, analyticsInstance })\n formsByCore.set(core, client)\n return client\n}\n\nexport function registerFormsClient(\n options: InitFormsOptions<RegisteredSchemas>,\n): FormsClient<RegisteredSchemas> {\n return register(options)\n}\n\nexport function registerFormsModuleClient(\n options: RegisterFormsModuleClientOptions,\n): FormsClient<RegisteredSchemas> {\n return register(options)\n}\n","import type { EmbeddablesReactModule } from '@embeddables/core/react'\n\nimport { FormsError } from '../errors.js'\nimport { FORMS_MODULE_KEY } from './forms-registry.js'\nimport { registerFormsModuleClient } from './register-forms-client.js'\n\nimport type { AnalyticsInstance } from '../core/analytics.js'\nimport type { FormsClient } from '../core/form.js'\nimport type { RegisteredSchemas } from './schema-registry.js'\nimport type { FormsReactOptions } from './use-forms.js'\n\nexport type { FormsReactOptions } from './use-forms.js'\n\n/**\n * What `EmbeddablesProvider` accepts under its top-level `forms` prop.\n *\n * `analyticsInstance` is deliberately absent: the provider injects the\n * Analytics module client through module dependencies, so an app never wires\n * that instance by hand.\n */\nexport type FormsProviderOptions = Omit<FormsReactOptions<RegisteredSchemas>, 'analyticsInstance'>\n\ndeclare module '@embeddables/core/react' {\n interface EmbeddablesReactModuleParameters {\n readonly forms: FormsProviderOptions\n }\n}\n\nconst ANALYTICS_MODULE_KEY = 'analytics'\n\nfunction resolveInjectedAnalytics(value: unknown): AnalyticsInstance | undefined {\n if (value === undefined) return undefined\n if (typeof value !== 'object' || value === null) {\n throw new FormsError('Forms received an invalid Analytics module dependency.')\n }\n const candidate = value as Partial<AnalyticsInstance>\n if (\n typeof candidate.trackEvent !== 'function' ||\n typeof candidate.getAppUserId !== 'function' ||\n typeof candidate.getProjectId !== 'function'\n ) {\n throw new FormsError('Forms received an invalid Analytics module dependency.')\n }\n return candidate as AnalyticsInstance\n}\n\nexport function forms(\n options: FormsReactOptions<RegisteredSchemas> = {},\n): EmbeddablesReactModule<FormsClient<RegisteredSchemas>, FormsProviderOptions> {\n return {\n key: FORMS_MODULE_KEY,\n dependencies: [{ key: ANALYTICS_MODULE_KEY, optional: true }],\n init: (core, context) => {\n const analyticsInstance = context?.analyticsExcluded\n ? undefined\n : (options.analyticsInstance ??\n resolveInjectedAnalytics(context?.dependencies.get(ANALYTICS_MODULE_KEY)))\n\n return registerFormsModuleClient({\n core,\n customValidations: context?.parameters?.customValidations ?? options.customValidations,\n serverFormData: context?.parameters?.serverFormData ?? options.serverFormData,\n analyticsInstance,\n disableAnalyticsFallback: context?.analyticsExcluded,\n })\n },\n }\n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5B,OAAO;AACT;AAEA,SAAgB,oBACd,SACgC;CAChC,OAAO,SAAS,OAAO;AACzB;AAEA,SAAgB,0BACd,SACgC;CAChC,OAAO,SAAS,OAAO;AACzB;;;ACdA,MAAM,uBAAuB;AAE7B,SAAS,yBAAyB,OAA+C;CAC/E,IAAI,UAAU,KAAA,GAAW,OAAO,KAAA;CAChC,IAAI,OAAO,UAAU,YAAY,UAAU,MACzC,MAAM,IAAIC,aAAAA,WAAW,wDAAwD;CAE/E,MAAM,YAAY;CAClB,IACE,OAAO,UAAU,eAAe,cAChC,OAAO,UAAU,iBAAiB,cAClC,OAAO,UAAU,iBAAiB,YAElC,MAAM,IAAIA,aAAAA,WAAW,wDAAwD;CAE/E,OAAO;AACT;AAEA,SAAgB,MACd,UAAgD,CAAC,GAC6B;CAC9E,OAAO;EACL,KAAK;EACL,cAAc,CAAC;GAAE,KAAK;GAAsB,UAAU;EAAK,CAAC;EAC5D,OAAO,MAAM,YAAY;GACvB,MAAM,oBAAoB,SAAS,oBAC/B,KAAA,IACC,QAAQ,qBACT,yBAAyB,SAAS,aAAa,IAAI,oBAAoB,CAAC;GAE5E,OAAO,0BAA0B;IAC/B;IACA,mBAAmB,SAAS,YAAY,qBAAqB,QAAQ;IACrE,gBAAgB,SAAS,YAAY,kBAAkB,QAAQ;IAC/D;IACA,0BAA0B,SAAS;GACrC,CAAC;EACH;CACF;AACF"}
|
package/dist/react.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import "./index-CmcgIxTH.js";
|
|
2
|
+
import { C as FormValues, S as FormSchema, a as FormSchemaMap, f as SetResult, i as FormInstance, l as InitFormsOptions, n as FieldErrors, s as FormsClient, x as FormFieldKey } from "./form-x4EJcR9A.js";
|
|
3
|
+
import { ChangeEvent } from "react";
|
|
2
4
|
import { EmbeddablesReactModule } from "@embeddables/core/react";
|
|
3
5
|
//#region src/react/use-form-store.d.ts
|
|
4
6
|
type FormSnapshot<TSchema extends FormSchema> = Readonly<{
|
|
@@ -44,9 +46,8 @@ type RegisteredSchemas = EmbeddablesSchemaRegistry extends {
|
|
|
44
46
|
* (`useForm<'signup', OtherSchemas>`) only to work against a map other than the
|
|
45
47
|
* registered one.
|
|
46
48
|
*/
|
|
47
|
-
declare function useForm<TFormId extends keyof TSchemas & string, TSchemas extends FormSchemaMap = RegisteredSchemas>({ formId
|
|
49
|
+
declare function useForm<TFormId extends keyof TSchemas & string, TSchemas extends FormSchemaMap = RegisteredSchemas>({ formId }: {
|
|
48
50
|
formId: TFormId;
|
|
49
|
-
customValidations?: CustomValidationsFor<TSchemas[TFormId]>;
|
|
50
51
|
}): {
|
|
51
52
|
form: FormInstance<TSchemas[TFormId]> | null;
|
|
52
53
|
values: ReturnType<typeof useFormSnapshot<TSchemas[TFormId]>>['values'];
|
|
@@ -63,21 +64,62 @@ declare function useFormField<const TSchema extends FormSchema, K extends FormFi
|
|
|
63
64
|
}): {
|
|
64
65
|
value: FormValues<TSchema>[K] | undefined;
|
|
65
66
|
error: readonly string[] | undefined;
|
|
66
|
-
setValue: (value: FormValues<TSchema>[K]) => void;
|
|
67
|
+
setValue: (value: FormValues<TSchema>[K] | undefined) => void;
|
|
68
|
+
commit: (value: FormValues<TSchema>[K] | undefined) => Promise<SetResult<TSchema>>;
|
|
67
69
|
onBlur: () => Promise<SetResult<TSchema>>;
|
|
68
70
|
};
|
|
69
71
|
//#endregion
|
|
72
|
+
//#region src/react/use-form-file-upload.d.ts
|
|
73
|
+
/** Keys declared with `type: file` on a schema. */
|
|
74
|
+
type FormFileFieldKey<TSchema extends FormSchema> = Extract<TSchema['fields'][number], {
|
|
75
|
+
type: 'file';
|
|
76
|
+
}>['key'];
|
|
77
|
+
/**
|
|
78
|
+
* Reactive binding for a `type: file` field: uploads through the public API,
|
|
79
|
+
* commits the returned `FormFileRef`, and exposes a native `<input type="file">`
|
|
80
|
+
* handler.
|
|
81
|
+
*/
|
|
82
|
+
declare function useFormFileUpload<const TSchema extends FormSchema, K extends FormFileFieldKey<TSchema>>({ form, key }: {
|
|
83
|
+
form: FormInstance<TSchema> | null;
|
|
84
|
+
key: K;
|
|
85
|
+
}): {
|
|
86
|
+
value: FormValues<TSchema>[K] | undefined;
|
|
87
|
+
error: readonly string[] | undefined;
|
|
88
|
+
isLoading: boolean;
|
|
89
|
+
uploadError: string | undefined;
|
|
90
|
+
upload: (file: File) => Promise<SetResult<TSchema>>;
|
|
91
|
+
clear: () => Promise<SetResult<TSchema>>;
|
|
92
|
+
inputProps: {
|
|
93
|
+
type: 'file';
|
|
94
|
+
disabled: boolean;
|
|
95
|
+
onChange: (event: ChangeEvent<HTMLInputElement>) => void;
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
//#endregion
|
|
70
99
|
//#region src/react/use-forms.d.ts
|
|
71
100
|
type FormsReactOptions<TSchemas extends FormSchemaMap = FormSchemaMap> = Omit<InitFormsOptions<TSchemas>, 'core'>;
|
|
72
101
|
//#endregion
|
|
73
102
|
//#region src/react/forms-module.d.ts
|
|
74
|
-
|
|
103
|
+
/**
|
|
104
|
+
* What `EmbeddablesProvider` accepts under its top-level `forms` prop.
|
|
105
|
+
*
|
|
106
|
+
* `analyticsInstance` is deliberately absent: the provider injects the
|
|
107
|
+
* Analytics module client through module dependencies, so an app never wires
|
|
108
|
+
* that instance by hand.
|
|
109
|
+
*/
|
|
110
|
+
type FormsProviderOptions = Omit<FormsReactOptions<RegisteredSchemas>, 'analyticsInstance'>;
|
|
111
|
+
declare module '@embeddables/core/react' {
|
|
112
|
+
interface EmbeddablesReactModuleParameters {
|
|
113
|
+
readonly forms: FormsProviderOptions;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
declare function forms(options?: FormsReactOptions<RegisteredSchemas>): EmbeddablesReactModule<FormsClient<RegisteredSchemas>, FormsProviderOptions>;
|
|
75
117
|
//#endregion
|
|
76
118
|
//#region src/react/register-forms-client.d.ts
|
|
77
|
-
declare function registerFormsClient
|
|
119
|
+
declare function registerFormsClient(options: InitFormsOptions<RegisteredSchemas>): FormsClient<RegisteredSchemas>;
|
|
78
120
|
//#endregion
|
|
79
121
|
//#region src/react/forms-registry.d.ts
|
|
80
122
|
declare function getRegisteredFormsClient(core: object): FormsClient<FormSchemaMap> | undefined;
|
|
81
123
|
//#endregion
|
|
82
|
-
export { type EmbeddablesSchemaRegistry, type FormsReactOptions, type RegisteredSchemas, forms, getRegisteredFormsClient, registerFormsClient, useForm, useFormErrors, useFormField };
|
|
124
|
+
export { type EmbeddablesSchemaRegistry, type FormFileFieldKey, type FormsProviderOptions, type FormsReactOptions, type RegisteredSchemas, forms, getRegisteredFormsClient, registerFormsClient, useForm, useFormErrors, useFormField, useFormFileUpload };
|
|
83
125
|
//# sourceMappingURL=react.d.ts.map
|
package/dist/react.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react.d.ts","names":[],"sources":["../src/react/use-form-store.ts","../src/react/schema-registry.ts","../src/react/use-form.ts","../src/react/use-form-errors.ts","../src/react/use-form-field.ts","../src/react/use-forms.ts","../src/react/forms-module.ts","../src/react/register-forms-client.ts","../src/react/forms-registry.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"react.d.ts","names":[],"sources":["../src/react/use-form-store.ts","../src/react/schema-registry.ts","../src/react/use-form.ts","../src/react/use-form-errors.ts","../src/react/use-form-field.ts","../src/react/use-form-file-upload.ts","../src/react/use-forms.ts","../src/react/forms-module.ts","../src/react/register-forms-client.ts","../src/react/forms-registry.ts"],"mappings":";;;;;KAKY,aAAa,gBAAgB,cAAc;EACrD,QAAQ,QAAQ,WAAW;EAC3B,QAAQ,YAAY;;iBAsBN,gBAAgB,gBAAgB,YAC9C,MAAM,aAAa,kBAClB,aAAa;;;;;;;;;;;;;;;;;;;;UCXC;;;;;;KAOL,oBAAoB;EAC9B,aAAa,iBAAiB;IAE5B,WACA;;;;;;;;;;;iBCjBY,QACd,sBAAsB,mBACtB,iBAAiB,gBAAgB,qBAEjC;EAEA,QAAQ;;EAER,MAAM,aAAa,SAAS;EAC5B,QAAQ,kBAAkB,gBAAgB,SAAS;EACnD,QAAQ,kBAAkB,gBAAgB,SAAS;;;;iBCnBrC,cAAc,gBAAgB,YAC5C,MAAM,aAAa,kBAClB,YAAY;;;iBCKC,mBAAmB,gBAAgB,YAAY,UAAU,aAAa,YACpF,MACA;EAEA,MAAM,aAAa;EACnB,KAAK;;EAEL,OAAO,WAAW,SAAS;EAC3B;EACA,WAAW,OAAO,WAAW,SAAS;EACtC,SAAS,OAAO,WAAW,SAAS,mBAAmB,QAAQ,UAAU;EACzE,cAAc,QAAQ,UAAU;;;;;KCbtB,iBAAiB,gBAAgB,cAAc,QACzD;EACE;;;;;;;iBAaY,wBACR,gBAAgB,YACtB,UAAU,iBAAiB,YAE3B,MACA;EAEA,MAAM,aAAa;EACnB,KAAK;;EAEL,OAAO,WAAW,SAAS;EAC3B;EACA;EACA;EACA,SAAS,MAAM,SAAS,QAAQ,UAAU;EAC1C,aAAa,QAAQ,UAAU;EAC/B;IACE;IACA;IACA,WAAW,OAAO,YAAY;;;;;KCtCtB,kBAAkB,iBAAiB,gBAAgB,iBAAiB,KAC9E,iBAAiB;;;;;;;;;;KCaP,uBAAuB,KAAK,kBAAkB;;YAG9C;aACC,OAAO;;;iBAsBJ,MACd,UAAS,kBAAkB,qBAC1B,uBAAuB,YAAY,oBAAoB;;;iBChB1C,oBACd,SAAS,iBAAiB,qBACzB,YAAY;;;iBCxBC,yBAAyB,eAAe,YAAY"}
|
package/dist/react.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { n as initForms, s as FormsError } from "./form-7wmC3G_q.js";
|
|
2
2
|
import { useCallback, useEffect, useRef, useState, useSyncExternalStore } from "react";
|
|
3
3
|
import { useEmbeddablesModule } from "@embeddables/core/react";
|
|
4
4
|
//#region src/react/use-form-store.ts
|
|
@@ -65,14 +65,9 @@ function useRegisteredFormsClient() {
|
|
|
65
65
|
* (`useForm<'signup', OtherSchemas>`) only to work against a map other than the
|
|
66
66
|
* registered one.
|
|
67
67
|
*/
|
|
68
|
-
function useForm({ formId
|
|
68
|
+
function useForm({ formId }) {
|
|
69
69
|
const client = useRegisteredFormsClient();
|
|
70
|
-
const
|
|
71
|
-
customValidationsRef.current = customValidations;
|
|
72
|
-
const form = client === null ? null : client.getForm({
|
|
73
|
-
formId,
|
|
74
|
-
customValidations: customValidationsRef.current
|
|
75
|
-
});
|
|
70
|
+
const form = client === null ? null : client.getForm({ formId });
|
|
76
71
|
const { values, errors } = useFormSnapshot(form);
|
|
77
72
|
return {
|
|
78
73
|
form,
|
|
@@ -106,6 +101,11 @@ function useFormField({ form, key }) {
|
|
|
106
101
|
const setValue = useCallback((value) => {
|
|
107
102
|
setDraft(value);
|
|
108
103
|
}, []);
|
|
104
|
+
const commit = useCallback((value) => {
|
|
105
|
+
if (form === null) return noopSetValue();
|
|
106
|
+
setDraft(value);
|
|
107
|
+
return form.set({ [key]: value });
|
|
108
|
+
}, [form, key]);
|
|
109
109
|
const onBlur = useCallback(async () => {
|
|
110
110
|
if (form === null) return noopSetValue();
|
|
111
111
|
if (draft === committed) return {
|
|
@@ -123,16 +123,111 @@ function useFormField({ form, key }) {
|
|
|
123
123
|
value: void 0,
|
|
124
124
|
error: void 0,
|
|
125
125
|
setValue: () => void 0,
|
|
126
|
+
commit: async () => noopSetValue(),
|
|
126
127
|
onBlur: async () => noopSetValue()
|
|
127
128
|
};
|
|
128
129
|
return {
|
|
129
130
|
value: draft,
|
|
130
131
|
error: errors[key],
|
|
131
132
|
setValue,
|
|
133
|
+
commit,
|
|
132
134
|
onBlur
|
|
133
135
|
};
|
|
134
136
|
}
|
|
135
137
|
//#endregion
|
|
138
|
+
//#region src/react/use-form-file-upload.ts
|
|
139
|
+
const noopSetResult = async () => ({
|
|
140
|
+
ok: false,
|
|
141
|
+
errors: {}
|
|
142
|
+
});
|
|
143
|
+
/**
|
|
144
|
+
* Reactive binding for a `type: file` field: uploads through the public API,
|
|
145
|
+
* commits the returned `FormFileRef`, and exposes a native `<input type="file">`
|
|
146
|
+
* handler.
|
|
147
|
+
*/
|
|
148
|
+
function useFormFileUpload({ form, key }) {
|
|
149
|
+
const { values, errors } = useFormSnapshot(form);
|
|
150
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
151
|
+
const [uploadError, setUploadError] = useState(void 0);
|
|
152
|
+
const uploadGenerationRef = useRef(0);
|
|
153
|
+
useEffect(() => {
|
|
154
|
+
return () => {
|
|
155
|
+
uploadGenerationRef.current += 1;
|
|
156
|
+
setIsLoading(false);
|
|
157
|
+
setUploadError(void 0);
|
|
158
|
+
};
|
|
159
|
+
}, [form, key]);
|
|
160
|
+
const upload = useCallback(async (file) => {
|
|
161
|
+
if (form === null) return noopSetResult();
|
|
162
|
+
const generation = ++uploadGenerationRef.current;
|
|
163
|
+
setIsLoading(true);
|
|
164
|
+
setUploadError(void 0);
|
|
165
|
+
try {
|
|
166
|
+
const ref = await form.uploadFile({
|
|
167
|
+
key,
|
|
168
|
+
file
|
|
169
|
+
});
|
|
170
|
+
if (generation !== uploadGenerationRef.current) return {
|
|
171
|
+
ok: false,
|
|
172
|
+
errors: {}
|
|
173
|
+
};
|
|
174
|
+
return await form.set({ [key]: ref });
|
|
175
|
+
} catch (error) {
|
|
176
|
+
if (generation !== uploadGenerationRef.current) return {
|
|
177
|
+
ok: false,
|
|
178
|
+
errors: {}
|
|
179
|
+
};
|
|
180
|
+
const message = error instanceof FormsError ? error.message : "File upload failed.";
|
|
181
|
+
setUploadError(message);
|
|
182
|
+
return {
|
|
183
|
+
ok: false,
|
|
184
|
+
errors: {}
|
|
185
|
+
};
|
|
186
|
+
} finally {
|
|
187
|
+
if (generation === uploadGenerationRef.current) setIsLoading(false);
|
|
188
|
+
}
|
|
189
|
+
}, [form, key]);
|
|
190
|
+
const clear = useCallback(async () => {
|
|
191
|
+
if (form === null) return noopSetResult();
|
|
192
|
+
uploadGenerationRef.current += 1;
|
|
193
|
+
setIsLoading(false);
|
|
194
|
+
setUploadError(void 0);
|
|
195
|
+
return form.set({ [key]: null });
|
|
196
|
+
}, [form, key]);
|
|
197
|
+
const onChange = useCallback((event) => {
|
|
198
|
+
const file = event.target.files?.[0];
|
|
199
|
+
if (file === void 0) return;
|
|
200
|
+
upload(file);
|
|
201
|
+
event.target.value = "";
|
|
202
|
+
}, [upload]);
|
|
203
|
+
if (form === null) return {
|
|
204
|
+
value: void 0,
|
|
205
|
+
error: void 0,
|
|
206
|
+
isLoading: false,
|
|
207
|
+
uploadError: void 0,
|
|
208
|
+
upload: async () => noopSetResult(),
|
|
209
|
+
clear: async () => noopSetResult(),
|
|
210
|
+
inputProps: {
|
|
211
|
+
type: "file",
|
|
212
|
+
disabled: true,
|
|
213
|
+
onChange: () => void 0
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
return {
|
|
217
|
+
value: values[key],
|
|
218
|
+
error: errors[key],
|
|
219
|
+
isLoading,
|
|
220
|
+
uploadError,
|
|
221
|
+
upload,
|
|
222
|
+
clear,
|
|
223
|
+
inputProps: {
|
|
224
|
+
type: "file",
|
|
225
|
+
disabled: isLoading,
|
|
226
|
+
onChange
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
//#endregion
|
|
136
231
|
//#region src/react/resolve-core-analytics.ts
|
|
137
232
|
function resolveCoreAnalyticsInstance(core) {
|
|
138
233
|
if (typeof core.getAnalyticsInstance !== "function") return void 0;
|
|
@@ -140,10 +235,11 @@ function resolveCoreAnalyticsInstance(core) {
|
|
|
140
235
|
}
|
|
141
236
|
//#endregion
|
|
142
237
|
//#region src/react/register-forms-client.ts
|
|
143
|
-
function
|
|
238
|
+
function register({ disableAnalyticsFallback, ...options }) {
|
|
239
|
+
const { core } = options;
|
|
144
240
|
const existing = formsByCore.get(core);
|
|
145
241
|
if (existing !== void 0) return existing;
|
|
146
|
-
const analyticsInstance = options.analyticsInstance ?? resolveCoreAnalyticsInstance(core);
|
|
242
|
+
const analyticsInstance = disableAnalyticsFallback ? void 0 : options.analyticsInstance ?? resolveCoreAnalyticsInstance(core);
|
|
147
243
|
const client = initForms({
|
|
148
244
|
...options,
|
|
149
245
|
core,
|
|
@@ -152,18 +248,42 @@ function registerFormsClient({ core, ...options }) {
|
|
|
152
248
|
formsByCore.set(core, client);
|
|
153
249
|
return client;
|
|
154
250
|
}
|
|
251
|
+
function registerFormsClient(options) {
|
|
252
|
+
return register(options);
|
|
253
|
+
}
|
|
254
|
+
function registerFormsModuleClient(options) {
|
|
255
|
+
return register(options);
|
|
256
|
+
}
|
|
155
257
|
//#endregion
|
|
156
258
|
//#region src/react/forms-module.ts
|
|
157
|
-
|
|
259
|
+
const ANALYTICS_MODULE_KEY = "analytics";
|
|
260
|
+
function resolveInjectedAnalytics(value) {
|
|
261
|
+
if (value === void 0) return void 0;
|
|
262
|
+
if (typeof value !== "object" || value === null) throw new FormsError("Forms received an invalid Analytics module dependency.");
|
|
263
|
+
const candidate = value;
|
|
264
|
+
if (typeof candidate.trackEvent !== "function" || typeof candidate.getAppUserId !== "function" || typeof candidate.getProjectId !== "function") throw new FormsError("Forms received an invalid Analytics module dependency.");
|
|
265
|
+
return candidate;
|
|
266
|
+
}
|
|
267
|
+
function forms(options = {}) {
|
|
158
268
|
return {
|
|
159
269
|
key: FORMS_MODULE_KEY,
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
}
|
|
270
|
+
dependencies: [{
|
|
271
|
+
key: ANALYTICS_MODULE_KEY,
|
|
272
|
+
optional: true
|
|
273
|
+
}],
|
|
274
|
+
init: (core, context) => {
|
|
275
|
+
const analyticsInstance = context?.analyticsExcluded ? void 0 : options.analyticsInstance ?? resolveInjectedAnalytics(context?.dependencies.get(ANALYTICS_MODULE_KEY));
|
|
276
|
+
return registerFormsModuleClient({
|
|
277
|
+
core,
|
|
278
|
+
customValidations: context?.parameters?.customValidations ?? options.customValidations,
|
|
279
|
+
serverFormData: context?.parameters?.serverFormData ?? options.serverFormData,
|
|
280
|
+
analyticsInstance,
|
|
281
|
+
disableAnalyticsFallback: context?.analyticsExcluded
|
|
282
|
+
});
|
|
283
|
+
}
|
|
164
284
|
};
|
|
165
285
|
}
|
|
166
286
|
//#endregion
|
|
167
|
-
export { forms, getRegisteredFormsClient, registerFormsClient, useForm, useFormErrors, useFormField };
|
|
287
|
+
export { forms, getRegisteredFormsClient, registerFormsClient, useForm, useFormErrors, useFormField, useFormFileUpload };
|
|
168
288
|
|
|
169
289
|
//# sourceMappingURL=react.js.map
|
package/dist/react.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react.js","names":[],"sources":["../src/react/use-form-store.ts","../src/react/forms-registry.ts","../src/react/use-forms.ts","../src/react/use-form.ts","../src/react/use-form-errors.ts","../src/react/use-form-field.ts","../src/react/resolve-core-analytics.ts","../src/react/register-forms-client.ts","../src/react/forms-module.ts"],"sourcesContent":["import { useCallback, useRef, useSyncExternalStore } from 'react'\n\nimport type { FormSchema, FormValues } from '../core/config.js'\nimport type { FieldErrors, FormInstance } from '../core/form.js'\n\nexport type FormSnapshot<TSchema extends FormSchema> = Readonly<{\n values: Partial<FormValues<TSchema>>\n errors: FieldErrors<TSchema>\n}>\n\nconst EMPTY_SNAPSHOT: FormSnapshot<FormSchema> = Object.freeze({\n values: {},\n errors: {},\n})\n\nfunction createSnapshot<TSchema extends FormSchema>(\n form: FormInstance<TSchema>,\n): FormSnapshot<TSchema> {\n return Object.freeze({\n values: form.getAll(),\n errors: form.errors(),\n })\n}\n\ninterface CachedSnapshot<TSchema extends FormSchema> {\n readonly form: FormInstance<TSchema>\n readonly snapshot: FormSnapshot<TSchema>\n}\n\nexport function useFormSnapshot<TSchema extends FormSchema>(\n form: FormInstance<TSchema> | null,\n): FormSnapshot<TSchema> {\n const cachedRef = useRef<CachedSnapshot<TSchema> | null>(null)\n const emptyRef = useRef<FormSnapshot<TSchema>>(EMPTY_SNAPSHOT as FormSnapshot<TSchema>)\n\n const subscribe = useCallback(\n (listener: () => void) => {\n if (form === null) return () => undefined\n return form.subscribe(() => {\n cachedRef.current = { form, snapshot: createSnapshot(form) }\n listener()\n })\n },\n [form],\n )\n\n const getSnapshot = useCallback((): FormSnapshot<TSchema> => {\n if (form === null) {\n // * Otherwise a later A -> null -> mutate A -> A sequence would resurrect\n // * a snapshot cached before the detach, missing the mutation in between.\n cachedRef.current = null\n return emptyRef.current\n }\n // * A cached snapshot from a previous form instance must not survive a\n // * form change — recompute so switching forms without a mutation still\n // * returns the new form's values and errors.\n if (cachedRef.current === null || cachedRef.current.form !== form) {\n cachedRef.current = { form, snapshot: createSnapshot(form) }\n }\n return cachedRef.current.snapshot\n }, [form])\n\n return useSyncExternalStore(subscribe, getSnapshot, getSnapshot)\n}\n","import type { FormSchemaMap, FormsClient } from '../core/form.js'\n\nexport const FORMS_MODULE_KEY = 'forms'\n\n/**\n * Idempotence guard for `registerFormsClient` only — React reads the client\n * from provider context, not from here.\n */\nexport const formsByCore = new WeakMap<object, FormsClient<FormSchemaMap>>()\n\nexport function getRegisteredFormsClient(core: object): FormsClient<FormSchemaMap> | undefined {\n return formsByCore.get(core)\n}\n","import { useEmbeddablesModule } from '@embeddables/core/react'\n\nimport { FORMS_MODULE_KEY } from './forms-registry.js'\n\nimport type { FormSchemaMap, FormsClient, InitFormsOptions } from '../core/form.js'\n\nexport type FormsReactOptions<TSchemas extends FormSchemaMap = FormSchemaMap> = Omit<\n InitFormsOptions<TSchemas>,\n 'core'\n>\n\n/** @internal Product hooks read the module client from provider context. */\nexport function useRegisteredFormsClient<\n TSchemas extends FormSchemaMap = FormSchemaMap,\n>(): FormsClient<TSchemas> | null {\n return useEmbeddablesModule<FormsClient<TSchemas>>({ key: FORMS_MODULE_KEY })\n}\n","import { useRef } from 'react'\n\nimport { useFormSnapshot } from './use-form-store.js'\nimport { useRegisteredFormsClient } from './use-forms.js'\n\nimport type { CustomValidationsFor, FormInstance, FormSchemaMap } from '../core/form.js'\nimport type { RegisteredSchemas } from './schema-registry.js'\n\n/**\n * Reactive binding for one declared form.\n *\n * Takes no type arguments: `TFormId` is inferred from `formId`, and the schema\n * map comes from the registry `em build` augments. Pass both explicitly\n * (`useForm<'signup', OtherSchemas>`) only to work against a map other than the\n * registered one.\n */\nexport function useForm<\n TFormId extends keyof TSchemas & string,\n TSchemas extends FormSchemaMap = RegisteredSchemas,\n>({\n formId,\n customValidations,\n}: {\n formId: TFormId\n customValidations?: CustomValidationsFor<TSchemas[TFormId]>\n}): {\n form: FormInstance<TSchemas[TFormId]> | null\n values: ReturnType<typeof useFormSnapshot<TSchemas[TFormId]>>['values']\n errors: ReturnType<typeof useFormSnapshot<TSchemas[TFormId]>>['errors']\n} {\n const client = useRegisteredFormsClient<TSchemas>()\n // * Held in a ref because only the first call for a form id builds the\n // * instance; a later render passing new validators must not look like a change.\n const customValidationsRef = useRef(customValidations)\n customValidationsRef.current = customValidations\n\n // * Safe to call on every render: the client returns one instance per form id.\n const form =\n client === null\n ? null\n : client.getForm({ formId, customValidations: customValidationsRef.current })\n const { values, errors } = useFormSnapshot(form)\n\n return { form, values, errors }\n}\n","import { useFormSnapshot } from './use-form-store.js'\n\nimport type { FormSchema } from '../core/config.js'\nimport type { FieldErrors, FormInstance } from '../core/form.js'\n\nexport function useFormErrors<TSchema extends FormSchema>(\n form: FormInstance<TSchema> | null,\n): FieldErrors<TSchema> {\n const { errors } = useFormSnapshot(form)\n return errors\n}\n","import { useCallback, useEffect, useState } from 'react'\n\nimport { useFormSnapshot } from './use-form-store.js'\n\nimport type { FormFieldKey, FormSchema, FormValues } from '../core/config.js'\nimport type { FieldErrors, FormInstance, SetResult } from '../core/form.js'\n\nconst noopSetValue = async (): Promise<SetResult<FormSchema>> => ({\n ok: false,\n errors: {},\n})\n\nexport function useFormField<const TSchema extends FormSchema, K extends FormFieldKey<TSchema>>({\n form,\n key,\n}: {\n form: FormInstance<TSchema> | null\n key: K\n}): {\n value: FormValues<TSchema>[K] | undefined\n error: readonly string[] | undefined\n setValue: (value: FormValues<TSchema>[K]) => void\n onBlur: () => Promise<SetResult<TSchema>>\n} {\n const { values, errors } = useFormSnapshot(form)\n const committed = values[key]\n const [draft, setDraft] = useState<FormValues<TSchema>[K] | undefined>(committed)\n\n useEffect(() => {\n setDraft(committed)\n }, [form, key, committed])\n\n const setValue = useCallback((value: FormValues<TSchema>[K]) => {\n setDraft(value)\n }, [])\n\n const onBlur = useCallback(async (): Promise<SetResult<TSchema>> => {\n if (form === null) return noopSetValue()\n if (draft === committed) {\n return { ok: true, errors: {} as FieldErrors<TSchema> }\n }\n return form.set({ [key]: draft } as Partial<FormValues<TSchema>>)\n }, [committed, draft, form, key])\n\n if (form === null) {\n return {\n value: undefined,\n error: undefined,\n setValue: () => undefined,\n onBlur: async () => noopSetValue(),\n }\n }\n\n return {\n value: draft,\n error: errors[key],\n setValue,\n onBlur,\n }\n}\n","import type { AnalyticsInstance } from '@embeddables/core'\nimport type { EmbeddablesInstance } from '@embeddables/core'\n\nexport function resolveCoreAnalyticsInstance(\n core: EmbeddablesInstance,\n): AnalyticsInstance | undefined {\n if (typeof core.getAnalyticsInstance !== 'function') return undefined\n return core.getAnalyticsInstance() ?? undefined\n}\n","import { initForms } from '../core/form.js'\nimport { formsByCore } from './forms-registry.js'\nimport { resolveCoreAnalyticsInstance } from './resolve-core-analytics.js'\n\nimport type { FormSchemaMap, FormsClient, InitFormsOptions } from '../core/form.js'\n\nexport function registerFormsClient<const TSchemas extends FormSchemaMap>({\n core,\n ...options\n}: InitFormsOptions<TSchemas>): FormsClient<TSchemas> {\n const existing = formsByCore.get(core) as FormsClient<TSchemas> | undefined\n if (existing !== undefined) return existing\n\n const analyticsInstance = options.analyticsInstance ?? resolveCoreAnalyticsInstance(core)\n\n const client = initForms({ ...options, core, analyticsInstance })\n formsByCore.set(core, client as FormsClient<FormSchemaMap>)\n return client\n}\n","import type { EmbeddablesReactModule } from '@embeddables/core/react'\n\nimport { FORMS_MODULE_KEY } from './forms-registry.js'\nimport { registerFormsClient } from './register-forms-client.js'\n\nimport type { FormSchemaMap, FormsClient } from '../core/form.js'\nimport type { FormsReactOptions } from './use-forms.js'\n\nexport type { FormsReactOptions } from './use-forms.js'\n\nexport function forms<const TSchemas extends FormSchemaMap>(\n options: FormsReactOptions<TSchemas>,\n): EmbeddablesReactModule<FormsClient<TSchemas>> {\n return {\n key: FORMS_MODULE_KEY,\n init: (core) => registerFormsClient({ core, ...options }),\n }\n}\n"],"mappings":";;;;AAUA,MAAM,iBAA2C,OAAO,OAAO;CAC7D,QAAQ,CAAC;CACT,QAAQ,CAAC;AACX,CAAC;AAED,SAAS,eACP,MACuB;CACvB,OAAO,OAAO,OAAO;EACnB,QAAQ,KAAK,OAAO;EACpB,QAAQ,KAAK,OAAO;CACtB,CAAC;AACH;AAOA,SAAgB,gBACd,MACuB;CACvB,MAAM,YAAY,OAAuC,IAAI;CAC7D,MAAM,WAAW,OAA8B,cAAuC;CAEtF,MAAM,YAAY,aACf,aAAyB;EACxB,IAAI,SAAS,MAAM,aAAa,KAAA;EAChC,OAAO,KAAK,gBAAgB;GAC1B,UAAU,UAAU;IAAE;IAAM,UAAU,eAAe,IAAI;GAAE;GAC3D,SAAS;EACX,CAAC;CACH,GACA,CAAC,IAAI,CACP;CAEA,MAAM,cAAc,kBAAyC;EAC3D,IAAI,SAAS,MAAM;GAGjB,UAAU,UAAU;GACpB,OAAO,SAAS;EAClB;EAIA,IAAI,UAAU,YAAY,QAAQ,UAAU,QAAQ,SAAS,MAC3D,UAAU,UAAU;GAAE;GAAM,UAAU,eAAe,IAAI;EAAE;EAE7D,OAAO,UAAU,QAAQ;CAC3B,GAAG,CAAC,IAAI,CAAC;CAET,OAAO,qBAAqB,WAAW,aAAa,WAAW;AACjE;;;AC7DA,MAAa,mBAAmB;;;;;AAMhC,MAAa,8BAAc,IAAI,QAA4C;AAE3E,SAAgB,yBAAyB,MAAsD;CAC7F,OAAO,YAAY,IAAI,IAAI;AAC7B;;;;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ACF;AACF;;;ACxDA,SAAgB,6BACd,MAC+B;CAC/B,IAAI,OAAO,KAAK,yBAAyB,YAAY,OAAO,KAAA;CAC5D,OAAO,KAAK,qBAAqB,KAAK,KAAA;AACxC;;;ACFA,SAAgB,oBAA0D,EACxE,MACA,GAAG,WACiD;CACpD,MAAM,WAAW,YAAY,IAAI,IAAI;CACrC,IAAI,aAAa,KAAA,GAAW,OAAO;CAEnC,MAAM,oBAAoB,QAAQ,qBAAqB,6BAA6B,IAAI;CAExF,MAAM,SAAS,UAAU;EAAE,GAAG;EAAS;EAAM;CAAkB,CAAC;CAChE,YAAY,IAAI,MAAM,MAAoC;CAC1D,OAAO;AACT;;;ACRA,SAAgB,MACd,SAC+C;CAC/C,OAAO;EACL,KAAK;EACL,OAAO,SAAS,oBAAoB;GAAE;GAAM,GAAG;EAAQ,CAAC;CAC1D;AACF"}
|
|
1
|
+
{"version":3,"file":"react.js","names":[],"sources":["../src/react/use-form-store.ts","../src/react/forms-registry.ts","../src/react/use-forms.ts","../src/react/use-form.ts","../src/react/use-form-errors.ts","../src/react/use-form-field.ts","../src/react/use-form-file-upload.ts","../src/react/resolve-core-analytics.ts","../src/react/register-forms-client.ts","../src/react/forms-module.ts"],"sourcesContent":["import { useCallback, useRef, useSyncExternalStore } from 'react'\n\nimport type { FormSchema, FormValues } from '../core/config.js'\nimport type { FieldErrors, FormInstance } from '../core/form.js'\n\nexport type FormSnapshot<TSchema extends FormSchema> = Readonly<{\n values: Partial<FormValues<TSchema>>\n errors: FieldErrors<TSchema>\n}>\n\nconst EMPTY_SNAPSHOT: FormSnapshot<FormSchema> = Object.freeze({\n values: {},\n errors: {},\n})\n\nfunction createSnapshot<TSchema extends FormSchema>(\n form: FormInstance<TSchema>,\n): FormSnapshot<TSchema> {\n return Object.freeze({\n values: form.getAll(),\n errors: form.errors(),\n })\n}\n\ninterface CachedSnapshot<TSchema extends FormSchema> {\n readonly form: FormInstance<TSchema>\n readonly snapshot: FormSnapshot<TSchema>\n}\n\nexport function useFormSnapshot<TSchema extends FormSchema>(\n form: FormInstance<TSchema> | null,\n): FormSnapshot<TSchema> {\n const cachedRef = useRef<CachedSnapshot<TSchema> | null>(null)\n const emptyRef = useRef<FormSnapshot<TSchema>>(EMPTY_SNAPSHOT as FormSnapshot<TSchema>)\n\n const subscribe = useCallback(\n (listener: () => void) => {\n if (form === null) return () => undefined\n return form.subscribe(() => {\n cachedRef.current = { form, snapshot: createSnapshot(form) }\n listener()\n })\n },\n [form],\n )\n\n const getSnapshot = useCallback((): FormSnapshot<TSchema> => {\n if (form === null) {\n // * Otherwise a later A -> null -> mutate A -> A sequence would resurrect\n // * a snapshot cached before the detach, missing the mutation in between.\n cachedRef.current = null\n return emptyRef.current\n }\n // * A cached snapshot from a previous form instance must not survive a\n // * form change — recompute so switching forms without a mutation still\n // * returns the new form's values and errors.\n if (cachedRef.current === null || cachedRef.current.form !== form) {\n cachedRef.current = { form, snapshot: createSnapshot(form) }\n }\n return cachedRef.current.snapshot\n }, [form])\n\n return useSyncExternalStore(subscribe, getSnapshot, getSnapshot)\n}\n","import type { FormSchemaMap, FormsClient } from '../core/form.js'\n\nexport const FORMS_MODULE_KEY = 'forms'\n\n/**\n * Idempotence guard for `registerFormsClient` only — React reads the client\n * from provider context, not from here.\n */\nexport const formsByCore = new WeakMap<object, FormsClient<FormSchemaMap>>()\n\nexport function getRegisteredFormsClient(core: object): FormsClient<FormSchemaMap> | undefined {\n return formsByCore.get(core)\n}\n","import { useEmbeddablesModule } from '@embeddables/core/react'\n\nimport { FORMS_MODULE_KEY } from './forms-registry.js'\n\nimport type { FormSchemaMap, FormsClient, InitFormsOptions } from '../core/form.js'\n\nexport type FormsReactOptions<TSchemas extends FormSchemaMap = FormSchemaMap> = Omit<\n InitFormsOptions<TSchemas>,\n 'core'\n>\n\n/** @internal Product hooks read the module client from provider context. */\nexport function useRegisteredFormsClient<\n TSchemas extends FormSchemaMap = FormSchemaMap,\n>(): FormsClient<TSchemas> | null {\n return useEmbeddablesModule<FormsClient<TSchemas>>({ key: FORMS_MODULE_KEY })\n}\n","import { useFormSnapshot } from './use-form-store.js'\nimport { useRegisteredFormsClient } from './use-forms.js'\n\nimport type { FormInstance, FormSchemaMap } from '../core/form.js'\nimport type { RegisteredSchemas } from './schema-registry.js'\n\n/**\n * Reactive binding for one declared form.\n *\n * Takes no type arguments: `TFormId` is inferred from `formId`, and the schema\n * map comes from the registry `em build` augments. Pass both explicitly\n * (`useForm<'signup', OtherSchemas>`) only to work against a map other than the\n * registered one.\n */\nexport function useForm<\n TFormId extends keyof TSchemas & string,\n TSchemas extends FormSchemaMap = RegisteredSchemas,\n>({\n formId,\n}: {\n formId: TFormId\n}): {\n form: FormInstance<TSchemas[TFormId]> | null\n values: ReturnType<typeof useFormSnapshot<TSchemas[TFormId]>>['values']\n errors: ReturnType<typeof useFormSnapshot<TSchemas[TFormId]>>['errors']\n} {\n const client = useRegisteredFormsClient<TSchemas>()\n\n // * Safe to call on every render: the client returns one instance per form id.\n const form = client === null ? null : client.getForm({ formId })\n const { values, errors } = useFormSnapshot(form)\n\n return { form, values, errors }\n}\n","import { useFormSnapshot } from './use-form-store.js'\n\nimport type { FormSchema } from '../core/config.js'\nimport type { FieldErrors, FormInstance } from '../core/form.js'\n\nexport function useFormErrors<TSchema extends FormSchema>(\n form: FormInstance<TSchema> | null,\n): FieldErrors<TSchema> {\n const { errors } = useFormSnapshot(form)\n return errors\n}\n","import { useCallback, useEffect, useState } from 'react'\n\nimport { useFormSnapshot } from './use-form-store.js'\n\nimport type { FormFieldKey, FormSchema, FormValues } from '../core/config.js'\nimport type { FieldErrors, FormInstance, SetResult } from '../core/form.js'\n\nconst noopSetValue = async (): Promise<SetResult<FormSchema>> => ({\n ok: false,\n errors: {},\n})\n\nexport function useFormField<const TSchema extends FormSchema, K extends FormFieldKey<TSchema>>({\n form,\n key,\n}: {\n form: FormInstance<TSchema> | null\n key: K\n}): {\n value: FormValues<TSchema>[K] | undefined\n error: readonly string[] | undefined\n setValue: (value: FormValues<TSchema>[K] | undefined) => void\n commit: (value: FormValues<TSchema>[K] | undefined) => Promise<SetResult<TSchema>>\n onBlur: () => Promise<SetResult<TSchema>>\n} {\n const { values, errors } = useFormSnapshot(form)\n const committed = values[key]\n const [draft, setDraft] = useState<FormValues<TSchema>[K] | undefined>(committed)\n\n useEffect(() => {\n setDraft(committed)\n }, [form, key, committed])\n\n const setValue = useCallback((value: FormValues<TSchema>[K] | undefined) => {\n setDraft(value)\n }, [])\n\n // * Takes the value as an argument rather than reading `draft`, so it is\n // * correct when called in the same tick as the change that produced it.\n // * `onBlur` cannot be: it compares the `draft`/`committed` pair captured in\n // * the current render, which a same-tick `setValue` has not updated yet.\n const commit = useCallback(\n (value: FormValues<TSchema>[K] | undefined): Promise<SetResult<TSchema>> => {\n if (form === null) return noopSetValue()\n // * Keeps the control responsive while `set` runs, and keeps a rejected\n // * value on screen next to its error instead of snapping back.\n setDraft(value)\n return form.set({ [key]: value } as Partial<FormValues<TSchema>>)\n },\n [form, key],\n )\n\n const onBlur = useCallback(async (): Promise<SetResult<TSchema>> => {\n if (form === null) return noopSetValue()\n if (draft === committed) {\n return { ok: true, errors: {} as FieldErrors<TSchema> }\n }\n return form.set({ [key]: draft } as Partial<FormValues<TSchema>>)\n }, [committed, draft, form, key])\n\n if (form === null) {\n return {\n value: undefined,\n error: undefined,\n setValue: () => undefined,\n commit: async () => noopSetValue(),\n onBlur: async () => noopSetValue(),\n }\n }\n\n return {\n value: draft,\n error: errors[key],\n setValue,\n commit,\n onBlur,\n }\n}\n","import { useCallback, useEffect, useRef, useState } from 'react'\n\nimport { FormsError } from '../errors.js'\nimport { useFormSnapshot } from './use-form-store.js'\n\nimport type { FormSchema, FormValues } from '../core/config.js'\nimport type { FieldErrors, FormInstance, SetResult } from '../core/form.js'\nimport type { ChangeEvent } from 'react'\n\n/** Keys declared with `type: file` on a schema. */\nexport type FormFileFieldKey<TSchema extends FormSchema> = Extract<\n TSchema['fields'][number],\n { type: 'file' }\n>['key']\n\nconst noopSetResult = async <TSchema extends FormSchema>(): Promise<SetResult<TSchema>> => ({\n ok: false,\n errors: {} as FieldErrors<TSchema>,\n})\n\n/**\n * Reactive binding for a `type: file` field: uploads through the public API,\n * commits the returned `FormFileRef`, and exposes a native `<input type=\"file\">`\n * handler.\n */\nexport function useFormFileUpload<\n const TSchema extends FormSchema,\n K extends FormFileFieldKey<TSchema>,\n>({\n form,\n key,\n}: {\n form: FormInstance<TSchema> | null\n key: K\n}): {\n value: FormValues<TSchema>[K] | undefined\n error: readonly string[] | undefined\n isLoading: boolean\n uploadError: string | undefined\n upload: (file: File) => Promise<SetResult<TSchema>>\n clear: () => Promise<SetResult<TSchema>>\n inputProps: {\n type: 'file'\n disabled: boolean\n onChange: (event: ChangeEvent<HTMLInputElement>) => void\n }\n} {\n const { values, errors } = useFormSnapshot(form)\n const [isLoading, setIsLoading] = useState(false)\n const [uploadError, setUploadError] = useState<string | undefined>(undefined)\n const uploadGenerationRef = useRef(0)\n\n useEffect(() => {\n return () => {\n uploadGenerationRef.current += 1\n setIsLoading(false)\n setUploadError(undefined)\n }\n }, [form, key])\n\n const upload = useCallback(\n async (file: File): Promise<SetResult<TSchema>> => {\n if (form === null) return noopSetResult<TSchema>()\n\n const generation = ++uploadGenerationRef.current\n setIsLoading(true)\n setUploadError(undefined)\n try {\n const ref = await form.uploadFile({ key, file })\n if (generation !== uploadGenerationRef.current) {\n return { ok: false, errors: {} as FieldErrors<TSchema> }\n }\n return await form.set({ [key]: ref } as Partial<FormValues<TSchema>>)\n } catch (error) {\n if (generation !== uploadGenerationRef.current) {\n return { ok: false, errors: {} as FieldErrors<TSchema> }\n }\n const message = error instanceof FormsError ? error.message : 'File upload failed.'\n setUploadError(message)\n return { ok: false, errors: {} as FieldErrors<TSchema> }\n } finally {\n if (generation === uploadGenerationRef.current) {\n setIsLoading(false)\n }\n }\n },\n [form, key],\n )\n\n const clear = useCallback(async (): Promise<SetResult<TSchema>> => {\n if (form === null) return noopSetResult<TSchema>()\n uploadGenerationRef.current += 1\n setIsLoading(false)\n setUploadError(undefined)\n return form.set({ [key]: null } as Partial<FormValues<TSchema>>)\n }, [form, key])\n\n const onChange = useCallback(\n (event: ChangeEvent<HTMLInputElement>) => {\n const file = event.target.files?.[0]\n if (file === undefined) return\n void upload(file)\n // * Lets the user pick the same file again after a failed upload.\n event.target.value = ''\n },\n [upload],\n )\n\n if (form === null) {\n return {\n value: undefined,\n error: undefined,\n isLoading: false,\n uploadError: undefined,\n upload: async () => noopSetResult<TSchema>(),\n clear: async () => noopSetResult<TSchema>(),\n inputProps: {\n type: 'file',\n disabled: true,\n onChange: () => undefined,\n },\n }\n }\n\n return {\n value: values[key],\n error: errors[key],\n isLoading,\n uploadError,\n upload,\n clear,\n inputProps: {\n type: 'file',\n disabled: isLoading,\n onChange,\n },\n }\n}\n","import type { AnalyticsInstance } from '@embeddables/core'\nimport type { EmbeddablesInstance } from '@embeddables/core'\n\nexport function resolveCoreAnalyticsInstance(\n core: EmbeddablesInstance,\n): AnalyticsInstance | undefined {\n if (typeof core.getAnalyticsInstance !== 'function') return undefined\n return core.getAnalyticsInstance() ?? undefined\n}\n","import { initForms } from '../core/form.js'\nimport { formsByCore } from './forms-registry.js'\nimport { resolveCoreAnalyticsInstance } from './resolve-core-analytics.js'\n\nimport type { FormsClient, InitFormsOptions } from '../core/form.js'\nimport type { RegisteredSchemas } from './schema-registry.js'\n\ninterface RegisterFormsModuleClientOptions extends InitFormsOptions<RegisteredSchemas> {\n /**\n * Set when `excludeAnalyticsInModules` names Forms. The exclusion is absolute:\n * it suppresses the Core-instance fallback as well as the injected client.\n */\n disableAnalyticsFallback?: boolean\n}\n\nfunction register({\n disableAnalyticsFallback,\n ...options\n}: RegisterFormsModuleClientOptions): FormsClient<RegisteredSchemas> {\n const { core } = options\n const existing = formsByCore.get(core)\n if (existing !== undefined) return existing\n\n const analyticsInstance = disableAnalyticsFallback\n ? undefined\n : (options.analyticsInstance ?? resolveCoreAnalyticsInstance(core))\n\n const client = initForms<RegisteredSchemas>({ ...options, core, analyticsInstance })\n formsByCore.set(core, client)\n return client\n}\n\nexport function registerFormsClient(\n options: InitFormsOptions<RegisteredSchemas>,\n): FormsClient<RegisteredSchemas> {\n return register(options)\n}\n\nexport function registerFormsModuleClient(\n options: RegisterFormsModuleClientOptions,\n): FormsClient<RegisteredSchemas> {\n return register(options)\n}\n","import type { EmbeddablesReactModule } from '@embeddables/core/react'\n\nimport { FormsError } from '../errors.js'\nimport { FORMS_MODULE_KEY } from './forms-registry.js'\nimport { registerFormsModuleClient } from './register-forms-client.js'\n\nimport type { AnalyticsInstance } from '../core/analytics.js'\nimport type { FormsClient } from '../core/form.js'\nimport type { RegisteredSchemas } from './schema-registry.js'\nimport type { FormsReactOptions } from './use-forms.js'\n\nexport type { FormsReactOptions } from './use-forms.js'\n\n/**\n * What `EmbeddablesProvider` accepts under its top-level `forms` prop.\n *\n * `analyticsInstance` is deliberately absent: the provider injects the\n * Analytics module client through module dependencies, so an app never wires\n * that instance by hand.\n */\nexport type FormsProviderOptions = Omit<FormsReactOptions<RegisteredSchemas>, 'analyticsInstance'>\n\ndeclare module '@embeddables/core/react' {\n interface EmbeddablesReactModuleParameters {\n readonly forms: FormsProviderOptions\n }\n}\n\nconst ANALYTICS_MODULE_KEY = 'analytics'\n\nfunction resolveInjectedAnalytics(value: unknown): AnalyticsInstance | undefined {\n if (value === undefined) return undefined\n if (typeof value !== 'object' || value === null) {\n throw new FormsError('Forms received an invalid Analytics module dependency.')\n }\n const candidate = value as Partial<AnalyticsInstance>\n if (\n typeof candidate.trackEvent !== 'function' ||\n typeof candidate.getAppUserId !== 'function' ||\n typeof candidate.getProjectId !== 'function'\n ) {\n throw new FormsError('Forms received an invalid Analytics module dependency.')\n }\n return candidate as AnalyticsInstance\n}\n\nexport function forms(\n options: FormsReactOptions<RegisteredSchemas> = {},\n): EmbeddablesReactModule<FormsClient<RegisteredSchemas>, FormsProviderOptions> {\n return {\n key: FORMS_MODULE_KEY,\n dependencies: [{ key: ANALYTICS_MODULE_KEY, optional: true }],\n init: (core, context) => {\n const analyticsInstance = context?.analyticsExcluded\n ? undefined\n : (options.analyticsInstance ??\n resolveInjectedAnalytics(context?.dependencies.get(ANALYTICS_MODULE_KEY)))\n\n return registerFormsModuleClient({\n core,\n customValidations: context?.parameters?.customValidations ?? options.customValidations,\n serverFormData: context?.parameters?.serverFormData ?? options.serverFormData,\n analyticsInstance,\n disableAnalyticsFallback: context?.analyticsExcluded,\n })\n },\n }\n}\n"],"mappings":";;;;AAUA,MAAM,iBAA2C,OAAO,OAAO;CAC7D,QAAQ,CAAC;CACT,QAAQ,CAAC;AACX,CAAC;AAED,SAAS,eACP,MACuB;CACvB,OAAO,OAAO,OAAO;EACnB,QAAQ,KAAK,OAAO;EACpB,QAAQ,KAAK,OAAO;CACtB,CAAC;AACH;AAOA,SAAgB,gBACd,MACuB;CACvB,MAAM,YAAY,OAAuC,IAAI;CAC7D,MAAM,WAAW,OAA8B,cAAuC;CAEtF,MAAM,YAAY,aACf,aAAyB;EACxB,IAAI,SAAS,MAAM,aAAa,KAAA;EAChC,OAAO,KAAK,gBAAgB;GAC1B,UAAU,UAAU;IAAE;IAAM,UAAU,eAAe,IAAI;GAAE;GAC3D,SAAS;EACX,CAAC;CACH,GACA,CAAC,IAAI,CACP;CAEA,MAAM,cAAc,kBAAyC;EAC3D,IAAI,SAAS,MAAM;GAGjB,UAAU,UAAU;GACpB,OAAO,SAAS;EAClB;EAIA,IAAI,UAAU,YAAY,QAAQ,UAAU,QAAQ,SAAS,MAC3D,UAAU,UAAU;GAAE;GAAM,UAAU,eAAe,IAAI;EAAE;EAE7D,OAAO,UAAU,QAAQ;CAC3B,GAAG,CAAC,IAAI,CAAC;CAET,OAAO,qBAAqB,WAAW,aAAa,WAAW;AACjE;;;AC7DA,MAAa,mBAAmB;;;;;AAMhC,MAAa,8BAAc,IAAI,QAA4C;AAE3E,SAAgB,yBAAyB,MAAsD;CAC7F,OAAO,YAAY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d,SACgC;CAChC,OAAO,SAAS,OAAO;AACzB;;;ACdA,MAAM,uBAAuB;AAE7B,SAAS,yBAAyB,OAA+C;CAC/E,IAAI,UAAU,KAAA,GAAW,OAAO,KAAA;CAChC,IAAI,OAAO,UAAU,YAAY,UAAU,MACzC,MAAM,IAAI,WAAW,wDAAwD;CAE/E,MAAM,YAAY;CAClB,IACE,OAAO,UAAU,eAAe,cAChC,OAAO,UAAU,iBAAiB,cAClC,OAAO,UAAU,iBAAiB,YAElC,MAAM,IAAI,WAAW,wDAAwD;CAE/E,OAAO;AACT;AAEA,SAAgB,MACd,UAAgD,CAAC,GAC6B;CAC9E,OAAO;EACL,KAAK;EACL,cAAc,CAAC;GAAE,KAAK;GAAsB,UAAU;EAAK,CAAC;EAC5D,OAAO,MAAM,YAAY;GACvB,MAAM,oBAAoB,SAAS,oBAC/B,KAAA,IACC,QAAQ,qBACT,yBAAyB,SAAS,aAAa,IAAI,oBAAoB,CAAC;GAE5E,OAAO,0BAA0B;IAC/B;IACA,mBAAmB,SAAS,YAAY,qBAAqB,QAAQ;IACrE,gBAAgB,SAAS,YAAY,kBAAkB,QAAQ;IAC/D;IACA,0BAA0B,SAAS;GACrC,CAAC;EACH;CACF;AACF"}
|
package/dist/server.cjs
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_form = require("./form-CdIfHqNc.cjs");
|
|
3
|
+
//#region src/core/server.ts
|
|
4
|
+
function toServerFormInstance(instance) {
|
|
5
|
+
return {
|
|
6
|
+
key: instance.key,
|
|
7
|
+
set: (patch) => instance.set(patch),
|
|
8
|
+
get: (key) => instance.get(key),
|
|
9
|
+
getAll: () => instance.getAll(),
|
|
10
|
+
getValueByProtocolFieldId: (protocolFieldId) => instance.getValueByProtocolFieldId(protocolFieldId)
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
function initFormsServer(options) {
|
|
14
|
+
const server = options.server;
|
|
15
|
+
if (typeof server.getProjectId !== "function" || typeof server.getAppUserId !== "function") throw new require_form.FormsError("initFormsServer requires an initialized Embeddables server instance.");
|
|
16
|
+
if (typeof server.getFormIds !== "function" || typeof server.getFormSchema !== "function") throw new require_form.FormsError("initFormsServer requires a Core instance with registered form schemas.");
|
|
17
|
+
const projectId = server.getProjectId();
|
|
18
|
+
const storage = require_form.createMemoryFormsStorage();
|
|
19
|
+
require_form.seedServerFormsStorageFromCookies({
|
|
20
|
+
storage,
|
|
21
|
+
projectId,
|
|
22
|
+
formIds: server.getFormIds(),
|
|
23
|
+
getFormSchema: (formId) => server.getFormSchema(formId),
|
|
24
|
+
getCookie: (key) => server.getCookie(key)
|
|
25
|
+
});
|
|
26
|
+
const client = require_form.createFormsClient({
|
|
27
|
+
core: server,
|
|
28
|
+
customValidations: options.customValidations,
|
|
29
|
+
analyticsInstance: options.analyticsInstance,
|
|
30
|
+
storage,
|
|
31
|
+
persistence: require_form.createNoopPersistence()
|
|
32
|
+
});
|
|
33
|
+
return {
|
|
34
|
+
getForm({ formId }) {
|
|
35
|
+
return toServerFormInstance(client.getForm({ formId }));
|
|
36
|
+
},
|
|
37
|
+
getServerFormData() {
|
|
38
|
+
const data = {};
|
|
39
|
+
for (const formId of server.getFormIds()) {
|
|
40
|
+
const key = formId;
|
|
41
|
+
data[key] = client.getForm({ formId: key }).getAll();
|
|
42
|
+
}
|
|
43
|
+
return data;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
//#endregion
|
|
48
|
+
exports.initFormsServer = initFormsServer;
|
|
49
|
+
|
|
50
|
+
//# sourceMappingURL=server.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.cjs","names":["FormsError","createMemoryFormsStorage","createFormsClient","createNoopPersistence"],"sources":["../src/core/server.ts"],"sourcesContent":["import { FormsError } from '../errors.js'\nimport { seedServerFormsStorageFromCookies } from '../storage/cookie-form-data.js'\nimport { createNoopPersistence } from '../storage/persistence.js'\nimport { createMemoryFormsStorage } from '../storage/storage.js'\nimport { createFormsClient } from './form.js'\n\nimport type { FormSchema } from './config.js'\nimport type {\n FormInstance,\n FormSchemaMap,\n FormsClientOptions,\n FormsServerInitOptions,\n ServerFormDataByFormId,\n ServerFormInstance,\n} from './form.js'\n\nexport type { FormsServerInitOptions } from './form.js'\n\nexport interface FormsServerClient<TSchemas extends FormSchemaMap = FormSchemaMap> {\n getForm<K extends keyof TSchemas & string>(params: { formId: K }): ServerFormInstance<TSchemas[K]>\n /** Current values for every initialized form — pass to client `initForms` as `serverFormData`. */\n getServerFormData(): ServerFormDataByFormId<TSchemas>\n}\n\nfunction toServerFormInstance<TSchema extends FormSchema = FormSchema>(\n instance: FormInstance<TSchema>,\n): ServerFormInstance<TSchema> {\n return {\n key: instance.key,\n set: (patch) => instance.set(patch),\n get: (key) => instance.get(key),\n getAll: () => instance.getAll(),\n getValueByProtocolFieldId: (protocolFieldId) =>\n instance.getValueByProtocolFieldId(protocolFieldId),\n }\n}\n\nexport function initFormsServer<TSchemas extends FormSchemaMap = FormSchemaMap>(\n options: FormsServerInitOptions<TSchemas>,\n): FormsServerClient<TSchemas> {\n const server = options.server\n if (typeof server.getProjectId !== 'function' || typeof server.getAppUserId !== 'function') {\n throw new FormsError('initFormsServer requires an initialized Embeddables server instance.')\n }\n\n if (typeof server.getFormIds !== 'function' || typeof server.getFormSchema !== 'function') {\n throw new FormsError('initFormsServer requires a Core instance with registered form schemas.')\n }\n\n const projectId = server.getProjectId()\n const storage = createMemoryFormsStorage()\n seedServerFormsStorageFromCookies({\n storage,\n projectId,\n formIds: server.getFormIds(),\n getFormSchema: (formId) => server.getFormSchema(formId),\n getCookie: (key) => server.getCookie(key),\n })\n\n const client = createFormsClient<TSchemas>({\n core: server,\n customValidations: options.customValidations,\n analyticsInstance: options.analyticsInstance,\n storage,\n persistence: createNoopPersistence(),\n } satisfies FormsClientOptions)\n\n const serverClient = {\n getForm({ formId }: { formId: keyof TSchemas & string }) {\n return toServerFormInstance(client.getForm({ formId }))\n },\n getServerFormData() {\n const data: ServerFormDataByFormId<TSchemas> = {}\n for (const formId of server.getFormIds()) {\n const key = formId as keyof TSchemas & string\n data[key] = client.getForm({ formId: key }).getAll()\n }\n return data\n },\n } satisfies FormsServerClient<TSchemas>\n\n return serverClient\n}\n"],"mappings":";;;AAwBA,SAAS,qBACP,UAC6B;CAC7B,OAAO;EACL,KAAK,SAAS;EACd,MAAM,UAAU,SAAS,IAAI,KAAK;EAClC,MAAM,QAAQ,SAAS,IAAI,GAAG;EAC9B,cAAc,SAAS,OAAO;EAC9B,4BAA4B,oBAC1B,SAAS,0BAA0B,eAAe;CACtD;AACF;AAEA,SAAgB,gBACd,SAC6B;CAC7B,MAAM,SAAS,QAAQ;CACvB,IAAI,OAAO,OAAO,iBAAiB,cAAc,OAAO,OAAO,iBAAiB,YAC9E,MAAM,IAAIA,aAAAA,WAAW,sEAAsE;CAG7F,IAAI,OAAO,OAAO,eAAe,cAAc,OAAO,OAAO,kBAAkB,YAC7E,MAAM,IAAIA,aAAAA,WAAW,wEAAwE;CAG/F,MAAM,YAAY,OAAO,aAAa;CACtC,MAAM,UAAUC,aAAAA,yBAAyB;CACzC,aAAA,kCAAkC;EAChC;EACA;EACA,SAAS,OAAO,WAAW;EAC3B,gBAAgB,WAAW,OAAO,cAAc,MAAM;EACtD,YAAY,QAAQ,OAAO,UAAU,GAAG;CAC1C,CAAC;CAED,MAAM,SAASC,aAAAA,kBAA4B;EACzC,MAAM;EACN,mBAAmB,QAAQ;EAC3B,mBAAmB,QAAQ;EAC3B;EACA,aAAaC,aAAAA,sBAAsB;CACrC,CAA8B;CAgB9B,OAAO;EAbL,QAAQ,EAAE,UAA+C;GACvD,OAAO,qBAAqB,OAAO,QAAQ,EAAE,OAAO,CAAC,CAAC;EACxD;EACA,oBAAoB;GAClB,MAAM,OAAyC,CAAC;GAChD,KAAK,MAAM,UAAU,OAAO,WAAW,GAAG;IACxC,MAAM,MAAM;IACZ,KAAK,OAAO,OAAO,QAAQ,EAAE,QAAQ,IAAI,CAAC,CAAC,CAAC,OAAO;GACrD;GACA,OAAO;EACT;CAGgB;AACpB"}
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { a as FormSchemaMap, c as FormsServerInitOptions, d as ServerFormInstance, u as ServerFormDataByFormId } from "./form-x4EJcR9A.js";
|
|
2
|
+
//#region src/core/server.d.ts
|
|
3
|
+
interface FormsServerClient<TSchemas extends FormSchemaMap = FormSchemaMap> {
|
|
4
|
+
getForm<K extends keyof TSchemas & string>(params: {
|
|
5
|
+
formId: K;
|
|
6
|
+
}): ServerFormInstance<TSchemas[K]>;
|
|
7
|
+
/** Current values for every initialized form — pass to client `initForms` as `serverFormData`. */
|
|
8
|
+
getServerFormData(): ServerFormDataByFormId<TSchemas>;
|
|
9
|
+
}
|
|
10
|
+
declare function initFormsServer<TSchemas extends FormSchemaMap = FormSchemaMap>(options: FormsServerInitOptions<TSchemas>): FormsServerClient<TSchemas>;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { type FormsServerClient, type FormsServerInitOptions, type ServerFormDataByFormId, type ServerFormInstance, initFormsServer };
|
|
13
|
+
//# sourceMappingURL=server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.d.ts","names":[],"sources":["../src/core/server.ts"],"mappings":";;UAkBiB,kBAAkB,iBAAiB,gBAAgB;EAClE,QAAQ,gBAAgB,mBAAmB;IAAU,QAAQ;MAAM,mBAAmB,SAAS;;EAE/F,qBAAqB,uBAAuB;;iBAgB9B,gBAAgB,iBAAiB,gBAAgB,eAC/D,SAAS,uBAAuB,YAC/B,kBAAkB"}
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { i as seedServerFormsStorageFromCookies, o as createMemoryFormsStorage, r as createNoopPersistence, s as FormsError, t as createFormsClient } from "./form-7wmC3G_q.js";
|
|
2
|
+
//#region src/core/server.ts
|
|
3
|
+
function toServerFormInstance(instance) {
|
|
4
|
+
return {
|
|
5
|
+
key: instance.key,
|
|
6
|
+
set: (patch) => instance.set(patch),
|
|
7
|
+
get: (key) => instance.get(key),
|
|
8
|
+
getAll: () => instance.getAll(),
|
|
9
|
+
getValueByProtocolFieldId: (protocolFieldId) => instance.getValueByProtocolFieldId(protocolFieldId)
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
function initFormsServer(options) {
|
|
13
|
+
const server = options.server;
|
|
14
|
+
if (typeof server.getProjectId !== "function" || typeof server.getAppUserId !== "function") throw new FormsError("initFormsServer requires an initialized Embeddables server instance.");
|
|
15
|
+
if (typeof server.getFormIds !== "function" || typeof server.getFormSchema !== "function") throw new FormsError("initFormsServer requires a Core instance with registered form schemas.");
|
|
16
|
+
const projectId = server.getProjectId();
|
|
17
|
+
const storage = createMemoryFormsStorage();
|
|
18
|
+
seedServerFormsStorageFromCookies({
|
|
19
|
+
storage,
|
|
20
|
+
projectId,
|
|
21
|
+
formIds: server.getFormIds(),
|
|
22
|
+
getFormSchema: (formId) => server.getFormSchema(formId),
|
|
23
|
+
getCookie: (key) => server.getCookie(key)
|
|
24
|
+
});
|
|
25
|
+
const client = createFormsClient({
|
|
26
|
+
core: server,
|
|
27
|
+
customValidations: options.customValidations,
|
|
28
|
+
analyticsInstance: options.analyticsInstance,
|
|
29
|
+
storage,
|
|
30
|
+
persistence: createNoopPersistence()
|
|
31
|
+
});
|
|
32
|
+
return {
|
|
33
|
+
getForm({ formId }) {
|
|
34
|
+
return toServerFormInstance(client.getForm({ formId }));
|
|
35
|
+
},
|
|
36
|
+
getServerFormData() {
|
|
37
|
+
const data = {};
|
|
38
|
+
for (const formId of server.getFormIds()) {
|
|
39
|
+
const key = formId;
|
|
40
|
+
data[key] = client.getForm({ formId: key }).getAll();
|
|
41
|
+
}
|
|
42
|
+
return data;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
//#endregion
|
|
47
|
+
export { initFormsServer };
|
|
48
|
+
|
|
49
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","names":[],"sources":["../src/core/server.ts"],"sourcesContent":["import { FormsError } from '../errors.js'\nimport { seedServerFormsStorageFromCookies } from '../storage/cookie-form-data.js'\nimport { createNoopPersistence } from '../storage/persistence.js'\nimport { createMemoryFormsStorage } from '../storage/storage.js'\nimport { createFormsClient } from './form.js'\n\nimport type { FormSchema } from './config.js'\nimport type {\n FormInstance,\n FormSchemaMap,\n FormsClientOptions,\n FormsServerInitOptions,\n ServerFormDataByFormId,\n ServerFormInstance,\n} from './form.js'\n\nexport type { FormsServerInitOptions } from './form.js'\n\nexport interface FormsServerClient<TSchemas extends FormSchemaMap = FormSchemaMap> {\n getForm<K extends keyof TSchemas & string>(params: { formId: K }): ServerFormInstance<TSchemas[K]>\n /** Current values for every initialized form — pass to client `initForms` as `serverFormData`. */\n getServerFormData(): ServerFormDataByFormId<TSchemas>\n}\n\nfunction toServerFormInstance<TSchema extends FormSchema = FormSchema>(\n instance: FormInstance<TSchema>,\n): ServerFormInstance<TSchema> {\n return {\n key: instance.key,\n set: (patch) => instance.set(patch),\n get: (key) => instance.get(key),\n getAll: () => instance.getAll(),\n getValueByProtocolFieldId: (protocolFieldId) =>\n instance.getValueByProtocolFieldId(protocolFieldId),\n }\n}\n\nexport function initFormsServer<TSchemas extends FormSchemaMap = FormSchemaMap>(\n options: FormsServerInitOptions<TSchemas>,\n): FormsServerClient<TSchemas> {\n const server = options.server\n if (typeof server.getProjectId !== 'function' || typeof server.getAppUserId !== 'function') {\n throw new FormsError('initFormsServer requires an initialized Embeddables server instance.')\n }\n\n if (typeof server.getFormIds !== 'function' || typeof server.getFormSchema !== 'function') {\n throw new FormsError('initFormsServer requires a Core instance with registered form schemas.')\n }\n\n const projectId = server.getProjectId()\n const storage = createMemoryFormsStorage()\n seedServerFormsStorageFromCookies({\n storage,\n projectId,\n formIds: server.getFormIds(),\n getFormSchema: (formId) => server.getFormSchema(formId),\n getCookie: (key) => server.getCookie(key),\n })\n\n const client = createFormsClient<TSchemas>({\n core: server,\n customValidations: options.customValidations,\n analyticsInstance: options.analyticsInstance,\n storage,\n persistence: createNoopPersistence(),\n } satisfies FormsClientOptions)\n\n const serverClient = {\n getForm({ formId }: { formId: keyof TSchemas & string }) {\n return toServerFormInstance(client.getForm({ formId }))\n },\n getServerFormData() {\n const data: ServerFormDataByFormId<TSchemas> = {}\n for (const formId of server.getFormIds()) {\n const key = formId as keyof TSchemas & string\n data[key] = client.getForm({ formId: key }).getAll()\n }\n return data\n },\n } satisfies FormsServerClient<TSchemas>\n\n return serverClient\n}\n"],"mappings":";;AAwBA,SAAS,qBACP,UAC6B;CAC7B,OAAO;EACL,KAAK,SAAS;EACd,MAAM,UAAU,SAAS,IAAI,KAAK;EAClC,MAAM,QAAQ,SAAS,IAAI,GAAG;EAC9B,cAAc,SAAS,OAAO;EAC9B,4BAA4B,oBAC1B,SAAS,0BAA0B,eAAe;CACtD;AACF;AAEA,SAAgB,gBACd,SAC6B;CAC7B,MAAM,SAAS,QAAQ;CACvB,IAAI,OAAO,OAAO,iBAAiB,cAAc,OAAO,OAAO,iBAAiB,YAC9E,MAAM,IAAI,WAAW,sEAAsE;CAG7F,IAAI,OAAO,OAAO,eAAe,cAAc,OAAO,OAAO,kBAAkB,YAC7E,MAAM,IAAI,WAAW,wEAAwE;CAG/F,MAAM,YAAY,OAAO,aAAa;CACtC,MAAM,UAAU,yBAAyB;CACzC,kCAAkC;EAChC;EACA;EACA,SAAS,OAAO,WAAW;EAC3B,gBAAgB,WAAW,OAAO,cAAc,MAAM;EACtD,YAAY,QAAQ,OAAO,UAAU,GAAG;CAC1C,CAAC;CAED,MAAM,SAAS,kBAA4B;EACzC,MAAM;EACN,mBAAmB,QAAQ;EAC3B,mBAAmB,QAAQ;EAC3B;EACA,aAAa,sBAAsB;CACrC,CAA8B;CAgB9B,OAAO;EAbL,QAAQ,EAAE,UAA+C;GACvD,OAAO,qBAAqB,OAAO,QAAQ,EAAE,OAAO,CAAC,CAAC;EACxD;EACA,oBAAoB;GAClB,MAAM,OAAyC,CAAC;GAChD,KAAK,MAAM,UAAU,OAAO,WAAW,GAAG;IACxC,MAAM,MAAM;IACZ,KAAK,OAAO,OAAO,QAAQ,EAAE,QAAQ,IAAI,CAAC,CAAC,CAAC,OAAO;GACrD;GACA,OAAO;EACT;CAGgB;AACpB"}
|