Skip to content

Commit

Permalink
Add endpointMethods overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
razor-x committed Sep 26, 2023
1 parent fd3a4eb commit 394e657
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 27 deletions.
37 changes: 31 additions & 6 deletions generate-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ const ignoredEndpointPaths = [
'/noise_sensors/simulate/trigger_noise_threshold',
]

const endpointMethods: Partial<Record<keyof typeof openapi.paths, Method>> = {
'/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,
Expand Down Expand Up @@ -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}`)
Expand All @@ -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
Expand Down Expand Up @@ -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'
Expand All @@ -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

Expand Down
14 changes: 8 additions & 6 deletions src/lib/seam/connect/routes/access-codes-unmanaged.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,14 @@ export class SeamHttpAccessCodesUnmanaged {

async convertToManaged(
body: AccessCodesUnmanagedConvertToManagedBody,
): Promise<void> {
await this.client.request<AccessCodesUnmanagedConvertToManagedResponse>({
url: '/access_codes/unmanaged/convert_to_managed',
method: 'patch',
data: body,
})
): Promise<AccessCodesUnmanagedConvertToManagedResponse['access_code']> {

Check failure on line 69 in src/lib/seam/connect/routes/access-codes-unmanaged.ts

View workflow job for this annotation

GitHub Actions / Typecheck (Node.js v16)

Property 'access_code' does not exist on type 'SetNonNullable<Required<{}>, never>'.

Check failure on line 69 in src/lib/seam/connect/routes/access-codes-unmanaged.ts

View workflow job for this annotation

GitHub Actions / Typecheck (Node.js v18)

Property 'access_code' does not exist on type 'SetNonNullable<Required<{}>, never>'.

Check failure on line 69 in src/lib/seam/connect/routes/access-codes-unmanaged.ts

View workflow job for this annotation

GitHub Actions / Build / Package

Property 'access_code' does not exist on type 'SetNonNullable<Required<{}>, never>'.
const { data } =
await this.client.request<AccessCodesUnmanagedConvertToManagedResponse>({
url: '/access_codes/unmanaged/convert_to_managed',
method: 'post',
data: body,
})
return data.access_code

Check failure on line 76 in src/lib/seam/connect/routes/access-codes-unmanaged.ts

View workflow job for this annotation

GitHub Actions / Typecheck (Node.js v16)

Property 'access_code' does not exist on type 'SetNonNullable<Required<{}>, never>'.

Check failure on line 76 in src/lib/seam/connect/routes/access-codes-unmanaged.ts

View workflow job for this annotation

GitHub Actions / Typecheck (Node.js v18)

Property 'access_code' does not exist on type 'SetNonNullable<Required<{}>, never>'.

Check failure on line 76 in src/lib/seam/connect/routes/access-codes-unmanaged.ts

View workflow job for this annotation

GitHub Actions / Build / Package

Property 'access_code' does not exist on type 'SetNonNullable<Required<{}>, never>'.
}

async delete(body: AccessCodesUnmanagedDeleteBody): Promise<void> {
Expand Down
16 changes: 10 additions & 6 deletions src/lib/seam/connect/routes/access-codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,16 @@ export class SeamHttpAccessCodes {
return data.access_code
}

async createMultiple(body: AccessCodesCreateMultipleBody): Promise<void> {
await this.client.request<AccessCodesCreateMultipleResponse>({
url: '/access_codes/create_multiple',
method: 'put',
data: body,
})
async createMultiple(
body: AccessCodesCreateMultipleBody,
): Promise<AccessCodesCreateMultipleResponse['access_code']> {

Check failure on line 84 in src/lib/seam/connect/routes/access-codes.ts

View workflow job for this annotation

GitHub Actions / Typecheck (Node.js v16)

Property 'access_code' does not exist on type 'SetNonNullable<Required<{ access_codes: { common_code_key: string | null; is_scheduled_on_device?: boolean | undefined; type: "time_bound" | "ongoing"; is_waiting_for_code_assignment?: boolean | undefined; ... 14 more ...; is_external_modification_allowed: boolean; }[]; }>, "access_codes">'.

Check failure on line 84 in src/lib/seam/connect/routes/access-codes.ts

View workflow job for this annotation

GitHub Actions / Typecheck (Node.js v18)

Property 'access_code' does not exist on type 'SetNonNullable<Required<{ access_codes: { common_code_key: string | null; is_scheduled_on_device?: boolean | undefined; type: "time_bound" | "ongoing"; is_waiting_for_code_assignment?: boolean | undefined; ... 14 more ...; is_external_modification_allowed: boolean; }[]; }>, "access_codes">'.

Check failure on line 84 in src/lib/seam/connect/routes/access-codes.ts

View workflow job for this annotation

GitHub Actions / Build / Package

Property 'access_code' does not exist on type 'SetNonNullable<Required<{ access_codes: { common_code_key: string | null; is_scheduled_on_device?: boolean | undefined; type: "time_bound" | "ongoing"; is_waiting_for_code_assignment?: boolean | undefined; ... 14 more ...; is_external_modification_allowed: boolean; }[]; }>, "access_codes">'.
const { data } =
await this.client.request<AccessCodesCreateMultipleResponse>({
url: '/access_codes/create_multiple',
method: 'post',
data: body,
})
return data.access_code

Check failure on line 91 in src/lib/seam/connect/routes/access-codes.ts

View workflow job for this annotation

GitHub Actions / Typecheck (Node.js v16)

Property 'access_code' does not exist on type 'SetNonNullable<Required<{ access_codes: { common_code_key: string | null; is_scheduled_on_device?: boolean | undefined; type: "time_bound" | "ongoing"; is_waiting_for_code_assignment?: boolean | undefined; ... 14 more ...; is_external_modification_allowed: boolean; }[]; }>, "access_codes">'. Did you mean 'access_codes'?

Check failure on line 91 in src/lib/seam/connect/routes/access-codes.ts

View workflow job for this annotation

GitHub Actions / Typecheck (Node.js v18)

Property 'access_code' does not exist on type 'SetNonNullable<Required<{ access_codes: { common_code_key: string | null; is_scheduled_on_device?: boolean | undefined; type: "time_bound" | "ongoing"; is_waiting_for_code_assignment?: boolean | undefined; ... 14 more ...; is_external_modification_allowed: boolean; }[]; }>, "access_codes">'. Did you mean 'access_codes'?

Check failure on line 91 in src/lib/seam/connect/routes/access-codes.ts

View workflow job for this annotation

GitHub Actions / Build / Package

Property 'access_code' does not exist on type 'SetNonNullable<Required<{ access_codes: { common_code_key: string | null; is_scheduled_on_device?: boolean | undefined; type: "time_bound" | "ongoing"; is_waiting_for_code_assignment?: boolean | undefined; ... 14 more ...; is_external_modification_allowed: boolean; }[]; }>, "access_codes">'. Did you mean 'access_codes'?
}

async delete(body: AccessCodesDeleteBody): Promise<void> {
Expand Down
9 changes: 6 additions & 3 deletions src/lib/seam/connect/routes/client-sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,15 @@ export class SeamHttpClientSessions {
return new SeamHttpClientSessions(opts)
}

async create(body: ClientSessionsCreateBody): Promise<void> {
await this.client.request<ClientSessionsCreateResponse>({
async create(
body: ClientSessionsCreateBody,
): Promise<ClientSessionsCreateResponse['client_session']> {
const { data } = await this.client.request<ClientSessionsCreateResponse>({
url: '/client_sessions/create',
method: 'put',
method: 'post',
data: body,
})
return data.client_session
}

async delete(body: ClientSessionsDeleteBody): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -81,13 +85,13 @@ export class SeamHttpThermostatsClimateSettingSchedules {
}

async delete(
body: ThermostatsClimateSettingSchedulesDeleteBody,
params?: ThermostatsClimateSettingSchedulesDeleteParams,
): Promise<void> {
await this.client.request<ThermostatsClimateSettingSchedulesDeleteResponse>(
{
url: '/thermostats/climate_setting_schedules/delete',
method: 'put',
data: body,
method: 'delete',
params,
},
)
}
Expand Down Expand Up @@ -143,8 +147,8 @@ export type ThermostatsClimateSettingSchedulesCreateResponse = SetNonNullable<
Required<RouteResponse<'/thermostats/climate_setting_schedules/create'>>
>

export type ThermostatsClimateSettingSchedulesDeleteBody = SetNonNullable<
Required<RouteRequestBody<'/thermostats/climate_setting_schedules/delete'>>
export type ThermostatsClimateSettingSchedulesDeleteParams = SetNonNullable<
Required<RouteRequestParams<'/thermostats/climate_setting_schedules/delete'>>
>

export type ThermostatsClimateSettingSchedulesDeleteResponse = SetNonNullable<
Expand Down

0 comments on commit 394e657

Please sign in to comment.