jsm-core
Version: 
Core library for JSM project
86 lines (85 loc) • 5.86 kB
TypeScript
import 'reflect-metadata';
import { RequestHandler, Request, Response, NextFunction, Router } from "express";
import { ExtractRouteParams, TRegisterRouteHandlerOptions } from './types';
export declare enum HttpMethods {
    GET = "get",
    POST = "post",
    PUT = "put",
    DELETE = "delete",
    PATCH = "patch",
    OPTIONS = "options",
    HEAD = "head"
}
export declare const JsmRequestHandlerMiddleware: (errorHandler?: (err: Error | Jsm.Core.Utils.Api.Response.Error, req: Request, res: Response, next: NextFunction) => void) => Router;
/**
  * Register a request handler for a specific HTTP method and path.
  * @param {HttpMethods} method - The HTTP method to register the handler for.
  * @param {string} path - The path to register the handler for.
  * @param {boolean} addApiPrefix - Whether to add the API prefix to the path. @default true
  * @param {string} [customId] - Used to check if it's already registered to prevent duplications.
  *
  */
export declare const registerRouteHandler: <Path extends `/${string}`>(method: HttpMethods, path: Path, requestHandler: RequestHandler<ExtractRouteParams<Path>> | RequestHandler<ExtractRouteParams<Path>>[], opt?: {
    customId?: string;
    addApiPrefix?: boolean;
}) => void;
export declare const router: {
    /**
   * Registers a GET request handler for the specified path.
   * @param {`/${string}`} path - The path to register the handler for.
   * @param {...(RequestHandler | RequestHandler[] | TRegisterRouteHandlerOptions)} args - The request handlers or options for the route. Multiple handlers can be provided.
   * @param {boolean} [addApiPrefix] - Whether to add the API prefix to the path. Defaults to true.
   * @param {string} [customId] - A custom identifier for the route, used to prevent duplicate registrations.
   */
    get(path: `/${string}`, ...args: (RequestHandler | RequestHandler[] | TRegisterRouteHandlerOptions)[]): void;
    /**
   * Registers a POST request handler for the specified path.
   * @param {`/${string}`} path - The path to register the handler for.
   * @param {...(RequestHandler | RequestHandler[] | TRegisterRouteHandlerOptions)} args - The request handlers or options for the route. Multiple handlers can be provided.
   * @param {boolean} [addApiPrefix] - Whether to add the API prefix to the path. Defaults to true.
   * @param {string} [customId] - A custom identifier for the route, used to prevent duplicate registrations.
   */
    post(path: `/${string}`, ...args: (RequestHandler | RequestHandler[] | TRegisterRouteHandlerOptions)[]): void;
    /**
   * Registers a PUT request handler for the specified path.
   * @param {`/${string}`} path - The path to register the handler for.
   * @param {...(RequestHandler | RequestHandler[] | TRegisterRouteHandlerOptions)} args - The request handlers or options for the route. Multiple handlers can be provided.
   * @param {boolean} [addApiPrefix] - Whether to add the API prefix to the path. Defaults to true.
   * @param {string} [customId] - A custom identifier for the route, used to prevent duplicate registrations.
   */
    put(path: `/${string}`, ...args: (RequestHandler | RequestHandler[] | TRegisterRouteHandlerOptions)[]): void;
    /**
   * Registers a DELETE request handler for the specified path.
   * @param {`/${string}`} path - The path to register the handler for.
   * @param {...(RequestHandler | RequestHandler[] | TRegisterRouteHandlerOptions)} args - The request handlers or options for the route. Multiple handlers can be provided.
   * @param {boolean} [addApiPrefix] - Whether to add the API prefix to the path. Defaults to true.
   * @param {string} [customId] - A custom identifier for the route, used to prevent duplicate registrations.
   */
    delete(path: `/${string}`, ...args: (RequestHandler | RequestHandler[] | TRegisterRouteHandlerOptions)[]): void;
    /**
   * Registers a PATCH request handler for the specified path.
   * @param {`/${string}`} path - The path to register the handler for.
   * @param {...(RequestHandler | RequestHandler[] | TRegisterRouteHandlerOptions)} args - The request handlers or options for the route. Multiple handlers can be provided.
   * @param {boolean} [addApiPrefix] - Whether to add the API prefix to the path. Defaults to true.
   * @param {string} [customId] - A custom identifier for the route, used to prevent duplicate registrations.
   */
    patch(path: `/${string}`, ...args: (RequestHandler | RequestHandler[] | TRegisterRouteHandlerOptions)[]): void;
    /**
   * Registers an OPTIONS request handler for the specified path.
   * @param {`/${string}`} path - The path to register the handler for.
   * @param {...(RequestHandler | RequestHandler[] | TRegisterRouteHandlerOptions)} args - The request handlers or options for the route. Multiple handlers can be provided.
   * @param {boolean} [addApiPrefix] - Whether to add the API prefix to the path. Defaults to true.
   * @param {string} [customId] - A custom identifier for the route, used to prevent duplicate registrations.
   */
    options(path: `/${string}`, ...args: (RequestHandler | RequestHandler[] | TRegisterRouteHandlerOptions)[]): void;
    /**
   * Registers a HEAD request handler for the specified path.
   * @param {`/${string}`} path - The path to register the handler for.
   * @param {...(RequestHandler | RequestHandler[] | TRegisterRouteHandlerOptions)} args - The request handlers or options for the route. Multiple handlers can be provided.
   * @param {boolean} [addApiPrefix] - Whether to add the API prefix to the path. Defaults to true.
   * @param {string} [customId] - A custom identifier for the route, used to prevent duplicate registrations.
   */
    head(path: `/${string}`, ...args: (RequestHandler | RequestHandler[] | TRegisterRouteHandlerOptions)[]): void;
};
export * from './jsm-router';
export * from './types';