Merge pull request #1460 from valadaptive/presets-router

Use Express router for presets endpoint
This commit is contained in:
Cohee 2023-12-04 23:53:58 +02:00 committed by GitHub
commit 56da2d1f81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 86 additions and 91 deletions

View File

@ -3582,7 +3582,7 @@ app.use('/api/openai', require('./src/endpoints/openai').router);
require('./src/endpoints/tokenizers').registerEndpoints(app, jsonParser);
// Preset management
require('./src/endpoints/presets').registerEndpoints(app, jsonParser);
app.use('/api/presets', require('./src/endpoints/presets').router);
// Secrets managemenet
require('./src/endpoints/secrets').registerEndpoints(app, jsonParser);

View File

@ -1,9 +1,11 @@
const fs = require('fs');
const path = require('path');
const express = require('express');
const sanitize = require('sanitize-filename');
const writeFileAtomicSync = require('write-file-atomic').sync;
const { DIRECTORIES } = require('../constants');
const { getDefaultPresetFile, getDefaultPresets } = require('./content-manager');
const { jsonParser } = require('../express-common');
/**
* Gets the folder and extension for the preset settings based on the API source ID.
@ -30,13 +32,9 @@ function getPresetSettingsByAPI(apiId) {
}
}
/**
* Registers the preset management endpoints.
* @param {import('express').Express} app Express app
* @param {any} jsonParser JSON parser middleware
*/
function registerEndpoints(app, jsonParser) {
app.post('/api/presets/save', jsonParser, function (request, response) {
const router = express.Router();
router.post('/save', jsonParser, function (request, response) {
const name = sanitize(request.body.name);
if (!request.body.preset || !name) {
return response.sendStatus(400);
@ -54,7 +52,7 @@ function registerEndpoints(app, jsonParser) {
return response.send({ name });
});
app.post('/api/presets/delete', jsonParser, function (request, response) {
router.post('/delete', jsonParser, function (request, response) {
const name = sanitize(request.body.name);
if (!name) {
return response.sendStatus(400);
@ -77,7 +75,7 @@ function registerEndpoints(app, jsonParser) {
}
});
app.post('/api/presets/restore', jsonParser, function (request, response) {
router.post('/restore', jsonParser, function (request, response) {
try {
const settings = getPresetSettingsByAPI(request.body.apiId);
const name = sanitize(request.body.name);
@ -100,7 +98,7 @@ function registerEndpoints(app, jsonParser) {
});
// TODO: Merge with /api/presets/save
app.post('/api/presets/save-openai', jsonParser, function (request, response) {
router.post('/save-openai', jsonParser, function (request, response) {
if (!request.body || typeof request.query.name !== 'string') return response.sendStatus(400);
const name = sanitize(request.query.name);
if (!name) return response.sendStatus(400);
@ -112,7 +110,7 @@ function registerEndpoints(app, jsonParser) {
});
// TODO: Merge with /api/presets/delete
app.post('/api/presets/delete-openai', jsonParser, function (request, response) {
router.post('/delete-openai', jsonParser, function (request, response) {
if (!request.body || !request.body.name) {
return response.sendStatus(400);
}
@ -127,8 +125,5 @@ function registerEndpoints(app, jsonParser) {
return response.send({ error: true });
});
}
module.exports = {
registerEndpoints,
};
module.exports = { router };