diff options
| author | Mohamed Bassem <me@mbassem.com> | 2024-10-20 17:36:02 +0000 |
|---|---|---|
| committer | Mohamed Bassem <me@mbassem.com> | 2024-10-20 17:36:02 +0000 |
| commit | 3c1ec3aa2f7d64932fd26c8cbcb1aee1e57861bd (patch) | |
| tree | 34eceb016bec57c7c3b59315a210747cdf0c8f33 /packages/open-api/index.ts | |
| parent | 4086c37b830c3c4141b37052e3c192a750470084 (diff) | |
| download | karakeep-3c1ec3aa2f7d64932fd26c8cbcb1aee1e57861bd.tar.zst | |
chore: Define hoarder's rest API in zod format
Diffstat (limited to 'packages/open-api/index.ts')
| -rw-r--r-- | packages/open-api/index.ts | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/packages/open-api/index.ts b/packages/open-api/index.ts new file mode 100644 index 00000000..da5b3729 --- /dev/null +++ b/packages/open-api/index.ts @@ -0,0 +1,46 @@ +import * as fs from "fs"; +import { + OpenApiGeneratorV3, + OpenAPIRegistry, +} from "@asteasolutions/zod-to-openapi"; +import * as yaml from "yaml"; + +import { registry as bookmarksRegistry } from "./lib/bookmarks"; +import { registry as commonRegistry } from "./lib/common"; +import { registry as listsRegistry } from "./lib/lists"; +import { registry as tagsRegistry } from "./lib/tags"; + +function getOpenApiDocumentation() { + const registry = new OpenAPIRegistry([ + commonRegistry, + bookmarksRegistry, + listsRegistry, + tagsRegistry, + ]); + + const generator = new OpenApiGeneratorV3(registry.definitions); + + return generator.generateDocument({ + openapi: "3.0.0", + info: { + version: "1.0.0", + title: "Hoarder API", + description: "The API for the Hoarder app", + }, + servers: [{ url: "v1" }], + }); +} + +function writeDocumentation() { + // OpenAPI JSON + const docs = getOpenApiDocumentation(); + + // YAML equivalent + const fileContent = yaml.stringify(docs); + + fs.writeFileSync(`./openapi-spec.yml`, fileContent, { + encoding: "utf-8", + }); +} + +writeDocumentation(); |
