Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add routePath to Resource and blueprint.events #141

Merged
merged 17 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 60 additions & 11 deletions src/lib/blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
title: string
routes: Route[]
resources: Record<string, Resource>
events: EventResource[]
}

export interface Route {
Expand All @@ -51,6 +52,13 @@
draftMessage: string
}

interface EventResource extends Resource {
resourceType: 'event'
eventType: string
routePath: string
targetResourceType: string
}

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

const createEvents = (
schemas: Openapi['components']['schemas'],
): EventResource[] => {
const eventSchema = schemas['event']
if (
!eventSchema ||

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

View workflow job for this annotation

GitHub Actions / Format code

Unexpected nullable object value in conditional. An explicit null check is required

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

View workflow job for this annotation

GitHub Actions / Lint (Node.js v18)

Unexpected nullable object value in conditional. An explicit null check is required

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

View workflow job for this annotation

GitHub Actions / Lint (Node.js v20)

Unexpected nullable object value in conditional. An explicit null check is required
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]

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

View workflow job for this annotation

GitHub Actions / Format code

Unexpected any value in conditional. An explicit comparison or type cast is required

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

View workflow job for this annotation

GitHub Actions / Lint (Node.js v18)

Unexpected any value in conditional. An explicit comparison or type cast is required

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

View workflow job for this annotation

GitHub Actions / Lint (Node.js v20)

Unexpected any value in conditional. An explicit comparison or type cast is required
) {
return null
}

return {
...createResource('event', schema),

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

View workflow job for this annotation

GitHub Actions / Format code

Unsafe argument of type `any` assigned to a parameter of type `OpenapiSchema`

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

View workflow job for this annotation

GitHub Actions / Lint (Node.js v18)

Unsafe argument of type `any` assigned to a parameter of type `OpenapiSchema`

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

View workflow job for this annotation

GitHub Actions / Lint (Node.js v20)

Unsafe argument of type `any` assigned to a parameter of type `OpenapiSchema`
eventType: schema.properties.event_type.enum[0],
// TODO: Compute routePath and targetResourceType
routePath: '',
targetResourceType: '',
}
})
.filter((event): event is EventResource => event !== null)
}

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

Expand Down Expand Up @@ -766,17 +808,7 @@
) {
return {
...acc,
[schemaName]: {
resourceType: schemaName,
properties: createProperties(schema.properties, [schemaName]),
description: schema.description ?? '',
isDeprecated: schema.deprecated ?? false,
deprecationMessage: schema['x-deprecated'] ?? '',
isUndocumented: (schema['x-undocumented'] ?? '').length > 0,
undocumentedMessage: schema['x-undocumented'] ?? '',
isDraft: (schema['x-draft'] ?? '').length > 0,
draftMessage: schema['x-draft'] ?? '',
},
[schemaName]: createResource(schemaName, schema),
}
}
return acc
Expand All @@ -785,6 +817,23 @@
)
}

const createResource = (
schemaName: string,
schema: OpenapiSchema,
): Resource => {
return {
resourceType: schemaName,
properties: createProperties(schema.properties ?? {}, [schemaName]),
description: schema.description ?? '',
isDeprecated: schema.deprecated ?? false,
deprecationMessage: schema['x-deprecated'] ?? '',
isUndocumented: (schema['x-undocumented'] ?? '').length > 0,
undocumentedMessage: schema['x-undocumented'] ?? '',
isDraft: (schema['x-draft'] ?? '').length > 0,
draftMessage: schema['x-draft'] ?? '',
}
}

const createResponse = (
operation: OpenapiOperation,
path: string,
Expand Down
31 changes: 31 additions & 0 deletions test/fixtures/types/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,37 @@ export default {
required: ['undocumented_resource_id'],
'x-undocumented': 'This resource is undocumented',
},
event: {
oneOf: [
{
type: 'object',
description: 'A foo.created event',
properties: {
event_id: {
description: 'Event ID',
format: 'uuid',
type: 'string',
},
event_type: {
description: 'Type of event',
type: 'string',
enum: ['foo.created'],
},
foo_id: {
description: 'ID of the foo that was created',
format: 'uuid',
type: 'string',
},
created_at: {
description: 'When the event occurred',
type: 'string',
format: 'date-time',
},
},
required: ['event_id', 'event_type', 'foo_id', 'created_at'],
},
],
},
},
},
paths: {
Expand Down
140 changes: 140 additions & 0 deletions test/snapshots/blueprint.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,76 @@ Generated by [AVA](https://avajs.dev).
> blueprint

{
events: [
{
deprecationMessage: '',
description: 'A foo.created event',
draftMessage: '',
eventType: 'foo.created',
isDeprecated: false,
isDraft: false,
isUndocumented: false,
properties: [
{
deprecationMessage: '',
description: 'Event ID',
draftMessage: '',
format: 'id',
isDeprecated: false,
isDraft: false,
isUndocumented: false,
jsonType: 'string',
name: 'event_id',
undocumentedMessage: '',
},
{
deprecationMessage: '',
description: 'Type of event',
draftMessage: '',
format: 'enum',
isDeprecated: false,
isDraft: false,
isUndocumented: false,
jsonType: 'string',
name: 'event_type',
undocumentedMessage: '',
values: [
{
name: 'foo.created',
},
],
},
{
deprecationMessage: '',
description: 'ID of the foo that was created',
draftMessage: '',
format: 'id',
isDeprecated: false,
isDraft: false,
isUndocumented: false,
jsonType: 'string',
name: 'foo_id',
undocumentedMessage: '',
},
{
deprecationMessage: '',
description: 'When the event occurred',
draftMessage: '',
format: 'datetime',
isDeprecated: false,
isDraft: false,
isUndocumented: false,
jsonType: 'string',
name: 'created_at',
undocumentedMessage: '',
},
],
resourceType: 'event',
routePath: '',
targetResourceType: '',
undocumentedMessage: '',
},
],
resources: {
deprecated_resource: {
deprecationMessage: 'This resource is deprecated',
Expand Down Expand Up @@ -907,6 +977,76 @@ Generated by [AVA](https://avajs.dev).
> blueprint

{
events: [
{
deprecationMessage: '',
description: 'A foo.created event',
draftMessage: '',
eventType: 'foo.created',
isDeprecated: false,
isDraft: false,
isUndocumented: false,
properties: [
{
deprecationMessage: '',
description: 'Event ID',
draftMessage: '',
format: 'id',
isDeprecated: false,
isDraft: false,
isUndocumented: false,
jsonType: 'string',
name: 'event_id',
undocumentedMessage: '',
},
{
deprecationMessage: '',
description: 'Type of event',
draftMessage: '',
format: 'enum',
isDeprecated: false,
isDraft: false,
isUndocumented: false,
jsonType: 'string',
name: 'event_type',
undocumentedMessage: '',
values: [
{
name: 'foo.created',
},
],
},
{
deprecationMessage: '',
description: 'ID of the foo that was created',
draftMessage: '',
format: 'id',
isDeprecated: false,
isDraft: false,
isUndocumented: false,
jsonType: 'string',
name: 'foo_id',
undocumentedMessage: '',
},
{
deprecationMessage: '',
description: 'When the event occurred',
draftMessage: '',
format: 'datetime',
isDeprecated: false,
isDraft: false,
isUndocumented: false,
jsonType: 'string',
name: 'created_at',
undocumentedMessage: '',
},
],
resourceType: 'event',
routePath: '',
targetResourceType: '',
undocumentedMessage: '',
},
],
resources: {
deprecated_resource: {
deprecationMessage: 'This resource is deprecated',
Expand Down
Binary file modified test/snapshots/blueprint.test.ts.snap
Binary file not shown.
Loading
Loading