From 79fc089a352cfd261bd3269951be0749c3784f10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivar=20Conradi=20=C3=98sthus?= Date: Thu, 17 Dec 2020 19:43:01 +0100 Subject: [PATCH] fix: stateService undefined --- lib/app.js | 4 ++-- lib/routes/admin-api/index.js | 40 ++++++++++++++++++++++++--------- lib/routes/admin-api/state.js | 7 +++--- lib/routes/index.js | 6 ++--- lib/server-impl.js | 2 +- test/e2e/helpers/test-helper.js | 23 +++++++++++-------- 6 files changed, 53 insertions(+), 29 deletions(-) diff --git a/lib/app.js b/lib/app.js index eddd0b5228..c8d145f970 100644 --- a/lib/app.js +++ b/lib/app.js @@ -15,7 +15,7 @@ const simpleAuthentication = require('./middleware/simple-authentication'); const noAuthentication = require('./middleware/no-authentication'); const secureHeaders = require('./middleware/secure-headers'); -module.exports = function(config) { +module.exports = function(config, services = {}) { const app = express(); const baseUriPath = config.baseUriPath || ''; @@ -60,7 +60,7 @@ module.exports = function(config) { } // Setup API routes - app.use(`${baseUriPath}/`, new IndexRouter(config).router); + app.use(`${baseUriPath}/`, new IndexRouter(config, services).router); if (process.env.NODE_ENV !== 'production') { app.use(errorHandler()); diff --git a/lib/routes/admin-api/index.js b/lib/routes/admin-api/index.js index b87af7bd27..664d501983 100644 --- a/lib/routes/admin-api/index.js +++ b/lib/routes/admin-api/index.js @@ -14,23 +14,41 @@ const StateController = require('./state'); const apiDef = require('./api-def.json'); class AdminApi extends Controller { - constructor(config) { + constructor(config, services) { super(config); this.app.get('/', this.index); - this.app.use('/features', new FeatureController(config).router); + this.app.use( + '/features', + new FeatureController(config, services).router, + ); this.app.use( '/feature-types', - new FeatureTypeController(config).router, + new FeatureTypeController(config, services).router, ); - this.app.use('/archive', new ArchiveController(config).router); - this.app.use('/strategies', new StrategyController(config).router); - this.app.use('/events', new EventController(config).router); - this.app.use('/metrics', new MetricsController(config).router); - this.app.use('/user', new UserController(config).router); - this.app.use('/ui-config', new ConfigController(config).router); - this.app.use('/context', new ContextController(config).router); - this.app.use('/state', new StateController(config).router); + this.app.use( + '/archive', + new ArchiveController(config, services).router, + ); + this.app.use( + '/strategies', + new StrategyController(config, services).router, + ); + this.app.use('/events', new EventController(config, services).router); + this.app.use( + '/metrics', + new MetricsController(config, services).router, + ); + this.app.use('/user', new UserController(config, services).router); + this.app.use( + '/ui-config', + new ConfigController(config, services).router, + ); + this.app.use( + '/context', + new ContextController(config, services).router, + ); + this.app.use('/state', new StateController(config, services).router); } index(req, res) { diff --git a/lib/routes/admin-api/state.js b/lib/routes/admin-api/state.js index 80fd22f498..5ecac1ddaf 100644 --- a/lib/routes/admin-api/state.js +++ b/lib/routes/admin-api/state.js @@ -12,9 +12,10 @@ const { handleErrors } = require('./util'); const upload = multer({ limits: { fileSize: 5242880 } }); class StateController extends Controller { - constructor(config) { + constructor(config, services) { super(config); this.logger = config.getLogger('/admin-api/state.js'); + this.stateService = services.stateService; this.fileupload('/import', upload.single('file'), this.import, ADMIN); this.get('/export', this.export, ADMIN); } @@ -35,7 +36,7 @@ class StateController extends Controller { data = req.body; } - await this.config.stateService.import({ + await this.stateService.import({ data, userName, dropBeforeImport: drop, @@ -61,7 +62,7 @@ class StateController extends Controller { } try { - const data = await this.config.stateService.export({ + const data = await this.stateService.export({ includeStrategies, includeFeatureToggles, }); diff --git a/lib/routes/index.js b/lib/routes/index.js index 322e81ed56..4a94abf10d 100644 --- a/lib/routes/index.js +++ b/lib/routes/index.js @@ -11,14 +11,14 @@ const LogoutController = require('./logout'); const api = require('./api-def'); class IndexRouter extends Controller { - constructor(config) { + constructor(config, services) { super(); this.use('/health', new HealthCheckController(config).router); this.use('/internal-backstage', new BackstageCTR(config).router); this.use('/logout', new LogoutController(config).router); this.get(api.uri, this.index); - this.use(api.links.admin.uri, new AdminApi(config).router); - this.use(api.links.client.uri, new ClientApi(config).router); + this.use(api.links.admin.uri, new AdminApi(config, services).router); + this.use(api.links.client.uri, new ClientApi(config, services).router); // legacy support (remove in 4.x) if (config.enableLegacyRoutes) { diff --git a/lib/server-impl.js b/lib/server-impl.js index a68302c2d6..dcabe7f5c5 100644 --- a/lib/server-impl.js +++ b/lib/server-impl.js @@ -53,7 +53,7 @@ async function createApp(options) { ...options, }; - const app = getApp(config); + const app = getApp(config, services); const metricsMonitor = createMetricsMonitor(); metricsMonitor.startMonitoring(config); diff --git a/test/e2e/helpers/test-helper.js b/test/e2e/helpers/test-helper.js index e169c77da7..521b8abc7f 100644 --- a/test/e2e/helpers/test-helper.js +++ b/test/e2e/helpers/test-helper.js @@ -12,16 +12,21 @@ const StateService = require('../../../lib/services/state-service'); const eventBus = new EventEmitter(); function createApp(stores, adminAuthentication = 'none', preHook) { - return getApp({ - stores, - eventBus, - preHook, - adminAuthentication, - secret: 'super-secret', - sessionAge: 4000, + const services = { stateService: new StateService(stores, { getLogger }), - getLogger, - }); + }; + return getApp( + { + stores, + eventBus, + preHook, + adminAuthentication, + secret: 'super-secret', + sessionAge: 4000, + getLogger, + }, + services, + ); } module.exports = {