rjweb-server
Version:
Easy and Robust Way to create a Web Server with Many Easy-to-use Features in NodeJS
111 lines (110 loc) • 4.4 kB
TypeScript
import { UsableMiddleware } from "../Middleware";
import { UsableValidator } from "../Validator";
import { RateLimitConfig } from "../../types/internal";
import RateLimit from "./RateLimit";
import GlobalContext from "../../types/internal/classes/GlobalContext";
import { OperationObject } from "openapi3-ts/oas31";
import Path from "./Path";
export default class File<Middlewares extends UsableMiddleware[], Validators extends UsableValidator[] = [], Context extends Record<string, any> = {}, Excluded extends (keyof File<Middlewares>)[] = []> {
private validators;
protected _httpRatelimit: RateLimitConfig;
protected _wsRatelimit: RateLimitConfig;
protected promises: Promise<any>[];
protected openApi: OperationObject;
private _global;
private prefix;
private computePath;
/**
* Create a new File Loader
* @since 9.0.0
*/ constructor(prefix: string, global: GlobalContext, validators?: Validators, ratelimits?: [RateLimitConfig, RateLimitConfig], promises?: Promise<any>[], openApi?: OperationObject);
/**
* Add OpenAPI Documentation to all HTTP Endpoints in this Path (and all children)
* @since 9.0.0
*/ document(item: OperationObject): Omit<File<Middlewares, Validators, Context, [...Excluded, 'document']>, Excluded[number] | 'document'>;
/**
* Add a Ratelimit to all HTTP Endpoints in this Path (and all children)
*
* When a User requests an Endpoint, that will count against their hit count, if
* the hits exceeds the `<maxHits>` in `<timeWindow>ms`, they wont be able to access
* the route for `<penalty>ms`.
* @example
* ```
* import { time } from "@rjweb/utils"
*
* server.path('/', (path) => path
* .httpRateLimit((limit) => limit
* .hits(5)
* .window(time(20).s())
* .penalty(0)
* ) // This will allow 5 requests every 20 seconds
* .http('GET', '/hello', (ws) => ws
* .onRequest(async(ctr) => {
* ctr.print('Hello bro!')
* })
* )
* )
*
* server.rateLimit('httpRequest', (ctr) => {
* ctr.print(`Please wait ${ctr.getRateLimit()?.resetIn}ms!!!!!`)
* })
* ```
* @since 9.0.0
*/ httpRatelimit(callback: (limit: RateLimit) => any): Omit<File<Middlewares, Validators, Context, Excluded>, Excluded[number] | 'httpRatelimit'>;
/**
* Add a Ratelimit to all WebSocket Endpoints in this Path (and all children)
*
* When a User sends a message to a Socket, that will count against their hit count, if
* the hits exceeds the `<maxHits>` in `<timeWindow>ms`, they wont be able to access
* the route for `<penalty>ms`.
* @example
* ```
* import { time } from "@rjweb/utils"
*
* server.path('/', (path) => path
* .httpRateLimit((limit) => limit
* .hits(5)
* .window(time(20).s())
* .penalty(0)
* ) // This will allow 5 messages every 20 seconds
* .ws('/echo', (ws) => ws
* .onMessage(async(ctr) => {
* ctr.print(await ctr.rawMessageBytes())
* })
* )
* )
*
* server.rateLimit('wsMessage', (ctr) => {
* ctr.print(`Please wait ${ctr.getRateLimit()?.resetIn}ms!!!!!`)
* })
* ```
* @since 9.0.0
*/ wsRatelimit(callback: (limit: RateLimit) => any): Omit<File<Middlewares, Validators, Context, Excluded>, Excluded[number] | 'wsRatelimit'>;
/**
* Use a Validator on all Endpoints in this Path
*
* This will attach a Validator to all Endpoints in this Path
* @since 9.0.0
*/ validate<_Validator extends UsableValidator<any>>(validator: _Validator): File<Middlewares, [...Validators, _Validator], Context, Excluded>;
/**
* Load Files from a Directory
* @since 9.0.0
*/ load(directory: string, options?: {
/**
* Function to filter which files get loaded
* @default (path) => path.endsWith('js')
* @since 9.0.0
*/ filter?: (path: string) => boolean;
/**
* Whether to use file based routing
* @default false
* @since 9.0.0
*/ fileBasedRouting?: boolean;
}): this;
/**
* Export the File Loader to be used in route files
* @since 9.0.0
*/ export(): {
Path: new (prefix: string) => Path<Middlewares, Validators, Context>;
};
}