TSDoc: mail.ts, middleware.ts, ratelimit.ts

This commit is contained in:
May 2023-10-03 13:21:28 -07:00 committed by GitHub
parent 9f480cbc87
commit 937363c9ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 6 deletions

View file

@ -19,6 +19,13 @@ transport =
// lazy but
/**
* @description Sends an email
* @param to Target email address
* @param subject Email subject
* @param content Email content
* @returns Promise which resolves to the output from nodemailer.transport.sendMail
*/
export function sendMail(to: string, subject: string, content: string) {
return new Promise((resolve,reject) => {
transport.sendMail({

View file

@ -3,11 +3,17 @@ import express, { type RequestHandler } from "express"
import ServeError from "../lib/errors";
import * as auth from "./auth";
/**
* @description Middleware which adds an account, if any, to res.locals.acc
*/
export const getAccount: RequestHandler = function(req, res, next) {
res.locals.acc = Accounts.getFromToken(auth.tokenFor(req))
next()
}
/**
* @description Middleware which blocks requests which do not have res.locals.acc set
*/
export const requiresAccount: RequestHandler = function(_req, res, next) {
if (!res.locals.acc) {
ServeError(res, 401, "not logged in")
@ -16,6 +22,9 @@ export const requiresAccount: RequestHandler = function(_req, res, next) {
next()
}
/**
* @description Middleware which blocks requests that have res.locals.acc.admin set to a falsy value
*/
export const requiresAdmin: RequestHandler = function(_req, res, next) {
if (!res.locals.acc.admin) {
ServeError(res, 403, "you are not an administrator")
@ -25,10 +34,10 @@ export const requiresAdmin: RequestHandler = function(_req, res, next) {
}
/**
* @description Blocks requests based on the permissions which a token has. Does not apply to routes being accessed with a token of type `User`
* @param tokenPermissions Permissions which your route requires.
* @returns Express middleware
*/
* @description Blocks requests based on the permissions which a token has. Does not apply to routes being accessed with a token of type `User`
* @param tokenPermissions Permissions which your route requires.
* @returns Express middleware
*/
export const requiresPermissions = function(...tokenPermissions: auth.TokenPermission[]): RequestHandler {
return function(req, res, next) {

View file

@ -2,14 +2,19 @@ import { RequestHandler } from "express"
import { type Account } from "./accounts"
import ServeError from "./errors"
interface ratelimitSettings {
interface RatelimitSettings {
requests: number
per: number
}
export function accountRatelimit( settings: ratelimitSettings ): RequestHandler {
/**
* @description Ratelimits a route based on res.locals.acc
* @param settings Ratelimit settings
* @returns Express middleware
*/
export function accountRatelimit( settings: RatelimitSettings ): RequestHandler {
let activeLimits: {
[ key: string ]: {
requests: number,