UNPKG

@zerva/core

Version:

🌱 Simple event driven server

150 lines (143 loc) • 6.08 kB
import { SchemaEnvOptions, Type, Infer, LogConfig, LogLevel, LoggerInterface, Emitter, DisposerFunction } from 'zeed'; interface ZervaConfigOptions<T> extends SchemaEnvOptions<T> { moduleName?: string; } /** * Get the configuration object based on the provided schema and environment variables. * * @param schema Description of the configuration as schema. * @param options ZervaConfigOptions * @returns The configuration object inferred from the schema and the environment variables. */ declare function getConfig<T extends Type<any>>(schema: T, options?: ZervaConfigOptions<T>): Infer<T>; /** * * @returns A string representation suitable as template for an .env file. */ declare function dumpConfig(): string; declare function getModuleContext<T extends Type<unknown> = Type<any>>(name: string): ZervaModuleContext<T> | undefined; /** * Check existance of registered module. * * @param name Name of module * @param strict Log error if check fails * @returns `true` if `module` has been registered before */ declare function hasModule(name: string, strict?: boolean): boolean; /** * Check existance of registered modules, log error if missing. */ declare function requireModules(...requiredModules: (string | string[])[]): boolean; declare function assertModules(...requiredModules: (string | string[])[]): void; /** * Register module by name and check for modules it depends on * * @param moduleName Module name to register * @param dependencies List of modules names that have to be registered before in this context * @deprecation Use `registerModule` instead */ declare function register(moduleName: string, ...dependencies: (string | string[])[]): boolean; interface ZervaModuleOptions<T> { name?: string; description?: string; url?: string; requires?: string[] | string; configSchema?: T; configOptions?: ZervaConfigOptions<T>; options?: Partial<Infer<T>>; log?: LogConfig; logLevel?: LogLevel; } interface ZervaModuleContext<T> { name: string; config: Infer<T>; moduleOptions?: ZervaModuleOptions<T>; log: LoggerInterface; on: typeof on; once: typeof once; emit: typeof emit; } /** * Register a module with its configuration and dependencies. * * This function registers a module by its name, checks for required modules, * and retrieves its configuration based on the provided schema. * * It also initializes a logger for the module, where log details can be customized. * If `configSchema.log` is of type `LogConfig` the logger will be created from it. * * `options` can be used to override the default configuration values i.e. first * apply `options` and then the `configSchema` values. * * @param name Name of the module to register * @param moduleOptions Additional options for the module * @returns { name: string, config: Infer<T>, log: LoggerInterface } */ declare function registerModule<T extends Type<unknown> = Type<any>>(name: string, moduleOptions?: ZervaModuleOptions<T>): ZervaModuleContext<T>; declare function use<T extends Type<unknown> = Type<any>, R = any>(moduleOptions: ZervaModuleOptions<T> & { name: string; setup: (context: ZervaModuleContext<T>) => R; }): (options?: Partial<Infer<T>>) => R; declare global { interface ZContextEvents { close: () => void; } interface ZeedGlobalContext { zerva?: ZContext; } const ZERVA_DEVELOPMENT: boolean; const ZERVA_PRODUCTION: boolean; const ZERVA_VERSION: string; } declare class ZContext extends Emitter<ZContextEvents> { name: string; modules: string[]; eventNamesEmitted: Record<string, boolean>; uses: Record<string, ZervaModuleContext<any>>; } declare let setContext: (newContext?: ZContext) => void; declare let getContext: () => ZContext; /** The global context as constant */ declare const zerva: ZContext; /** Emit via the current global context */ declare function emit<U extends keyof ZContextEvents>(event: U, ...args: Parameters<ZContextEvents[U]>): Promise<boolean>; /** Listener that binds to the current global context */ declare function on<U extends keyof ZContextEvents>(first: Partial<ZContextEvents>): DisposerFunction; declare function on<U extends keyof ZContextEvents>(first: U, listener: ZContextEvents[U]): DisposerFunction; declare function once<U extends keyof ZContextEvents>(first: U, listener: ZContextEvents[U]): DisposerFunction; /** * Set a different global context. Restores previous context after execution. * This is not async to avoid sideeffects! * * @param newContext New context * @param handler Executed with `newContext` set as global context */ declare function withContext(newContext: ZContext | undefined, handler: (context?: ZContext) => void): void; /** * Set a different global context. Restores previous context after execution. * * @param handler Executed with `newContext` set as global context */ declare function createContext(handler: (context?: ZContext) => void): void; declare global { interface ZContextEvents { serveInit: () => void; serveStart: () => void; serveStop: () => void; serveDispose: () => void; } } /** @deprecated use `on('serveInit', handler)` */ declare function onInit(handler: () => void): void; /** @deprecated use `on('serveStart', handler)` */ declare function onStart(handler: () => void): void; /** @deprecated use `on('serveStop', handler)` */ declare function onStop(handler: () => void): void; declare function serveStop(): Promise<void>; /** * A simple context to serve modules. Most modules listen to the evnts emitted by it. * * @param fn Call your modules in here to add them to the context */ declare function serve(fn?: () => void): Promise<void>; export { ZContext, type ZervaConfigOptions, type ZervaModuleContext, type ZervaModuleOptions, assertModules, createContext, dumpConfig, emit, getConfig, getContext, getModuleContext, hasModule, on, onInit, onStart, onStop, once, register, registerModule, requireModules, serve, serveStop, setContext, use, withContext, zerva };