/// import { TlsOptions } from 'tls'; import { MiddlewareCallback, Middleware } from './middleware'; import { SubApp } from './subapps'; import { RouteCallback, RouteCallbackError, Route, RouteFunction } from './routes'; /** * An alias for calling the constructor for the `Jups` class. * * Creates an instance of the `Jups` class that will be used for handling the entire HTTP * framework backend. This will be the class that you call methods like `route` and `listen`. * * @param options - The options for the instance. */ declare function jups(options?: jups.JupsOptions): jups.Jups; declare namespace jups { type Method = 'ALL' | 'GET' | 'POST' | 'HEAD' | 'PATCH' | 'PUT' | 'OPTIONS' | 'CONNECT' | 'DELETE' | 'TRACE' | 'ACL' | 'BIND' | 'CHECKOUT' | 'COPY' | 'LINK' | 'LOCK' | 'M-SEARCH' | 'MERGE' | 'MKACTIVITY' | 'MKCALENDAR' | 'MKCOL' | 'MOVE' | 'NOTIFY' | 'PROPFIND' | 'PROPPATCH' | 'PURGE' | 'REBIND' | 'REPORT' | 'SEARCH' | 'SOURCE' | 'SUBSCRIBE' | 'UNBIND' | 'UNLINK' | 'UNLOCK' | 'UNSUBSCRIBE'; interface SSLOptions extends TlsOptions { redirect?: boolean; } interface JupsOptions { noOtherwise?: boolean; noErrorHandler?: boolean; ssl?: SSLOptions; } class Jups { private _server; private _sserver?; routes: Route[]; subApps: SubApp[]; middlewares: Middleware[]; _otherwise?: RouteCallback; _error?: RouteCallbackError; all: RouteFunction; get: RouteFunction; post: RouteFunction; head: RouteFunction; patch: RouteFunction; options: RouteFunction; connect: RouteFunction; delete: RouteFunction; trace: RouteFunction; put: RouteFunction; acl: RouteFunction; bind: RouteFunction; checkout: RouteFunction; copy: RouteFunction; link: RouteFunction; lock: RouteFunction; msearch: RouteFunction; merge: RouteFunction; mkactivity: RouteFunction; mkcalendar: RouteFunction; mkcol: RouteFunction; move: RouteFunction; notify: RouteFunction; propfind: RouteFunction; proppatch: RouteFunction; purge: RouteFunction; rebind: RouteFunction; report: RouteFunction; search: RouteFunction; source: RouteFunction; subscribe: RouteFunction; unbind: RouteFunction; unlink: RouteFunction; unlock: RouteFunction; unsubscribe: RouteFunction; /** * Creates an instance of the `Jups` class that will be used for handling the entire HTTP * framework backend. This will be the class that you call methods like `route` and `listen`. * * @param options - The options for the instance. * @returns The new instance for chaining purposes. */ constructor(options?: JupsOptions); /** * This method will add a route endpoint handler to the client. * * There can be multiple handlers per endpoint as long as the preceding handlers do not end the response. * The route handling is converted to custom regex which then gets checked for matches. * * As with most web frameworks, there can be variables within the routes (though currently this is only supported with string routes). * Also, there can also be optional parameters within the route at any point. * * The accepted methods are as follows: * `ALL`, `GET`, `POST`, `HEAD`, `PATCH`, `PUT`, `OPTIONS`, `CONNECT`, `DELETE`, `TRACE`, `ACL`, `BIND`, `CHECKOUT`, `COPY`, `LINK`, `LOCK`, * `M-SEARCH`, `MERGE`, `MKACTIVITY`, `MKCALENDAR`, `MKCOL`, `MOVE`, `NOTIFY`, `PROPFIND`, `PROPPATCH`, `PURGE`, `REBIND`, `REPORT`, * `SEARCH`, `SOURCE`, `SUBSCRIBE`, `UNBIND`, `UNLINK`, `UNLOCK`, and `UNSUBSCRIBE`. * * @param method - The method for the route. Will only call the callback whenever it matches the method. * @param route - The route to match for it to call the callback. Can be either a string `'/home'`, a regex `/(home|h)/` or a mixture of string and regex `'/(home|h)'` * @param callback - The function for it to call when it matches. Needs to include `Request` and `Response` parameters. * @returns The same class to allow chaining. * * ```ts * jups() * .route('PUT', '/', (req, res) => { * res.end('Welcome to the home page!'); * }) * .route('GET', /\/(get|g)/, async (req, res) => { * res.end('This also works with async functions!'); * }) * .route('POST', 'post/(name|n)/:name/:id?', (req, res) => { * console.log(req.params); * res.end('This contains a mix of the string-regex as well as parameters and optional parameters.'); * }) * .listen(); * ``` */ route(method: Method, route: string, callback: RouteCallback): this; route(method: Method, route: RegExp, callback: RouteCallback): this; /** * Tells the `Jups` instance to use middleware or subapps. Subapps provide an easy way to organise your code. * Middleware is called before routes and are usually used to fire the same piece of code * for multiple routes following a specific base endpoint. * * @param base - This is the route that has to match for this middleware/subapp to apply. * For example, if given `'/players'`, it would only match routes beginning with `/players`. * Note, this can also just be middleware/a subapp which would default the base to `/`. * @param uses - As many middleware/subapps that you want to apply to this base. You can also mix * middleware/subapps if you please. * @returns The same class to allow chaining. * * ```ts * jups() * // This use would match any route (this is a JSON middleware). * .use('/', (req, res, next) => { * res.setHeader('content-type', 'application/json; charset=utf-8'); * next(); * }) * // Again, this would match any route since not given a base (which then defaults to `/`). * .use(async (req, res, next) => { * req.params.middleware = 2; * next(); * }) * .get('/', (req, res) => { * // This will call the first * res.end(JSON.stringify({ response: 'Testing' })); * )} * .listen(); * ``` */ use(base: Jups, ...uses: Jups[]): this; use(base: MiddlewareCallback, ...uses: Jups[]): this; use(base: Jups | MiddlewareCallback, ...uses: Jups[]): this; use(base: string, ...uses: Jups[]): this; use(base: RegExp, ...uses: Jups[]): this; use(base: MiddlewareCallback, ...uses: MiddlewareCallback[]): this; use(base: Jups, ...uses: MiddlewareCallback[]): this; use(base: Jups | MiddlewareCallback, ...uses: MiddlewareCallback[]): this; use(base: string, ...uses: MiddlewareCallback[]): this; use(base: RegExp, ...uses: MiddlewareCallback[]): this; use(base: MiddlewareCallback, ...uses: Array): this; use(base: Jups, ...uses: Array): this; use(base: Jups | MiddlewareCallback, ...uses: Array): this; use(base: string, ...uses: Array): this; use(base: RegExp, ...uses: Array): this; /** * This method is used for defining a function to be used for the `otherwise` route AKA * the `404` route. * * Simply put, this function ill be called if no other routes match the request. * * @param callback - The callback function that will be called if no other routes were matched. * @returns The same class to allow chaining. * * ```ts * jups() * // Since there are no routes to match, it will always hit this otherwise endpoint. * .otherwise((req, res) => { * res.status(404).end(); * }) * .listen(); * ``` */ otherwise(callback: RouteCallback): this; /** * This method is used for defining a function to be used incase errors occur during * the route listener. * * For example if there is an error in the code for a route, this error handler will be called. * * Note, this does not catch every single error in your code. * * @param callback - The callback function that will be called if there were errors. * @returns The same class to allow chaining. * * ```ts * jups() * // This get will error, calling the status. * .get('/', (req, res) => req.endnd()) * .error((req, res) => { * res.status(500).end(); * }) * .listen(); * ``` */ error(callback: RouteCallbackError): this; /** * This method is for initiating the app to listen for connections. Basically it starts up the server. * If you are using SSL, you want to pass it an httpsPort if you do not want to use port 443. * This method should be called AFTER defining all of your routes, subapps, and middleware. * * @param port The port to run the app on. Defaults to port 80. * @param httpsPort The port to run the app on for SSL/HTTPS. Only needed if using SSL. Defaults to port 443. */ listen(port?: number, httpsPort?: number): this; private _listener; } } export = jups;