docs-util: add events to generated OAS (#12390)

This commit is contained in:
Shahed Nasser
2025-05-07 10:44:02 +03:00
committed by GitHub
parent cdb135e491
commit 5d3f1d0118
2 changed files with 59 additions and 1 deletions

View File

@@ -7,13 +7,17 @@ import { capitalize, kebabToTitle } from "utils"
import { parse, stringify } from "yaml"
import { DEFAULT_OAS_RESPONSES, SUMMARY_PLACEHOLDER } from "../../constants.js"
import {
OasEvent,
OpenApiDocument,
OpenApiOperation,
OpenApiSchema,
} from "../../types/index.js"
import formatOas from "../../utils/format-oas.js"
import getCorrectZodTypeName from "../../utils/get-correct-zod-type-name.js"
import { getOasOutputBasePath } from "../../utils/get-output-base-paths.js"
import {
getEventsOutputBasePath,
getOasOutputBasePath,
} from "../../utils/get-output-base-paths.js"
import isZodObject from "../../utils/is-zod-object.js"
import parseOas, { ExistingOas } from "../../utils/parse-oas.js"
import OasExamplesGenerator from "../examples/oas.js"
@@ -32,6 +36,7 @@ import {
isLevelExceeded,
maybeIncrementLevel,
} from "../../utils/level-utils.js"
import { MedusaEvent } from "types"
const RES_STATUS_REGEX = /^res[\s\S]*\.status\((\d+)\)/
@@ -134,6 +139,7 @@ class OasKindGenerator extends FunctionKindGenerator {
protected oasSchemaHelper: OasSchemaHelper
protected schemaFactory: SchemaFactory
protected typesHelper: TypesHelper
protected events: MedusaEvent[] = []
constructor(options: GeneratorOptions) {
super(options)
@@ -242,6 +248,7 @@ class OasKindGenerator extends FunctionKindGenerator {
if (!this.isAllowed(node)) {
return await super.getDocBlock(node, options)
}
this.readEventsJson()
const actualNode = ts.isVariableStatement(node)
? this.extractFunctionNode(node)
@@ -422,6 +429,11 @@ class OasKindGenerator extends FunctionKindGenerator {
// get associated workflow
oas["x-workflow"] = this.getAssociatedWorkflow(node)
if (oas["x-workflow"]) {
// get associated events
oas["x-events"] = this.getOasEvents(oas["x-workflow"])
}
return formatOas(oas, oasPrefix)
}
@@ -750,6 +762,11 @@ class OasKindGenerator extends FunctionKindGenerator {
// get associated workflow
oas["x-workflow"] = this.getAssociatedWorkflow(node)
if (oas["x-workflow"]) {
// get associated events
oas["x-events"] = this.getOasEvents(oas["x-workflow"])
}
return formatOas(oas, oasPrefix)
}
@@ -2647,6 +2664,38 @@ class OasKindGenerator extends FunctionKindGenerator {
fnText.includes(responseType)
)
}
readEventsJson() {
if (this.events.length) {
return
}
const eventsJsonPath = getEventsOutputBasePath()
this.events = JSON.parse(readFileSync(eventsJsonPath, "utf-8"))
}
getOasEvents(workflow: string): OasEvent[] {
const events: OasEvent[] = []
const workflowEvents = this.events.filter((event) =>
event.workflows.includes(workflow)
)
if (workflowEvents.length) {
events.push(
...workflowEvents.map((event) => ({
name: event.name,
payload: event.payload,
description: event.description,
deprecated: event.deprecated,
deprecated_message: event.deprecated_message,
}))
)
}
return events
}
}
export default OasKindGenerator