Security News
PyPI’s New Archival Feature Closes a Major Security Gap
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
react-global-configuration
Advanced tools
For setting a global config object managed as a requirement
Provide what is essentially an explicitly set of frozen global variables which can then be required by any module that needs them.
This can be preferable to having to pass any configuration all the way through your node application, or put your configuration inside state of component. This method is usually better than setting global variables.
$ npm install react-global-configuration
set( configuration, [options] )
import config from 'react-global-configuration';
config.set({ qux: 'corge', garply: 'fred' }, { freeze: false, environment: 'test' });
config.set({ qux: 'grault' }, { freeze: false, environment: 'production' });
config.set({
foo: 'bar',
bar: {
baz: 'qux'
},
baz: ['qux'],
qux: 'quux',
garply: 'waldo'
});
Sets a configuration.
react-global-configuration
.setEnvironment( environment )
import config from 'react-global-configuration';
config.setEnvironment('production');
Defines the current environment in order to obtain the appropriate configurations.
get()
function to obtain the appropriate setting.get( [key], [default] )
import config from 'react-global-configuration';
config.get('foo'); //'bar'
config.get('bar'); //{ baz: 'qux' }
config.get('bar.baz'); //'qux'
config.get('baz'); //['qux']
config.get('baz.0'); //'qux'
config.get('qux'); //'grault' -> environment value
config.get('garply'); //'waldo' -> global value
Obtains a specific configuration.
null
value by default.serialize( [environment] )
import config from 'react-global-configuration';
config.serialize(); //"{foo:'bar',bar:{baz:'qux'},baz:['qux'],qux:'quux',garply:'waldo'}"
config.serialize('test'); //"{qux:'corge',garply:'fred'}"
config.serialize('production'); //"{qux:'grault'}"
Serialize configuration to a superset of JSON.
reset()
import reset from 'react-global-configuration/reset';
reset();
This is a testing utility that removes the existing configuration from the require cache. By calling this, calling config.set(configuration)
and then re-requiring any target file, that target file will then be returned from require with the new configuration
applied.
config.js (global configuration file)
const config = {
foo: 'bar'
};
export default config;
server.js (initiation of server side process)
import config from 'react-global-configuration';
import configuration from './config';
import App from './app';
config.set(configuration);
new App();
render.js (render of server side process)
import config from 'react-global-configuration';
export renderScripts = () =>
`
<script>
window.__INITIAL_CONFIG__ = ${config.serialize()};
</script>
`;
client.js (initiation of client side js, assume compiled via browserify / webpack / similar)
import React from 'react';
import config from 'react-global-configuration';
import App from './app';
(function clientJS() {
config.set(window.__INITIAL_CONFIG__);
React.render(<App/>, document);
}());
component.js (somewhere inside the client side app)
import React from 'react';
import config from 'react-global-configuration';
class Component extends React.Component {
render() {
return (
<div>{ config.get('foo') }</div>
);
}
};
export default Component;
gulp/test.js
import gulp from 'gulp';
import mocha from 'gulp-mocha';
import config from 'react-global-configuration';
config.set({ foo: 'baz' }, { freeze: false });
gulp.task('test', function gulpTest() {
return (
gulp
.src([ 'app/**.test.*' ], { read: false })
.pipe(mocha())
);
});
appLogic.test.js
import reset from 'react-global-configuration/reset';
import assert from 'assert';
describe('appLogic', () => {
it('should return foo from configuration', () => {
import config from 'react-global-configuration';
const foos = [ 'alpha', 'beta', 'gamma' ];
foos.forEach((foo) => {
// This only works because `freeze: false` was set the first time set was called (in gulp/test.js).
config.set({ foo: foo });
const appLogic = require('./appLogic');
assert(appLogic() === foo);
});
});
afterEach(() => {
reset();
});
});
Issues and pull requests are most welcome.
React global configuration was initially inspired by global-configuration. Many thanks to Josh-a-e.
FAQs
For setting a global config object managed as a requirement
The npm package react-global-configuration receives a total of 1,334 weekly downloads. As such, react-global-configuration popularity was classified as popular.
We found that react-global-configuration demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
Research
Security News
Malicious npm package postcss-optimizer delivers BeaverTail malware, targeting developer systems; similarities to past campaigns suggest a North Korean connection.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.