docs-util: add examples generator for workflows and steps (#10914)

* initial changes

* docs-util: generate examples for workflows and steps
This commit is contained in:
Shahed Nasser
2025-01-10 19:16:43 +02:00
committed by GitHub
parent e82645ba55
commit c5915451b8
17 changed files with 810 additions and 142 deletions

View File

@@ -1,8 +1,13 @@
import { faker } from "@faker-js/faker"
import { OpenAPIV3 } from "openapi-types"
import { OasArea } from "../kinds/oas.js"
import { CodeSample } from "../../types/index.js"
import { capitalize, kebabToCamel, wordsToCamel, wordsToKebab } from "utils"
import {
capitalize,
getFakeStrValue,
kebabToCamel,
wordsToCamel,
wordsToKebab,
} from "utils"
import { API_ROUTE_PARAM_REGEX } from "../../constants.js"
type CodeSampleData = Omit<CodeSample, "source">
@@ -274,7 +279,7 @@ class OasExamplesGenerator {
? this.getSchemaRequiredData(
typedChildProp as OpenAPIV3.SchemaObject
)
: this.getFakeValue({
: getFakeStrValue({
name: childName,
type: typedChildProp.type,
format: typedChildProp.format,
@@ -296,7 +301,7 @@ class OasExamplesGenerator {
? this.getSchemaRequiredData(
property.items as OpenAPIV3.SchemaObject
)
: this.getFakeValue({
: getFakeStrValue({
name: propertyName,
type: propertyItems.type,
format: propertyItems.format,
@@ -305,7 +310,7 @@ class OasExamplesGenerator {
}
} else if (property.type) {
// retrieve fake value for all other types
value = this.getFakeValue({
value = getFakeStrValue({
name: propertyName,
type: property.type,
format: property.format,
@@ -320,64 +325,6 @@ class OasExamplesGenerator {
return data
}
/**
* Retrieve the fake value of a property. The value is used in examples.
*
* @param param0 - The property's details
* @returns The fake value
*/
getFakeValue({
name,
type,
format,
}: {
/**
* The name of the property. It can help when generating the fake value.
* For example, if the name is `id`, the fake value generated will be of the format `id_<randomstring>`.
*/
name: string
/**
* The type of the property.
*/
type: OpenAPIV3.NonArraySchemaObjectType | "array"
/**
* The OAS format of the property. For example, `date-time`.
*/
format?: string
}): unknown {
let value: unknown
switch (true) {
case type === "string" && format === "date-time":
value = faker.date.future().toISOString()
break
case type === "boolean":
value = faker.datatype.boolean()
break
case type === "integer" || type === "number":
value = faker.number.int()
break
case type === "array":
value = []
break
case type === "string":
value = faker.helpers
.mustache(`{{${name}}}`, {
id: () =>
`id_${faker.string.alphanumeric({
length: { min: 10, max: 20 },
})}`,
name: () => faker.person.firstName(),
email: () => faker.internet.email(),
password: () => faker.internet.password({ length: 8 }),
currency: () => faker.finance.currencyCode(),
})
.replace(`{{${name}}}`, "{value}")
}
return value !== undefined ? value : "{value}"
}
}
export default OasExamplesGenerator