sap-cf-features
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -36,2 +36,2 @@ export { IFeatureFlagExport, IFeatureFlagMap, IReturnFlag, IFeatureService } from './types'; | ||
*/ | ||
export declare function featureFlagRouter(forConnect?: boolean): import("express-serve-static-core").Router; | ||
export declare function featureFlagRouter(identifierFn?: (Request: any) => string, forConnect?: boolean): import("express-serve-static-core").Router; |
@@ -82,2 +82,11 @@ "use strict"; | ||
/** | ||
* Default function to determine identifier. | ||
* @param req | ||
*/ | ||
function getDomain(req) { | ||
// when using passport req.authInfo will contain tenant info. | ||
// when using the approuter, req.user.tenant is containing the tenant id. | ||
return req.authInfo ? req.authInfo.subdomain : req.user ? req.user.tenant : ""; | ||
} | ||
/** | ||
* Easy way to enable a ui5 app to read tenant aware feature flags. | ||
@@ -88,9 +97,4 @@ * @returns An express router. | ||
*/ | ||
function featureFlagRouter(forConnect = false) { | ||
function featureFlagRouter(identifierFn = getDomain, forConnect = false) { | ||
const router = express_1.Router(); | ||
function getDomain(req) { | ||
// when using passport req.authInfo will contain tenant info. | ||
// when using the approuter, req.user.tenant is containing the tenant id. | ||
return req.authInfo ? req.authInfo.subdomain : req.user ? req.user.tenant : ""; | ||
} | ||
function respond(res, next, result, status = 200) { | ||
@@ -118,3 +122,3 @@ if (forConnect) { | ||
const { flagName } = req.params; | ||
const result = yield getFeatureFlags(flagName, getDomain(req)); | ||
const result = yield getFeatureFlags(flagName, identifierFn(req)); | ||
respond(res, next, result); | ||
@@ -125,3 +129,3 @@ })); | ||
const flagNames = yield FeatureFlagsApi_1.getAllFlagNames(); | ||
const result = yield getFeatureFlags(flagNames, getDomain(req)); | ||
const result = yield getFeatureFlags(flagNames, identifierFn(req)); | ||
respond(res, next, result); | ||
@@ -128,0 +132,0 @@ })); |
{ | ||
"name": "sap-cf-features", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "Read SAP CF feature flags in nodejs", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -13,2 +13,5 @@ # sap-cf-features | ||
You can follow this blog post to get an example application on your trial account. | ||
https://blogs.sap.com/2020/07/01/library-for-feature-flags-in-nodejs-on-cloud-foundry/ | ||
### Read a feature-flag | ||
@@ -34,5 +37,14 @@ | ||
const {featureFlagRouter} = require('sap-cf-features'); | ||
app.use("/feature-flags", featureFlagRouter()); | ||
/** | ||
* function to identify the identifier. | ||
* @param req Request object | ||
* @returns the identifier (string) | ||
*/ | ||
const identifierFn = (req) => req.authInfo.subdomain; | ||
app.use("/feature-flags", featureFlagRouter(identifierFn)); | ||
``` |
17885
346
49