@creamapi/cream
Version:
Concise REST API Maker - An extension library for express to create REST APIs faster
123 lines (122 loc) • 4.61 kB
TypeScript
import { ExpressModule, ExpressModules } from './ExpressAdapter/ExpressModule';
import { Express } from 'express';
import { ExpressErrorHandler } from './ExpressErrorHandler/ExpressErrorHandler';
import { ExpressService, ExpressServices } from './ExpressService/ExpressService';
/**
* This class is the main class for your Cream-based REST API
* It will handle controllers, services and will communicate with
* express for you.
*
* @example To use it you can either extend from it or create a new object
* ```ts
* import express from "express";
* import { ExpressApplication } from "@creamapi/cream";
*
* let expressApp = express();
* expressApp.use(express.json());
* let app = new ExpressApplication(expressApp, 4040);
* app.addControllers([<add your controller here>]);
* app.start();
* ```
*/
export declare class ExpressApplication {
private app;
private _errorHandler?;
/**
* The map of active and registered controllers.
* The key will be the name of the controller.
* By this I mean the literal class name.
* Only objects that extend ExpressModule can be
* used as a controller
*/
private controllers;
/**
* The map of active and registered services.
* The key will be the id given to the service when describing it.
*/
private services;
/**
* The port to which the server will be bounded to.
*/
private port;
/**
* The server instance given by the express API
*/
private server?;
/**
* @param app is the express application that will handle the requests.
* @param port is the port that the server will be bound to
* @param _errorHandler is you custom implementation of the error handler that extends ExpressErrorHandler
*/
constructor(app: Express, port: number, _errorHandler?: ExpressErrorHandler | undefined);
/**
* This attribute setter allows for setting a new custom error handler
*/
set errorHandler(v: ExpressErrorHandler);
/**
* This method adds a controller that handles a specific URL Zone
* declared by the ExpressController decorator
* @param controller An instance of the controller that handles the
* specific route
*/
addController<T extends ExpressModule>(controller: T): void;
/**
* This method adds a list of controllers to the current application
* It will add them in the provided order
* @param controllers The list of controllers to be added to the current application
* @returns void
*/
addControllers(controllers: ExpressModules): void;
/**
* This method adds a service to the current application
* The service must be uniquely identified by @ExpressSerivce.IdentifiedBy(id: string)
* decorator
*
* @param service the service to be added to the current application
* @returns void
*/
addService(service: ExpressService): void;
/**
* Adds a list of services to the current application
* Remember that the services must be uniquely identified
* @param services The list of services to be added
*/
addServices(services: ExpressServices): void;
/**
* This method is an interface to express for providing a custom error
* handler
*/
private handleErrors;
/**
* This method initializes all the registered services.
* It tries to initialize all services before throwing an error
* In this way the developer can see all the issues at once and
* doesn't need to recompile for each error
* @returns true if all services are correctly intialized, false otherwise
*/
private initServices;
/**
* Starts the express application
*/
start(): Promise<void>;
/**
* This function is used to stop the server on purpose
* @returns void
* @throws any generated error by Server.close
*/
stop(): Promise<void>;
/**
* @returns the active express application
*/
getExpressApp(): Express;
/**
* This method is used when we want to retreive a shared service
* within a controller. This is useful for example when we want to share
* user data among the services but we don't want to access the database
* everytime so a runtime service that is synced with the DB but caches data
* locally can be used.
* @param serviceId the service identifier that is given with IdentifiedBy decorator
* @returns the requested service or undefined if the service was not found
*/
getService<T extends ExpressService>(serviceId: string): T;
}