/
generate-opeapi-types.ts
executable file
·43 lines (33 loc) · 1.38 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
#!/usr/bin/env -S deno run --allow-net --allow-env --allow-write=./src --allow-read=.
import { convert } from "https://esm.sh/[email protected]?dev"
import openapiTS from "https://esm.sh/[email protected]?bundle"
import converter from "https://esm.sh/[email protected]"
const url = new URL("https://generativelanguage.googleapis.com/$discovery/rest")
url.searchParams.set("key", Deno.env.get("YOUR_GEMINI_API_KEY") ?? "")
url.searchParams.set("version", "v1beta")
const data = await fetch(url).then((res) => res.json())
import { stringify } from "https://esm.sh/[email protected]?dev"
const res = convert(JSON.parse(stringify(data) ?? "{}"))
const { openapi } = await converter.convert(res, {})
import { Buffer } from "node:buffer"
import { Readable } from "node:stream"
import { stringify as yamlStringify } from "jsr:@std/yaml"
const openapis = [
{
url: "https://raw.githubusercontent.com/openai/openai-openapi/master/openapi.yaml",
path: "./src/generated-types/openai-types.ts",
},
{
path: "./src/generated-types/gemini-types.ts",
url: Readable.from(Buffer.from(yamlStringify(openapi), "utf-8")),
},
] as const
for (const { url, path } of openapis) {
const code = await openapiTS(url, {
excludeDeprecated: false,
cwd: ".",
alphabetize: false,
additionalProperties: true,
})
await Deno.writeTextFile(path, code)
}