Skip to content

Commit

Permalink
feat: Introduce endpoint authMethods (#135)
Browse files Browse the repository at this point in the history
Co-authored-by: Seam Bot <[email protected]>
  • Loading branch information
andrii-balitskyi and seambot authored Nov 29, 2024
1 parent a38e087 commit 67fb41a
Show file tree
Hide file tree
Showing 8 changed files with 876 additions and 3 deletions.
40 changes: 39 additions & 1 deletion 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 {
type AuthMethodSchema,
OpenapiOperationSchema,
PropertySchema,
} from './openapi-schema.js'

export interface Blueprint {
title: string
Expand Down Expand Up @@ -68,8 +72,16 @@ export interface Endpoint {
request: Request
response: Response
codeSamples: CodeSample[]
authMethods: SeamAuthMethod[]
}

export type SeamAuthMethod =
| 'api_key'
| 'personal_access_token'
| 'console_session_token'
| 'client_session_token'
| 'publishable_key'

interface BaseParameter {
name: string
description: string
Expand Down Expand Up @@ -491,6 +503,13 @@ const createEndpointFromOperation = async (
const request = createRequest(methods, operation, path)
const response = createResponse(operation, path)

const authMethods = parsedOperation.security
.map((securitySchema) => {
const [authMethod = ''] = Object.keys(securitySchema)
return mapOpenapiToSeamAuthMethod(authMethod)
})
.filter((authMethod): authMethod is SeamAuthMethod => authMethod != null)

const endpoint: Omit<Endpoint, 'codeSamples'> = {
title,
name,
Expand All @@ -504,6 +523,7 @@ const createEndpointFromOperation = async (
draftMessage,
response,
request,
authMethods,
}

return {
Expand All @@ -522,6 +542,24 @@ const createEndpointFromOperation = async (
}
}

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

const mapOpenapiToSeamAuthMethod = (
method: string,
): SeamAuthMethod | undefined => {
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 = (
methods: Method[],
operation: OpenapiOperation,
Expand Down
1 change: 1 addition & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export {
type Resource,
type Response,
type Route,
type SeamAuthMethod,
type TypesModule,
type TypesModuleInput,
TypesModuleSchema,
Expand Down
13 changes: 13 additions & 0 deletions src/lib/openapi-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ const ResponseSchema = z.record(
}),
)

export const AuthMethodSchema = z
.enum([
'api_key',
'client_session',
'console_session',
'pat_with_workspace',
'pat_without_workspace',
'publishable_key',
'unknown',
])
.catch('unknown')

export const OpenapiOperationSchema = z.object({
operationId: z.string(),
summary: z.string().optional(),
Expand All @@ -59,6 +71,7 @@ export const OpenapiOperationSchema = z.object({
})
.optional(),
responses: ResponseSchema,
security: z.array(z.record(AuthMethodSchema, z.array(z.never()))).default([]),
deprecated: z.boolean().default(false),
'x-response-key': z.string().nullable().optional(),
'x-title': z.string().default(''),
Expand Down
15 changes: 13 additions & 2 deletions test/fixtures/types/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ export default {
400: { description: 'Bad Request' },
401: { description: 'Unauthorized' },
},
security: [],
security: [
{
api_key: [],
},
],
summary: '/foos/get',
tags: ['/foos'],
'x-response-key': 'foo',
Expand Down Expand Up @@ -194,7 +198,14 @@ export default {
400: { description: 'Bad Request' },
401: { description: 'Unauthorized' },
},
security: [],
security: [
{
api_key: [],
},
{
client_session: [],
},
],
summary: '/foos/list',
tags: ['/foos'],
'x-response-key': 'foos',
Expand Down
20 changes: 20 additions & 0 deletions test/snapshots/blueprint.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ Generated by [AVA](https://avajs.dev).
{
endpoints: [
{
authMethods: [
'api_key',
],
codeSamples: [
{
code: {
Expand Down Expand Up @@ -386,6 +389,10 @@ Generated by [AVA](https://avajs.dev).
undocumentedMessage: '',
},
{
authMethods: [
'api_key',
'client_session_token',
],
codeSamples: [
{
code: {
Expand Down Expand Up @@ -527,6 +534,7 @@ Generated by [AVA](https://avajs.dev).
{
endpoints: [
{
authMethods: [],
codeSamples: [
{
code: {
Expand Down Expand Up @@ -655,6 +663,7 @@ Generated by [AVA](https://avajs.dev).
{
endpoints: [
{
authMethods: [],
codeSamples: [
{
code: {
Expand Down Expand Up @@ -773,6 +782,7 @@ Generated by [AVA](https://avajs.dev).
{
endpoints: [
{
authMethods: [],
codeSamples: [
{
code: {
Expand Down Expand Up @@ -1140,6 +1150,9 @@ Generated by [AVA](https://avajs.dev).
{
endpoints: [
{
authMethods: [
'api_key',
],
codeSamples: [
{
code: {
Expand Down Expand Up @@ -1285,6 +1298,10 @@ Generated by [AVA](https://avajs.dev).
undocumentedMessage: '',
},
{
authMethods: [
'api_key',
'client_session_token',
],
codeSamples: [
{
code: {
Expand Down Expand Up @@ -1442,6 +1459,7 @@ Generated by [AVA](https://avajs.dev).
{
endpoints: [
{
authMethods: [],
codeSamples: [
{
code: {
Expand Down Expand Up @@ -1586,6 +1604,7 @@ Generated by [AVA](https://avajs.dev).
{
endpoints: [
{
authMethods: [],
codeSamples: [
{
code: {
Expand Down Expand Up @@ -1720,6 +1739,7 @@ Generated by [AVA](https://avajs.dev).
{
endpoints: [
{
authMethods: [],
codeSamples: [
{
code: {
Expand Down
Binary file modified test/snapshots/blueprint.test.ts.snap
Binary file not shown.
Loading

0 comments on commit 67fb41a

Please sign in to comment.