2022-02-16 17:42:50 +01:00
|
|
|
const swaggerJsdoc = require("swagger-jsdoc")
|
|
|
|
const { join } = require("path")
|
|
|
|
const { writeFileSync } = require("fs")
|
2022-02-22 16:06:08 +01:00
|
|
|
const { examples, schemas } = require("./resources")
|
2022-02-18 16:47:15 +01:00
|
|
|
const parameters = require("./parameters")
|
|
|
|
const security = require("./security")
|
2022-02-16 17:42:50 +01:00
|
|
|
|
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",
|
|
|
|
},
|
2022-02-17 13:40:08 +01:00
|
|
|
{
|
2022-03-07 14:21:30 +01:00
|
|
|
url: "{protocol}://{hostname}/api/public/v1",
|
2022-02-17 13:40:08 +01:00
|
|
|
description: "Budibase self hosted API",
|
2022-03-07 14:21:30 +01:00
|
|
|
variables: {
|
|
|
|
protocol: {
|
|
|
|
default: "http",
|
|
|
|
description:
|
|
|
|
"Whether HTTP or HTTPS should be used to communicate with your Budibase instance.",
|
|
|
|
},
|
|
|
|
hostname: {
|
|
|
|
default: "localhost:10000",
|
|
|
|
description: "The URL of your Budibase instance.",
|
|
|
|
},
|
|
|
|
},
|
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: {
|
2022-02-17 20:55:37 +01:00
|
|
|
parameters: {
|
2022-02-18 16:47:15 +01:00
|
|
|
...parameters,
|
2022-02-17 20:55:37 +01:00
|
|
|
},
|
2022-02-17 19:58:09 +01:00
|
|
|
examples: {
|
2022-02-18 16:47:15 +01:00
|
|
|
...examples,
|
|
|
|
},
|
|
|
|
securitySchemes: {
|
|
|
|
...security,
|
2022-02-17 19:58:09 +01:00
|
|
|
},
|
2022-02-18 18:44:08 +01:00
|
|
|
schemas: {
|
|
|
|
...schemas,
|
|
|
|
},
|
2022-02-17 19:58:09 +01:00
|
|
|
},
|
2022-02-18 16:47:15 +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
|
|
|
}
|
|
|
|
|
2022-02-25 00:21:10 +01:00
|
|
|
function writeFile(output, filename) {
|
2022-02-17 13:40:08 +01:00
|
|
|
try {
|
|
|
|
const path = join(__dirname, filename)
|
|
|
|
let spec = output
|
2022-02-25 00:21:10 +01:00
|
|
|
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}`)
|
2022-02-25 00:21:10 +01:00
|
|
|
return path
|
2022-02-17 13:40:08 +01:00
|
|
|
} catch (err) {
|
|
|
|
console.error(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
|
|
|
|
2022-02-25 00:21:10 +01:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = run
|