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
currentCollection
being worked on. - Manages the
currentEnvironmentKey
to specify which environment within the collection is active. - Provides a getter for
currentEnvironment
based on thecurrentCollection
andcurrentEnvironmentKey
.
- 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-stream
to replace template variables (e.g., ``) in aReadable
stream 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
. UpdatesSettingsService
with 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 queryingSettingsService
and loading basic info for each viaPersistenceService
.
Interactions with Other Services
PersistenceService
:- Relies on
PersistenceService
to load collection data from the file system (loadCollection
). - When collection variables are updated via
setCollectionVariables
, theEnvironmentService
itself does not directly save. TheRendererEventService
callsMainEventService.setCollectionVariables
which in turn callsEnvironmentService.setCollectionVariables
and then explicitly callsPersistenceService.saveCollection
to persist the changes.
- Relies on
SettingsService
:- Reads the list of known collections and the current collection index from
SettingsService
during initialization and when listing/changing collections. - Updates
SettingsService
when 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
EnvironmentService
allows switching between these. (Note: The UI for managing environments beyond variable setting in the collection’s root settings might be a future feature, asenvironments
map exists inCollection
type, 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.