Creation Helpers
@elsikora/cladi
provides convenient utility functions (exported from the root of the package) to quickly instantiate the default implementations of the core components.
These helpers simplify setup and reduce boilerplate code.
createContainer
Instantiates BaseContainer
(which implements IContainer
).
import { createContainer, type IBaseContainerOptions, type IContainer, type ILogger } from '@elsikora/cladi';
// Assuming logger is created
declare const logger: ILogger;
// Option 1: Without options (no internal logging for the container itself)
const container1: IContainer = createContainer({});
// Option 2: Provide a logger for the container's internal operations
const containerOptions: IBaseContainerOptions = { logger };
const container2: IContainer = createContainer(containerOptions);
// Now register dependencies in container2
// container2.register(...);
- Purpose: Quickly get a dependency injection container ready for registrations.
- Options (
IBaseContainerOptions
): Allows injecting a logger to monitor the container’s own actions (register, get, etc.).
Name | Type | Default |
---|---|---|
logger | any The logger to use for the container. | new ConsoleLoggerService() |
See Also: Core Concepts: Container
createRegistry<T>
Instantiates BaseRegistry<T>
(which implements IRegistry<T>
). Requires a type parameter T
that extends { name: string }
.
import { createRegistry, type IBaseRegistryOptions, type IRegistry, type ILogger } from '@elsikora/cladi';
// Define the type of item the registry will hold
interface ServiceEndpoint {
name: string; // Mandatory key
url: string;
timeoutMs: number;
}
declare const logger: ILogger;
// Option 1: Without options
const registry1: IRegistry<ServiceEndpoint> = createRegistry<ServiceEndpoint>({});
// Option 2: With options (e.g., providing a logger)
const registryOptions: IBaseRegistryOptions = { logger };
const registry2: IRegistry<ServiceEndpoint> = createRegistry<ServiceEndpoint>(registryOptions);
// Now register items in registry2
// registry2.register({ name: 'auth', url: '...', timeoutMs: 5000 });
- Purpose: Set up a named collection for storing configuration templates or object prototypes.
- Options (
IBaseRegistryOptions
): Allows injecting a logger for monitoring registry operations.
Name | Type | Default |
---|---|---|
logger | any The logger to use for logging. | new ConsoleLoggerService() |
See Also: Core Concepts: Registry
createFactory<T>
Instantiates BaseFactory<T>
(which implements IFactory<T>
). Requires a type parameter T
and must be provided with an existing IRegistry<T>
instance.
import { createFactory, type IBaseFactoryOptions, type IFactory, type IRegistry, type ILogger } from '@elsikora/cladi';
// Assume ServiceEndpoint and a populated registry instance exist
interface ServiceEndpoint { name: string; url: string; timeoutMs: number; }
declare const registry: IRegistry<ServiceEndpoint>;
declare const logger: ILogger;
// Factory requires a registry
const factoryOptions: IBaseFactoryOptions<ServiceEndpoint> = {
registry: registry, // Mandatory
logger: logger, // Optional
// transformer: (template) => { ... } // Optional
};
const factory: IFactory<ServiceEndpoint> = createFactory<ServiceEndpoint>(factoryOptions);
// Now use the factory to create instances
// const authServiceEndpoint = factory.create('auth');
- Purpose: Create an object factory that uses a registry as its source for templates.
- Options (
IBaseFactoryOptions
): Requiresregistry
. Optionally takes alogger
and atransformer
function.
See Also: Core Concepts: Factory
createLogger
Instantiates ConsoleLoggerService
(which implements ILogger
).
import { createLogger, type IConsoleLoggerOptions, type ILogger, ELoggerLogLevel } from '@elsikora/cladi';
// Option 1: Use defaults (LogLevel: INFO, no source)
const defaultLogger: ILogger = createLogger();
defaultLogger.info("Default logger message.");
// Option 2: Provide custom options
const customOptions: IConsoleLoggerOptions = {
level: ELoggerLogLevel.DEBUG, // Set minimum level
source: "MyModule", // Set default source identifier
};
const customLogger: ILogger = createLogger(customOptions);
customLogger.debug("Debug message from MyModule.");
- Purpose: Quickly get a standard console logger instance.
- Options (
IConsoleLoggerOptions
): Allows setting the minimumlevel
and a defaultsource
identifier.
Name | Type | Default |
---|---|---|
level | ELoggerLogLevel The log level to use. | ELoggerLogLevel.INFO |
source | string The source to use for the logger. |
See Also: Services: Logging