1- import type { Blueprint , Namespace , Route } from '@seamapi/blueprint'
1+ import type { Blueprint , Endpoint , Namespace , Route } from '@seamapi/blueprint'
22import { camelCase , kebabCase , pascalCase } from 'change-case'
33import type Metalsmith from 'metalsmith'
44
@@ -83,14 +83,17 @@ interface RouteLayoutContext {
8383 className : string
8484 endpoints : EndpointLayoutContext [ ]
8585 subroutes : SubrouteLayoutContext [ ]
86- namespaces : SubrouteLayoutContext [ ]
8786 isClientSessionRoute : boolean
88- isActionAttemptsRoute : boolean
89- needsActionAttempts : boolean
9087}
9188
9289interface EndpointLayoutContext {
93- routeName : string
90+ path : string
91+ requestFormat : 'params' | 'body'
92+ requestTypeName : string
93+ responseTypeName : string
94+ requestFormatSuffix : string
95+ optionsTypeName : string
96+ returnsActionAttempt : boolean
9497}
9598
9699interface SubrouteLayoutContext {
@@ -108,20 +111,17 @@ const setRouteLayoutContext = (
108111 blueprint : Blueprint ,
109112) : void => {
110113 file . className = getClassName ( node ?. path ?? null )
111-
112- file . needsActionAttempts =
113- node != null &&
114- 'endpoints' in node &&
115- node . endpoints . some (
116- ( e ) =>
117- e . response . responseType === 'resource' &&
118- e . response . resourceType === 'action_attempt' ,
119- )
120-
121114 file . isClientSessionRoute = node ?. name === '/client_sessions'
122- file . isActionAttemptsRoute = node ?. name === '/action_attempts'
123115
124- file . endpoints = [ ] // TODO
116+ file . endpoints = [ ]
117+ if ( node != null && 'endpoints' in node ) {
118+ const endpoints = node . endpoints . filter (
119+ ( { isUndocumented } ) => ! isUndocumented ,
120+ )
121+ file . endpoints = endpoints . map ( ( endpoint ) =>
122+ getEndpointLayoutContext ( endpoint , node ) ,
123+ )
124+ }
125125
126126 file . subroutes = [ ...blueprint . routes , ...blueprint . namespaces ]
127127 . sort ( ( n1 , n2 ) => n1 . name . localeCompare ( n2 . name ) )
@@ -138,3 +138,29 @@ const getSubrouteLayoutContext = (
138138 className : getClassName ( route . path ) ,
139139 }
140140}
141+
142+ const getEndpointLayoutContext = (
143+ endpoint : Endpoint ,
144+ route : Pick < Route , 'name' > ,
145+ ) : EndpointLayoutContext => {
146+ const prefix = `${ pascalCase ( route . name ) } ${ pascalCase ( endpoint . name ) } `
147+
148+ const requestFormat = [ 'GET' , 'DELETE' ] . includes (
149+ endpoint . request . semanticMethod ,
150+ )
151+ ? 'params'
152+ : 'body'
153+
154+ const requestFormatSuffix = pascalCase ( requestFormat )
155+ return {
156+ path : endpoint . path ,
157+ requestFormat,
158+ requestFormatSuffix,
159+ returnsActionAttempt :
160+ endpoint . response . responseType === 'resource' &&
161+ endpoint . response . resourceType === 'action_attempt' ,
162+ requestTypeName : `${ prefix } ${ requestFormatSuffix } ` ,
163+ responseTypeName : `${ prefix } Response` ,
164+ optionsTypeName : `${ prefix } Options` ,
165+ }
166+ }
0 commit comments