UNPKG

rjweb-server

Version:

Easy and Robust Way to create a Web Server with Many Easy-to-use Features in NodeJS

114 lines (113 loc) 4.33 kB
import { BaseImplementation } from "../types/implementation"; import { ServerStatus } from "../types/global"; import { FullServerOptions, ServerOptions } from "../types/structures/ServerOptions"; import GlobalContext from "../types/internal/classes/GlobalContext"; import ContentTypes from "./router/ContentTypes"; import { DataContext, EndFn, ErrorCallbacks, RatelimitCallbacks, RealAny } from "../types/internal"; import FileLoader from "./router/File"; import { UsableMiddleware } from "./Middleware"; import Validator from "./Validator"; import Path from "./router/Path"; import { oas31 } from "openapi3-ts"; import HttpRequestContext from "./request/HttpRequestContext"; export declare const defaultOptions: FullServerOptions; export default class Server<const Options extends ServerOptions, Middlewares extends UsableMiddleware[] = [], Context extends Record<string, any> = {}> { private context; private options; private middlewares; private implementation; private _status; private global; private promises; private openAPISchemas; /** * Construct a new Server Instance * @example * ``` * import { Server } from "rjweb-server" * import { Runtime } from "@rjweb/runtime-bun" * * const server = new Server(Runtime, { * port: 3000 * }) * ``` * @since 3.0.0 */ constructor(implementation: BaseImplementation, options: Options, middlewares?: Middlewares, context?: Context); /** * Add a Content Type Mapping to override (or expand) content types * @since 5.3.0 */ contentTypes(callback: (builder: ContentTypes) => ContentTypes): this; Validator: new <Data extends Record<string, any>>(...args: ConstructorParameters<typeof Validator<Data, Context, Middlewares>>) => Validator<Data, Context, Middlewares>; get FileLoader(): new (prefix: string) => FileLoader<Middlewares, [], Context>; /** * Listen to Error Callbacks * @since 9.0.0 */ error<Key extends keyof ErrorCallbacks<Middlewares>>(key: Key, callback: ErrorCallbacks<Middlewares>[Key]): this; /** * Listen to Ratelimit Callbacks * @since 9.0.0 */ rateLimit<Key extends keyof RatelimitCallbacks<Middlewares>>(key: Key, callback: RatelimitCallbacks<Middlewares>[Key]): this; /** * Listen to Not Found Callbacks * @since 9.0.0 */ notFound(callback: (ctr: DataContext<'HttpRequest', 'GET', HttpRequestContext<GlobalContext>, Middlewares>) => RealAny): this; /** * Create a new Path * @since 6.0.0 */ path(prefix: string, callback: (path: Path<Middlewares, [], Context>) => any): this; /** * Listen to all HTTP Requests (does not override routed ones) * @since 9.0.0 */ http(callback: (ctr: DataContext<'HttpRequest', 'GET', HttpRequestContext<Context>, Middlewares>, end: EndFn) => RealAny): this; /** * Generate an OpenAPI Specification for the Server * @since 9.0.0 */ openAPI(name: string, version: string, server: oas31.ServerObject, contact?: oas31.ContactObject): oas31.SchemaObject; /** * Add an OpenAPI Schema to the Server * @since 9.0.0 */ schema(name: string, schema: oas31.SchemaObject | oas31.ReferenceObject): this; /** * Get the Server's Port that its listening on * @since 9.0.0 */ port(): (Options extends { port: number; } ? Options['port'] extends 0 ? number : Options['port'] : number) | null; /** * Get the Server's Status * @since 9.0.0 */ status(): ServerStatus; /** * Start the Server Instance * @example * ``` * import { Server } from "rjweb-server" * * const server = new Server({}) * * server.start().then((port) => { * console.log(`Server Started on Port ${port}`) * }) * ``` * @since 3.0.0 */ start(): Promise<number>; /** * Stop the Server Instance * @example * ``` * import { Server } from "rjweb-server" * * const server = new Server({}) * * server.start().then((port) => { * console.log(`Server Started on Port ${port}`) * * setTimeout(() => { * server.stop() * console.log('Server Stopped after 5 seconds') * }, 5000) * }) * ``` * @since 3.0.0 */ stop(): this; }