@@ -25,11 +25,12 @@ const { FeatureToggles } = require("@cap-js-community/feature-toggle-library");
2525const instance = new FeatureToggles ({ uniqueName: " snowflake" });
2626```
2727
28- The library prepares a convenient ` singleton ` instance of FeatureToggles for out-of-the-box usage, where the Cloud
29- Foundry _ app name_ is used as unique name. This should be sufficient for most use-cases.
28+ The library prepares a convenient singleton instance of FeatureToggles for out-of-the-box usage, where the Cloud
29+ Foundry _ app name_ is used as unique name. This should be sufficient for most use-cases and is the default export of
30+ the library.
3031
3132``` javascript
32- const { singleton } = require (" @cap-js-community/feature-toggle-library" );
33+ const toggles = require (" @cap-js-community/feature-toggle-library" );
3334```
3435
3536{: .warn }
@@ -134,8 +135,8 @@ initialization settings are in `package.json`. For example:
134135` ` `
135136
136137In this example, the path `./srv/feature/feature.yaml` points to the previously discussed configuration file. With
137- these settings in place, the ` singleton` instance of the library will be initialized and is ready for usage at and
138- after the [bootstrap](https://cap.cloud.sap/cap/docs/node.js/cds-server#bootstrap) event.
138+ these settings in place, the singleton instance of the library will be initialized and is ready for usage at and after
139+ the [bootstrap](https://cap.cloud.sap/cap/docs/node.js/cds-server#bootstrap) event.
139140
140141{: .info }
141142Using the feature toggles in CAP projects also enables a [REST service]({{ site.baseurl }}/plugin/), where toggles can
@@ -147,24 +148,20 @@ Other projects will need to use the corresponding filepath, in order to initiali
147148
148149` ` ` javascript
149150const pathlib = require("path");
150- const {
151- singleton: { initializeFeatures },
152- } = require("@cap-js-community/feature-toggle-library");
153- const FEATURES_FILEPATH = pathlib.join(__dirname, "featureTogglesConfig.yml");
151+ const toggles = require("@cap-js-community/feature-toggle-library");
152+ const FEATURES_FILEPATH = pathlib.join(__dirname, ".toggles.yml");
154153
155154// ... during application bootstrap
156- await initializeFeatures({ configFile: FEATURES_FILEPATH });
155+ await toggles. initializeFeatures({ configFile: FEATURES_FILEPATH });
157156` ` `
158157
159158Alternatively, a runtime configuration object is also supported.
160159
161160` ` ` javascript
162- const {
163- singleton: { initializeFeatures },
164- } = require("@cap-js-community/feature-toggle-library");
161+ const toggles = require("@cap-js-community/feature-toggle-library");
165162
166163// ... during application bootstrap
167- await initializeFeatures({
164+ await toggles. initializeFeatures({
168165 config: {
169166 runNewCode: {
170167 type: "boolean",
@@ -183,16 +180,14 @@ feature toggles.
183180
184181` ` ` javascript
185182const pathlib = require("path");
186- const {
187- singleton: { initializeFeatures },
188- readConfigFromFile,
189- } = require("@cap-js-community/feature-toggle-library");
190- const FEATURES_FILEPATH = pathlib.join(__dirname, "featureTogglesConfig.yml");
183+ const toggles = require("@cap-js-community/feature-toggle-library");
184+ const FEATURES_FILEPATH = pathlib.join(__dirname, ".toggles.yml");
185+ const { FeatureToggles } = toggles;
191186
192187// ... during application bootstrap
193- const config = await readConfigFromFile(FEATURES_FILEPATH);
188+ const config = await FeatureToggles. readConfigFromFile(FEATURES_FILEPATH);
194189// ... manipulate
195- await initializeFeatures({ config });
190+ await toggles. initializeFeatures({ config });
196191` ` `
197192
198193# # Integration Mode
@@ -232,41 +227,36 @@ a feature toggle with the key `/srv/util/logger/logLevel`, similar to the one de
232227You can read the current in memory state of any feature toggle :
233228
234229` ` ` javascript
235- const {
236- singleton: { getFeatureValue },
237- } = require("@cap-js-community/feature-toggle-library");
230+ const toggles = require("@cap-js-community/feature-toggle-library");
238231
239232// ... in some function
240- const logLevel = getFeatureValue("/srv/util/logger/logLevel");
233+ const logLevel = toggles. getFeatureValue("/srv/util/logger/logLevel");
241234
242235// ... with runtime scope information
243- const logLevel = getFeatureValue("/srv/util/logger/logLevel", {
236+ const logLevel = toggles. getFeatureValue("/srv/util/logger/logLevel", {
244237 tenant: cds.context.tenant,
245238 user: cds.context.user.id,
246239});
247240` ` `
248241
249242{: .warn }
250243While `getFeatureValue` is synchronous, and could happen on the top-level of a module. The function will throw, if it
251- is called _before_ `initializeFeatures`, which is asynchronous. So, it's never sensible to have this on
252- top-level.
244+ is called _before_ `initializeFeatures`, which is asynchronous. So, it's never sensible to have this on top-level.
253245
254246# ## Observing Feature Value Changes
255247
256248You can register a callback for all updates to a feature toggle :
257249
258250` ` ` javascript
259- const {
260- singleton: { registerFeatureValueChangeHandler },
261- } = require("@cap-js-community/feature-toggle-library");
251+ const toggles = require("@cap-js-community/feature-toggle-library");
262252
263- registerFeatureValueChangeHandler("/srv/util/logger/logLevel", (newValue, oldValue, scopeMap) => {
253+ toggles. registerFeatureValueChangeHandler("/srv/util/logger/logLevel", (newValue, oldValue, scopeMap) => {
264254 console.log("changing log level from %s to %s (scope %j)", oldValue, newValue, scopeMap);
265255 updateLogLevel(newValue);
266256});
267257
268258// ... or for async APIs
269- registerFeatureValueChangeHandler("/srv/util/logger/logLevel", async (newValue, oldValue, scopeMap) => {
259+ toggles. registerFeatureValueChangeHandler("/srv/util/logger/logLevel", async (newValue, oldValue, scopeMap) => {
270260 await updateLogLevel(newValue);
271261});
272262` ` `
@@ -279,13 +269,11 @@ Registering any callback will not require that the feature toggles are initializ
279269Finally, updating the feature toggle value :
280270
281271` ` ` javascript
282- const {
283- singleton: { changeFeatureValue },
284- } = require("@cap-js-community/feature-toggle-library");
272+ const toggles = require("@cap-js-community/feature-toggle-library");
285273
286274// optionally pass in a scopeMap, which describes the least specific scope where the change should happen
287275async function changeIt(newValue, scopeMap) {
288- const validationErrors = await changeFeatureValue("/srv/util/logger/logLevel", newValue, scopeMap);
276+ const validationErrors = await toggles. changeFeatureValue("/srv/util/logger/logLevel", newValue, scopeMap);
289277 if (Array.isArray(validationErrors) && validationErrors.length > 0) {
290278 for (const { errorMessage, errorMessageValues } of validationErrors) {
291279 // show errors to the user, the change did not happen
@@ -306,7 +294,7 @@ scope-combination, which overrides that value, then you can use the option `{ cl
306294argument. For example
307295
308296` ` ` javascript
309- await changeFeatureValue("/srv/util/logger/logLevel", "error", {}, { clearSubScopes: true });
297+ await toggles. changeFeatureValue("/srv/util/logger/logLevel", "error", {}, { clearSubScopes: true });
310298` ` `
311299
312300will set the root-scope value to `"error"` and remove all sub-scopes. See
@@ -318,15 +306,13 @@ There is a convenience reset API just to reset a feature toggle and remove all a
318306the feature toggle afterward will only yield the fallback value until new changes are made.
319307
320308` ` ` javascript
321- const {
322- singleton: { resetFeatureValue, changeFeatureValue },
323- } = require("@cap-js-community/feature-toggle-library");
309+ const toggles = require("@cap-js-community/feature-toggle-library");
324310
325311// ... in some function
326- await resetFeatureValue("/srv/util/logger/logLevel");
312+ await toggles. resetFeatureValue("/srv/util/logger/logLevel");
327313
328314// this is functionally equivalent to
329- await changeFeatureValue("/srv/util/logger/logLevel", null, {}, { clearSubScopes: true });
315+ await toggles. changeFeatureValue("/srv/util/logger/logLevel", null, {}, { clearSubScopes: true });
330316` ` `
331317
332318# ## External Validation
@@ -335,11 +321,9 @@ The `string`-type feature toggles can theoretically encode very complex data str
335321inputs in-depth before allowing changes to be published and propagated.
336322
337323` ` ` javascript
338- const {
339- singleton: { registerFeatureValueValidation },
340- } = require("@cap-js-community/feature-toggle-library");
324+ const toggles = require("@cap-js-community/feature-toggle-library");
341325
342- registerFeatureValueValidation("/srv/util/logger/logLevel", (newValue) => {
326+ toggles. registerFeatureValueValidation("/srv/util/logger/logLevel", (newValue) => {
343327 if (isBad(newValue)) {
344328 return { errorMessage: "got bad value" };
345329 }
0 commit comments