mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Merge pull request #1456 from valadaptive/extensions-router
Use Express router for extensions endpoint
This commit is contained in:
@@ -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);
|
||||||
|
@@ -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,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,
|
* 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.');
|
||||||
}
|
}
|
||||||
@@ -92,9 +90,9 @@ function registerEndpoints(app, jsonParser) {
|
|||||||
console.log('Importing custom content failed', error);
|
console.log('Importing custom content failed', error);
|
||||||
return response.status(500).send(`Server Error: ${error.message}`);
|
return response.status(500).send(`Server Error: ${error.message}`);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HTTP POST handler function to pull the latest updates from a git repository
|
* 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,
|
* 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),
|
* 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}
|
* @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.');
|
||||||
@@ -138,9 +136,9 @@ function registerEndpoints(app, jsonParser) {
|
|||||||
console.log('Updating custom content failed', error);
|
console.log('Updating custom content failed', error);
|
||||||
return response.status(500).send(`Server Error: ${error.message}`);
|
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.
|
* 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
|
* It checks whether the repository is up-to-date with the remote, and returns the status along with
|
||||||
* the remote URL of the repository.
|
* the remote URL of the repository.
|
||||||
@@ -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.');
|
||||||
@@ -178,9 +176,9 @@ function registerEndpoints(app, jsonParser) {
|
|||||||
console.log('Getting extension version failed', error);
|
console.log('Getting extension version failed', error);
|
||||||
return response.status(500).send(`Server Error: ${error.message}`);
|
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.
|
* 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.
|
* @param {Object} request - HTTP Request object, expects a JSON body with a 'url' property.
|
||||||
@@ -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.');
|
||||||
}
|
}
|
||||||
@@ -212,14 +210,13 @@ function registerEndpoints(app, jsonParser) {
|
|||||||
console.log('Deleting custom content failed', error);
|
console.log('Deleting custom content failed', error);
|
||||||
return response.status(500).send(`Server Error: ${error.message}`);
|
return response.status(500).send(`Server Error: ${error.message}`);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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)
|
||||||
@@ -242,9 +239,6 @@ function registerEndpoints(app, jsonParser) {
|
|||||||
|
|
||||||
|
|
||||||
return response.send(extensions);
|
return response.send(extensions);
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = { router };
|
||||||
registerEndpoints,
|
|
||||||
};
|
|
||||||
|
Reference in New Issue
Block a user