Skip to content

Commit

Permalink
added public config support (#1288)
Browse files Browse the repository at this point in the history
  • Loading branch information
airarrazaval committed Apr 6, 2022
1 parent fe86324 commit 323c7e5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
5 changes: 5 additions & 0 deletions index.js
Expand Up @@ -197,6 +197,11 @@ module.exports = function(config) {
router.get('/access', router.formio.middleware.accessHandler);
}

// The public config handler.
if (!router.formio.hook.invoke('init', 'config', router.formio)) {
router.use('/config.json', router.formio.middleware.configHandler);
}

// Authorize all urls based on roles and permissions.
if (!router.formio.hook.invoke('init', 'perms', router.formio)) {
router.use(router.formio.middleware.permissionHandler);
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/alias.js
Expand Up @@ -44,7 +44,7 @@ module.exports = function(router) {

// If this is normal request, then pass this middleware.
/* eslint-disable no-useless-escape */
if (!alias || alias.match(/^(form$|form[\?\/])/) || alias === 'spec.json') {
if (!alias || alias.match(/^(form$|form[\?\/])/) || alias === 'spec.json' || alias === 'config.json') {
return next();
}
/* eslint-enable no-useless-escape */
Expand Down
37 changes: 37 additions & 0 deletions src/middleware/configHandler.js
@@ -0,0 +1,37 @@
'use strict';
const debug = require('debug')('formio:config');
const _ = require('lodash');

/**
* The Config handler returns the project's public configuration.
*
* @param router
*/
module.exports = function(router) {
const hook = require('../util/hook')(router.formio);
const formio = hook.alter('formio', router.formio);
const config = {};

if (_.isPlainObject(formio.config.public)) {
_.forOwn(formio.config.public, (value, key) => {
config[key] = value;
});
}

// Allow the PUBLIC_CONFIG variable define or overwrite the default public configuration.
if (process.env.PUBLIC_CONFIG) {
try {
_.forOwn(JSON.parse(process.env.PUBLIC_CONFIG), (value, key) => {
config[key] = value;
});
}
catch (err) {
debug('Failed to parse public configuration.');
debug(err);
}
}

return function configHandler(req, res, next) {
return res.json({config});
};
};
1 change: 1 addition & 0 deletions src/middleware/middleware.js
Expand Up @@ -12,6 +12,7 @@ module.exports = function(router) {
bootstrapSubmissionAccess: require('./bootstrapSubmissionAccess')(router),
condensePermissionTypes: require('./condensePermissionTypes')(router),
condenseSubmissionPermissionTypes: require('./condenseSubmissionPermissionTypes')(router),
configHandler: require('./configHandler')(router),
filterIdCreate: require('./filterIdCreate')(router),
filterMongooseExists: require('./filterMongooseExists')(router),
filterResourcejsResponse: require('./filterResourcejsResponse')(router),
Expand Down

0 comments on commit 323c7e5

Please sign in to comment.