-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathgenerate-opeapi-types.ts
executable file
·57 lines (50 loc) · 1.78 KB
/
generate-opeapi-types.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
#!/usr/bin/env -S deno run --allow-net --allow-write=./src/generated-types --allow-read=./src/generated-types --allow-read=. --no-prompt --allow-env
import { emptyDirSync } from "jsr:@std/fs"
import { parse as parseYaml } from "jsr:@std/yaml"
import openapiTS, { astToString } from "https://esm.sh/[email protected]?bundle"
// @ts-types="https://unpkg.com/[email protected]/types/openapi-format.d.ts"
import { openapiFilter } from "openapi-format"
const openapis = [
{
data: await fetch("https://raw.githubusercontent.com/openai/openai-openapi/refs/heads/master/openapi.yaml")
.then((res) => res.text())
.then(parseYaml),
path: "./src/generated-types/openai-types.ts",
},
{
data: await fetch(
"https://github.com/zuisong/googleapis-openapi/raw/refs/heads/main/googleapis/generativelanguage/v1alpha/generativelanguage-api-openapi_v3.json",
)
.then((res) => res.text())
.then(JSON.parse),
path: "./src/generated-types/gemini-types.ts",
},
] as const
emptyDirSync("./src/generated-types/")
for (const { path, data } of openapis) {
const { data: res } = await openapiFilter(data, {
filterSet: {
inverseOperationIds: [
/// openai
"createChatCompletion",
"createEmbedding",
"listModels",
/// googleapis
"generativelanguage.tunedModels.streamGenerateContent",
"generativelanguage.models.embedContent",
"generativelanguage.tunedModels.create",
],
unusedComponents: ["schemas"],
preserveEmptyObjects: false,
},
defaultFilter: {},
})
const ast = await openapiTS(res, {
excludeDeprecated: false,
cwd: "",
alphabetize: true,
additionalProperties: true,
})
const code = astToString(ast)
await Deno.writeTextFile(path, code)
}