mirror of
https://github.com/20kaushik02/spotify-manager.git
synced 2026-01-25 06:04:05 +00:00
MASSIVE commit
- moved to typescript - axios rate limitmodule is busted, removed for now, do something else for that - sequelize-typescript - dotenv, not dotenv-flow - removed playlist details route types for API ton of minor fixes and improvements
This commit is contained in:
@@ -1,42 +0,0 @@
|
||||
import { validationResult } from "express-validator";
|
||||
|
||||
import * as typedefs from "../typedefs.js";
|
||||
import { getNestedValuesString } from "../utils/jsonTransformer.js";
|
||||
import curriedLogger from "../utils/logger.js";
|
||||
const logger = curriedLogger(import.meta);
|
||||
|
||||
/**
|
||||
* Refer: https://stackoverflow.com/questions/58848625/access-messages-in-express-validator
|
||||
*
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
* @param {typedefs.Next} next
|
||||
*/
|
||||
export const validate = (req, res, next) => {
|
||||
const errors = validationResult(req);
|
||||
if (errors.isEmpty()) {
|
||||
return next();
|
||||
}
|
||||
|
||||
const extractedErrors = [];
|
||||
errors.array().forEach(err => {
|
||||
if (err.type === "alternative") {
|
||||
err.nestedErrors.forEach(nestedErr => {
|
||||
extractedErrors.push({
|
||||
[nestedErr.path]: nestedErr.msg
|
||||
});
|
||||
});
|
||||
} else if (err.type === "field") {
|
||||
extractedErrors.push({
|
||||
[err.path]: err.msg
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
res.status(400).json({
|
||||
message: getNestedValuesString(extractedErrors),
|
||||
errors: extractedErrors
|
||||
});
|
||||
logger.warn("invalid request", { extractedErrors });
|
||||
return;
|
||||
}
|
||||
38
validators/index.ts
Normal file
38
validators/index.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { validationResult } from "express-validator";
|
||||
|
||||
import type { RequestHandler } from "express";
|
||||
|
||||
import { getNestedValuesString } from "../utils/jsonTransformer.ts";
|
||||
|
||||
import curriedLogger from "../utils/logger.ts";
|
||||
const logger = curriedLogger(import.meta.filename);
|
||||
|
||||
/** Refer: https://stackoverflow.com/questions/58848625/access-messages-in-express-validator */
|
||||
export const validate: RequestHandler = (req, res, next) => {
|
||||
const errors = validationResult(req);
|
||||
if (errors.isEmpty()) {
|
||||
return next();
|
||||
}
|
||||
|
||||
const extractedErrors: Record<string, string>[] = [];
|
||||
errors.array().forEach((err) => {
|
||||
if (err.type === "alternative") {
|
||||
err.nestedErrors.forEach((nestedErr) => {
|
||||
extractedErrors.push({
|
||||
[nestedErr.path]: nestedErr.msg,
|
||||
});
|
||||
});
|
||||
} else if (err.type === "field") {
|
||||
extractedErrors.push({
|
||||
[err.path]: err.msg,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
res.status(400).send({
|
||||
message: getNestedValuesString(extractedErrors),
|
||||
errors: extractedErrors,
|
||||
});
|
||||
logger.warn("invalid request", { extractedErrors });
|
||||
return null;
|
||||
};
|
||||
@@ -1,27 +0,0 @@
|
||||
import { body, header, param, query } from "express-validator";
|
||||
import * as typedefs from "../typedefs.js";
|
||||
|
||||
/**
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
* @param {typedefs.Next} next
|
||||
*/
|
||||
export const createLinkValidator = async (req, res, next) => {
|
||||
await body("from")
|
||||
.notEmpty()
|
||||
.withMessage("from not defined in body")
|
||||
.isURL()
|
||||
.withMessage("from must be a valid link")
|
||||
.run(req);
|
||||
await body("to")
|
||||
.notEmpty()
|
||||
.withMessage("to not defined in body")
|
||||
.isURL()
|
||||
.withMessage("to must be a valid link")
|
||||
.run(req);
|
||||
next();
|
||||
}
|
||||
|
||||
export { createLinkValidator as removeLinkValidator };
|
||||
export { createLinkValidator as populateSingleLinkValidator };
|
||||
export { createLinkValidator as pruneSingleLinkValidator };
|
||||
25
validators/operations.ts
Normal file
25
validators/operations.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { body } from "express-validator";
|
||||
import type { RequestHandler } from "express";
|
||||
|
||||
const createLinkValidator: RequestHandler = async (req, _res, next) => {
|
||||
await body("from")
|
||||
.notEmpty()
|
||||
.withMessage("from not defined in body")
|
||||
.isURL()
|
||||
.withMessage("from must be a valid link")
|
||||
.run(req);
|
||||
await body("to")
|
||||
.notEmpty()
|
||||
.withMessage("to not defined in body")
|
||||
.isURL()
|
||||
.withMessage("to must be a valid link")
|
||||
.run(req);
|
||||
next();
|
||||
};
|
||||
|
||||
export {
|
||||
createLinkValidator,
|
||||
createLinkValidator as removeLinkValidator,
|
||||
createLinkValidator as populateSingleLinkValidator,
|
||||
createLinkValidator as pruneSingleLinkValidator,
|
||||
};
|
||||
@@ -1,17 +0,0 @@
|
||||
import { body, header, param, query } from "express-validator";
|
||||
import * as typedefs from "../typedefs.js";
|
||||
|
||||
/**
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
* @param {typedefs.Next} next
|
||||
*/
|
||||
export const getPlaylistDetailsValidator = async (req, res, next) => {
|
||||
await query("playlist_link")
|
||||
.notEmpty()
|
||||
.withMessage("playlist_link not defined in query")
|
||||
.isURL()
|
||||
.withMessage("playlist_link must be a valid link")
|
||||
.run(req);
|
||||
next();
|
||||
}
|
||||
14
validators/playlists.ts
Normal file
14
validators/playlists.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { query } from "express-validator";
|
||||
import type { RequestHandler } from "express";
|
||||
|
||||
const getPlaylistDetailsValidator: RequestHandler = async (req, _res, next) => {
|
||||
await query("playlist_link")
|
||||
.notEmpty()
|
||||
.withMessage("playlist_link not defined in query")
|
||||
.isURL()
|
||||
.withMessage("playlist_link must be a valid link")
|
||||
.run(req);
|
||||
next();
|
||||
};
|
||||
|
||||
export { getPlaylistDetailsValidator };
|
||||
Reference in New Issue
Block a user