Merge pull request #1456 from valadaptive/extensions-router

Use Express router for extensions endpoint
This commit is contained in:
Cohee
2023-12-04 21:55:22 +02:00
committed by GitHub
2 changed files with 185 additions and 191 deletions

View File

@@ -3594,7 +3594,7 @@ require('./src/endpoints/thumbnails').registerEndpoints(app, jsonParser);
require('./src/endpoints/novelai').registerEndpoints(app, jsonParser); require('./src/endpoints/novelai').registerEndpoints(app, jsonParser);
// Third-party extensions // Third-party extensions
require('./src/endpoints/extensions').registerEndpoints(app, jsonParser); app.use('/api/extensions', require('./src/endpoints/extensions').router);
// Asset management // Asset management
app.use('/api/assets', require('./src/endpoints/assets').router); app.use('/api/assets', require('./src/endpoints/assets').router);

View File

@@ -1,8 +1,10 @@
const path = require('path'); const path = require('path');
const fs = require('fs'); const fs = require('fs');
const express = require('express');
const { default: simpleGit } = require('simple-git'); const { default: simpleGit } = require('simple-git');
const sanitize = require('sanitize-filename'); const sanitize = require('sanitize-filename');
const { DIRECTORIES } = require('../constants'); const { DIRECTORIES } = require('../constants');
const { jsonParser } = require('../express-common');
/** /**
* This function extracts the extension information from the manifest file. * This function extracts the extension information from the manifest file.
@@ -45,12 +47,8 @@ async function checkIfRepoIsUpToDate(extensionPath) {
}; };
} }
/** const router = express.Router();
* Registers the endpoints for the third-party extensions API.
* @param {import('express').Express} app - Express app
* @param {any} jsonParser - JSON parser middleware
*/
function registerEndpoints(app, jsonParser) {
/** /**
* HTTP POST handler function to clone a git repository from a provided URL, read the extension manifest, * HTTP POST handler function to clone a git repository from a provided URL, read the extension manifest,
* and return extension information and path. * and return extension information and path.
@@ -60,7 +58,7 @@ function registerEndpoints(app, jsonParser) {
* *
* @returns {void} * @returns {void}
*/ */
app.post('/api/extensions/install', jsonParser, async (request, response) => { router.post('/install', jsonParser, async (request, response) => {
if (!request.body.url) { if (!request.body.url) {
return response.status(400).send('Bad Request: URL is required in the request body.'); return response.status(400).send('Bad Request: URL is required in the request body.');
} }
@@ -105,7 +103,7 @@ function registerEndpoints(app, jsonParser) {
* *
* @returns {void} * @returns {void}
*/ */
app.post('/api/extensions/update', jsonParser, async (request, response) => { router.post('/update', jsonParser, async (request, response) => {
const git = simpleGit(); const git = simpleGit();
if (!request.body.extensionName) { if (!request.body.extensionName) {
return response.status(400).send('Bad Request: extensionName is required in the request body.'); return response.status(400).send('Bad Request: extensionName is required in the request body.');
@@ -150,7 +148,7 @@ function registerEndpoints(app, jsonParser) {
* *
* @returns {void} * @returns {void}
*/ */
app.post('/api/extensions/version', jsonParser, async (request, response) => { router.post('/version', jsonParser, async (request, response) => {
const git = simpleGit(); const git = simpleGit();
if (!request.body.extensionName) { if (!request.body.extensionName) {
return response.status(400).send('Bad Request: extensionName is required in the request body.'); return response.status(400).send('Bad Request: extensionName is required in the request body.');
@@ -188,7 +186,7 @@ function registerEndpoints(app, jsonParser) {
* *
* @returns {void} * @returns {void}
*/ */
app.post('/api/extensions/delete', jsonParser, async (request, response) => { router.post('/delete', jsonParser, async (request, response) => {
if (!request.body.extensionName) { if (!request.body.extensionName) {
return response.status(400).send('Bad Request: extensionName is required in the request body.'); return response.status(400).send('Bad Request: extensionName is required in the request body.');
} }
@@ -218,8 +216,7 @@ function registerEndpoints(app, jsonParser) {
* Discover the extension folders * Discover the extension folders
* If the folder is called third-party, search for subfolders instead * If the folder is called third-party, search for subfolders instead
*/ */
app.get('/api/extensions/discover', jsonParser, function (_, response) { router.get('/discover', jsonParser, function (_, response) {
// get all folders in the extensions folder, except third-party // get all folders in the extensions folder, except third-party
const extensions = fs const extensions = fs
.readdirSync(DIRECTORIES.extensions) .readdirSync(DIRECTORIES.extensions)
@@ -243,8 +240,5 @@ function registerEndpoints(app, jsonParser) {
return response.send(extensions); return response.send(extensions);
}); });
}
module.exports = { module.exports = { router };
registerEndpoints,
};