import EventEmitter from 'node:events'; import winston from 'winston'; import type ControllerManager from './controllers/index.ts'; import type { TFolderConfig, TFolderConfigFolders } from './folderConfig.ts'; import type AbstractModel from './modules/AbstractModel.ts'; import type BaseCli from './modules/BaseCli.ts'; import type { BaseModel, TBaseModel } from './modules/BaseModel.ts'; import Cache from './services/cache/Cache.ts'; import type HttpServer from './services/http/HttpServer.ts'; import type { FrameworkRequest } from './services/http/HttpServer.ts'; import type { HttpResponse } from './services/http/responses.ts'; import type { I18n } from './services/i18n/I18n.ts'; interface AppCache { configs: Map; /** Config name → contributing source file paths (default + NODE_ENV-specific). * Read by codegen to recover env-only keys from source (`process.env.X` with * no default) that the value-based pass can't see. */ configPaths: Map; /** Config name → framework config file(s) an app config overrides. Read by * codegen to type spread keys (`...frameworkAuth`) that are `undefined` * at gen time. */ overriddenConfigPaths: Map; models: Map; modelConstructors: Map; modelPaths: { path: string; file: string; }[]; } /** Models shipped by the framework and therefore available without codegen. */ export type FrameworkModelName = 'KeyValue' | 'Lock' | 'Migration' | 'Sequence' | 'User' | 'UserOld'; /** * Model type used when a model name is not statically known: a model resolved * by runtime name is typed as a BaseModel-shaped Mongoose model. Legacy * `AbstractModel`-based models are stored under the same type until v6 removes * them. * * The raw document type is the BaseModel base shape, so writes through a * runtime-name model need a typed value, `new Model(...)`, or a cast to the * concrete model type: an inline object literal on `create` is checked against * that base shape and rejects the model's own fields. */ export type AppModel = TBaseModel; /** * Generated application model-name map. `node src/cli.ts generatetypes` * augments this interface in `genTypes.d.ts`; it is empty before code * generation by design. */ export interface AppModelTypes { } export interface IApp { getConfig(configName: string): Record; /** * Return a statically known model. Application model names and types are * generated into `genTypes.d.ts`; arbitrary strings must use * {@link getModelOrThrow}. */ getModel(modelName: TModelName): AppModelTypes[TModelName]; getModel(modelName: FrameworkModelName): AppModel | false; /** * Resolve a runtime model name or throw when it is unavailable. Generated * model mappings preserve the concrete model type for known names. */ getModelOrThrow(modelName: TModelName): AppModelTypes[TModelName]; getModelOrThrow(modelName: string): AppModel; runCliCommand(commandName: string): Promise; updateConfig(configName: string, config: Record): Record; getI18nService(): Promise; foldersConfig: TFolderConfigFolders; events: EventEmitter; readonly cache: Cache; readonly logger: winston.Logger; httpServer: null | HttpServer; controllerManager: null | ControllerManager; frameworkFolder: string; internalFilesCache: AppCache; } /** * Project HTTP boot hook. Passed to the {@link Server} constructor as the * `bootHttp` option; called with the live `app` during `startServer`, after * controllers are registered but before the adapter mounts — the seam for * app-wide HTTP wiring that doesn't fit a controller (ad-hoc routes via * `app.httpServer.routeRegistry.registerRoute(...)`, Express-level middleware, * boot setup). HTTP-specific: it needs `httpServer`, which only exists once the * server boots (CLI/worker processes never run it). Wired explicitly, not * file-discovered — the framework's discovery is folder-based and every folder * is already owned (config merges its files, controllers auto-loads its files). */ export type BootHttpHook = (app: IApp) => void | Promise; /** * Project response transform. Passed to the {@link Server} constructor as the * `transformResponse` option; called synchronously with every response the * framework renders (the `HttpResponse` a handler returns, validation and other * error responses, the 404 and 405 pages) just before it is written, and * returns the response to send: reshape the kinds you care about (typically * `json`) and return the others unchanged. A handler that writes to `res` * itself bypasses it. If the transform throws or returns anything but an * `HttpResponse`, the error is logged and the original response is sent. */ export type TransformResponseHook = (response: HttpResponse, context: { req: FrameworkRequest; }) => HttpResponse; /** Options accepted by the {@link Server} constructor. */ export interface ServerOptions extends TFolderConfig { /** * Project HTTP boot hook — see {@link BootHttpHook}. */ bootHttp?: BootHttpHook; /** * Reshapes every framework-rendered response — see {@link TransformResponseHook}. */ transformResponse?: TransformResponseHook; } /** * Main framework class. */ declare class Server { #private; cli: null | BaseCli; config: TFolderConfig; cache: AppCache; cacheService: null | Cache; app: IApp; /** * Construct new server * @param {Object} config main config object * @param {Object} config.folders folders config * @param {String} config.folders.config path to folder with config files * @param {String} config.folders.models path to folder with moidels files * @param {String} config.folders.controllers path to folder with controllers files * @param {String} config.folders.locales path to folder with locales files * @param {String} [config.folders.emails] path to folder with emails files * @param {String} config.folders.commands path to folder with commands files * @param {String} config.folders.migrations path to folder with migrations files */ constructor(config: ServerOptions); /** * Start server (http + init all http ralated functions) * @param {Function} callbackBefore404 code that should be executed before adding page 404 */ startServer(callbackBefore404?: () => Promise): Promise; /** * Do an initialization (config reading, etc) */ init({ isSkipModelInit, isSkipModelLoading, }?: { isSkipModelInit?: boolean | undefined; isSkipModelLoading?: boolean | undefined; }): Promise; /** * Load model and init them. * * `waitForConnection` (model-using CLI commands): block until the mongoose * connection is actually established before returning, so the command's first * query doesn't race a not-yet-ready connection. The HTTP server leaves it * `false` — it kicks the connection off and lets requests buffer until ready. * Schema registration below never needs a live connection either way. */ initAllModels({ waitForConnection, }?: { waitForConnection?: boolean; }): Promise; getModelFilesPathsWithInheritance(): Promise<{ path: string; file: string; }[]>; /** * Add error logging on promise reject */ addErrorHandling(): void; /** * Return config from {configName} (file name) on config folder. * Support cache and updating confing into cache * Also will update config based on NODE_ENV. If config.js and config.production.js * and NODE_ENV is production then we will load base config (config.js) and the load * environment config (config.production.js) and overwrite base config options * @see updateConfig * @param {String} configName name on config file to load * @returns {Object} config object. Structure depends of config file */ getConfig(configName: string): Record; /** * Return or create new logger instance. This is a main logger instance */ getLogger(): winston.Logger; /** * Primary designed for tests when we need to update some configs before start testing * Should be called before any initialization was done * @TODO send event to all inited components to update config * @param {String} configName * @param {Object} config */ updateConfig(configName: string, config: Record): Record; getI18nService(): Promise; /** * Return model from {modelName} (file name) on model folder. * Support cache * @param {String} modelName name on config file to load */ getModel(modelName: string): AppModel | false; /** * Resolve a model whose name is only known at runtime. * * Unlike {@link getModel}, this method never returns `false`: an unknown * model name or a lookup before initialization throws after the normal * lookup diagnostic has been logged. */ getModelOrThrow(modelName: string): AppModel; /** * Run cli command into framework (http, ws, etc) * @param {String} commandName name of command to load */ runCliCommand(commandName: string): Promise; /** * Get internal cache service */ getCache(): Cache; } export default Server;