budibase/packages/server/specs/generate.ts

90 lines
2.2 KiB
TypeScript
Raw Permalink Normal View History

import { join } from "path"
import { writeFileSync } from "fs"
import { examples, schemas } from "./resources"
import * as parameters from "./parameters"
import * as security from "./security"
2023-11-20 21:52:29 +01:00
2022-02-16 17:42:50 +01:00
const swaggerJsdoc = require("swagger-jsdoc")
2022-02-17 13:40:08 +01:00
const VARIABLES = {}
2022-02-16 17:42:50 +01:00
const options = {
definition: {
openapi: "3.0.0",
info: {
title: "Budibase API",
description: "The public API for Budibase apps and its services.",
version: "1.0.0",
},
servers: [
{
2022-03-07 17:41:22 +01:00
url: "https://budibase.app/api/public/v1",
2022-02-16 17:42:50 +01:00
description: "Budibase Cloud API",
variables: {
apiKey: {
default: "<user API key>",
description: "The API key of the user to assume for API call.",
},
appId: {
default: "<App ID>",
description:
"The ID of the app the calls will be executed within the context of, this should start with app_ (production) or app_dev (development).",
},
},
2022-02-17 13:40:08 +01:00
},
2022-02-16 17:42:50 +01:00
],
2022-02-17 19:58:09 +01:00
components: {
parameters: {
...parameters,
},
2022-02-17 19:58:09 +01:00
examples: {
...examples,
},
securitySchemes: {
...security,
2022-02-17 19:58:09 +01:00
},
schemas: {
...schemas,
},
2022-02-17 19:58:09 +01:00
},
security: [
{
ApiKeyAuth: [],
},
],
2022-02-16 17:42:50 +01:00
},
2022-02-17 19:58:09 +01:00
format: ".json",
2022-02-24 19:15:13 +01:00
apis: [join(__dirname, "..", "src", "api", "routes", "public", "*.ts")],
2022-02-16 17:42:50 +01:00
}
function writeFile(output: any, filename: string) {
2022-02-17 13:40:08 +01:00
try {
const path = join(__dirname, filename)
let spec = output
if (filename.endsWith("json")) {
2022-02-17 13:40:08 +01:00
spec = JSON.stringify(output, null, 2)
}
// input the static variables
for (let [key, replacement] of Object.entries(VARIABLES)) {
spec = spec.replace(new RegExp(`{${key}}`, "g"), replacement)
}
writeFileSync(path, spec)
console.log(`Wrote spec to ${path}`)
return path
2022-02-17 13:40:08 +01:00
} catch (err) {
2024-04-10 18:39:12 +02:00
console.error("Error writing spec file", err)
2022-02-16 19:23:38 +01:00
}
2022-02-16 17:42:50 +01:00
}
2022-02-17 13:40:08 +01:00
export function run() {
const outputJSON = swaggerJsdoc(options)
options.format = ".yaml"
const outputYAML = swaggerJsdoc(options)
writeFile(outputJSON, "openapi.json")
return writeFile(outputYAML, "openapi.yaml")
}
if (require.main === module) {
run()
}