-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improved implementation of createRouteMethods
- Loading branch information
1 parent
cbbf863
commit 4869821
Showing
2 changed files
with
51 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,55 @@ | ||
import { Resolved, Route, isPublicRoute } from '@/types' | ||
import { Resolved, Route, RouteMethodRoute, isPublicRoute } from '@/types' | ||
import { assembleUrl } from '@/utilities/urlAssembly' | ||
|
||
export function createRouteMethods(routes: Resolved<Route>[]): Record<string, any> { | ||
return routes.reduce<Record<string, any>>((methods, route) => { | ||
const thing = assembleRouteParentContext(route, [...route.parentNames]) | ||
type NonCallableNode = Record<string, any> | ||
type CallableNode = NonCallableNode & { | ||
(values: Record<string, unknown[]>): RouteMethodRoute, | ||
} | ||
type Node = CallableNode | NonCallableNode | ||
|
||
export function createRouteMethods(routes: Resolved<Route>[]): Record<string, Node> { | ||
const methods: Record<string, any> = {} | ||
|
||
routes.forEach(route => { | ||
const currentLevel = traverseParents(route, methods) | ||
const routeNode = createNodeForRoute(route) | ||
|
||
console.log(JSON.stringify(thing)) | ||
Object.assign(routeNode, currentLevel[route.name]) | ||
|
||
return methods | ||
}, {}) | ||
currentLevel[route.name] = routeNode | ||
}) | ||
|
||
return methods | ||
} | ||
|
||
export function assembleRouteParentContext(route: Resolved<Route>, parentNames: string[]): Record<string, any> { | ||
const nextLevel = parentNames.shift() | ||
function traverseParents(route: Resolved<Route>, currentLevel: Record<string, any>): Record<string, any> { | ||
route.parentNames.forEach(parentName => { | ||
if (!currentLevel[parentName]) { | ||
currentLevel[parentName] = {} | ||
} | ||
|
||
if (!nextLevel) { | ||
return isPublicRoute(route.matched) ? routeMethodInstance(route) : {} | ||
} | ||
currentLevel = currentLevel[parentName] | ||
}) | ||
|
||
return { | ||
[nextLevel]: assembleRouteParentContext(route, parentNames), | ||
} | ||
return currentLevel | ||
} | ||
|
||
export type RouteMethodInstance = (values: Record<string, unknown[]>) => { url: string } | ||
function routeMethodInstance(route: Resolved<Route>): RouteMethodInstance { | ||
return (values) => { | ||
const url = assembleUrl(route, values) | ||
function createNodeForRoute(route: Resolved<Route>): Node { | ||
if (isPublicRoute(route.matched)) { | ||
return createCallableNode(route) | ||
} | ||
|
||
return { url } | ||
return {} | ||
} | ||
|
||
function createCallableNode(route: Resolved<Route>): CallableNode { | ||
const node: CallableNode = (values) => { | ||
return { | ||
push: () => null, | ||
replace: () => null, | ||
url: assembleUrl(route, values), | ||
} | ||
} | ||
|
||
return node | ||
} |