From 0d6cb6309627cecce31ed78768c8b960260125c1 Mon Sep 17 00:00:00 2001 From: TomatoCake <60300461+DEVTomatoCake@users.noreply.github.com> Date: Wed, 21 Aug 2024 19:08:25 +0200 Subject: [PATCH] Slight code cleanup & update project desc --- assets/openapi.json | Bin 608216 -> 608332 bytes scripts/openapi.js | 62 +++++++++++++++++++++----------------------- 2 files changed, 29 insertions(+), 33 deletions(-) diff --git a/assets/openapi.json b/assets/openapi.json index ea95a1bde82e97a794d2dbe98b2ec7932b641039..71356aaa4dce819754eef54508b1cb62bb1194d7 100644 GIT binary patch delta 236 zcmYk0Jr2S!428u%oPgC!x2mT=;v7-pwwBVkO40%oV#o}a;Q-z_0LLL!7dAiXz0cqG zS$tl_%im8FG<#EN?jWEa(F1k8#SUFmk=TK92Jjlug`7&bP;4Q#2n{O>Tti;wenhBI z#la)=%2T|}#Xo!r%38RzWD%4xgF(g%D9Hv5QZ|V=wYIi(0dw{&d21T+11nx gem3KNnuY2iNft?#NmfbLNj6EgNp?y0Z#f*#Kl{sC^8f$< delta 129 zcmX>zLG?zz>VzWyw4&5hh5UllJcZ)?(xT*4h2qqlw2b`XlEkE(RE3nx;^h3I6ousc z+=9fCOps7=Mq-JMLRo$$P;Fvfib7dtN@~7BK~7>xT7FS(vngY{DI*9o0WmWWvj8zG S5VHX>I}mehH)Z4$cLxANt|~GB diff --git a/scripts/openapi.js b/scripts/openapi.js index 8258a76c..225c5175 100644 --- a/scripts/openapi.js +++ b/scripts/openapi.js @@ -1,17 +1,17 @@ /* Spacebar: A FOSS re-implementation and extension of the Discord.com backend. Copyright (C) 2023 Spacebar and Spacebar Contributors - + This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ @@ -28,15 +28,13 @@ require("missing-native-js-functions"); const openapiPath = path.join(__dirname, "..", "assets", "openapi.json"); const SchemaPath = path.join(__dirname, "..", "assets", "schemas.json"); const schemas = JSON.parse(fs.readFileSync(SchemaPath, { encoding: "utf8" })); -// const specification = JSON.parse( -// fs.readFileSync(openapiPath, { encoding: "utf8" }), -// ); + let specification = { openapi: "3.1.0", info: { title: "Spacebar Server", description: - "Spacebar is a free open source selfhostable discord compatible chat, voice and video platform", + "Spacebar is a Discord.com server implementation and extension, with the goal of complete feature parity with Discord.com, all while adding some additional goodies, security, privacy, and configuration options.", license: { name: "AGPLV3", url: "https://www.gnu.org/licenses/agpl-3.0.en.html", @@ -68,8 +66,9 @@ let specification = { paths: {}, }; +const schemaRegEx = new RegExp(/^[\w.]+$/); function combineSchemas(schemas) { - var definitions = {}; + let definitions = {}; for (const name in schemas) { definitions = { @@ -84,9 +83,8 @@ function combineSchemas(schemas) { } for (const key in definitions) { - const reg = new RegExp(/^[a-zA-Z0-9.\-_]+$/, "gm"); - if (!reg.test(key)) { - console.error(`Invalid schema name: ${key} (${reg.test(key)})`); + if (!schemaRegEx.test(key)) { + console.error(`Invalid schema name: ${key}`); continue; } specification.components = specification.components || {}; @@ -157,32 +155,30 @@ function apiRoutes() { }, }, }, - }.merge(obj.requestBody); + }; } if (route.responses) { - for (const [k, v] of Object.entries(route.responses)) { - let schema = { - $ref: `#/components/schemas/${v.body}`, - }; + obj.responses = {}; - obj.responses = { - [k]: { - ...(v.body - ? { - description: - obj?.responses?.[k]?.description || "", - content: { - "application/json": { - schema: schema, - }, - }, - } - : { - description: "No description available", - }), - }, - }.merge(obj.responses); + for (const [k, v] of Object.entries(route.responses)) { + if (v.body) + obj.responses[k] = { + description: obj?.responses?.[k]?.description || "", + content: { + "application/json": { + schema: { + $ref: `#/components/schemas/${v.body}`, + }, + }, + }, + }; + else + obj.responses[k] = { + description: + obj?.responses?.[k]?.description || + "No description available", + }; } } else { obj.responses = {