From 73a3432c5786ecb26757931d4db9b16dba4cb724 Mon Sep 17 00:00:00 2001 From: BinarySandia04 Date: Sat, 5 Oct 2024 19:27:28 +0200 Subject: [PATCH] Started api --- backend/services/api.js | 91 +++++++++++++-------- backend/services/plugins.js | 4 +- client/src/services/Api.js | 140 ++++++++++++++++++++++++++++----- client/src/services/Plugins.js | 4 +- jsdoc.json | 4 +- tutorials/Test.md | 2 - tutorials/plugin.md | 1 + tutorials/tutorials.json | 4 +- 8 files changed, 189 insertions(+), 61 deletions(-) delete mode 100644 tutorials/Test.md create mode 100644 tutorials/plugin.md diff --git a/backend/services/api.js b/backend/services/api.js index 862d5d6d..257dcc07 100644 --- a/backend/services/api.js +++ b/backend/services/api.js @@ -2,16 +2,55 @@ const mongoose = require("mongoose"); const Schema = mongoose.Schema; /** - * Test + * Class for managing the backend api + * @hideconstructor */ -class Router { - constructor(){ - +class BackendApi { + #_plugin; + #_router; + + /** + * This object is already created for you + * @param {plugin} Plugin instance + */ + constructor(plugin){ + this.#_plugin = plugin; + this.#_router = new BackendRouter(plugin.package); + } + + /** + * Gets the router for your plugin + * @type {BackendRouter} + */ + get router(){ + return this.#_router; + } + + /** + * Returns a new database model for the plugin + * @param {String} name + * @param {Object} schema + * @returns {Model} + */ + createModel(name, schema){ + return new Model(name, this.#_plugin, schema); + } +}; + +/** + * This class registers routes to the backend that can be accessed from the frontend + * @hideconstructor + */ +class BackendRouter { + #_root; + + constructor(path){ + this.#_root = `plugins/${path}`; } /** - * @param {String} route * Hola + * @param {String} route */ get(route){ @@ -42,39 +81,23 @@ class Router { } } -class BackendApi { +/** + * @hideconstructor + */ +class Model { + #_name; #_plugin; - #_router; - - /** - * This object is already created for you - * @param {plugin} Plugin instance - */ - constructor(plugin){ - this._plugin = plugin; - this._router = new Router(); - } + #_schema; - /** - * Gets the router for your plugin - * @type {Router} router - */ - get router(){ - return this.router; - } - - /** - * Returns a new database model for the plguin - * @param {String} name - * @param {Object} schema - * @returns {mongoose.model} - */ - createModel(name, schema){ - return mongoose.model(name, new Schema(schema)) + constructor(name, plugin, schema){ + this.#_name = name; + this.#_plugin = plugin; + this.#_schema = schema; } }; + + module.exports = { - BackendApi, - Router + BackendApi } \ No newline at end of file diff --git a/backend/services/plugins.js b/backend/services/plugins.js index a1d24a1d..64884c76 100644 --- a/backend/services/plugins.js +++ b/backend/services/plugins.js @@ -20,12 +20,12 @@ function init(){ console.log("Launching using the following plugins:"); pluginsInfo.forEach(pluginInfo => { console.log(`\t- ${pluginInfo.name}`); - plugins[pluginInfo.package] = require(`${basePath}/plugins/${pluginInfo.package}/${pluginInfo.backend.entrypoint}`); + plugins[pluginInfo.package] = {info: pluginInfo, payload: require(`${basePath}/plugins/${pluginInfo.package}/${pluginInfo.backend.entrypoint}`)}; }); // Execute main Object.keys(plugins).forEach(k => { - plugins[k].Main(new BackendApi(k)) + plugins[k].payload.Main(new BackendApi(plugins[k].info)) }) } diff --git a/client/src/services/Api.js b/client/src/services/Api.js index ef480e9a..e00d9511 100644 --- a/client/src/services/Api.js +++ b/client/src/services/Api.js @@ -1,23 +1,127 @@ -import * as Dragonroll from "@/services/Dragonroll" -import * as Chat from "@/services/Chat" -import * as ContextMenu from "@/services/ContextMenu" -import * as Map from "@/services/Map" -import * as Modules from "@/services/Modules" -import * as Sound from "@/services/Sound" -import * as Tooltip from "@/services/Tooltip" -import * as Windows from "@/services/Windows" +import * as _Dragonroll from "@/services/Dragonroll" +import * as _Chat from "@/services/Chat" +import * as _ContextMenu from "@/services/ContextMenu" +import * as _Map from "@/services/Map" +import * as _Modules from "@/services/Modules" +import * as _Sound from "@/services/Sound" +import * as _Tooltip from "@/services/Tooltip" +import * as _Windows from "@/services/Windows" +import Server from '@/services/Server'; -const Api = { - Dragonroll, - Chat, - ContextMenu, - Map, - Modules, - Sound, - Tooltip, - Windows +/** + * Class for managing the client api + * @hideconstructor + */ +class ClientApi { + #_plugin + #_router + + /** + * @param {*} plugin + */ + constructor(plugin){ + this.#_plugin = plugin + this.#_router = new ClientRouter() + } + + /** + * Returns the client router + * @type {ClientRouter} + */ + get router(){ + return this.#_router; + } } +/** + * @typedef {Object} Response + * @property {Object} data + * @property {number} status + * @property {string} statusText + * @property {Object} headers + * @property {Object} config + * @property {Object} request + */ + +/** + * @callback routerCallback + * @param {Response} response + */ + +/** + * Class for accessing backend routes + * @hideconstructor + */ +class ClientRouter { + #_path; + + constructor(path){ + this.#_path = path; + } + + /** + * Sends a get request to an specified route of a plugin, and then executes the routerCallback with the response + * @param {String} route + * @param {routerCallback} callback + */ + get(route, callback){ + Server().get(`${path}/${route}`).then(response => callback(response)).catch(err => console.log(err)); + } + + /** + * @param {String} route + * + */ + post(route){ + + } + + /** + * @param {String} route + * + */ + put(route){ + + } + + /** + * @param {String} route + * + */ + delete(route){ + + } + + /** + * + */ + baseGet(){ + + } + + /** + * + */ + basePost(){ + + } + + /** + * + */ + baseUpdate(){ + + } + + /** + * + */ + baseDelete(){ + + } +} + + export { - Api + ClientApi } \ No newline at end of file diff --git a/client/src/services/Plugins.js b/client/src/services/Plugins.js index fac74277..3f7c7ce8 100644 --- a/client/src/services/Plugins.js +++ b/client/src/services/Plugins.js @@ -1,5 +1,5 @@ import { GetPluginPaths } from "./Resources" -import { Api } from '@/services/Api' +import { ClientApi } from '@/services/Api' let pluginInfo = [] @@ -23,7 +23,7 @@ async function FetchPlugins(){ }); import(/* @vite-ignore */ `../../plugins/${pluginName}/${pluginData.client.entrypoint}`).then(module => { - module.Main(Api); + module.Main(new ClientApi(pluginData)); }) } } diff --git a/jsdoc.json b/jsdoc.json index 4c56dd44..99e5f683 100644 --- a/jsdoc.json +++ b/jsdoc.json @@ -15,12 +15,14 @@ "template": "node_modules/clean-jsdoc-theme", "tutorials": "./tutorials", "theme_opts": { + "sort": false, "default_theme": "dark", "homepageTitle": "Dragonroll API", "title": "", "static_dir": ["./static"], "include_js": ["./static/scripts/themeWatch.js"], - "favicon": "static/media/logo.png" + "favicon": "static/media/logo.png", + "outputSourceFiles": false } }, "markdown": { diff --git a/tutorials/Test.md b/tutorials/Test.md deleted file mode 100644 index f0de4041..00000000 --- a/tutorials/Test.md +++ /dev/null @@ -1,2 +0,0 @@ -# Hola -hola test \ No newline at end of file diff --git a/tutorials/plugin.md b/tutorials/plugin.md new file mode 100644 index 00000000..794b38f5 --- /dev/null +++ b/tutorials/plugin.md @@ -0,0 +1 @@ +This guide will help you through creating a plugin for Dragonroll \ No newline at end of file diff --git a/tutorials/tutorials.json b/tutorials/tutorials.json index 48579229..2a3dbff2 100644 --- a/tutorials/tutorials.json +++ b/tutorials/tutorials.json @@ -1,5 +1,5 @@ { - "test": { - "title": "Test tutorial" + "plugin": { + "title": "Creating a plugin" } } \ No newline at end of file