项目作者: yusinto

项目描述 :
A library to integrate launch darkly with react redux
高级语言: JavaScript
项目地址: git://github.com/yusinto/ld-redux.git
创建时间: 2016-10-06T10:26:18Z
项目社区:https://github.com/yusinto/ld-redux

开源协议:MIT License

下载


ld-redux

npm version npm downloads npm npm

A library to integrate launch darkly with react redux :clap:

Launch Darkly is a great tool for feature flagging and a/b testing. It has a fully capable client-side javascript sdk, so why this package?

If you use react redux and you want to store your feature flags as part of your redux state, this package will do that for you. It does the heavy lifting of:

  • Fetching your flags from launch darkly.
  • Storing it in your redux state.
  • Camel casing your keys so you can use them in code with the dot operator. The keys by default are dash separated so you can’t do this out of the box with the official sdk.
  • Server Sent Event works as well so your app will respond live to feature flag changes without the users having to refresh the browser!

Breaking changes in v3.1

ld-redux v3.1. is NOT* backwards compatible! The init method now accepts dispatch instead of
store. Follow the quickstart example below to see this.

Installation

yarn add ld-redux

Quickstart

  1. In your client bootstrap, initialise the launch darkly client by invoking the init method:

    1. import createStore from '<your-project>/store';
    2. import ldRedux from 'ld-redux';
    3. // standard redux createStore
    4. const store = createStore();
    5. // do this once
    6. ldRedux.init({
    7. clientSideId: 'your-client-side-id',
    8. dispatch: store.dispatch,
    9. });
    10. render(
    11. <Provider store={store}>
    12. <Router routes={routes} history={browserHistory}></Router>
    13. </Provider>,
    14. document.getElementById('reactDiv')
    15. );
  2. Include ldReducer as one of the reducers in your app:

    1. import { combineReducers } from 'redux';
    2. import ldRedux from 'ld-redux';
    3. import reducers from '<your-project>/reducers';
    4. export default combineReducers({
    5. ...reducers,
    6. LD: ldRedux.reducer(), // Note: the LD key can be anything you want
    7. });
  3. Use the flag:

    1. import React, {Component} from 'react';
    2. import {connect} from 'react-redux';
    3. const mapStateToProps = (state) => {
    4. const {featureFlagKey} = state.LD; // Note: the key LD must be the same as step 2.
    5. return {
    6. featureFlagKey,
    7. };
    8. };
    9. @connect(mapStateToProps)
    10. export default class Home extends Component {
    11. render() {
    12. return (
    13. <div>
    14. {
    15. /* look ma, feature flag! */
    16. this.props.featureFlagKey ?
    17. <div>
    18. <p>Welcome to feature toggling!</p>
    19. </div>
    20. :
    21. 'nothing'
    22. }
    23. </div>
    24. );
    25. }
    26. }

API

init({clientSideId, dispatch, flags, useCamelCaseFlagKeys, user, subscribe, options})

The init method accepts an object with the above properties. clientSideId, dispatch are mandatory.

The flags property is optional. This is an object containing all the flags you want to use and subscribe to in your app.
If you don’t specify this, ld-redux will subscribe to all flags in your ld environment.

  1. // standard redux createStore
  2. const store = createStore();
  3. const flags = { 'feature-flag-key': false }; // only subscribe to this one flag
  4. // do this once
  5. ldRedux.init({
  6. clientSideId: 'your-client-side-id',
  7. dispatch: store.dispatch,
  8. flags,
  9. });

The subscribe property is optional. This defaults to true which means by default you’ll get automatic live updates
of flag changes from the server. You can turn this off and manually subscribe to flag changes through the ldClient
object if for some reason you don’t want to get live updates.

The user property is optional. You can initialise the sdk with a custom user by specifying one. This must be an object containing
at least a “key” property. If you don’t specify a user object, ldRedux will create a default one that looks like this:

  1. const defaultUser = {
  2. key: uuid.v4(), // random guid
  3. ip: ip.address(),
  4. custom: {
  5. browser: userAgentParser.getResult().browser.name,
  6. device
  7. }
  8. };

For more info on the user object, see here.

The useCamelCaseFlagKeys property is optional. This defaults to true which means by default the flags that are stored
in redux will be camel cased. If this property is false, no transformation on the flag name will be done.

The options property is optional. It can be used to pass in extra options such as Bootstrapping.
For example:

  1. ldRedux.init({
  2. clientSideId,
  3. dispatch,
  4. flags,
  5. options: {
  6. bootstrap: 'localStorage',
  7. }
  8. });

reducer()

This is ld-redux’s reducer. You must include this reducer in your app as per step 2 above with any key of your choice.
You then use this key to retrieve your flags from redux’s state.

window.ldClient

Internally the ldRedux.init method above initialises the js sdk and stores the resultant ldClient object in window.ldClient. You can use
this object to access the official sdk methods directly. For example, you can do things like:

  1. // track goals
  2. window.ldClient.track('add to cart');
  3. // change user context
  4. window.ldClient.identify({key: 'someUserId'});

For more info on changing user context, see the official documentation.

isLDReady

You no longer need to deal with isLDReady. However if you need to, it is still available in the store. You can access it via
the LD state like so:

  1. const mapStateToProps = (state) => {
  2. const {isLDReady} = state.LD; // Note: the key LD must be the same as step 2.
  3. return {
  4. isLDReady,
  5. };
  6. };

This is useful to solve “flickering” issues above the fold on your front page caused by a flag transitioning from a default false value
to true.

Example

Check the example for a fully working spa with
react, redux and react-router. Remember to enter your client side sdk in the client bootstrap file
before running the example!