Skip to content

Commit c3228d0

Browse files
committed
feat: Generate SeamHttpEndpoints
1 parent eb8e0b3 commit c3228d0

5 files changed

Lines changed: 81 additions & 15 deletions

File tree

codegen/layouts/endpoints.hbs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Automatically generated by codegen/smith.ts.
3+
* Do not edit this file or add other files to this directory.
4+
*/
5+
6+
{{> route-imports }}
7+
8+
{{#each routes}}
9+
import { {{className}} } from './{{fileName}}'
10+
{{/each}}
11+
12+
export class SeamHttpEndpoints {
13+
{{> route-class-methods }}
14+
15+
{{#each endpoints}}
16+
{{> route-class-endpoint }}
17+
18+
{{/each}}
19+
}

codegen/lib/connect.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@ import type { Blueprint } from '@seamapi/blueprint'
22
import { kebabCase } from 'change-case'
33
import type Metalsmith from 'metalsmith'
44

5+
import {
6+
type EndpointsLayoutContext,
7+
setEndpointsLayoutContext,
8+
} from './layouts/endpoints.js'
59
import {
610
type RouteIndexLayoutContext,
711
type RouteLayoutContext,
812
setRouteLayoutContext,
913
} from './layouts/route.js'
14+
import { toFilePath } from './path.js'
1015

1116
interface Metadata {
1217
blueprint: Blueprint
1318
}
1419

15-
type File = RouteLayoutContext & RouteIndexLayoutContext & { layout: string }
20+
type File = RouteLayoutContext &
21+
RouteIndexLayoutContext &
22+
EndpointsLayoutContext & { layout: string }
1623

1724
const rootPath = 'src/lib/seam/connect/routes'
1825

@@ -34,15 +41,22 @@ export const connect = (
3441

3542
const routeIndexes: Record<string, Set<string>> = {}
3643

37-
const rootRouteKey = `${rootPath}/seam-http.ts`
38-
files[rootRouteKey] = { contents: Buffer.from('\n') }
39-
const file = files[rootRouteKey] as unknown as File
44+
const k = `${rootPath}/seam-http.ts`
45+
files[k] = { contents: Buffer.from('\n') }
46+
const file = files[k] as unknown as File
4047
file.layout = 'route.hbs'
4148
setRouteLayoutContext(file, null, nodes)
4249

4350
routeIndexes[''] ??= new Set()
4451
routeIndexes['']?.add('seam-http.js')
4552

53+
const endpointsKey = `${rootPath}/seam-http-endpoints.ts`
54+
files[endpointsKey] = { contents: Buffer.from('\n') }
55+
const endpointFile = files[endpointsKey] as unknown as File
56+
endpointFile.layout = 'endpoints.hbs'
57+
setEndpointsLayoutContext(endpointFile, blueprint.routes)
58+
routeIndexes['']?.add('seam-http-endpoints.js')
59+
4660
for (const node of nodes) {
4761
const path = toFilePath(node.path)
4862
const name = kebabCase(node.name)
@@ -75,10 +89,3 @@ export const connect = (
7589
file.routes = [...routes]
7690
}
7791
}
78-
79-
const toFilePath = (path: string): string =>
80-
path
81-
.slice(1)
82-
.split('/')
83-
.map((p) => kebabCase(p))
84-
.join('/')

codegen/lib/layouts/endpoints.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { Route } from '@seamapi/blueprint'
2+
3+
import { toFilePath } from '../path.js'
4+
import {
5+
type EndpointLayoutContext,
6+
getClassName,
7+
getEndpointLayoutContext,
8+
type SubrouteLayoutContext,
9+
} from './route.js'
10+
11+
export interface EndpointsLayoutContext {
12+
endpoints: EndpointLayoutContext[]
13+
routes: Array<Pick<SubrouteLayoutContext, 'className' | 'fileName'>>
14+
skipClientSessionImport: boolean
15+
}
16+
17+
export const setEndpointsLayoutContext = (
18+
file: Partial<EndpointsLayoutContext>,
19+
routes: Route[],
20+
): void => {
21+
file.skipClientSessionImport = true
22+
file.routes = routes.map(({ name, path }) => ({
23+
className: getClassName(name),
24+
fileName: `${toFilePath(path)}/index.js`,
25+
}))
26+
file.endpoints = routes.flatMap((r) =>
27+
r.endpoints.map((e) => ({
28+
...getEndpointLayoutContext(e, r),
29+
methodName: `get['${e.path}']`,
30+
})),
31+
)
32+
}

codegen/lib/layouts/route.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface RouteIndexLayoutContext {
1313
routes: string[]
1414
}
1515

16-
interface EndpointLayoutContext {
16+
export interface EndpointLayoutContext {
1717
path: string
1818
methodName: string
1919
method: Method
@@ -30,7 +30,7 @@ interface EndpointLayoutContext {
3030
isOptionalParamsOk: boolean
3131
}
3232

33-
interface SubrouteLayoutContext {
33+
export interface SubrouteLayoutContext {
3434
methodName: string
3535
className: string
3636
fileName: string
@@ -71,7 +71,7 @@ const getSubrouteLayoutContext = (
7171
}
7272
}
7373

74-
const getEndpointLayoutContext = (
74+
export const getEndpointLayoutContext = (
7575
endpoint: Endpoint,
7676
route: Pick<Route, 'path' | 'name'>,
7777
): EndpointLayoutContext => {
@@ -129,5 +129,5 @@ const getResponseContext = (
129129
}
130130
}
131131

132-
const getClassName = (name: string | null): string =>
132+
export const getClassName = (name: string | null): string =>
133133
`SeamHttp${pascalCase(name ?? '')}`

codegen/lib/path.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { kebabCase } from 'change-case'
2+
3+
export const toFilePath = (path: string): string =>
4+
path
5+
.slice(1)
6+
.split('/')
7+
.map((p) => kebabCase(p))
8+
.join('/')

0 commit comments

Comments
 (0)