@@ -41,6 +41,16 @@ const ignoredEndpointPaths = [
4141 '/noise_sensors/simulate/trigger_noise_threshold' ,
4242]
4343
44+ const endpointMethods : Partial < Record < keyof typeof openapi . paths , Method > > = {
45+ '/access_codes/create_multiple' : 'POST' ,
46+ '/access_codes/unmanaged/convert_to_managed' : 'POST' ,
47+ // '/access_codes/update': 'PATCH',
48+ '/client_sessions/create' : 'POST' ,
49+ // '/noise_sensors/noise_thresholds/update': 'PATCH',
50+ '/thermostats/climate_setting_schedules/delete' : 'DELETE' ,
51+ // '/thermostats/climate_setting_schedules/update': 'PATCH',
52+ }
53+
4454const endpointResources : Partial <
4555 Record <
4656 keyof typeof openapi . paths ,
@@ -137,7 +147,7 @@ const createEndpoint = (
137147 throw new Error ( `Did not find ${ endpointPath } in OpenAPI spec` )
138148 }
139149 const spec = openapi . paths [ endpointPath ]
140- const method = deriveSemanticMethod ( Object . keys ( spec ) )
150+ const method = deriveSemanticMethod ( endpointPath , Object . keys ( spec ) )
141151 const name = endpointPath . split ( routePath ) [ 1 ] ?. slice ( 1 )
142152 if ( name == null ) {
143153 throw new Error ( `Could not parse name from ${ endpointPath } ` )
@@ -158,10 +168,8 @@ const deriveResource = (
158168 name : string ,
159169 method : Method ,
160170) : string | null => {
161- if ( endpointPath in endpointResources ) {
162- return (
163- endpointResources [ endpointPath as keyof typeof endpointResources ] ?? null
164- )
171+ if ( isEndpointResource ( endpointPath ) ) {
172+ return endpointResources [ endpointPath ] ?? null
165173 }
166174 if ( [ 'DELETE' , 'PATCH' , 'PUT' ] . includes ( method ) ) return null
167175 if ( [ 'update' , 'delete' ] . includes ( name ) ) return null
@@ -189,7 +197,17 @@ const deriveGroupFromRoutePath = (routePath: string): string | undefined => {
189197 return parts [ 0 ]
190198}
191199
192- const deriveSemanticMethod = ( methods : string [ ] ) : Method => {
200+ const deriveSemanticMethod = (
201+ endpointPath : string ,
202+ methods : string [ ] ,
203+ ) : Method => {
204+ if ( isEndpointMethod ( endpointPath ) ) {
205+ const endpointMethod = endpointMethods [ endpointPath ]
206+ if ( endpointMethod == null ) {
207+ throw new Error ( `Got undefined method for ${ endpointMethod } ` )
208+ }
209+ return endpointMethod
210+ }
193211 if ( methods . includes ( 'get' ) ) return 'GET'
194212 if ( methods . includes ( 'delete' ) ) return 'DELETE'
195213 if ( methods . includes ( 'patch' ) ) return 'PATCH'
@@ -198,6 +216,13 @@ const deriveSemanticMethod = (methods: string[]): Method => {
198216 throw new Error ( `Could not find valid method in ${ methods . join ( ', ' ) } ` )
199217}
200218
219+ const isEndpointResource = (
220+ key : string ,
221+ ) : key is keyof typeof endpointResources => key in endpointResources
222+
223+ const isEndpointMethod = ( key : string ) : key is keyof typeof endpointMethods =>
224+ key in endpointMethods
225+
201226const isOpenApiPath = ( key : string ) : key is keyof typeof openapi . paths =>
202227 key in openapi . paths
203228
0 commit comments