feat: Add automatic type generation

Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
Tim Hårek Andreassen 2024-03-30 12:44:03 +01:00
parent 33ccbe3e34
commit cdd424e6ca
No known key found for this signature in database
GPG Key ID: E59C7734F0E10EB5
2 changed files with 28 additions and 2 deletions

View File

@ -2,7 +2,8 @@
"lock": false, "lock": false,
"tasks": { "tasks": {
"start": "deno run -A --watch=static/,routes/ dev.ts", "start": "deno run -A --watch=static/,routes/ dev.ts",
"check": "deno fmt --check && deno lint && deno check **/*.ts && deno check **/*.tsx" "check": "deno fmt --check && deno lint && deno check **/*.ts && deno check **/*.tsx",
"types:nrk": "deno run -A generate-types.ts && deno fmt ./lib"
}, },
"imports": { "imports": {
"$fresh/": "https://deno.land/x/fresh@1.6.8/", "$fresh/": "https://deno.land/x/fresh@1.6.8/",
@ -13,7 +14,8 @@
"@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.5.1", "@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.5.1",
"twind": "https://esm.sh/twind@0.16.17", "twind": "https://esm.sh/twind@0.16.17",
"twind/": "https://esm.sh/twind@0.16.17/", "twind/": "https://esm.sh/twind@0.16.17/",
"zod": "https://deno.land/x/zod@v3.22.4/mod.ts" "zod": "https://deno.land/x/zod@v3.22.4/mod.ts",
"openapi-typescript": "npm:openapi-typescript@6.7.5"
}, },
"compilerOptions": { "compilerOptions": {
"jsx": "react-jsx", "jsx": "react-jsx",

24
generate-types.ts Normal file
View File

@ -0,0 +1,24 @@
import openapiTS from "openapi-typescript";
const nrkTypes: { filename: string; url: URL }[] = [
{
filename: "./lib/nrk-search.ts",
url: new URL("https://psapi.nrk.no/documentation/openapi/search-radio/v1/openapi.yml"),
},
{
filename: "./lib/nrk-catalog.ts",
url: new URL("https://psapi.nrk.no/documentation/openapi/programsider-radio/openapi.yml"),
},
] as const;
async function generateTypes() {
for (const type of nrkTypes) {
const contents = await openapiTS(type.url);
await Deno.writeTextFile(new URL(type.filename, import.meta.url), contents);
}
}
if (import.meta.main) {
await generateTypes();
}