Skip to content

Commit ff34aa9

Browse files
committed
Generate namespaces
1 parent c1fea46 commit ff34aa9

3 files changed

Lines changed: 59 additions & 14 deletions

File tree

codegen/layouts/partials/route-imports.hbs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,11 @@ import {
4949
import { SeamHttpActionAttempts } from './action-attempts.js'
5050
{{/unless}}
5151
{{/if}}
52+
53+
{{#each subroutes}}
54+
import { {{className}} } from './{{fileName}}'
55+
{{/each}}
56+
57+
{{#each namespaces}}
58+
import { {{className}} } from './{{fileName}}'
59+
{{/each}}

codegen/layouts/route.hbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
class {{className}} {
44
{{> route-class-methods }}
55

6+
{{#each namespaces}}
7+
{{> route-class-subroute }}
8+
{{/each}}
9+
610
{{#each subroutes}}
711
{{> route-class-subroute }}
812
{{/each}}

codegen/lib/connect.ts

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,31 @@ export const connect = (
2424
setRouteLayoutContext(file, null, blueprint)
2525

2626
const routeIndexes: Record<string, Set<string>> = {}
27-
console.log(file)
2827

29-
for (const route of Object.values(blueprint.routes ?? {})) {
28+
const namespaces = blueprint.namespaces.filter(
29+
({ isUndocumented }) => !isUndocumented,
30+
)
31+
const routes = blueprint.routes.filter(
32+
({ isUndocumented }) => !isUndocumented,
33+
)
34+
35+
for (const namespace of namespaces) {
36+
const k = `${rootPath}/${namespace.path
37+
.slice(1)
38+
.split('/')
39+
.map((p) => kebabCase(p))
40+
.join('/')}/${kebabCase(namespace.name)}.ts`
41+
42+
files[k] = { contents: Buffer.from('\n') }
43+
const file = files[k] as unknown as File
44+
file.layout = 'route.hbs'
45+
setRouteLayoutContext(file, namespace, blueprint)
46+
47+
routeIndexes[namespace.path] ??= new Set()
48+
routeIndexes[namespace.path]?.add(`${kebabCase(namespace.name)}.js`)
49+
}
50+
51+
for (const route of routes) {
3052
const k = `${rootPath}/${route.path
3153
.slice(1)
3254
.split('/')
@@ -76,37 +98,48 @@ interface EndpointLayoutContext {
7698
interface SubrouteLayoutContext {
7799
methodName: string
78100
className: string
101+
fileName: string
79102
}
80103

81104
const getClassName = (name: string | null): string =>
82105
`SeamHttp${pascalCase(name ?? '')}`
83106

84107
const setRouteLayoutContext = (
85108
file: Partial<RouteLayoutContext>,
86-
route: Route | null,
109+
node: Route | Namespace | null,
87110
blueprint: Blueprint,
88111
): void => {
89-
file.className = getClassName(route?.name ?? null)
112+
file.className = getClassName(node?.name ?? null)
90113
file.needsActionAttempts = false // TODO
91-
file.isClientSessionRoute = route?.name === '/client_sessions'
92-
file.isActionAttemptsRoute = route?.name === '/action_attempts'
114+
file.isClientSessionRoute = node?.name === '/client_sessions'
115+
file.isActionAttemptsRoute = node?.name === '/action_attempts'
93116

94117
file.endpoints = [] // TODO
95118

96119
file.namespaces = blueprint.namespaces
97-
.filter(({ parentPath }) => parentPath === (route?.path ?? null))
98-
.map((r) => getSubrouteLayoutContext(r))
120+
.filter(({ parentPath }) => parentPath === (node?.path ?? null))
121+
.map((r) => getSubrouteLayoutContextFromNamespace(r))
99122

100123
file.subroutes = blueprint.routes
101-
.filter(({ parentPath }) => parentPath === (route?.path ?? null))
102-
.map((r) => getSubrouteLayoutContext(r))
124+
.filter(({ parentPath }) => parentPath === (node?.path ?? null))
125+
.map((r) => getSubrouteLayoutContextFromRoute(r))
126+
}
127+
128+
const getSubrouteLayoutContextFromRoute = (
129+
route: Pick<Route, 'path' | 'name'>,
130+
): SubrouteLayoutContext => {
131+
return {
132+
fileName: `${kebabCase(route.name)}.js`,
133+
methodName: camelCase(route.name),
134+
className: getClassName(route.path),
135+
}
103136
}
104137

105-
const getSubrouteLayoutContext = (
106-
node: Route | Namespace,
138+
const getSubrouteLayoutContextFromNamespace = (
139+
namespace: Namespace,
107140
): SubrouteLayoutContext => {
108141
return {
109-
methodName: camelCase(node.name),
110-
className: getClassName(node.path),
142+
...getSubrouteLayoutContextFromRoute(namespace),
143+
fileName: `${kebabCase(namespace.name)}/index.js`,
111144
}
112145
}

0 commit comments

Comments
 (0)