@formspec/runtime 0.1.0-alpha.19 → 0.1.0-alpha.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/resolvers.d.ts +8 -0
- package/dist/resolvers.d.ts.map +1 -1
- package/dist/runtime.d.ts +8 -0
- package/package.json +3 -3
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/resolvers.ts"],"sourcesContent":["/**\n * `@formspec/runtime` - Runtime helpers for FormSpec\n *\n * This package provides utilities for working with FormSpec forms at runtime:\n * - `defineResolvers()` - Type-safe resolver definitions for dynamic enum fields\n *\n * @example\n * ```typescript\n * import { defineResolvers } from \"@formspec/runtime\";\n * import { formspec, field } from \"@formspec/dsl\";\n *\n * // Define a form with dynamic enum fields\n * const form = formspec(\n * field.dynamicEnum(\"country\", \"countries\", { label: \"Country\" }),\n * );\n *\n * // Define resolvers for the form's data sources\n * const resolvers = defineResolvers(form, {\n * countries: async () => ({\n * options: [\n * { value: \"us\", label: \"United States\" },\n * { value: \"ca\", label: \"Canada\" },\n * ],\n * validity: \"valid\",\n * }),\n * });\n *\n * // Use the resolver\n * const result = await resolvers.get(\"countries\")();\n * console.log(result.options); // [{ value: \"us\", ... }, { value: \"ca\", ... }]\n * ```\n *\n * @packageDocumentation\n */\n\nexport {\n defineResolvers,\n type Resolver,\n type ResolverMap,\n type ResolverRegistry,\n} from \"./resolvers.js\";\n","/**\n * Resolver helpers for dynamic FormSpec data.\n *\n * Resolvers are functions that fetch options for dynamic enum fields\n * at runtime. This module provides type-safe utilities for defining\n * and using resolvers.\n */\n\nimport type {\n DataSourceRegistry,\n FetchOptionsResponse,\n FormElement,\n FormSpec,\n DynamicEnumField,\n Group,\n Conditional,\n} from \"@formspec/core\";\n\n/**\n * A resolver function that fetches options for a data source.\n *\n * @typeParam Source - The data source key from DataSourceRegistry\n * @typeParam T - The data type for options (from DataSourceRegistry)\n */\nexport type Resolver<Source extends keyof DataSourceRegistry, T = DataSourceRegistry[Source]> = (\n params?: Record<string, unknown>\n) => Promise<FetchOptionsResponse<T>>;\n\n/**\n * Extracts all dynamic enum source keys from a form's elements.\n */\ntype ExtractDynamicSources<E> =\n E extends DynamicEnumField<string, infer S>\n ? S\n : E extends Group<infer Elements>\n ? ExtractDynamicSourcesFromArray<Elements>\n : E extends Conditional<string, unknown, infer Elements>\n ? ExtractDynamicSourcesFromArray<Elements>\n : never;\n\ntype ExtractDynamicSourcesFromArray<Elements> = Elements extends readonly [\n infer First,\n ...infer Rest,\n]\n ? ExtractDynamicSources<First> | ExtractDynamicSourcesFromArray<Rest>\n : never;\n\n/**\n * Map of resolver functions for a form's dynamic data sources.\n */\nexport type ResolverMap<Sources extends string> = {\n [S in Sources]: S extends keyof DataSourceRegistry\n ? Resolver<S>\n : (params?: Record<string, unknown>) => Promise<FetchOptionsResponse>;\n};\n\n/**\n * A resolver registry that provides type-safe access to resolvers.\n */\nexport interface ResolverRegistry<Sources extends string> {\n /**\n * Gets a resolver by data source name.\n */\n get<S extends Sources>(\n source: S\n ): S extends keyof DataSourceRegistry\n ? Resolver<S>\n : (params?: Record<string, unknown>) => Promise<FetchOptionsResponse>;\n\n /**\n * Checks if a resolver exists for a data source.\n */\n has(source: string): boolean;\n\n /**\n * Gets all registered data source names.\n */\n sources(): Sources[];\n}\n\n/**\n * Extracts all dynamic enum field sources from form elements.\n */\nfunction extractSources(elements: readonly FormElement[]): Set<string> {\n const sources = new Set<string>();\n\n function visit(el: FormElement): void {\n if (el._type === \"field\" && el._field === \"dynamic_enum\") {\n // After checking _field, we know this is a DynamicEnumField\n const dynamicField: DynamicEnumField<string, string> = el;\n sources.add(dynamicField.source);\n } else if (el._type === \"group\") {\n (el as Group<readonly FormElement[]>).elements.forEach(visit);\n } else if (el._type === \"conditional\") {\n (el as Conditional<string, unknown, readonly FormElement[]>).elements.forEach(visit);\n }\n }\n\n elements.forEach(visit);\n return sources;\n}\n\n/**\n * Defines resolvers for a form's dynamic data sources.\n *\n * This function provides type-safe resolver definitions that match\n * the form's dynamic enum fields.\n *\n * @example\n * ```typescript\n * declare module \"@formspec/core\" {\n * interface DataSourceRegistry {\n * countries: { id: string; code: string; name: string };\n * }\n * }\n *\n * const form = formspec(\n * field.dynamicEnum(\"country\", \"countries\", { label: \"Country\" }),\n * );\n *\n * const resolvers = defineResolvers(form, {\n * countries: async () => ({\n * options: [\n * { value: \"us\", label: \"United States\", data: { id: \"us\", code: \"US\", name: \"United States\" } },\n * { value: \"ca\", label: \"Canada\", data: { id: \"ca\", code: \"CA\", name: \"Canada\" } },\n * ],\n * validity: \"valid\",\n * }),\n * });\n *\n * // Use the resolver\n * const result = await resolvers.get(\"countries\")();\n * ```\n *\n * @param form - The FormSpec containing dynamic enum fields\n * @param resolvers - Map of resolver functions for each data source\n * @returns A ResolverRegistry for type-safe access to resolvers\n */\nexport function defineResolvers<\n E extends readonly FormElement[],\n Sources extends string = ExtractDynamicSourcesFromArray<E> & string,\n>(form: FormSpec<E>, resolvers: ResolverMap<Sources>): ResolverRegistry<Sources> {\n const sourceSet = extractSources(form.elements);\n const resolverMap = new Map<string, Resolver<keyof DataSourceRegistry>>(\n Object.entries(resolvers) as [string, Resolver<keyof DataSourceRegistry>][]\n );\n\n // Validate that all sources have resolvers\n for (const source of sourceSet) {\n if (!resolverMap.has(source)) {\n console.warn(`Missing resolver for data source: ${source}`);\n }\n }\n\n return {\n get<S extends Sources>(source: S) {\n const resolver = resolverMap.get(source);\n if (resolver === undefined) {\n throw new Error(`No resolver found for data source: ${source}`);\n }\n return resolver as S extends keyof DataSourceRegistry\n ? Resolver<S>\n : (params?: Record<string, unknown>) => Promise<FetchOptionsResponse>;\n },\n\n has(source: string): boolean {\n return resolverMap.has(source);\n },\n\n sources(): Sources[] {\n return Array.from(resolverMap.keys()) as Sources[];\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/resolvers.ts"],"sourcesContent":["/**\n * `@formspec/runtime` - Runtime helpers for FormSpec\n *\n * This package provides utilities for working with FormSpec forms at runtime:\n * - `defineResolvers()` - Type-safe resolver definitions for dynamic enum fields\n *\n * @example\n * ```typescript\n * import { defineResolvers } from \"@formspec/runtime\";\n * import { formspec, field } from \"@formspec/dsl\";\n *\n * // Define a form with dynamic enum fields\n * const form = formspec(\n * field.dynamicEnum(\"country\", \"countries\", { label: \"Country\" }),\n * );\n *\n * // Define resolvers for the form's data sources\n * const resolvers = defineResolvers(form, {\n * countries: async () => ({\n * options: [\n * { value: \"us\", label: \"United States\" },\n * { value: \"ca\", label: \"Canada\" },\n * ],\n * validity: \"valid\",\n * }),\n * });\n *\n * // Use the resolver\n * const result = await resolvers.get(\"countries\")();\n * console.log(result.options); // [{ value: \"us\", ... }, { value: \"ca\", ... }]\n * ```\n *\n * @packageDocumentation\n */\n\nexport {\n defineResolvers,\n type Resolver,\n type ResolverMap,\n type ResolverRegistry,\n} from \"./resolvers.js\";\n","/**\n * Resolver helpers for dynamic FormSpec data.\n *\n * Resolvers are functions that fetch options for dynamic enum fields\n * at runtime. This module provides type-safe utilities for defining\n * and using resolvers.\n */\n\nimport type {\n DataSourceRegistry,\n FetchOptionsResponse,\n FormElement,\n FormSpec,\n DynamicEnumField,\n Group,\n Conditional,\n} from \"@formspec/core\";\n\n/**\n * A resolver function that fetches options for a data source.\n *\n * @typeParam Source - The data source key from DataSourceRegistry\n * @typeParam T - The data type for options (from DataSourceRegistry)\n *\n * @public\n */\nexport type Resolver<Source extends keyof DataSourceRegistry, T = DataSourceRegistry[Source]> = (\n params?: Record<string, unknown>\n) => Promise<FetchOptionsResponse<T>>;\n\n/**\n * Extracts all dynamic enum source keys from a form's elements.\n */\ntype ExtractDynamicSources<E> =\n E extends DynamicEnumField<string, infer S>\n ? S\n : E extends Group<infer Elements>\n ? ExtractDynamicSourcesFromArray<Elements>\n : E extends Conditional<string, unknown, infer Elements>\n ? ExtractDynamicSourcesFromArray<Elements>\n : never;\n\ntype ExtractDynamicSourcesFromArray<Elements> = Elements extends readonly [\n infer First,\n ...infer Rest,\n]\n ? ExtractDynamicSources<First> | ExtractDynamicSourcesFromArray<Rest>\n : never;\n\n/**\n * Map of resolver functions for a form's dynamic data sources.\n *\n * @public\n */\nexport type ResolverMap<Sources extends string> = {\n [S in Sources]: S extends keyof DataSourceRegistry\n ? Resolver<S>\n : (params?: Record<string, unknown>) => Promise<FetchOptionsResponse>;\n};\n\n/**\n * A resolver registry that provides type-safe access to resolvers.\n *\n * @public\n */\nexport interface ResolverRegistry<Sources extends string> {\n /**\n * Gets a resolver by data source name.\n */\n get<S extends Sources>(\n source: S\n ): S extends keyof DataSourceRegistry\n ? Resolver<S>\n : (params?: Record<string, unknown>) => Promise<FetchOptionsResponse>;\n\n /**\n * Checks if a resolver exists for a data source.\n */\n has(source: string): boolean;\n\n /**\n * Gets all registered data source names.\n */\n sources(): Sources[];\n}\n\n/**\n * Extracts all dynamic enum field sources from form elements.\n */\nfunction extractSources(elements: readonly FormElement[]): Set<string> {\n const sources = new Set<string>();\n\n function visit(el: FormElement): void {\n if (el._type === \"field\" && el._field === \"dynamic_enum\") {\n // After checking _field, we know this is a DynamicEnumField\n const dynamicField: DynamicEnumField<string, string> = el;\n sources.add(dynamicField.source);\n } else if (el._type === \"group\") {\n (el as Group<readonly FormElement[]>).elements.forEach(visit);\n } else if (el._type === \"conditional\") {\n (el as Conditional<string, unknown, readonly FormElement[]>).elements.forEach(visit);\n }\n }\n\n elements.forEach(visit);\n return sources;\n}\n\n/**\n * Defines resolvers for a form's dynamic data sources.\n *\n * This function provides type-safe resolver definitions that match\n * the form's dynamic enum fields.\n *\n * @example\n * ```typescript\n * declare module \"@formspec/core\" {\n * interface DataSourceRegistry {\n * countries: { id: string; code: string; name: string };\n * }\n * }\n *\n * const form = formspec(\n * field.dynamicEnum(\"country\", \"countries\", { label: \"Country\" }),\n * );\n *\n * const resolvers = defineResolvers(form, {\n * countries: async () => ({\n * options: [\n * { value: \"us\", label: \"United States\", data: { id: \"us\", code: \"US\", name: \"United States\" } },\n * { value: \"ca\", label: \"Canada\", data: { id: \"ca\", code: \"CA\", name: \"Canada\" } },\n * ],\n * validity: \"valid\",\n * }),\n * });\n *\n * // Use the resolver\n * const result = await resolvers.get(\"countries\")();\n * ```\n *\n * @param form - The FormSpec containing dynamic enum fields\n * @param resolvers - Map of resolver functions for each data source\n * @returns A ResolverRegistry for type-safe access to resolvers\n *\n * @public\n */\nexport function defineResolvers<\n E extends readonly FormElement[],\n Sources extends string = ExtractDynamicSourcesFromArray<E> & string,\n>(form: FormSpec<E>, resolvers: ResolverMap<Sources>): ResolverRegistry<Sources> {\n const sourceSet = extractSources(form.elements);\n const resolverMap = new Map<string, Resolver<keyof DataSourceRegistry>>(\n Object.entries(resolvers) as [string, Resolver<keyof DataSourceRegistry>][]\n );\n\n // Validate that all sources have resolvers\n for (const source of sourceSet) {\n if (!resolverMap.has(source)) {\n console.warn(`Missing resolver for data source: ${source}`);\n }\n }\n\n return {\n get<S extends Sources>(source: S) {\n const resolver = resolverMap.get(source);\n if (resolver === undefined) {\n throw new Error(`No resolver found for data source: ${source}`);\n }\n return resolver as S extends keyof DataSourceRegistry\n ? Resolver<S>\n : (params?: Record<string, unknown>) => Promise<FetchOptionsResponse>;\n },\n\n has(source: string): boolean {\n return resolverMap.has(source);\n },\n\n sources(): Sources[] {\n return Array.from(resolverMap.keys()) as Sources[];\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACyFA,SAAS,eAAe,UAA+C;AACrE,QAAM,UAAU,oBAAI,IAAY;AAEhC,WAAS,MAAM,IAAuB;AACpC,QAAI,GAAG,UAAU,WAAW,GAAG,WAAW,gBAAgB;AAExD,YAAM,eAAiD;AACvD,cAAQ,IAAI,aAAa,MAAM;AAAA,IACjC,WAAW,GAAG,UAAU,SAAS;AAC/B,MAAC,GAAqC,SAAS,QAAQ,KAAK;AAAA,IAC9D,WAAW,GAAG,UAAU,eAAe;AACrC,MAAC,GAA4D,SAAS,QAAQ,KAAK;AAAA,IACrF;AAAA,EACF;AAEA,WAAS,QAAQ,KAAK;AACtB,SAAO;AACT;AAwCO,SAAS,gBAGd,MAAmB,WAA4D;AAC/E,QAAM,YAAY,eAAe,KAAK,QAAQ;AAC9C,QAAM,cAAc,IAAI;AAAA,IACtB,OAAO,QAAQ,SAAS;AAAA,EAC1B;AAGA,aAAW,UAAU,WAAW;AAC9B,QAAI,CAAC,YAAY,IAAI,MAAM,GAAG;AAC5B,cAAQ,KAAK,qCAAqC,MAAM,EAAE;AAAA,IAC5D;AAAA,EACF;AAEA,SAAO;AAAA,IACL,IAAuB,QAAW;AAChC,YAAM,WAAW,YAAY,IAAI,MAAM;AACvC,UAAI,aAAa,QAAW;AAC1B,cAAM,IAAI,MAAM,sCAAsC,MAAM,EAAE;AAAA,MAChE;AACA,aAAO;AAAA,IAGT;AAAA,IAEA,IAAI,QAAyB;AAC3B,aAAO,YAAY,IAAI,MAAM;AAAA,IAC/B;AAAA,IAEA,UAAqB;AACnB,aAAO,MAAM,KAAK,YAAY,KAAK,CAAC;AAAA,IACtC;AAAA,EACF;AACF;","names":[]}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/resolvers.ts"],"sourcesContent":["/**\n * Resolver helpers for dynamic FormSpec data.\n *\n * Resolvers are functions that fetch options for dynamic enum fields\n * at runtime. This module provides type-safe utilities for defining\n * and using resolvers.\n */\n\nimport type {\n DataSourceRegistry,\n FetchOptionsResponse,\n FormElement,\n FormSpec,\n DynamicEnumField,\n Group,\n Conditional,\n} from \"@formspec/core\";\n\n/**\n * A resolver function that fetches options for a data source.\n *\n * @typeParam Source - The data source key from DataSourceRegistry\n * @typeParam T - The data type for options (from DataSourceRegistry)\n */\nexport type Resolver<Source extends keyof DataSourceRegistry, T = DataSourceRegistry[Source]> = (\n params?: Record<string, unknown>\n) => Promise<FetchOptionsResponse<T>>;\n\n/**\n * Extracts all dynamic enum source keys from a form's elements.\n */\ntype ExtractDynamicSources<E> =\n E extends DynamicEnumField<string, infer S>\n ? S\n : E extends Group<infer Elements>\n ? ExtractDynamicSourcesFromArray<Elements>\n : E extends Conditional<string, unknown, infer Elements>\n ? ExtractDynamicSourcesFromArray<Elements>\n : never;\n\ntype ExtractDynamicSourcesFromArray<Elements> = Elements extends readonly [\n infer First,\n ...infer Rest,\n]\n ? ExtractDynamicSources<First> | ExtractDynamicSourcesFromArray<Rest>\n : never;\n\n/**\n * Map of resolver functions for a form's dynamic data sources.\n */\nexport type ResolverMap<Sources extends string> = {\n [S in Sources]: S extends keyof DataSourceRegistry\n ? Resolver<S>\n : (params?: Record<string, unknown>) => Promise<FetchOptionsResponse>;\n};\n\n/**\n * A resolver registry that provides type-safe access to resolvers.\n */\nexport interface ResolverRegistry<Sources extends string> {\n /**\n * Gets a resolver by data source name.\n */\n get<S extends Sources>(\n source: S\n ): S extends keyof DataSourceRegistry\n ? Resolver<S>\n : (params?: Record<string, unknown>) => Promise<FetchOptionsResponse>;\n\n /**\n * Checks if a resolver exists for a data source.\n */\n has(source: string): boolean;\n\n /**\n * Gets all registered data source names.\n */\n sources(): Sources[];\n}\n\n/**\n * Extracts all dynamic enum field sources from form elements.\n */\nfunction extractSources(elements: readonly FormElement[]): Set<string> {\n const sources = new Set<string>();\n\n function visit(el: FormElement): void {\n if (el._type === \"field\" && el._field === \"dynamic_enum\") {\n // After checking _field, we know this is a DynamicEnumField\n const dynamicField: DynamicEnumField<string, string> = el;\n sources.add(dynamicField.source);\n } else if (el._type === \"group\") {\n (el as Group<readonly FormElement[]>).elements.forEach(visit);\n } else if (el._type === \"conditional\") {\n (el as Conditional<string, unknown, readonly FormElement[]>).elements.forEach(visit);\n }\n }\n\n elements.forEach(visit);\n return sources;\n}\n\n/**\n * Defines resolvers for a form's dynamic data sources.\n *\n * This function provides type-safe resolver definitions that match\n * the form's dynamic enum fields.\n *\n * @example\n * ```typescript\n * declare module \"@formspec/core\" {\n * interface DataSourceRegistry {\n * countries: { id: string; code: string; name: string };\n * }\n * }\n *\n * const form = formspec(\n * field.dynamicEnum(\"country\", \"countries\", { label: \"Country\" }),\n * );\n *\n * const resolvers = defineResolvers(form, {\n * countries: async () => ({\n * options: [\n * { value: \"us\", label: \"United States\", data: { id: \"us\", code: \"US\", name: \"United States\" } },\n * { value: \"ca\", label: \"Canada\", data: { id: \"ca\", code: \"CA\", name: \"Canada\" } },\n * ],\n * validity: \"valid\",\n * }),\n * });\n *\n * // Use the resolver\n * const result = await resolvers.get(\"countries\")();\n * ```\n *\n * @param form - The FormSpec containing dynamic enum fields\n * @param resolvers - Map of resolver functions for each data source\n * @returns A ResolverRegistry for type-safe access to resolvers\n */\nexport function defineResolvers<\n E extends readonly FormElement[],\n Sources extends string = ExtractDynamicSourcesFromArray<E> & string,\n>(form: FormSpec<E>, resolvers: ResolverMap<Sources>): ResolverRegistry<Sources> {\n const sourceSet = extractSources(form.elements);\n const resolverMap = new Map<string, Resolver<keyof DataSourceRegistry>>(\n Object.entries(resolvers) as [string, Resolver<keyof DataSourceRegistry>][]\n );\n\n // Validate that all sources have resolvers\n for (const source of sourceSet) {\n if (!resolverMap.has(source)) {\n console.warn(`Missing resolver for data source: ${source}`);\n }\n }\n\n return {\n get<S extends Sources>(source: S) {\n const resolver = resolverMap.get(source);\n if (resolver === undefined) {\n throw new Error(`No resolver found for data source: ${source}`);\n }\n return resolver as S extends keyof DataSourceRegistry\n ? Resolver<S>\n : (params?: Record<string, unknown>) => Promise<FetchOptionsResponse>;\n },\n\n has(source: string): boolean {\n return resolverMap.has(source);\n },\n\n sources(): Sources[] {\n return Array.from(resolverMap.keys()) as Sources[];\n },\n };\n}\n"],"mappings":";
|
|
1
|
+
{"version":3,"sources":["../src/resolvers.ts"],"sourcesContent":["/**\n * Resolver helpers for dynamic FormSpec data.\n *\n * Resolvers are functions that fetch options for dynamic enum fields\n * at runtime. This module provides type-safe utilities for defining\n * and using resolvers.\n */\n\nimport type {\n DataSourceRegistry,\n FetchOptionsResponse,\n FormElement,\n FormSpec,\n DynamicEnumField,\n Group,\n Conditional,\n} from \"@formspec/core\";\n\n/**\n * A resolver function that fetches options for a data source.\n *\n * @typeParam Source - The data source key from DataSourceRegistry\n * @typeParam T - The data type for options (from DataSourceRegistry)\n *\n * @public\n */\nexport type Resolver<Source extends keyof DataSourceRegistry, T = DataSourceRegistry[Source]> = (\n params?: Record<string, unknown>\n) => Promise<FetchOptionsResponse<T>>;\n\n/**\n * Extracts all dynamic enum source keys from a form's elements.\n */\ntype ExtractDynamicSources<E> =\n E extends DynamicEnumField<string, infer S>\n ? S\n : E extends Group<infer Elements>\n ? ExtractDynamicSourcesFromArray<Elements>\n : E extends Conditional<string, unknown, infer Elements>\n ? ExtractDynamicSourcesFromArray<Elements>\n : never;\n\ntype ExtractDynamicSourcesFromArray<Elements> = Elements extends readonly [\n infer First,\n ...infer Rest,\n]\n ? ExtractDynamicSources<First> | ExtractDynamicSourcesFromArray<Rest>\n : never;\n\n/**\n * Map of resolver functions for a form's dynamic data sources.\n *\n * @public\n */\nexport type ResolverMap<Sources extends string> = {\n [S in Sources]: S extends keyof DataSourceRegistry\n ? Resolver<S>\n : (params?: Record<string, unknown>) => Promise<FetchOptionsResponse>;\n};\n\n/**\n * A resolver registry that provides type-safe access to resolvers.\n *\n * @public\n */\nexport interface ResolverRegistry<Sources extends string> {\n /**\n * Gets a resolver by data source name.\n */\n get<S extends Sources>(\n source: S\n ): S extends keyof DataSourceRegistry\n ? Resolver<S>\n : (params?: Record<string, unknown>) => Promise<FetchOptionsResponse>;\n\n /**\n * Checks if a resolver exists for a data source.\n */\n has(source: string): boolean;\n\n /**\n * Gets all registered data source names.\n */\n sources(): Sources[];\n}\n\n/**\n * Extracts all dynamic enum field sources from form elements.\n */\nfunction extractSources(elements: readonly FormElement[]): Set<string> {\n const sources = new Set<string>();\n\n function visit(el: FormElement): void {\n if (el._type === \"field\" && el._field === \"dynamic_enum\") {\n // After checking _field, we know this is a DynamicEnumField\n const dynamicField: DynamicEnumField<string, string> = el;\n sources.add(dynamicField.source);\n } else if (el._type === \"group\") {\n (el as Group<readonly FormElement[]>).elements.forEach(visit);\n } else if (el._type === \"conditional\") {\n (el as Conditional<string, unknown, readonly FormElement[]>).elements.forEach(visit);\n }\n }\n\n elements.forEach(visit);\n return sources;\n}\n\n/**\n * Defines resolvers for a form's dynamic data sources.\n *\n * This function provides type-safe resolver definitions that match\n * the form's dynamic enum fields.\n *\n * @example\n * ```typescript\n * declare module \"@formspec/core\" {\n * interface DataSourceRegistry {\n * countries: { id: string; code: string; name: string };\n * }\n * }\n *\n * const form = formspec(\n * field.dynamicEnum(\"country\", \"countries\", { label: \"Country\" }),\n * );\n *\n * const resolvers = defineResolvers(form, {\n * countries: async () => ({\n * options: [\n * { value: \"us\", label: \"United States\", data: { id: \"us\", code: \"US\", name: \"United States\" } },\n * { value: \"ca\", label: \"Canada\", data: { id: \"ca\", code: \"CA\", name: \"Canada\" } },\n * ],\n * validity: \"valid\",\n * }),\n * });\n *\n * // Use the resolver\n * const result = await resolvers.get(\"countries\")();\n * ```\n *\n * @param form - The FormSpec containing dynamic enum fields\n * @param resolvers - Map of resolver functions for each data source\n * @returns A ResolverRegistry for type-safe access to resolvers\n *\n * @public\n */\nexport function defineResolvers<\n E extends readonly FormElement[],\n Sources extends string = ExtractDynamicSourcesFromArray<E> & string,\n>(form: FormSpec<E>, resolvers: ResolverMap<Sources>): ResolverRegistry<Sources> {\n const sourceSet = extractSources(form.elements);\n const resolverMap = new Map<string, Resolver<keyof DataSourceRegistry>>(\n Object.entries(resolvers) as [string, Resolver<keyof DataSourceRegistry>][]\n );\n\n // Validate that all sources have resolvers\n for (const source of sourceSet) {\n if (!resolverMap.has(source)) {\n console.warn(`Missing resolver for data source: ${source}`);\n }\n }\n\n return {\n get<S extends Sources>(source: S) {\n const resolver = resolverMap.get(source);\n if (resolver === undefined) {\n throw new Error(`No resolver found for data source: ${source}`);\n }\n return resolver as S extends keyof DataSourceRegistry\n ? Resolver<S>\n : (params?: Record<string, unknown>) => Promise<FetchOptionsResponse>;\n },\n\n has(source: string): boolean {\n return resolverMap.has(source);\n },\n\n sources(): Sources[] {\n return Array.from(resolverMap.keys()) as Sources[];\n },\n };\n}\n"],"mappings":";AAyFA,SAAS,eAAe,UAA+C;AACrE,QAAM,UAAU,oBAAI,IAAY;AAEhC,WAAS,MAAM,IAAuB;AACpC,QAAI,GAAG,UAAU,WAAW,GAAG,WAAW,gBAAgB;AAExD,YAAM,eAAiD;AACvD,cAAQ,IAAI,aAAa,MAAM;AAAA,IACjC,WAAW,GAAG,UAAU,SAAS;AAC/B,MAAC,GAAqC,SAAS,QAAQ,KAAK;AAAA,IAC9D,WAAW,GAAG,UAAU,eAAe;AACrC,MAAC,GAA4D,SAAS,QAAQ,KAAK;AAAA,IACrF;AAAA,EACF;AAEA,WAAS,QAAQ,KAAK;AACtB,SAAO;AACT;AAwCO,SAAS,gBAGd,MAAmB,WAA4D;AAC/E,QAAM,YAAY,eAAe,KAAK,QAAQ;AAC9C,QAAM,cAAc,IAAI;AAAA,IACtB,OAAO,QAAQ,SAAS;AAAA,EAC1B;AAGA,aAAW,UAAU,WAAW;AAC9B,QAAI,CAAC,YAAY,IAAI,MAAM,GAAG;AAC5B,cAAQ,KAAK,qCAAqC,MAAM,EAAE;AAAA,IAC5D;AAAA,EACF;AAEA,SAAO;AAAA,IACL,IAAuB,QAAW;AAChC,YAAM,WAAW,YAAY,IAAI,MAAM;AACvC,UAAI,aAAa,QAAW;AAC1B,cAAM,IAAI,MAAM,sCAAsC,MAAM,EAAE;AAAA,MAChE;AACA,aAAO;AAAA,IAGT;AAAA,IAEA,IAAI,QAAyB;AAC3B,aAAO,YAAY,IAAI,MAAM;AAAA,IAC/B;AAAA,IAEA,UAAqB;AACnB,aAAO,MAAM,KAAK,YAAY,KAAK,CAAC;AAAA,IACtC;AAAA,EACF;AACF;","names":[]}
|
package/dist/resolvers.d.ts
CHANGED
|
@@ -11,6 +11,8 @@ import type { DataSourceRegistry, FetchOptionsResponse, FormElement, FormSpec, D
|
|
|
11
11
|
*
|
|
12
12
|
* @typeParam Source - The data source key from DataSourceRegistry
|
|
13
13
|
* @typeParam T - The data type for options (from DataSourceRegistry)
|
|
14
|
+
*
|
|
15
|
+
* @public
|
|
14
16
|
*/
|
|
15
17
|
export type Resolver<Source extends keyof DataSourceRegistry, T = DataSourceRegistry[Source]> = (params?: Record<string, unknown>) => Promise<FetchOptionsResponse<T>>;
|
|
16
18
|
/**
|
|
@@ -23,12 +25,16 @@ type ExtractDynamicSourcesFromArray<Elements> = Elements extends readonly [
|
|
|
23
25
|
] ? ExtractDynamicSources<First> | ExtractDynamicSourcesFromArray<Rest> : never;
|
|
24
26
|
/**
|
|
25
27
|
* Map of resolver functions for a form's dynamic data sources.
|
|
28
|
+
*
|
|
29
|
+
* @public
|
|
26
30
|
*/
|
|
27
31
|
export type ResolverMap<Sources extends string> = {
|
|
28
32
|
[S in Sources]: S extends keyof DataSourceRegistry ? Resolver<S> : (params?: Record<string, unknown>) => Promise<FetchOptionsResponse>;
|
|
29
33
|
};
|
|
30
34
|
/**
|
|
31
35
|
* A resolver registry that provides type-safe access to resolvers.
|
|
36
|
+
*
|
|
37
|
+
* @public
|
|
32
38
|
*/
|
|
33
39
|
export interface ResolverRegistry<Sources extends string> {
|
|
34
40
|
/**
|
|
@@ -79,6 +85,8 @@ export interface ResolverRegistry<Sources extends string> {
|
|
|
79
85
|
* @param form - The FormSpec containing dynamic enum fields
|
|
80
86
|
* @param resolvers - Map of resolver functions for each data source
|
|
81
87
|
* @returns A ResolverRegistry for type-safe access to resolvers
|
|
88
|
+
*
|
|
89
|
+
* @public
|
|
82
90
|
*/
|
|
83
91
|
export declare function defineResolvers<E extends readonly FormElement[], Sources extends string = ExtractDynamicSourcesFromArray<E> & string>(form: FormSpec<E>, resolvers: ResolverMap<Sources>): ResolverRegistry<Sources>;
|
|
84
92
|
export {};
|
package/dist/resolvers.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolvers.d.ts","sourceRoot":"","sources":["../src/resolvers.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EACV,kBAAkB,EAClB,oBAAoB,EACpB,WAAW,EACX,QAAQ,EACR,gBAAgB,EAChB,KAAK,EACL,WAAW,EACZ,MAAM,gBAAgB,CAAC;AAExB
|
|
1
|
+
{"version":3,"file":"resolvers.d.ts","sourceRoot":"","sources":["../src/resolvers.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EACV,kBAAkB,EAClB,oBAAoB,EACpB,WAAW,EACX,QAAQ,EACR,gBAAgB,EAChB,KAAK,EACL,WAAW,EACZ,MAAM,gBAAgB,CAAC;AAExB;;;;;;;GAOG;AACH,MAAM,MAAM,QAAQ,CAAC,MAAM,SAAS,MAAM,kBAAkB,EAAE,CAAC,GAAG,kBAAkB,CAAC,MAAM,CAAC,IAAI,CAC9F,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAC7B,OAAO,CAAC,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAC;AAEtC;;GAEG;AACH,KAAK,qBAAqB,CAAC,CAAC,IAC1B,CAAC,SAAS,gBAAgB,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GACvC,CAAC,GACD,CAAC,SAAS,KAAK,CAAC,MAAM,QAAQ,CAAC,GAC7B,8BAA8B,CAAC,QAAQ,CAAC,GACxC,CAAC,SAAS,WAAW,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC,GACpD,8BAA8B,CAAC,QAAQ,CAAC,GACxC,KAAK,CAAC;AAEhB,KAAK,8BAA8B,CAAC,QAAQ,IAAI,QAAQ,SAAS,SAAS;IACxE,MAAM,KAAK;IACX,GAAG,MAAM,IAAI;CACd,GACG,qBAAqB,CAAC,KAAK,CAAC,GAAG,8BAA8B,CAAC,IAAI,CAAC,GACnE,KAAK,CAAC;AAEV;;;;GAIG;AACH,MAAM,MAAM,WAAW,CAAC,OAAO,SAAS,MAAM,IAAI;KAC/C,CAAC,IAAI,OAAO,GAAG,CAAC,SAAS,MAAM,kBAAkB,GAC9C,QAAQ,CAAC,CAAC,CAAC,GACX,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,CAAC,oBAAoB,CAAC;CACxE,CAAC;AAEF;;;;GAIG;AACH,MAAM,WAAW,gBAAgB,CAAC,OAAO,SAAS,MAAM;IACtD;;OAEG;IACH,GAAG,CAAC,CAAC,SAAS,OAAO,EACnB,MAAM,EAAE,CAAC,GACR,CAAC,SAAS,MAAM,kBAAkB,GACjC,QAAQ,CAAC,CAAC,CAAC,GACX,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,CAAC,oBAAoB,CAAC,CAAC;IAExE;;OAEG;IACH,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;IAE7B;;OAEG;IACH,OAAO,IAAI,OAAO,EAAE,CAAC;CACtB;AAwBD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,wBAAgB,eAAe,CAC7B,CAAC,SAAS,SAAS,WAAW,EAAE,EAChC,OAAO,SAAS,MAAM,GAAG,8BAA8B,CAAC,CAAC,CAAC,GAAG,MAAM,EACnE,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,WAAW,CAAC,OAAO,CAAC,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAgC/E"}
|
package/dist/runtime.d.ts
CHANGED
|
@@ -76,6 +76,8 @@ import type { Group } from '@formspec/core';
|
|
|
76
76
|
* @param form - The FormSpec containing dynamic enum fields
|
|
77
77
|
* @param resolvers - Map of resolver functions for each data source
|
|
78
78
|
* @returns A ResolverRegistry for type-safe access to resolvers
|
|
79
|
+
*
|
|
80
|
+
* @public
|
|
79
81
|
*/
|
|
80
82
|
export declare function defineResolvers<E extends readonly FormElement[], Sources extends string = ExtractDynamicSourcesFromArray<E> & string>(form: FormSpec<E>, resolvers: ResolverMap<Sources>): ResolverRegistry<Sources>;
|
|
81
83
|
|
|
@@ -94,11 +96,15 @@ infer First,
|
|
|
94
96
|
*
|
|
95
97
|
* @typeParam Source - The data source key from DataSourceRegistry
|
|
96
98
|
* @typeParam T - The data type for options (from DataSourceRegistry)
|
|
99
|
+
*
|
|
100
|
+
* @public
|
|
97
101
|
*/
|
|
98
102
|
export declare type Resolver<Source extends keyof DataSourceRegistry, T = DataSourceRegistry[Source]> = (params?: Record<string, unknown>) => Promise<FetchOptionsResponse<T>>;
|
|
99
103
|
|
|
100
104
|
/**
|
|
101
105
|
* Map of resolver functions for a form's dynamic data sources.
|
|
106
|
+
*
|
|
107
|
+
* @public
|
|
102
108
|
*/
|
|
103
109
|
export declare type ResolverMap<Sources extends string> = {
|
|
104
110
|
[S in Sources]: S extends keyof DataSourceRegistry ? Resolver<S> : (params?: Record<string, unknown>) => Promise<FetchOptionsResponse>;
|
|
@@ -106,6 +112,8 @@ export declare type ResolverMap<Sources extends string> = {
|
|
|
106
112
|
|
|
107
113
|
/**
|
|
108
114
|
* A resolver registry that provides type-safe access to resolvers.
|
|
115
|
+
*
|
|
116
|
+
* @public
|
|
109
117
|
*/
|
|
110
118
|
export declare interface ResolverRegistry<Sources extends string> {
|
|
111
119
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@formspec/runtime",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.21",
|
|
4
4
|
"description": "Runtime helpers for FormSpec - resolvers and data fetching",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -18,11 +18,11 @@
|
|
|
18
18
|
"README.md"
|
|
19
19
|
],
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@formspec/core": "0.1.0-alpha.
|
|
21
|
+
"@formspec/core": "0.1.0-alpha.21"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"vitest": "^3.0.0",
|
|
25
|
-
"@formspec/dsl": "0.1.0-alpha.
|
|
25
|
+
"@formspec/dsl": "0.1.0-alpha.21"
|
|
26
26
|
},
|
|
27
27
|
"publishConfig": {
|
|
28
28
|
"access": "public"
|