adba
Version:
Any DataBase to API
97 lines (96 loc) • 4.11 kB
TypeScript
/**
* @file express-router.ts
* @description This file provides functionalities for creating an Express router with dynamic routes based on ORM models.
*/
import { Model } from "objection";
import express from "express";
import type { IExpressRouterConf, IRoutesObject } from "./types";
import GenericController from "./controller";
declare const ctrlRef: {
GenericController: typeof GenericController;
};
/**
* Modifies predefined routes by either adding new ones or removing existing ones.
* @param defs - The definitions to be added or removed.
* @param remove - Whether to remove the definitions.
*/
export declare function modifyDefinedRoutes(defs: Record<string, string> | string[], remove?: boolean): void;
/**
* Adds alias mappings for table names.
* @param alias - The alias mappings.
*/
export declare function addTableAlias(alias: Record<string, string>): void;
/**
* Generates an object containing routes for each model and controller based on provided configuration.
* @param models - The models to generate routes for.
* @param controllers - The associated controllers for each model.
* @param config - The configuration for including or excluding routes.
* @returns The constructed routes object.
*/
export declare function routesObject(models: Record<string, typeof Model>, controllers?: Record<string, typeof ctrlRef.GenericController>, config?: IExpressRouterConf): IRoutesObject;
/**
* Lists the routes currently defined in the Express router.
* @param router - The Express router.
* @returns An array of strings representing each route method and path.
*/
export declare function listRoutes(router: express.Router): string[];
/**
* Generates a summary of available services (tables) with their base endpoints.
* Groups routes by table name and returns one representative endpoint per table.
* Excludes custom endpoints.
*
* @param routesObj - The routes object containing route definitions.
* @param customEndpointPaths - Optional set of custom endpoint paths to exclude.
* @returns An object mapping table names (in kebab-case) to their base endpoints.
*
* @example
* ```typescript
* // Returns:
* // {
* // "users": "GET /users",
* // "join-users": "GET /join-users",
* // "other-table": "GET /other-table"
* // }
* ```
*/
export declare function generateServicesSummary(routesObj: IRoutesObject, customEndpointPaths?: Set<string>): Record<string, string>;
/**
* Replaces the default GenericController with a custom controller reference.
*
* @param CustomController - A class that extends from the original GenericController.
* @returns {boolean} True if replacement was successful.
*
* @example
* ```typescript
* class MyController extends GenericController {}
* replaceGenericController(MyController);
* ```
*/
export declare function replaceGenericController(CustomController: typeof GenericController): boolean;
/**
* Main function to configure an Express router with dynamic routes.
*
* @param routesObject - The routes object containing route definitions.
* @param config - Configuration options for the router.
* @param config.router - Express router instance.
* @param config.beforeProcess - Hook called before processing a request.
* @param config.afterProcess - Hook called after processing a request.
* @param config.debugLog - Enable debug logging.
* @returns The configured Express router.
*
* @example
* ```typescript
* import { expressRouter, routesObject } from 'adba';
* const models = await generateModels(knexInstance);
* const routes = routesObject(models);
* const router = expressRouter(routes, { debugLog: true });
* app.use('/api', router);
* ```
*/
export default function expressRouter(routesObject: IRoutesObject, { router, beforeProcess, afterProcess, debugLog, }?: {
router?: import("express-serve-static-core").Router | undefined;
beforeProcess?: ((tn: string, a: string, data: any, i: string) => any) | undefined;
afterProcess?: ((tn: string, a: string, data: any, i: string) => any) | undefined;
debugLog?: boolean | undefined;
}): express.Router;
export {};