Environment Service
Location: src/main/environment/service/environment-service.ts
The EnvironmentService is a crucial main process service responsible for managing the application’s contextual data, including the currently active collection, its environments, and the resolution of variables.
Responsibilities
- Current Collection Management:
- Holds a reference to the
currentCollectionbeing worked on. - Manages the
currentEnvironmentKeyto specify which environment within the collection is active. - Provides a getter for
currentEnvironmentbased on thecurrentCollectionandcurrentEnvironmentKey.
- Holds a reference to the
- Variable Management:
- Hierarchy: Resolves variables based on a defined hierarchy:
- Active Environment variables (if an environment is selected).
- Collection variables.
- System variables (dynamic, like
$timestampIso,$randomUuid).
- See Variables documentation for details on variable types and usage.
getVariables(): Returns all currently available variables (merged from environment, collection, and system).getVariable(key): Retrieves a single variable object based on the hierarchy.getVariableValue(key): Retrieves the string value of a variable.setCollectionVariables(variables): Updates the variables for thecurrentCollection.
- Hierarchy: Resolves variables based on a defined hierarchy:
- Variable Substitution:
setVariablesInStream(stream): Usestemplate-replace-streamto replace template variables (e.g., ``) in aReadablestream with their resolved values. This is used for processing request bodies.
- Collection Lifecycle:
init(): Initializes the service by loading the last used collection (determined bySettingsService) or the default collection if the last one fails.changeCollection(collectionOrPath): Switches thecurrentCollection. If a path is provided, it loads the collection viaPersistenceService. UpdatesSettingsServicewith the new current collection.closeCollection(path?): Removes a collection from the list of open collections inSettingsService. If the closed collection was active, it switches to the default collection. The default collection cannot be closed.listCollections(): Retrieves a list of all known/open collections by queryingSettingsServiceand loading basic info for each viaPersistenceService.
Interactions with Other Services
PersistenceService:- Relies on
PersistenceServiceto load collection data from the file system (loadCollection). - When collection variables are updated via
setCollectionVariables, theEnvironmentServiceitself does not directly save. TheRendererEventServicecallsMainEventService.setCollectionVariableswhich in turn callsEnvironmentService.setCollectionVariablesand then explicitly callsPersistenceService.saveCollectionto persist the changes.
- Relies on
SettingsService:- Reads the list of known collections and the current collection index from
SettingsServiceduring initialization and when listing/changing collections. - Updates
SettingsServicewhen collections are changed or closed.
- Reads the list of known collections and the current collection index from
- System Variables (
src/main/environment/service/system-variable.ts):- Uses
getSystemVariable(key)andgetSystemVariables()to access predefined dynamic variables (e.g.,$timestampIso,$randomInt).
- Uses
Key Concepts
- Environments: Collections can have multiple environments (e.g., “Development”, “Staging”, “Production”), each with its own set of variables. The
EnvironmentServiceallows switching between these. (Note: The UI for managing environments beyond variable setting in the collection’s root settings might be a future feature, asenvironmentsmap exists inCollectiontype, but UI mainly focuses on collection-level vars for now). - Variable Resolution: The layered approach to variable resolution (environment > collection > system) allows for flexible configuration and overrides.
The EnvironmentService acts as the central hub for contextual data that can influence how requests are made and data is interpreted within Trufos.