|
1
|
import { OpenAPI } from "types" |
|
2
|
|
|
3
|
export function parseEventPayload(payloadStr: string) { |
|
4
|
const payloadParams = payloadStr.matchAll(/([\w_]+),? \/\/ (\(\w*\) )*(.*)/g) |
|
5
|
const payloadForSnippet: Record<string, unknown> = {} |
|
6
|
const payload = Array.from(payloadParams).map((match) => { |
|
7
|
const name = match[1] |
|
8
|
const type = (match[2]?.replace(/\(|\)/g, "") || "string").trim() |
|
9
|
const description = match[3] |
|
10
|
|
|
11
|
if (type === "string") { |
|
12
|
payloadForSnippet[name] = "test" |
|
13
|
} else if (type === "number") { |
|
14
|
payloadForSnippet[name] = 1 |
|
15
|
} else if (type === "boolean") { |
|
16
|
payloadForSnippet[name] = true |
|
17
|
} else if (type === "object") { |
|
18
|
payloadForSnippet[name] = {} |
|
19
|
} else if (type === "array") { |
|
20
|
payloadForSnippet[name] = [{}] |
|
21
|
} |
|
22
|
|
|
23
|
return { |
|
24
|
name, |
|
25
|
type, |
|
26
|
description, |
|
27
|
} |
|
28
|
}) |
|
29
|
return { |
|
30
|
parsed_payload: { |
|
31
|
type: "object", |
|
32
|
required: ["payload"], |
|
33
|
properties: { |
|
34
|
payload: { |
|
35
|
type: "object", |
|
36
|
description: "The payload emitted with the event", |
|
37
|
required: [...payload.map((param) => param.name)], |
|
38
|
properties: payload.reduce( |
|
39
|
(acc, curr) => { |
|
40
|
acc[curr.name] = { |
|
41
|
type: curr.type as OpenAPI.OpenAPIV3.NonArraySchemaObjectType, |
|
42
|
description: curr.description, |
|
43
|
properties: {}, |
|
44
|
} |
|
45
|
return acc |
|
46
|
}, |
|
47
|
{} as Record<string, OpenAPI.SchemaObject> |
|
48
|
), |
|
49
|
}, |
|
50
|
}, |
|
51
|
} as OpenAPI.SchemaObject, |
|
52
|
payload_for_snippet: payloadForSnippet, |
|
53
|
} |
|
54
|
} |