Skip to content

Commit

Permalink
Use auth map instead of switch statement, fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-balitskyi committed Nov 29, 2024
1 parent 15c8a02 commit fb72c60
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions src/lib/blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import type {
OpenapiPaths,
OpenapiSchema,
} from './openapi.js'
import { OpenapiOperationSchema, PropertySchema } from './openapi-schema.js'
import {

Check failure on line 19 in src/lib/blueprint.ts

View workflow job for this annotation

GitHub Actions / Lint (Node.js v18)

Import "AuthMethodSchema" is only used as types

Check failure on line 19 in src/lib/blueprint.ts

View workflow job for this annotation

GitHub Actions / Lint (Node.js v20)

Import "AuthMethodSchema" is only used as types
AuthMethodSchema,
OpenapiOperationSchema,
PropertySchema,
} from './openapi-schema.js'

export interface Blueprint {
title: string
Expand Down Expand Up @@ -504,7 +508,7 @@ const createEndpointFromOperation = async (
const [authMethod = ''] = Object.keys(securitySchema)
return mapOpenapiToSeamAuthMethod(authMethod)
})
.filter((authMethod) => authMethod != null)
.filter((authMethod): authMethod is SeamAuthMethod => authMethod != null)

const endpoint: Omit<Endpoint, 'codeSamples'> = {
title,
Expand Down Expand Up @@ -538,23 +542,22 @@ const createEndpointFromOperation = async (
}
}

type OpenapiAuthMethod = z.infer<typeof AuthMethodSchema>
type KnownOpenapiAuthMethod = Exclude<OpenapiAuthMethod, 'unknown'>

const mapOpenapiToSeamAuthMethod = (
method: string,
): SeamAuthMethod | undefined => {
switch (method) {
case 'api_key':
return 'api_key'
case 'pat_with_workspace':
case 'pat_without_workspace':
return 'personal_access_token'
case 'console_session':
return 'console_session_token'
case 'client_session':
return 'client_session_token'
case 'publishable_key':
return 'publishable_key'
default:
}
const AUTH_METHOD_MAPPING: Record<KnownOpenapiAuthMethod, SeamAuthMethod> = {
api_key: 'api_key',
pat_with_workspace: 'personal_access_token',
pat_without_workspace: 'personal_access_token',
console_session: 'console_session_token',
client_session: 'client_session_token',
publishable_key: 'publishable_key',
} as const

return AUTH_METHOD_MAPPING[method as KnownOpenapiAuthMethod]
}

export const createRequest = (
Expand Down

0 comments on commit fb72c60

Please sign in to comment.