UNPKG

@decaf-ts/utils

Version:

module management utils for decaf-ts

71 lines (70 loc) 3.24 kB
import { EnvironmentFactory } from "./types"; import { ObjectAccumulator } from "typed-object-accumulator"; /** * @class Environment * @extends {ObjectAccumulator<T>} * @template T * @description A class representing an environment with accumulation capabilities. * @summary Manages environment-related data and provides methods for accumulation and key retrieval. * @param {T} [initialData] - The initial data to populate the environment with. */ export declare class Environment<T extends object> extends ObjectAccumulator<T> { /** * @static * @protected * @description A factory function for creating Environment instances. * @summary Defines how new instances of the Environment class should be created. * @return {Environment<any>} A new instance of the Environment class. */ protected static factory: EnvironmentFactory<any, any>; /** * @static * @private * @description The singleton instance of the Environment class. * @type {Environment<any>} */ private static _instance; protected constructor(); /** * @description Retrieves a value from the environment * @summary Gets a value from the environment variables, handling browser and Node.js environments differently * @param {string} k - The key to retrieve from the environment * @return {unknown} The value from the environment, or undefined if not found */ protected fromEnv(k: string): unknown; /** * @description Expands an object into the environment * @summary Defines properties on the environment object that can be accessed as getters and setters * @template V - Type of the object being expanded * @param {V} value - The object to expand into the environment * @return {void} */ protected expand<V extends object>(value: V): void; /** * @protected * @static * @description Retrieves or creates the singleton instance of the Environment class. * @summary Ensures only one instance of the Environment class exists. * @template E * @param {...unknown[]} args - Arguments to pass to the factory function if a new instance is created. * @return {E} The singleton instance of the Environment class. */ protected static instance<E extends Environment<any>>(...args: unknown[]): E; /** * @static * @description Accumulates the given value into the environment. * @summary Adds new properties to the environment from the provided object. * @template V * @param {V} value - The object to accumulate into the environment. * @return {V} The updated environment instance. */ static accumulate<V extends object>(value: V): typeof Environment._instance & V & ObjectAccumulator<typeof Environment._instance & V>; /** * @static * @description Retrieves the keys of the environment, optionally converting them to ENV format. * @summary Gets all keys in the environment, with an option to format them for environment variables. * @param {boolean} [toEnv=true] - Whether to convert the keys to ENV format. * @return {string[]} An array of keys from the environment. */ static keys(toEnv?: boolean): string[]; }