budibase/packages/server/specs/generate.js

96 lines
2.5 KiB
JavaScript
Raw Normal View History

2022-02-16 17:42:50 +01:00
const swaggerJsdoc = require("swagger-jsdoc")
const { join } = require("path")
const { writeFileSync } = require("fs")
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-02-17 13:40:08 +01:00
url: "http://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-02-17 19:58:09 +01:00
url: "{protocol}://{hostname}:10000/api/public/v1",
2022-02-17 13:40:08 +01:00
description: "Budibase self hosted API",
},
2022-02-16 17:42:50 +01:00
],
2022-02-17 19:58:09 +01:00
components: {
examples: {
row: {
value: {
_id: "ro_ta_5b1649e42a5b41dea4ef7742a36a7a70_e6dc7e38cf1343b2b56760265201cda4",
type: "row",
tableId: "ta_5b1649e42a5b41dea4ef7742a36a7a70",
name: "Mike",
age: 30,
relationship: [
{
primaryDisplay: "Joe",
_id: "ro_ta_...",
},
],
},
},
table: {
value: {
_id: "ta_5b1649e42a5b41dea4ef7742a36a7a70",
name: "People",
schema: {
name: {
type: "string",
name: "name",
},
age: {
type: "number",
name: "age",
},
relationship: {
type: "link",
name: "relationship",
tableId: "ta_...",
fieldName: "relatedColumn",
relationshipType: "many-to-many",
},
},
},
},
},
},
2022-02-16 17:42:50 +01:00
},
2022-02-17 19:58:09 +01:00
format: ".json",
2022-02-16 19:23:38 +01:00
apis: [join(__dirname, "..", "src", "api", "routes", "public", "*.js")],
2022-02-16 17:42:50 +01:00
}
2022-02-17 13:40:08 +01: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-16 19:23:38 +01:00
}
2022-02-16 17:42:50 +01:00
}
2022-02-17 13:40:08 +01:00
const outputJSON = swaggerJsdoc(options)
2022-02-17 19:58:09 +01:00
options.format = ".yaml"
2022-02-17 13:40:08 +01:00
const outputYAML = swaggerJsdoc(options)
2022-02-17 19:58:09 +01:00
writeFile(outputJSON, { isJson: true })
2022-02-17 13:40:08 +01:00
writeFile(outputYAML)