Skip to content

Commit c1fea46

Browse files
committed
Generate subroutes and namespaces
1 parent 83d6b7e commit c1fea46

3 files changed

Lines changed: 39 additions & 29 deletions

File tree

codegen/lib/connect.ts

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { Blueprint, Route } from '@seamapi/blueprint'
2-
import { kebabCase, pascalCase } from 'change-case'
1+
import type { Blueprint, Namespace, Route } from '@seamapi/blueprint'
2+
import { camelCase, kebabCase, pascalCase } from 'change-case'
33
import type Metalsmith from 'metalsmith'
44

55
interface Metadata {
@@ -19,11 +19,12 @@ export const connect = (
1919

2020
const rootRouteKey = `${rootPath}/seam-http.js`
2121
files[rootRouteKey] = { contents: Buffer.from('\n') }
22-
const rootRouteFile = files[rootRouteKey] as unknown as File
23-
rootRouteFile.layout = 'route.hbs'
24-
setRootRouteLayoutContext(rootRouteFile, blueprint)
22+
const file = files[rootRouteKey] as unknown as File
23+
file.layout = 'route.hbs'
24+
setRouteLayoutContext(file, null, blueprint)
2525

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

2829
for (const route of Object.values(blueprint.routes ?? {})) {
2930
const k = `${rootPath}/${route.path
@@ -62,6 +63,7 @@ interface RouteLayoutContext {
6263
className: string
6364
endpoints: EndpointLayoutContext[]
6465
subroutes: SubrouteLayoutContext[]
66+
namespaces: SubrouteLayoutContext[]
6567
isClientSessionRoute: boolean
6668
isActionAttemptsRoute: boolean
6769
needsActionAttempts: boolean
@@ -76,27 +78,35 @@ interface SubrouteLayoutContext {
7678
className: string
7779
}
7880

79-
const setRootRouteLayoutContext = (
80-
file: Partial<RouteLayoutContext>,
81-
_blueprint: Blueprint,
82-
): void => {
83-
file.className = 'SeamHttp'
84-
file.needsActionAttempts = false
85-
file.isClientSessionRoute = false
86-
file.isActionAttemptsRoute = false
87-
file.endpoints = []
88-
file.subroutes = []
89-
}
81+
const getClassName = (name: string | null): string =>
82+
`SeamHttp${pascalCase(name ?? '')}`
9083

9184
const setRouteLayoutContext = (
9285
file: Partial<RouteLayoutContext>,
93-
route: Route,
94-
_blueprint: Blueprint,
86+
route: Route | null,
87+
blueprint: Blueprint,
9588
): void => {
96-
file.className = `SeamHttp${pascalCase(route.name)}`
97-
file.needsActionAttempts = false
98-
file.isClientSessionRoute = route.name === '/client_sessions'
99-
file.isActionAttemptsRoute = route.name === '/action_attempts'
100-
file.endpoints = []
101-
file.subroutes = []
89+
file.className = getClassName(route?.name ?? null)
90+
file.needsActionAttempts = false // TODO
91+
file.isClientSessionRoute = route?.name === '/client_sessions'
92+
file.isActionAttemptsRoute = route?.name === '/action_attempts'
93+
94+
file.endpoints = [] // TODO
95+
96+
file.namespaces = blueprint.namespaces
97+
.filter(({ parentPath }) => parentPath === (route?.path ?? null))
98+
.map((r) => getSubrouteLayoutContext(r))
99+
100+
file.subroutes = blueprint.routes
101+
.filter(({ parentPath }) => parentPath === (route?.path ?? null))
102+
.map((r) => getSubrouteLayoutContext(r))
103+
}
104+
105+
const getSubrouteLayoutContext = (
106+
node: Route | Namespace,
107+
): SubrouteLayoutContext => {
108+
return {
109+
methodName: camelCase(node.name),
110+
className: getClassName(node.path),
111+
}
102112
}

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
"axios-retry": "^4.4.2"
9898
},
9999
"devDependencies": {
100-
"@seamapi/blueprint": "^0.48.0",
100+
"@seamapi/blueprint": "^0.48.1",
101101
"@seamapi/fake-seam-connect": "^1.77.0",
102102
"@seamapi/smith": "^0.4.4",
103103
"@seamapi/types": "^1.413.0",

0 commit comments

Comments
 (0)