docs: add copy subscriber button (#12405)
* docs: add copy subscriber button * re-generate * fixes + update copy button
This commit is contained in:
54
www/packages/docs-ui/src/utils/event-parser.ts
Normal file
54
www/packages/docs-ui/src/utils/event-parser.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { OpenAPI } from "types"
|
||||
|
||||
export function parseEventPayload(payloadStr: string) {
|
||||
const payloadParams = payloadStr.matchAll(/([\w_]+),? \/\/ (\(\w*\) )*(.*)/g)
|
||||
const payloadForSnippet: Record<string, unknown> = {}
|
||||
const payload = Array.from(payloadParams).map((match) => {
|
||||
const name = match[1]
|
||||
const type = (match[2]?.replace(/\(|\)/g, "") || "string").trim()
|
||||
const description = match[3]
|
||||
|
||||
if (type === "string") {
|
||||
payloadForSnippet[name] = "test"
|
||||
} else if (type === "number") {
|
||||
payloadForSnippet[name] = 1
|
||||
} else if (type === "boolean") {
|
||||
payloadForSnippet[name] = true
|
||||
} else if (type === "object") {
|
||||
payloadForSnippet[name] = {}
|
||||
} else if (type === "array") {
|
||||
payloadForSnippet[name] = [{}]
|
||||
}
|
||||
|
||||
return {
|
||||
name,
|
||||
type,
|
||||
description,
|
||||
}
|
||||
})
|
||||
return {
|
||||
parsed_payload: {
|
||||
type: "object",
|
||||
required: ["payload"],
|
||||
properties: {
|
||||
payload: {
|
||||
type: "object",
|
||||
description: "The payload emitted with the event",
|
||||
required: [...payload.map((param) => param.name)],
|
||||
properties: payload.reduce(
|
||||
(acc, curr) => {
|
||||
acc[curr.name] = {
|
||||
type: curr.type as OpenAPI.OpenAPIV3.NonArraySchemaObjectType,
|
||||
description: curr.description,
|
||||
properties: {},
|
||||
}
|
||||
return acc
|
||||
},
|
||||
{} as Record<string, OpenAPI.SchemaObject>
|
||||
),
|
||||
},
|
||||
},
|
||||
} as OpenAPI.SchemaObject,
|
||||
payload_for_snippet: payloadForSnippet,
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ export * from "./capitalize"
|
||||
export * from "./check-sidebar-item-visibility"
|
||||
export * from "./decode-str"
|
||||
export * from "./dom-utils"
|
||||
export * from "./event-parser"
|
||||
export * from "./get-link-with-base-path"
|
||||
export * from "./get-local-search"
|
||||
export * from "./get-navbar-items"
|
||||
|
||||
Reference in New Issue
Block a user