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 // 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) { export function sendMail(to: string, subject: string, content: string) {
return new Promise((resolve,reject) => { return new Promise((resolve,reject) => {
transport.sendMail({ transport.sendMail({

View file

@ -3,11 +3,17 @@ import express, { type RequestHandler } from "express"
import ServeError from "../lib/errors"; import ServeError from "../lib/errors";
import * as auth from "./auth"; 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) { export const getAccount: RequestHandler = function(req, res, next) {
res.locals.acc = Accounts.getFromToken(auth.tokenFor(req)) res.locals.acc = Accounts.getFromToken(auth.tokenFor(req))
next() next()
} }
/**
* @description Middleware which blocks requests which do not have res.locals.acc set
*/
export const requiresAccount: RequestHandler = function(_req, res, next) { export const requiresAccount: RequestHandler = function(_req, res, next) {
if (!res.locals.acc) { if (!res.locals.acc) {
ServeError(res, 401, "not logged in") ServeError(res, 401, "not logged in")
@ -16,6 +22,9 @@ export const requiresAccount: RequestHandler = function(_req, res, next) {
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) { export const requiresAdmin: RequestHandler = function(_req, res, next) {
if (!res.locals.acc.admin) { if (!res.locals.acc.admin) {
ServeError(res, 403, "you are not an administrator") ServeError(res, 403, "you are not an administrator")

View file

@ -2,14 +2,19 @@ import { RequestHandler } from "express"
import { type Account } from "./accounts" import { type Account } from "./accounts"
import ServeError from "./errors" import ServeError from "./errors"
interface ratelimitSettings { interface RatelimitSettings {
requests: number requests: number
per: 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: { let activeLimits: {
[ key: string ]: { [ key: string ]: {
requests: number, requests: number,