Class Febby

The Febby class represents the main application framework. It initializes and configures the Express.js application and provides methods for routing, middleware, and database connections.

Hierarchy

  • Febby

Implements

Constructors

Properties

appConfig: IAppConfig
expressApp: Express = ...
mainRouter: Router = ...
redis: Redis
server: Server<typeof IncomingMessage, typeof ServerResponse>
instance: Febby

Methods

  • Deprecated

    Initializes and starts the Febby application.

    Parameters

    • Optional cb: Function

      An optional callback function to be executed after the application has started.

    Returns Promise<void>

  • Deprecated

    Closes the database connection used by the Febby application (alias for closeDbConnection).

    Returns

    Example

    febby.closeConnection();
    

    Returns Promise<void>

  • Deprecated

    Closes the database connection used by the Febby application.

    Returns

    Example

    febby.closeDbConnection();
    

    Returns Promise<void>

  • Connects to the MongoDB database using Mongoose.

    Returns Promise<void>

  • Connects to the Redis server using ioredis.

    Parameters

    • redisOpts: RedisOptions

    Returns Promise<void>

  • Configures CRUD (Create, Read, Update, Delete) operations for a model and registers corresponding routes.

    Returns

    Example

    febby.crud("/api/users", {
    crud: true,
    middlewares: [],
    get: [GetMiddleware],
    post: [],
    put: [PutMiddleware],
    delete: [],
    }, userModel);

    Parameters

    • Optional path: string = "/"

      The base URL path for the CRUD operations.

    • config: ICrudConfig

      The CRUD configuration.

    • model: Model<Document<any, any, any>, {}, {}, {}, any>

      The Mongoose model for the data.

    • Optional router: Router = ...

      The router to which the CRUD routes should be registered (default is the main router).

    Returns Promise<void>

  • Deprecated

    • use case is very rare so deprecating it finalHandler will register final middleware function

    Returns

    Parameters

    • middleware: NextFunction

      Middleware function

    Returns Promise<void>

  • Deprecated

    Registers a final middleware function to be used at the end of the middleware chain.

    Returns

    Example

    febby.finalHandler((req, res, next) => {
    // Final middleware logic
    next();
    });

    Parameters

    • middlewares: NextFunction[]

    Returns Promise<void>

  • Loads default middlewares such as morgan, body-parser, helmet, and cors for the express app.

    Returns

    • A promise that resolves once the middlewares are loaded.

    Example

    await febby.loadDefaultMiddlewares();
    

    Returns Promise<void>

  • Load OpenAPI configuration from a YAML file and register routes based on the specification.

    This method is used when you want to create your application using an OpenAPI specification in YAML format. It reads the YAML file, parses it, and generates routes and controllers based on the specification. include x-controller,x-middlewares in each route definition for mapping.

    Returns

    A Promise that resolves when the OpenAPI configuration is successfully loaded and routes are registered.

    Example

    // Load OpenAPI configuration from a YAML file and register routes with default options.
    await loadOpenAPIConfigYAML("/path/to/openapi.yaml");

    Example

    // Load OpenAPI configuration from a YAML file and customize controller and middleware options.
    const customOptions = {
    controllers: {
    UserController: (req, res) => { /* Custom UserController logic * / },
    },
    middleware: [
    (req, res, next) => { /* Custom middleware logic * / },
    ],
    openApiValidatorOptions: {
    validateApiSpec: true,
    validateResponses: true,
    validateRequests: true,
    }
    };
    await loadOpenAPIConfigYAML("/path/to/openapi.yaml", customOptions);

    Example

    // Load OpenAPI configuration from a YAML file and provide controller and middleware options.
    const customOptions = {
    controllers: path.join(__dirname, 'controllers'), // path to controller directory
    middleware: path.join(__dirname, 'middlewares'), // path to middleware directory
    openApiValidatorOptions: {
    validateApiSpec: true,
    validateResponses: true,
    validateRequests: true,
    }
    };
    await loadOpenAPIConfigYAML("/path/to/openapi.yaml", customOptions);

    Parameters

    • path: string

      The path to the OpenAPI YAML spec file.

    • options: IOpenApiOptions = ...

      An optional configuration object that holds controller, middleware definitions, and validation settings.

    Returns Promise<void>

  • Registers a middleware function to be used in the application.

    Returns

    Example

    febby.middleware((req, res, next) => {
    // Custom middleware logic
    next();
    });

    Parameters

    • middleware: Handler

      The middleware function to be registered.

    • Optional router: Router = ...

      The router to which the middleware should be attached (default is the main router).

    Returns Promise<void>

  • Deprecated

    Registers an array of middleware functions to be used in the application.

    Returns

    Example

    febby.middlewares([
    (req, res, next) => {
    // Custom middleware 1 logic
    next();
    },
    (req, res, next) => {
    // Custom middleware 2 logic
    next();
    },
    ]);

    Parameters

    • list: Handler[]
    • Optional router: Router = ...

      The router to which the middlewares should be attached (default is the main router).

    Returns Promise<void>

  • Defines a Mongoose model for data storage and retrieval.

    Returns

    • The Mongoose model instance.

    Example

    const User = febby.model("User", userSchema);
    

    Parameters

    • name: string

      The name of the Mongoose model.

    • Optional schema: Schema<any, Model<any, any, any, any, any>, {}, {}, {}, {}, DefaultSchemaOptions, {}>

      The schema for the Mongoose model.

    Returns Promise<Model<any, {}, {}, {}, any>>

  • Retrieves all defined Mongoose models.

    Returns

    • An object containing all defined Mongoose models.

    Example

    const models = febby.models();
    const User = models["User"];

    Returns Promise<{
        [index: string]: Model<Document & any>;
    }>

  • Registers a route with a specified path, HTTP method, handler function, and optional middlewares.

    Returns

    Example

    febby.route({
    method: "get",
    path: "/api/users",
    handler: (req, res) => {
    // Handle GET request for /api/users
    },
    middlewares: [],
    });

    Parameters

    Returns Promise<void>

  • Creates and returns a new router instance.

    Returns

    • The newly created router instance.

    Example

    const apiRouter = febby.router("/api");
    

    Parameters

    • url: string

      The base URL path for the new router.

    • Optional router: Router

      The parent router to which the new router should be attached (default is the main router).

    • Optional options: RouterOptions

      Options for configuring the new router.

    Returns Promise<Router>

  • Deprecated

    Registers multiple routes from an array of route configurations.

    Returns

    Example

    febby.routes([
    {
    method: "get",
    path: "/api/users",
    handler: (req, res) => {
    // Handle GET request for /api/users
    },
    },
    {
    method: "post",
    path: "/api/users",
    handler: (req, res) => {
    // Handle POST request for /api/users
    },
    },
    ]);

    Parameters

    Returns Promise<void>

  • Deprecated

    Shuts down the Febby application.

    Returns

    Example

    febby.shutdown();
    

    Returns Promise<void>

  • Starts the Febby application.

    Returns

    • A promise that resolves once the application has started.

    Example

    await febby.start();
    

    Returns Promise<void>

Generated using TypeDoc