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);
// Third-party extensions
require('./src/endpoints/extensions').registerEndpoints(app, jsonParser);
app.use('/api/extensions', require('./src/endpoints/extensions').router);
// Asset management
app.use('/api/assets', require('./src/endpoints/assets').router);

View File

@@ -1,8 +1,10 @@
const path = require('path');
const fs = require('fs');
const express = require('express');
const { default: simpleGit } = require('simple-git');
const sanitize = require('sanitize-filename');
const { DIRECTORIES } = require('../constants');
const { jsonParser } = require('../express-common');
/**
* This function extracts the extension information from the manifest file.
@@ -45,13 +47,9 @@ 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,
* and return extension information and path.
*
@@ -60,7 +58,7 @@ function registerEndpoints(app, jsonParser) {
*
* @returns {void}
*/
app.post('/api/extensions/install', jsonParser, async (request, response) => {
router.post('/install', jsonParser, async (request, response) => {
if (!request.body.url) {
return response.status(400).send('Bad Request: URL is required in the request body.');
}
@@ -92,9 +90,9 @@ function registerEndpoints(app, jsonParser) {
console.log('Importing custom content failed', error);
return response.status(500).send(`Server Error: ${error.message}`);
}
});
});
/**
/**
* HTTP POST handler function to pull the latest updates from a git repository
* based on the extension name provided in the request body. It returns the latest commit hash,
* the path of the extension, the status of the repository (whether it's up-to-date or not),
@@ -105,7 +103,7 @@ function registerEndpoints(app, jsonParser) {
*
* @returns {void}
*/
app.post('/api/extensions/update', jsonParser, async (request, response) => {
router.post('/update', jsonParser, async (request, response) => {
const git = simpleGit();
if (!request.body.extensionName) {
return response.status(400).send('Bad Request: extensionName is required in the request body.');
@@ -138,9 +136,9 @@ function registerEndpoints(app, jsonParser) {
console.log('Updating custom content failed', error);
return response.status(500).send(`Server Error: ${error.message}`);
}
});
});
/**
/**
* HTTP POST handler function to get the current git commit hash and branch name for a given extension.
* It checks whether the repository is up-to-date with the remote, and returns the status along with
* the remote URL of the repository.
@@ -150,7 +148,7 @@ function registerEndpoints(app, jsonParser) {
*
* @returns {void}
*/
app.post('/api/extensions/version', jsonParser, async (request, response) => {
router.post('/version', jsonParser, async (request, response) => {
const git = simpleGit();
if (!request.body.extensionName) {
return response.status(400).send('Bad Request: extensionName is required in the request body.');
@@ -178,9 +176,9 @@ function registerEndpoints(app, jsonParser) {
console.log('Getting extension version failed', error);
return response.status(500).send(`Server Error: ${error.message}`);
}
});
});
/**
/**
* HTTP POST handler function to delete a git repository based on the extension name provided in the request body.
*
* @param {Object} request - HTTP Request object, expects a JSON body with a 'url' property.
@@ -188,7 +186,7 @@ function registerEndpoints(app, jsonParser) {
*
* @returns {void}
*/
app.post('/api/extensions/delete', jsonParser, async (request, response) => {
router.post('/delete', jsonParser, async (request, response) => {
if (!request.body.extensionName) {
return response.status(400).send('Bad Request: extensionName is required in the request body.');
}
@@ -212,14 +210,13 @@ function registerEndpoints(app, jsonParser) {
console.log('Deleting custom content failed', error);
return response.status(500).send(`Server Error: ${error.message}`);
}
});
});
/**
/**
* Discover the extension folders
* 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
const extensions = fs
.readdirSync(DIRECTORIES.extensions)
@@ -242,9 +239,6 @@ function registerEndpoints(app, jsonParser) {
return response.send(extensions);
});
}
});
module.exports = {
registerEndpoints,
};
module.exports = { router };