|
1 | 1 | import { useNuxtApp, useRoute, useRuntimeConfig } from '#imports' |
2 | 2 | import { createFormForgeClient } from '../api' |
3 | | -import type { FormForgeClient, FormForgeClientConfig, FormForgeJsonObject } from '../types' |
| 3 | +import type { FormForgeClient, FormForgeClientConfig, FormForgeJsonObject, FormForgeScopedRouteMap } from '../types' |
4 | 4 |
|
5 | 5 | interface FormForgeNuxtRouteBridge { |
6 | 6 | _route?: { |
@@ -55,7 +55,7 @@ function mergeRouteValues( |
55 | 55 | } |
56 | 56 |
|
57 | 57 | function templateSegmentKey(value: string): string | null { |
58 | | - const matched = value.match(/^\{([a-zA-Z0-9_]+)\}$/) |
| 58 | + const matched = value.match(/^\{([a-zA-Z0-9_]+)(?::[^}]+)?\}$/) |
59 | 59 | return matched?.[1] ?? null |
60 | 60 | } |
61 | 61 |
|
@@ -154,6 +154,25 @@ function withInferredMissingValues( |
154 | 154 | return resolved |
155 | 155 | } |
156 | 156 |
|
| 157 | +function inferScopeSourcesFromPath( |
| 158 | + scopedRoutes: FormForgeScopedRouteMap | undefined, |
| 159 | + currentPath: string | undefined |
| 160 | +): Record<string, string> { |
| 161 | + const inferredSources: Record<string, string> = {} |
| 162 | + |
| 163 | + for (const scopedRoute of Object.values(scopedRoutes ?? {})) { |
| 164 | + const inferredScopeParams = inferParamsFromPath(scopedRoute.prefix, currentPath) |
| 165 | + for (const [scopeParam, sourceParam] of Object.entries(scopedRoute.paramsFromRoute)) { |
| 166 | + const inferredValue = inferredScopeParams[scopeParam] |
| 167 | + if (inferredValue !== undefined && inferredValue !== '' && inferredSources[sourceParam] === undefined) { |
| 168 | + inferredSources[sourceParam] = inferredValue |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + return inferredSources |
| 174 | +} |
| 175 | + |
157 | 176 | export function useFormForgeClient(config: FormForgeClientConfig = {}): FormForgeClient { |
158 | 177 | const nuxtApp = useNuxtApp() as ReturnType<typeof useNuxtApp> & FormForgeNuxtRouteBridge |
159 | 178 | const route = useRoute() |
@@ -183,8 +202,13 @@ export function useFormForgeClient(config: FormForgeClientConfig = {}): FormForg |
183 | 202 | ...appRouteParams |
184 | 203 | } |
185 | 204 | ) |
186 | | - const inferredFromPath = inferParamsFromPath(config.baseURL ?? (runtimePublicConfig as FormForgeClientConfig | undefined)?.baseURL, composableRoutePath || appRoutePath) |
187 | | - return withInferredMissingValues(mergedRouteValues, inferredFromPath) |
| 205 | + const resolvedPath = composableRoutePath || appRoutePath |
| 206 | + const resolvedBaseURL = config.baseURL ?? (runtimePublicConfig as FormForgeClientConfig | undefined)?.baseURL |
| 207 | + const resolvedScopedRoutes = config.scopedRoutes ?? (runtimePublicConfig as FormForgeClientConfig | undefined)?.scopedRoutes |
| 208 | + const inferredFromBaseURL = inferParamsFromPath(resolvedBaseURL, resolvedPath) |
| 209 | + const withBaseURLValues = withInferredMissingValues(mergedRouteValues, inferredFromBaseURL) |
| 210 | + const inferredFromScopes = inferScopeSourcesFromPath(resolvedScopedRoutes, resolvedPath) |
| 211 | + return withInferredMissingValues(withBaseURLValues, inferredFromScopes) |
188 | 212 | } |
189 | 213 |
|
190 | 214 | const baseConfig = runtimePublicConfig as FormForgeClientConfig | undefined |
|
0 commit comments