Skip to content

Commit

Permalink
Resolve type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
razor-x committed Sep 26, 2023
1 parent 1fe40fc commit 2710e93
Show file tree
Hide file tree
Showing 22 changed files with 350 additions and 332 deletions.
66 changes: 61 additions & 5 deletions generate-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,36 @@ const ignoredEndpointPaths = [
'/noise_sensors/simulate/trigger_noise_threshold',
]

const endpointResources: Partial<
Record<
keyof typeof openapi.paths,
| 'action_attempt'
| 'device_providers'
| 'generated_code'
| 'backup_access_code'
| 'acs_users'
| null
>
> = {
'/access_codes/generate_code': 'generated_code',
'/access_codes/pull_backup_access_code': 'backup_access_code',
'/acs/users/add_to_access_group': null,
'/acs/users/remove_from_access_group': null,
'/acs/access_groups/list_users': 'acs_users',
'/acs/access_groups/remove_user': null,
'/connect_webviews/view': null,
'/devices/list_device_providers': 'device_providers',
'/locks/lock_door': null,
'/locks/unlock_door': null,
'/noise_sensors/noise_thresholds/create': null,
'/thermostats/cool': null,
'/thermostats/heat': null,
'/thermostats/heat_cool': null,
'/thermostats/off': null,
'/thermostats/set_fan_mode': null,
'/workspaces/reset_sandbox': null,
}

interface Route {
namespace: string
endpoints: Endpoint[]
Expand Down Expand Up @@ -117,24 +147,48 @@ const createEndpoint = (
namespace,
path: endpointPath,
method,
resource: deriveResource(routePath, name, method),
resource: deriveResource(endpointPath, routePath, name, method),
requestFormat: ['GET', 'DELETE'].includes(method) ? 'params' : 'body',
}
}

const deriveResource = (
endpointPath: string,
routePath: string,
name: string,
method: Method,
): string | null => {
if (endpointPath in endpointResources) {
return (
endpointResources[endpointPath as keyof typeof endpointResources] ?? null
)
}
if (['DELETE', 'PATCH', 'PUT'].includes(method)) return null
if (['update', 'delete'].includes(name)) return null
const group = routePath.split('/')[1]
const group = deriveGroupFromRoutePath(routePath)
if (group == null) throw new Error(`Could not parse group from ${routePath}`)
if (name === 'list') return group
return pluralize.singular(group)
}

const deriveGroupFromRoutePath = (routePath: string): string | undefined => {
const parts = routePath.split('/').slice(1)

if (routePath.endsWith('/unmanaged')) {
return parts[0]
}

if (routePath.startsWith('/acs')) {
return [parts[0], parts[1]].join('_')
}

if (parts.length === 2) {
return parts[1]
}

return parts[0]
}

const deriveSemanticMethod = (methods: string[]): Method => {
if (methods.includes('get')) return 'GET'
if (methods.includes('delete')) return 'DELETE'
Expand Down Expand Up @@ -213,7 +267,9 @@ const renderClassMethod = ({
path,
}: Endpoint): string => `
async ${camelCase(name)}(
${requestFormat}: ${renderRequestType({
${requestFormat}${
requestFormat === 'params' ? '?' : ''
}: ${renderRequestType({
name,
namespace,
requestFormat,
Expand Down Expand Up @@ -247,15 +303,15 @@ const renderEndpointExports = ({
namespace,
requestFormat,
}: Endpoint): string => `
type ${renderRequestType({
export type ${renderRequestType({
name,
namespace,
requestFormat,
})} = SetNonNullable<
Required<RouteRequest${pascalCase(requestFormat)}<'${path}'>>
>
type ${renderResponseType({ name, namespace })}= SetNonNullable<
export type ${renderResponseType({ name, namespace })}= SetNonNullable<
Required<RouteResponse<'${path}'>>
>
`
Expand Down
3 changes: 2 additions & 1 deletion src/lib/seam/connect/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ export class SeamHttp {
}

get workspaces(): SeamHttpWorkspaces {
if (this.#legacy) return new SeamHttpLegacyWorkspaces(this.client)
if (this.#legacy)
return new SeamHttpLegacyWorkspaces({ client: this.client })
return new SeamHttpWorkspaces({ client: this.client })
}
}
Expand Down
32 changes: 2 additions & 30 deletions src/lib/seam/connect/legacy/workspaces.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,3 @@
import type { RouteRequestParams, RouteResponse } from '@seamapi/types/connect'
import type { Axios } from 'axios'
import type { SetNonNullable } from 'type-fest'
import { SeamHttpWorkspaces } from 'lib/seam/connect/routes/workspaces.js'

export class SeamHttpLegacyWorkspaces {
client: Axios

constructor(client: Axios) {
this.client = client
}

async get(
params: WorkspacesGetParams,
): Promise<WorkspacesGetResponse['workspace']> {
const { data } = await this.client.request<WorkspacesGetResponse>({
url: '/workspaces/get',
method: 'get',
params,
})
return data.workspace
}
}

// TODO: Import from routes so no need to redefine here
type WorkspacesGetParams = SetNonNullable<
Required<RouteRequestParams<'/workspaces/get'>>
>

type WorkspacesGetResponse = SetNonNullable<
Required<RouteResponse<'/workspaces/get'>>
>
export class SeamHttpLegacyWorkspaces extends SeamHttpWorkspaces {}
20 changes: 10 additions & 10 deletions src/lib/seam/connect/routes/access-codes-unmanaged.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,42 +116,42 @@ export class SeamHttpAccessCodesUnmanaged {
}
}

type AccessCodesUnmanagedConvertToManagedBody = SetNonNullable<
export type AccessCodesUnmanagedConvertToManagedBody = SetNonNullable<
Required<RouteRequestBody<'/access_codes/unmanaged/convert_to_managed'>>
>

type AccessCodesUnmanagedConvertToManagedResponse = SetNonNullable<
export type AccessCodesUnmanagedConvertToManagedResponse = SetNonNullable<
Required<RouteResponse<'/access_codes/unmanaged/convert_to_managed'>>
>

type AccessCodesUnmanagedDeleteBody = SetNonNullable<
export type AccessCodesUnmanagedDeleteBody = SetNonNullable<
Required<RouteRequestBody<'/access_codes/unmanaged/delete'>>
>

type AccessCodesUnmanagedDeleteResponse = SetNonNullable<
export type AccessCodesUnmanagedDeleteResponse = SetNonNullable<
Required<RouteResponse<'/access_codes/unmanaged/delete'>>
>

type AccessCodesUnmanagedGetBody = SetNonNullable<
export type AccessCodesUnmanagedGetBody = SetNonNullable<
Required<RouteRequestBody<'/access_codes/unmanaged/get'>>
>

type AccessCodesUnmanagedGetResponse = SetNonNullable<
export type AccessCodesUnmanagedGetResponse = SetNonNullable<
Required<RouteResponse<'/access_codes/unmanaged/get'>>
>

type AccessCodesUnmanagedListBody = SetNonNullable<
export type AccessCodesUnmanagedListBody = SetNonNullable<
Required<RouteRequestBody<'/access_codes/unmanaged/list'>>
>

type AccessCodesUnmanagedListResponse = SetNonNullable<
export type AccessCodesUnmanagedListResponse = SetNonNullable<
Required<RouteResponse<'/access_codes/unmanaged/list'>>
>

type AccessCodesUnmanagedUpdateBody = SetNonNullable<
export type AccessCodesUnmanagedUpdateBody = SetNonNullable<
Required<RouteRequestBody<'/access_codes/unmanaged/update'>>
>

type AccessCodesUnmanagedUpdateResponse = SetNonNullable<
export type AccessCodesUnmanagedUpdateResponse = SetNonNullable<
Required<RouteResponse<'/access_codes/unmanaged/update'>>
>
42 changes: 21 additions & 21 deletions src/lib/seam/connect/routes/access-codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,16 @@ export class SeamHttpAccessCodes {
}

async generateCode(
params: AccessCodesGenerateCodeParams,
): Promise<AccessCodesGenerateCodeResponse['access_code']> {
params?: AccessCodesGenerateCodeParams,
): Promise<AccessCodesGenerateCodeResponse['generated_code']> {
const { data } = await this.client.request<AccessCodesGenerateCodeResponse>(
{
url: '/access_codes/generate_code',
method: 'get',
params,
},
)
return data.access_code
return data.generated_code
}

async get(
Expand All @@ -132,14 +132,14 @@ export class SeamHttpAccessCodes {

async pullBackupAccessCode(
body: AccessCodesPullBackupAccessCodeBody,
): Promise<AccessCodesPullBackupAccessCodeResponse['access_code']> {
): Promise<AccessCodesPullBackupAccessCodeResponse['backup_access_code']> {
const { data } =
await this.client.request<AccessCodesPullBackupAccessCodeResponse>({
url: '/access_codes/pull_backup_access_code',
method: 'post',
data: body,
})
return data.access_code
return data.backup_access_code
}

async update(body: AccessCodesUpdateBody): Promise<void> {
Expand All @@ -151,66 +151,66 @@ export class SeamHttpAccessCodes {
}
}

type AccessCodesCreateBody = SetNonNullable<
export type AccessCodesCreateBody = SetNonNullable<
Required<RouteRequestBody<'/access_codes/create'>>
>

type AccessCodesCreateResponse = SetNonNullable<
export type AccessCodesCreateResponse = SetNonNullable<
Required<RouteResponse<'/access_codes/create'>>
>

type AccessCodesCreateMultipleBody = SetNonNullable<
export type AccessCodesCreateMultipleBody = SetNonNullable<
Required<RouteRequestBody<'/access_codes/create_multiple'>>
>

type AccessCodesCreateMultipleResponse = SetNonNullable<
export type AccessCodesCreateMultipleResponse = SetNonNullable<
Required<RouteResponse<'/access_codes/create_multiple'>>
>

type AccessCodesDeleteBody = SetNonNullable<
export type AccessCodesDeleteBody = SetNonNullable<
Required<RouteRequestBody<'/access_codes/delete'>>
>

type AccessCodesDeleteResponse = SetNonNullable<
export type AccessCodesDeleteResponse = SetNonNullable<
Required<RouteResponse<'/access_codes/delete'>>
>

type AccessCodesGenerateCodeParams = SetNonNullable<
export type AccessCodesGenerateCodeParams = SetNonNullable<
Required<RouteRequestParams<'/access_codes/generate_code'>>
>

type AccessCodesGenerateCodeResponse = SetNonNullable<
export type AccessCodesGenerateCodeResponse = SetNonNullable<
Required<RouteResponse<'/access_codes/generate_code'>>
>

type AccessCodesGetBody = SetNonNullable<
export type AccessCodesGetBody = SetNonNullable<
Required<RouteRequestBody<'/access_codes/get'>>
>

type AccessCodesGetResponse = SetNonNullable<
export type AccessCodesGetResponse = SetNonNullable<
Required<RouteResponse<'/access_codes/get'>>
>

type AccessCodesListBody = SetNonNullable<
export type AccessCodesListBody = SetNonNullable<
Required<RouteRequestBody<'/access_codes/list'>>
>

type AccessCodesListResponse = SetNonNullable<
export type AccessCodesListResponse = SetNonNullable<
Required<RouteResponse<'/access_codes/list'>>
>

type AccessCodesPullBackupAccessCodeBody = SetNonNullable<
export type AccessCodesPullBackupAccessCodeBody = SetNonNullable<
Required<RouteRequestBody<'/access_codes/pull_backup_access_code'>>
>

type AccessCodesPullBackupAccessCodeResponse = SetNonNullable<
export type AccessCodesPullBackupAccessCodeResponse = SetNonNullable<
Required<RouteResponse<'/access_codes/pull_backup_access_code'>>
>

type AccessCodesUpdateBody = SetNonNullable<
export type AccessCodesUpdateBody = SetNonNullable<
Required<RouteRequestBody<'/access_codes/update'>>
>

type AccessCodesUpdateResponse = SetNonNullable<
export type AccessCodesUpdateResponse = SetNonNullable<
Required<RouteResponse<'/access_codes/update'>>
>
Loading

0 comments on commit 2710e93

Please sign in to comment.