Skip to content

Commit

Permalink
Extract valid action attempt types into the context instead of the wh…
Browse files Browse the repository at this point in the history
…ole ActionAttempt array
  • Loading branch information
andrii-balitskyi committed Feb 5, 2025
1 parent 19fd92f commit 8e2738d
Showing 1 changed file with 41 additions and 15 deletions.
56 changes: 41 additions & 15 deletions src/lib/blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ export type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'

interface Context extends Required<BlueprintOptions> {
codeSampleDefinitions: CodeSampleDefinition[]
// actionAttempts: ActionAttempt[]
validActionAttemptTypes: string[]
}

export const TypesModuleSchema = z.object({
Expand All @@ -303,10 +303,14 @@ export const createBlueprint = async (
// TODO: Move openapi to TypesModuleSchema
const openapi = typesModule.openapi as Openapi

const context = {
const validActionAttemptTypes = extractValidActionAttemptTypes(
openapi.components.schemas,
)

const context: Context = {
codeSampleDefinitions,
formatCode,
// actionAttempts,
validActionAttemptTypes,
}

const routes = await createRoutes(openapi.paths, context)
Expand All @@ -325,6 +329,30 @@ export const createBlueprint = async (
}
}

const extractValidActionAttemptTypes = (
schemas: Openapi['components']['schemas'],
): string[] => {
const actionAttemptSchema = schemas['action_attempt']
if (
actionAttemptSchema == null ||
typeof actionAttemptSchema !== 'object' ||
!('oneOf' in actionAttemptSchema) ||
!Array.isArray(actionAttemptSchema.oneOf)
) {
return []
}

const processedActionAttemptTypes = new Set<string>()
actionAttemptSchema.oneOf.forEach((schema) => {
const actionType = schema.properties?.action_type?.enum?.[0]
if (typeof actionType === 'string') {
processedActionAttemptTypes.add(actionType)
}
})

return Array.from(processedActionAttemptTypes)
}

const createRoutes = async (
paths: OpenapiPaths,
context: Context,
Expand Down Expand Up @@ -957,7 +985,7 @@ const createResponse = (
parsedOperation['x-action-attempt-type'],
responseKey,
path,
context,
context.validActionAttemptTypes,
)
const refKey = responseKey

Expand Down Expand Up @@ -985,7 +1013,7 @@ const validateActionAttemptType = (
actionAttemptType: string | undefined,
responseKey: string,
path: string,
context: Context,
validActionAttemptTypes: string[],
): string | undefined => {
const excludedPaths = ['/action_attempts']
const isPathExcluded = excludedPaths.some((p) => path.startsWith(p))
Expand All @@ -998,16 +1026,14 @@ const validateActionAttemptType = (
throw new Error(`Missing action_attempt_type for path ${path}`)
}

// if (
// actionAttemptType != null &&
// !context.actionAttempts.some(
// (attempt) => attempt.actionAttemptType === actionAttemptType,
// )
// ) {
// throw new Error(
// `Invalid action_attempt_type '${actionAttemptType}' for path ${path}`,
// )
// }
if (
actionAttemptType != null &&
!validActionAttemptTypes.includes(actionAttemptType)
) {
throw new Error(
`Invalid action_attempt_type '${actionAttemptType}' for path ${path}`,
)
}

return actionAttemptType
}
Expand Down

0 comments on commit 8e2738d

Please sign in to comment.