console styling & GET /series works again

master
Kit Kasune 1 year ago
parent bf3add353d
commit 351d7e7431
  1. 5
      api/ani/v1/router.js
  2. 1
      api/ani/v1/routes/series.js
  3. 2
      api/ani/v1/routes/series/add.js
  4. 47
      api/ani/v1/routes/series/edits.js
  5. 10
      api/index.js
  6. 7
      api/util/misc/rtcolors.js

@ -1,11 +1,14 @@
const fs = require('fs'); const fs = require('fs');
const {Router} = require("express"); const {Router} = require("express");
const chalk = require("chalk");
const reqTypeColors = require('../../util/misc/rtcolors');
const router = Router(); const router = Router();
module.exports = app => { module.exports = app => {
router router
.use((req, res, next) => {console.log(`[ANI/v1] ${req.path} | [${req.method}]`); next()}) .use((req, res, next) => {console.log(`${chalk.gray(`[ANI/v1]`)} ${req.path} ${chalk.gray(`|`)} [${chalk[reqTypeColors[req.method]](req.method)}]`); next()})
.get('/', (req, res) => res.send("This is the Natsuki Anime DB API v1 head.")) .get('/', (req, res) => res.send("This is the Natsuki Anime DB API v1 head."))
fs.readdirSync('./ani/v1/routes').filter(file => file.endsWith('.js')) fs.readdirSync('./ani/v1/routes').filter(file => file.endsWith('.js'))

@ -6,6 +6,7 @@ module.exports = (app, parentRouter) => {
router.use('/', (req, res, next) => { router.use('/', (req, res, next) => {
req.listData = Object.keys(app.cache.series).map(series => { req.listData = Object.keys(app.cache.series).map(series => {
return { return {
id: series,
name: app.cache.series[series].name, name: app.cache.series[series].name,
romaji: app.cache.series[series].romaji, romaji: app.cache.series[series].romaji,
kanji: app.cache.series[series].kanji kanji: app.cache.series[series].kanji

@ -105,7 +105,7 @@ module.exports = (app, router) => {
//TODO remove console error //TODO remove console error
}) })
.get(app.auth.tokenPass, app.auth.permsPass('series-approve'), async (req, res, next) => { .get(app.auth.tokenPass, app.auth.permsPass('series-approve'), async (req, res, next) => {
const series = await Anime.findOne(/*{$or: [*/{id: req.params.id.toLowerCase()}/*, {numericalId: req.params.id}]}*/); //TODO make sure all ID calls are lowercased const series = req.params.id.match(/^\d+$/) ? await Anime.findOne({numericalId: req.params.id.toLowerCase()}) : await Anime.findOne({id: req.params.id.toLowerCase()}); //TODO make sure all ID calls are lowercased
if (!series || (series && !series.meta.completed && req.unauthorized)) { if (!series || (series && !series.meta.completed && req.unauthorized)) {
if (req.params.id.toLowerCase() === 'queue') {return next();} if (req.params.id.toLowerCase() === 'queue') {return next();}
return res.status(400).send("A series with that ID doesn't exist!"); return res.status(400).send("A series with that ID doesn't exist!");

@ -53,53 +53,6 @@ module.exports = (app, router) => {
].forEach(field => edits.stringWrap(field[0], field[1])); ].forEach(field => edits.stringWrap(field[0], field[1]));
/*router.patch('/:id/name', app.auth.token, app.auth.perms('series-submit'), async (req, res) => {
if (!req.params.id) {return;}
const series = await Anime.findOne({id: req.params.id.toLowerCase()});
if (!editCheck(series, req, res)) {return;}
if (!req.body.name) {return res.status(400).send("You did not provide a new name in your body.");}
if (!req.body.name.match(/^[\w_\-!?.:; ]+$/gm) || req.body.name.length > 150) {return res.status(400).send("Your new name is too long or contains illegal characters.");}
try {
series.name = req.body.name.trim();
pushEdit("Updated name", req, series);
return series.save()
.then(() => res.send("Name updated."))
.catch(() => res.status(500).send("There was an error trying to update your name. Please try again."));
} catch {return res.status(500).send("There was an error trying to update your name. Please try again.");}
});
router.patch('/:id/romaji', app.auth.token, app.auth.perms('series-submit'), async (req, res) => {
if (!req.params.id) {return;}
const series = await Anime.findOne({id: req.params.id.toLowerCase()});
if (!editCheck(series, req, res)) {return;}
if (!req.body.romaji) {return res.status(400).send("You did not provide a new romanized name (romaji) in your body.");}
if (req.body.romaji.length > 150) {return res.status(400).send("Your new romanized name is too long or contains illegal characters.");}
try {
series.romaji = req.body.romaji.trim();
pushEdit("Updated romaji", req, series);
return series.save()
.then(() => res.send("Romanized name updated."))
.catch(() => res.status(500).send("There was an error trying to update your romaji. Please try again."));
} catch {return res.status(500).send("There was an error trying to update your romaji. Please try again.");}
});
router.patch('/:id/kanji', app.auth.token, app.auth.perms('series-submit'), async (req, res) => {
if (!req.params.id) {return;}
const series = await Anime.findOne({id: req.params.id.toLowerCase()});
if (!editCheck(series, req, res)) {return;}
if (!req.body.kanji) {return res.status(400).send("You did not provide a new kanji in your body.");}
if (req.body.kanji.length > 150) {return res.status(400).send("Your new kanji is too long or contains illegal characters.");}
try {
series.kanji = req.body.kanji.trim();
pushEdit("Updated kanji", req, series);
return series.save()
.then(() => res.send("Kanji updated."))
.catch(() => res.status(500).send("There was an error trying to update your kanji. Please try again."));
} catch {return res.status(500).send("There was an error trying to update your kanji. Please try again.");}
});*/
//router.use('/:id/altnames', app.auth.tokenPass, app.auth.permsPass('series-approve')) //router.use('/:id/altnames', app.auth.tokenPass, app.auth.permsPass('series-approve'))
}; };

@ -5,6 +5,8 @@ const helmet = require('helmet');
const {set, createConnection} = require('mongoose'); const {set, createConnection} = require('mongoose');
const chalk = require('chalk'); const chalk = require('chalk');
const reqTypeColors = require('./util/misc/rtcolors');
const app = express(); const app = express();
set('strictQuery', false); set('strictQuery', false);
@ -49,14 +51,6 @@ server = app.listen(4062, async () => {
require('./v1/index')(app); //initialize bot API branch require('./v1/index')(app); //initialize bot API branch
require('./ani/index')(app); //initialize ani API branch require('./ani/index')(app); //initialize ani API branch
const reqTypeColors = {
GET: 'greenBright',
POST: 'blueBright',
PATCH: 'yellowBright',
PUT: 'yellow',
DELETE: 'redBright'
};
const checkMiddleware = (middleware, string) => { const checkMiddleware = (middleware, string) => {
if (middleware.route) {Object.keys(middleware.route.methods).forEach(method => { if (middleware.route) {Object.keys(middleware.route.methods).forEach(method => {
if (middleware.route.methods[method]) {console.log(`${chalk.gray(`[`)}${chalk[reqTypeColors[method.toUpperCase()]](method.toUpperCase())}${chalk.gray(`]`)} ${string}${middleware.route.path}`);} if (middleware.route.methods[method]) {console.log(`${chalk.gray(`[`)}${chalk[reqTypeColors[method.toUpperCase()]](method.toUpperCase())}${chalk.gray(`]`)} ${string}${middleware.route.path}`);}

@ -0,0 +1,7 @@
module.exports = {
GET: 'greenBright',
POST: 'blueBright',
PATCH: 'yellowBright',
PUT: 'yellow',
DELETE: 'redBright'
};
Loading…
Cancel
Save