Skip to content

Commit

Permalink
Only use POST or GET
Browse files Browse the repository at this point in the history
  • Loading branch information
razor-x committed Sep 28, 2023
1 parent 135e2a1 commit e0651c8
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 56 deletions.
30 changes: 2 additions & 28 deletions generate-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,6 @@ 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 @@ -150,7 +140,7 @@ const createEndpoint = (
throw new Error(`Did not find ${endpointPath} in OpenAPI spec`)
}
const spec = openapi.paths[endpointPath]
const method = deriveSemanticMethod(endpointPath, Object.keys(spec))
const method = deriveSemanticMethod(Object.keys(spec))
const name = endpointPath.split(routePath)[1]?.slice(1)
if (name == null) {
throw new Error(`Could not parse name from ${endpointPath}`)
Expand Down Expand Up @@ -200,21 +190,8 @@ const deriveGroupFromRoutePath = (routePath: string): string | undefined => {
return parts[0]
}

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
}
const deriveSemanticMethod = (methods: string[]): Method => {
if (methods.includes('get')) return 'GET'
if (methods.includes('delete')) return 'DELETE'
if (methods.includes('patch')) return 'PATCH'
if (methods.includes('put')) return 'PUT'
if (methods.includes('post')) return 'POST'
throw new Error(`Could not find valid method in ${methods.join(', ')}`)
}
Expand All @@ -223,9 +200,6 @@ 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
2 changes: 1 addition & 1 deletion src/lib/seam/connect/routes/access-codes-unmanaged.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class SeamHttpAccessCodesUnmanaged {
async update(body: AccessCodesUnmanagedUpdateBody): Promise<void> {
await this.client.request<AccessCodesUnmanagedUpdateResponse>({
url: '/access_codes/unmanaged/update',
method: 'patch',
method: 'post',
data: body,
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/seam/connect/routes/access-codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class SeamHttpAccessCodes {
async update(body: AccessCodesUpdateBody): Promise<void> {
await this.client.request<AccessCodesUpdateResponse>({
url: '/access_codes/update',
method: 'put',
method: 'post',
data: body,
})
}
Expand Down
11 changes: 7 additions & 4 deletions src/lib/seam/connect/routes/acs-access-groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,15 @@ export class SeamHttpAcsAccessGroups {
return new SeamHttpAcsAccessGroups(opts)
}

async addUser(body: AcsAccessGroupsAddUserBody): Promise<void> {
await this.client.request<AcsAccessGroupsAddUserResponse>({
async addUser(
body: AcsAccessGroupsAddUserBody,
): Promise<AcsAccessGroupsAddUserResponse['acs_access_group']> {
const { data } = await this.client.request<AcsAccessGroupsAddUserResponse>({
url: '/acs/access_groups/add_user',
method: 'patch',
method: 'post',
data: body,
})
return data.acs_access_group
}

async create(
Expand Down Expand Up @@ -136,7 +139,7 @@ export class SeamHttpAcsAccessGroups {
async update(body: AcsAccessGroupsUpdateBody): Promise<void> {
await this.client.request<AcsAccessGroupsUpdateResponse>({
url: '/acs/access_groups/update',
method: 'patch',
method: 'post',
data: body,
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/seam/connect/routes/acs-users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class SeamHttpAcsUsers {
async addToAccessGroup(body: AcsUsersAddToAccessGroupBody): Promise<void> {
await this.client.request<AcsUsersAddToAccessGroupResponse>({
url: '/acs/users/add_to_access_group',
method: 'patch',
method: 'post',
data: body,
})
}
Expand Down Expand Up @@ -124,7 +124,7 @@ export class SeamHttpAcsUsers {
async update(body: AcsUsersUpdateBody): Promise<void> {
await this.client.request<AcsUsersUpdateResponse>({
url: '/acs/users/update',
method: 'patch',
method: 'post',
data: body,
})
}
Expand Down
16 changes: 10 additions & 6 deletions src/lib/seam/connect/routes/client-sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,16 @@ export class SeamHttpClientSessions {
return data.client_session
}

async grantAccess(body: ClientSessionsGrantAccessBody): Promise<void> {
await this.client.request<ClientSessionsGrantAccessResponse>({
url: '/client_sessions/grant_access',
method: 'patch',
data: body,
})
async grantAccess(
body: ClientSessionsGrantAccessBody,
): Promise<ClientSessionsGrantAccessResponse['client_session']> {
const { data } =
await this.client.request<ClientSessionsGrantAccessResponse>({
url: '/client_sessions/grant_access',
method: 'post',
data: body,
})
return data.client_session
}

async list(
Expand Down
2 changes: 1 addition & 1 deletion src/lib/seam/connect/routes/devices-unmanaged.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class SeamHttpDevicesUnmanaged {
async update(body: DevicesUnmanagedUpdateBody): Promise<void> {
await this.client.request<DevicesUnmanagedUpdateResponse>({
url: '/devices/unmanaged/update',
method: 'patch',
method: 'post',
data: body,
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/seam/connect/routes/devices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class SeamHttpDevices {
async update(body: DevicesUpdateBody): Promise<void> {
await this.client.request<DevicesUpdateResponse>({
url: '/devices/update',
method: 'patch',
method: 'post',
data: body,
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class SeamHttpNoiseSensorsNoiseThresholds {
async update(body: NoiseSensorsNoiseThresholdsUpdateBody): Promise<void> {
await this.client.request<NoiseSensorsNoiseThresholdsUpdateResponse>({
url: '/noise_sensors/noise_thresholds/update',
method: 'put',
method: 'post',
data: body,
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
* Do not edit this file or add other files to this directory.
*/

import type {
RouteRequestBody,
RouteRequestParams,
RouteResponse,
} from '@seamapi/types/connect'
import type { RouteRequestBody, RouteResponse } from '@seamapi/types/connect'
import type { Axios } from 'axios'
import type { SetNonNullable } from 'type-fest'

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

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

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

export type ThermostatsClimateSettingSchedulesDeleteResponse = SetNonNullable<
Expand Down

0 comments on commit e0651c8

Please sign in to comment.