Skip to content

Commit 6de36b7

Browse files
committed
feat: Generate SeamHttpEndpoints
1 parent eb8e0b3 commit 6de36b7

4 files changed

Lines changed: 64 additions & 6 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: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ 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,
@@ -12,7 +16,9 @@ interface Metadata {
1216
blueprint: Blueprint
1317
}
1418

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

1723
const rootPath = 'src/lib/seam/connect/routes'
1824

@@ -34,15 +40,22 @@ export const connect = (
3440

3541
const routeIndexes: Record<string, Set<string>> = {}
3642

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

4349
routeIndexes[''] ??= new Set()
4450
routeIndexes['']?.add('seam-http.js')
4551

52+
const endpointsKey = `${rootPath}/seam-http-endpoints.ts`
53+
files[endpointsKey] = { contents: Buffer.from('\n') }
54+
const endpointFile = files[endpointsKey] as unknown as File
55+
endpointFile.layout = 'endpoints.hbs'
56+
setEndpointsLayoutContext(endpointFile, blueprint.routes)
57+
routeIndexes['']?.add('seam-http-endpoints.js')
58+
4659
for (const node of nodes) {
4760
const path = toFilePath(node.path)
4861
const name = kebabCase(node.name)

codegen/lib/layouts/endpoints.ts

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

codegen/lib/layouts/route.ts

Lines changed: 2 additions & 2 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
@@ -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 => {

0 commit comments

Comments
 (0)