2022-02-17 05:42:50 +13:00
|
|
|
const swaggerJsdoc = require("swagger-jsdoc")
|
|
|
|
const { join } = require("path")
|
|
|
|
const { writeFileSync } = require("fs")
|
2022-02-19 04:47:15 +13:00
|
|
|
const examples = require("./examples")
|
|
|
|
const parameters = require("./parameters")
|
|
|
|
const security = require("./security")
|
2022-02-19 06:44:08 +13:00
|
|
|
const schemas = require("./schemas")
|
2022-02-17 05:42:50 +13:00
|
|
|
|
2022-02-18 01:40:08 +13:00
|
|
|
const VARIABLES = {}
|
2022-02-17 05:42:50 +13: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-02-18 01:40:08 +13:00
|
|
|
url: "http://budibase.app/api/public/v1",
|
2022-02-17 05:42:50 +13:00
|
|
|
description: "Budibase Cloud API",
|
|
|
|
},
|
2022-02-18 01:40:08 +13:00
|
|
|
{
|
2022-02-18 07:58:09 +13:00
|
|
|
url: "{protocol}://{hostname}:10000/api/public/v1",
|
2022-02-18 01:40:08 +13:00
|
|
|
description: "Budibase self hosted API",
|
|
|
|
},
|
2022-02-17 05:42:50 +13:00
|
|
|
],
|
2022-02-18 07:58:09 +13:00
|
|
|
components: {
|
2022-02-18 08:55:37 +13:00
|
|
|
parameters: {
|
2022-02-19 04:47:15 +13:00
|
|
|
...parameters,
|
2022-02-18 08:55:37 +13:00
|
|
|
},
|
2022-02-18 07:58:09 +13:00
|
|
|
examples: {
|
2022-02-19 04:47:15 +13:00
|
|
|
...examples,
|
|
|
|
},
|
|
|
|
securitySchemes: {
|
|
|
|
...security,
|
2022-02-18 07:58:09 +13:00
|
|
|
},
|
2022-02-19 06:44:08 +13:00
|
|
|
schemas: {
|
|
|
|
...schemas,
|
|
|
|
},
|
2022-02-18 07:58:09 +13:00
|
|
|
},
|
2022-02-19 04:47:15 +13:00
|
|
|
security: [
|
|
|
|
{
|
|
|
|
ApiKeyAuth: [],
|
|
|
|
},
|
|
|
|
],
|
2022-02-17 05:42:50 +13:00
|
|
|
},
|
2022-02-18 07:58:09 +13:00
|
|
|
format: ".json",
|
2022-02-17 07:23:38 +13:00
|
|
|
apis: [join(__dirname, "..", "src", "api", "routes", "public", "*.js")],
|
2022-02-17 05:42:50 +13:00
|
|
|
}
|
|
|
|
|
2022-02-18 01:40:08 +13:00
|
|
|
function writeFile(output, { isJson } = {}) {
|
|
|
|
try {
|
|
|
|
const filename = isJson ? "openapi.json" : "openapi.yaml"
|
|
|
|
const path = join(__dirname, filename)
|
|
|
|
let spec = output
|
|
|
|
if (isJson) {
|
|
|
|
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}`)
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
2022-02-17 07:23:38 +13:00
|
|
|
}
|
2022-02-17 05:42:50 +13:00
|
|
|
}
|
2022-02-18 01:40:08 +13:00
|
|
|
|
|
|
|
const outputJSON = swaggerJsdoc(options)
|
2022-02-18 07:58:09 +13:00
|
|
|
options.format = ".yaml"
|
2022-02-18 01:40:08 +13:00
|
|
|
const outputYAML = swaggerJsdoc(options)
|
2022-02-18 07:58:09 +13:00
|
|
|
writeFile(outputJSON, { isJson: true })
|
2022-02-18 01:40:08 +13:00
|
|
|
writeFile(outputYAML)
|