From 394e65788c11799c7347138f54394424935e6c38 Mon Sep 17 00:00:00 2001 From: Evan Sosenko Date: Tue, 26 Sep 2023 16:50:42 -0700 Subject: [PATCH] Add endpointMethods overrides --- generate-routes.ts | 37 ++++++++++++++++--- .../connect/routes/access-codes-unmanaged.ts | 14 ++++--- src/lib/seam/connect/routes/access-codes.ts | 16 +++++--- .../seam/connect/routes/client-sessions.ts | 9 +++-- .../thermostats-climate-setting-schedules.ts | 16 +++++--- 5 files changed, 65 insertions(+), 27 deletions(-) diff --git a/generate-routes.ts b/generate-routes.ts index 1431fd62..5655c06d 100644 --- a/generate-routes.ts +++ b/generate-routes.ts @@ -41,6 +41,16 @@ const ignoredEndpointPaths = [ '/noise_sensors/simulate/trigger_noise_threshold', ] +const endpointMethods: Partial> = { + '/access_codes/create_multiple': 'POST', + '/access_codes/unmanaged/convert_to_managed': 'POST', + // '/access_codes/update': 'PATCH', + '/client_sessions/create': 'POST', + // '/noise_sensors/noise_thresholds/update': 'PATCH', + '/thermostats/climate_setting_schedules/delete': 'DELETE', + // '/thermostats/climate_setting_schedules/update': 'PATCH', +} + const endpointResources: Partial< Record< keyof typeof openapi.paths, @@ -137,7 +147,7 @@ const createEndpoint = ( throw new Error(`Did not find ${endpointPath} in OpenAPI spec`) } const spec = openapi.paths[endpointPath] - const method = deriveSemanticMethod(Object.keys(spec)) + const method = deriveSemanticMethod(endpointPath, Object.keys(spec)) const name = endpointPath.split(routePath)[1]?.slice(1) if (name == null) { throw new Error(`Could not parse name from ${endpointPath}`) @@ -158,10 +168,8 @@ const deriveResource = ( name: string, method: Method, ): string | null => { - if (endpointPath in endpointResources) { - return ( - endpointResources[endpointPath as keyof typeof endpointResources] ?? null - ) + if (isEndpointResource(endpointPath)) { + return endpointResources[endpointPath] ?? null } if (['DELETE', 'PATCH', 'PUT'].includes(method)) return null if (['update', 'delete'].includes(name)) return null @@ -189,7 +197,17 @@ const deriveGroupFromRoutePath = (routePath: string): string | undefined => { return parts[0] } -const deriveSemanticMethod = (methods: string[]): Method => { +const deriveSemanticMethod = ( + endpointPath: string, + methods: string[], +): Method => { + if (isEndpointMethod(endpointPath)) { + const endpointMethod = endpointMethods[endpointPath] + if (endpointMethod == null) { + throw new Error(`Got undefined method for ${endpointMethod}`) + } + return endpointMethod + } if (methods.includes('get')) return 'GET' if (methods.includes('delete')) return 'DELETE' if (methods.includes('patch')) return 'PATCH' @@ -198,6 +216,13 @@ const deriveSemanticMethod = (methods: string[]): Method => { throw new Error(`Could not find valid method in ${methods.join(', ')}`) } +const isEndpointResource = ( + key: string, +): key is keyof typeof endpointResources => key in endpointResources + +const isEndpointMethod = (key: string): key is keyof typeof endpointMethods => + key in endpointMethods + const isOpenApiPath = (key: string): key is keyof typeof openapi.paths => key in openapi.paths diff --git a/src/lib/seam/connect/routes/access-codes-unmanaged.ts b/src/lib/seam/connect/routes/access-codes-unmanaged.ts index 802dea1f..71ed4644 100644 --- a/src/lib/seam/connect/routes/access-codes-unmanaged.ts +++ b/src/lib/seam/connect/routes/access-codes-unmanaged.ts @@ -66,12 +66,14 @@ export class SeamHttpAccessCodesUnmanaged { async convertToManaged( body: AccessCodesUnmanagedConvertToManagedBody, - ): Promise { - await this.client.request({ - url: '/access_codes/unmanaged/convert_to_managed', - method: 'patch', - data: body, - }) + ): Promise { + const { data } = + await this.client.request({ + url: '/access_codes/unmanaged/convert_to_managed', + method: 'post', + data: body, + }) + return data.access_code } async delete(body: AccessCodesUnmanagedDeleteBody): Promise { diff --git a/src/lib/seam/connect/routes/access-codes.ts b/src/lib/seam/connect/routes/access-codes.ts index 2d2abacf..76f4a3f7 100644 --- a/src/lib/seam/connect/routes/access-codes.ts +++ b/src/lib/seam/connect/routes/access-codes.ts @@ -79,12 +79,16 @@ export class SeamHttpAccessCodes { return data.access_code } - async createMultiple(body: AccessCodesCreateMultipleBody): Promise { - await this.client.request({ - url: '/access_codes/create_multiple', - method: 'put', - data: body, - }) + async createMultiple( + body: AccessCodesCreateMultipleBody, + ): Promise { + const { data } = + await this.client.request({ + url: '/access_codes/create_multiple', + method: 'post', + data: body, + }) + return data.access_code } async delete(body: AccessCodesDeleteBody): Promise { diff --git a/src/lib/seam/connect/routes/client-sessions.ts b/src/lib/seam/connect/routes/client-sessions.ts index 9999b2ea..09cce7d0 100644 --- a/src/lib/seam/connect/routes/client-sessions.ts +++ b/src/lib/seam/connect/routes/client-sessions.ts @@ -64,12 +64,15 @@ export class SeamHttpClientSessions { return new SeamHttpClientSessions(opts) } - async create(body: ClientSessionsCreateBody): Promise { - await this.client.request({ + async create( + body: ClientSessionsCreateBody, + ): Promise { + const { data } = await this.client.request({ url: '/client_sessions/create', - method: 'put', + method: 'post', data: body, }) + return data.client_session } async delete(body: ClientSessionsDeleteBody): Promise { diff --git a/src/lib/seam/connect/routes/thermostats-climate-setting-schedules.ts b/src/lib/seam/connect/routes/thermostats-climate-setting-schedules.ts index 8d08ad7e..24cfa241 100644 --- a/src/lib/seam/connect/routes/thermostats-climate-setting-schedules.ts +++ b/src/lib/seam/connect/routes/thermostats-climate-setting-schedules.ts @@ -3,7 +3,11 @@ * Do not edit this file or add other files to this directory. */ -import type { RouteRequestBody, RouteResponse } from '@seamapi/types/connect' +import type { + RouteRequestBody, + RouteRequestParams, + RouteResponse, +} from '@seamapi/types/connect' import type { Axios } from 'axios' import type { SetNonNullable } from 'type-fest' @@ -81,13 +85,13 @@ export class SeamHttpThermostatsClimateSettingSchedules { } async delete( - body: ThermostatsClimateSettingSchedulesDeleteBody, + params?: ThermostatsClimateSettingSchedulesDeleteParams, ): Promise { await this.client.request( { url: '/thermostats/climate_setting_schedules/delete', - method: 'put', - data: body, + method: 'delete', + params, }, ) } @@ -143,8 +147,8 @@ export type ThermostatsClimateSettingSchedulesCreateResponse = SetNonNullable< Required> > -export type ThermostatsClimateSettingSchedulesDeleteBody = SetNonNullable< - Required> +export type ThermostatsClimateSettingSchedulesDeleteParams = SetNonNullable< + Required> > export type ThermostatsClimateSettingSchedulesDeleteResponse = SetNonNullable<