Skip to content

Commit

Permalink
Merge branch 'main' of github.com:seamapi/blueprint into parse-annota…
Browse files Browse the repository at this point in the history
…ted-enums
  • Loading branch information
andrii-balitskyi committed Jan 13, 2025
2 parents 4b5b3d0 + 962602b commit 9b05909
Show file tree
Hide file tree
Showing 8 changed files with 2,362 additions and 1,016 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@seamapi/blueprint",
"version": "0.32.1",
"version": "0.33.0",
"description": "Build tools for the Seam API using this blueprint.",
"type": "module",
"main": "index.js",
Expand Down
138 changes: 101 additions & 37 deletions src/lib/blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface Blueprint {
routes: Route[]
resources: Record<string, Resource>
events: EventResource[]
actionAttempts: ActionAttempt[]
}

export interface Route {
Expand Down Expand Up @@ -62,6 +63,11 @@ interface EventResource extends Resource {
targetResourceType: string | null
}

interface ActionAttempt extends Resource {
resourceType: 'action_attempt'
actionAttemptType: string
}

export interface Namespace {
path: string
isDeprecated: boolean
Expand Down Expand Up @@ -286,43 +292,6 @@ export interface BlueprintOptions {
formatCode?: (content: string, syntax: CodeSampleSyntax) => Promise<string>
}

const createEvents = (
schemas: Openapi['components']['schemas'],
resources: Record<string, Resource>,
): EventResource[] => {
const eventSchema = schemas['event']
if (
eventSchema == null ||
typeof eventSchema !== 'object' ||
!('oneOf' in eventSchema) ||
!Array.isArray(eventSchema.oneOf)
) {
return []
}

return eventSchema.oneOf
.map((schema) => {
if (
typeof schema !== 'object' ||
schema.properties?.event_type?.enum?.[0] == null
) {
return null
}

const eventType = schema.properties.event_type.enum[0]
const targetResourceType = Object.keys(resources).find((resourceName) =>
eventType.split('.').includes(resourceName),
)

return {
...createResource('event', schema as OpenapiSchema),
eventType,
targetResourceType: targetResourceType ?? null,
}
})
.filter((event): event is EventResource => event !== null)
}

export const createBlueprint = async (
typesModule: TypesModuleInput,
{ formatCode = async (content) => content }: BlueprintOptions = {},
Expand All @@ -344,6 +313,7 @@ export const createBlueprint = async (
routes: await createRoutes(openapi.paths, context),
resources,
events: createEvents(openapi.components.schemas, resources),
actionAttempts: createActionAttempts(openapi.components.schemas),
}
}

Expand Down Expand Up @@ -1117,3 +1087,97 @@ export const getPreferredMethod = (

return semanticMethod
}

const createEvents = (
schemas: Openapi['components']['schemas'],
resources: Record<string, Resource>,
): EventResource[] => {
const eventSchema = schemas['event']
if (
eventSchema == null ||
typeof eventSchema !== 'object' ||
!('oneOf' in eventSchema) ||
!Array.isArray(eventSchema.oneOf)
) {
return []
}

return eventSchema.oneOf
.map((schema) => {
if (
typeof schema !== 'object' ||
schema.properties?.event_type?.enum?.[0] == null
) {
return null
}

const eventType = schema.properties.event_type.enum[0]
const targetResourceType = Object.keys(resources).find((resourceName) =>
eventType.split('.').includes(resourceName),
)

return {
...createResource('event', schema as OpenapiSchema),
eventType,
targetResourceType: targetResourceType ?? null,
}
})
.filter((event): event is EventResource => event !== null)
}

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

const processedActionTypes = new Set<string>()

return actionAttemptSchema.oneOf
.map((schema) => {
if (
typeof schema !== 'object' ||
schema.properties?.action_type?.enum?.[0] == null
) {
return null
}

const actionType = schema.properties.action_type.enum[0] as string

if (processedActionTypes.has(actionType)) {
return null
}
processedActionTypes.add(actionType)

const schemaWithStandardStatus: OpenapiSchema = {
...schema,
properties: {
...schema.properties,
status: {
...schema.properties.status,
type: 'string',
enum: ['success', 'pending', 'error'],
},
},
}

const resource = createResource(
'action_attempt',
schemaWithStandardStatus,
)

return {
...resource,
resourceType: 'action_attempt',
actionAttemptType: actionType,
}
})
.filter((attempt): attempt is ActionAttempt => attempt !== null)
}
94 changes: 94 additions & 0 deletions test/fixtures/types/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,100 @@ export default {
},
],
},
action_attempt: {
oneOf: [
{
type: 'object',
properties: {
action_attempt_id: {
type: 'string',
format: 'uuid',
},
status: {
type: 'string',
enum: ['pending'],
},
result: {
nullable: true,
},
error: {
nullable: true,
},
action_type: {
type: 'string',
enum: ['CREATE_FOO'],
},
},
required: [
'action_attempt_id',
'status',
'result',
'error',
'action_type',
],
},
{
type: 'object',
properties: {
action_attempt_id: {
type: 'string',
format: 'uuid',
},
status: {
type: 'string',
enum: ['success'],
},
result: {
nullable: true,
},
error: {
nullable: true,
},
action_type: {
type: 'string',
enum: ['CREATE_FOO'],
},
},
required: [
'action_attempt_id',
'status',
'result',
'error',
'action_type',
],
},
{
type: 'object',
properties: {
action_attempt_id: {
type: 'string',
format: 'uuid',
},
status: {
type: 'string',
enum: ['error'],
},
result: {
nullable: true,
},
error: {
nullable: true,
},
action_type: {
type: 'string',
enum: ['CREATE_FOO'],
},
},
required: [
'action_attempt_id',
'status',
'result',
'error',
'action_type',
],
},
],
},
},
},
paths: {
Expand Down
Loading

0 comments on commit 9b05909

Please sign in to comment.