forked from mckaywrigley/chatbot-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenapi-conversion.ts
170 lines (149 loc) · 4.42 KB
/
openapi-conversion.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import $RefParser from "@apidevtools/json-schema-ref-parser"
interface OpenAPIData {
info: {
title: string
description: string
server: string
}
routes: {
path: string
method: string
operationId: string
requestInBody?: boolean
}[]
functions: any
}
export const validateOpenAPI = async (openapiSpec: any) => {
if (!openapiSpec.info) {
throw new Error("('info'): field required")
}
if (!openapiSpec.info.title) {
throw new Error("('info', 'title'): field required")
}
if (!openapiSpec.info.version) {
throw new Error("('info', 'version'): field required")
}
if (
!openapiSpec.servers ||
!openapiSpec.servers.length ||
!openapiSpec.servers[0].url
) {
throw new Error("Could not find a valid URL in `servers`")
}
if (!openapiSpec.paths || Object.keys(openapiSpec.paths).length === 0) {
throw new Error("No paths found in the OpenAPI spec")
}
Object.keys(openapiSpec.paths).forEach(path => {
if (!path.startsWith("/")) {
throw new Error(`Path ${path} does not start with a slash; skipping`)
}
})
if (
Object.values(openapiSpec.paths).some((methods: any) =>
Object.values(methods).some((spec: any) => !spec.operationId)
)
) {
throw new Error("Some methods are missing operationId")
}
if (
Object.values(openapiSpec.paths).some((methods: any) =>
Object.values(methods).some(
(spec: any) => spec.requestBody && !spec.requestBody.content
)
)
) {
throw new Error(
"Some methods with a requestBody are missing requestBody.content"
)
}
if (
Object.values(openapiSpec.paths).some((methods: any) =>
Object.values(methods).some((spec: any) => {
if (spec.requestBody?.content?.["application/json"]?.schema) {
if (
!spec.requestBody.content["application/json"].schema.properties ||
Object.keys(spec.requestBody.content["application/json"].schema)
.length === 0
) {
throw new Error(
`In context=('paths', '${Object.keys(methods)[0]}', '${
Object.keys(spec)[0]
}', 'requestBody', 'content', 'application/json', 'schema'), object schema missing properties`
)
}
}
})
)
) {
throw new Error("Some object schemas are missing properties")
}
}
export const openapiToFunctions = async (
openapiSpec: any
): Promise<OpenAPIData> => {
const functions: any[] = [] // Define a proper type for function objects
const routes: {
path: string
method: string
operationId: string
requestInBody?: boolean // Add a flag to indicate if the request should be in the body
}[] = []
for (const [path, methods] of Object.entries(openapiSpec.paths)) {
if (typeof methods !== "object" || methods === null) {
continue
}
for (const [method, specWithRef] of Object.entries(
methods as Record<string, any>
)) {
const spec: any = await $RefParser.dereference(specWithRef)
const functionName = spec.operationId
const desc = spec.description || spec.summary || ""
const schema: { type: string; properties: any; required?: string[] } = {
type: "object",
properties: {}
}
const reqBody = spec.requestBody?.content?.["application/json"]?.schema
if (reqBody) {
schema.properties.requestBody = reqBody
}
const params = spec.parameters || []
if (params.length > 0) {
const paramProperties = params.reduce((acc: any, param: any) => {
if (param.schema) {
acc[param.name] = param.schema
}
return acc
}, {})
schema.properties.parameters = {
type: "object",
properties: paramProperties
}
}
functions.push({
type: "function",
function: {
name: functionName,
description: desc,
parameters: schema
}
})
// Determine if the request should be in the body based on the presence of requestBody
const requestInBody = !!spec.requestBody
routes.push({
path,
method,
operationId: functionName,
requestInBody // Include this flag in the route information
})
}
}
return {
info: {
title: openapiSpec.info.title,
description: openapiSpec.info.description,
server: openapiSpec.servers[0].url
},
routes,
functions
}
}