Configurations
Configuration is a part of software development that can quickly become messy. Validation, detection, performance… there is a lot to take care of. In this guide, we will see how Front-Commerce configuration works and how to create new configurations or override existing ones.
In this guide we will see how they work and how to create new configurations or override existing ones.
What is a configuration provider?
The goal is to give access to a configuration object that contains all the configuration of the store. This configuration object can contain any kind of configuration. It can be flags about a feature activation, some credentials to connect to a remote service, etc.
However, not all applications will need every single configuration. For instance, a shop that chose to use Algolia won't have the same configurations than a shop that chose Elasticsearch for product search. The goal of the configuration providers is to define the configurations needed for the specific modules you use in your application.
On server start, the configuration providers will then be combined to create the global configuration object. This configuration object will then be available on each server request.
This can be illustrated by starting your Front-Commerce server with the
environment variable DEBUG=front-commerce:config
. If your server is running on
http://localhost:4000
and you open the URL
/__front-commerce/debug
, you
will have a dump of the configuration for this specific request under the
section req.config
.
The section is called req.config
in reference to the req
object that
contains the current user request. Whenever you need to have access to the
configuration object, please look for a req
object will have the key config
.
For instance, in GraphQL modules,
the req
object is available in the contextEnhancer
.
{
"allShops": {
"store:default": {
"id": "default",
"url": "http://localhost:4000",
"locale": "en-GB",
"magentoStoreCode": "default",
"currency": "EUR"
}
},
"currentShopId": "default",
"cache": {
"defaultMaxBatchSize": 100,
"strategies": [
{
"implementation": "Redis",
"supports": "*",
"disabledFor": [],
"config": {
"host": "localhost"
}
}
]
},
"magento": {
"endpoint": "http://magento23.test",
"version": "2",
"proxiedPaths": []
}
}
Don't activate this on a public instance as it can expose sensitive data
Configuration provider definition
In practice, a configuration provider is an object with five properties: a name, a schema, static values (available independently from the request), a function to resolve values depending on the request and a function to fetch values remotely.
const serviceConfigurationProvider = {
name,
schema,
values,
slowValuesOnEachRequest,
fetchRemoteConfiguration,
};
See the sections below to understand what each key stands for.