mirror of
https://github.com/mollersuite/monofile.git
synced 2024-11-25 07:06:25 -08:00
TSDoc: mail.ts, middleware.ts, ratelimit.ts
This commit is contained in:
parent
9f480cbc87
commit
937363c9ee
|
@ -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({
|
||||||
|
|
|
@ -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")
|
||||||
|
@ -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`
|
* @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.
|
* @param tokenPermissions Permissions which your route requires.
|
||||||
* @returns Express middleware
|
* @returns Express middleware
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export const requiresPermissions = function(...tokenPermissions: auth.TokenPermission[]): RequestHandler {
|
export const requiresPermissions = function(...tokenPermissions: auth.TokenPermission[]): RequestHandler {
|
||||||
return function(req, res, next) {
|
return function(req, res, next) {
|
||||||
|
|
|
@ -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,
|
||||||
|
|
Loading…
Reference in a new issue