Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from contiamo/feat-get-all-config
Browse files Browse the repository at this point in the history
Add a `getAllConfig` method
  • Loading branch information
fabien0102 authored Sep 14, 2018
2 parents 059dda9 + f468678 commit edd246e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: node_js
node_js:
- "8.12.0"
deploy:
provider: npm
email: [email protected]
api_key:
secure: hnb+fmx2Hf95AKlt6sMKAaJ/fgbpfsABl3yNnbXbDXzlKNBQ+mxneBwGfkQ17h5s4owdw5VsDRoFgtVmyq0fwOSGK9bugvS6IxvRuznV57AKJTXdaAf4YVLwFeiJIILsXL1x1ftucMoIro3SIrPFfz3VnFyI8nMBvffAYwNFnwE5FMj827n668zHWs5Y93p8Y75dSjHYwppFtNK4yn6sbyurzou87bixET3B/uJSwi9lt2aLTCkWU9pI01SNEJLBvUsTSUrdvszLv6L4zMV7HXpklhADbUGAIF8SXWSfkAiD1rFQlMJinTjNrPz6fdVkFThxeN8bS0ijKEfz0Gb2kaD14mVI3Bmxc/JqXSumhHkoH1CSVtrYBnyFb8AbqdwEdUHkvWp0cQjd/c2QCbO5loDiqy4IwR1cxfW/9JgOhy0eDhqg1sCWd3R/dLZQtmo9/l03kxqcQ2kg8sNysklqZLQ/Av9qPygOHIoSOHf4pzc9JnKSj54G+mQDJ1UT+urmkSr4kKkB942zhPh9cjWZrj/B8SCzG6NNJxOrO3jYaYo52+t+7uzp7h8Iz2ntXVyFBhqRdSTr6zV/xxNK90GLtlgbx3UFq70IlLatRSOLzpxkQ+HUWDwlfr0WF+30QbadeozFfE28d8OvivA1pWiHTIZUJBkQ9Y22wh6JWZ048h0=
on:
tags: true
repo: contiamo/react-runtime-config
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->


- [Why](#why)
- [How](#how)
- [Getting started](#getting-started)
Expand Down Expand Up @@ -183,6 +182,6 @@ You have also access to `field.windowValue` and `field.storageValue` if you want

## Moar Power (if needed)

We also expose from `createConfig` a simple `getConfig` and `setConfig`. These functions can be used standalone and do not require use of the `Config` component. This can be useful for accessing or mutating configuration values in component lifecycle hooks, or anywhere else outside of render.
We also expose from `createConfig` a simple `getConfig`, `getAllConfig` and `setConfig`. These functions can be used standalone and do not require use of the `Config` component. This can be useful for accessing or mutating configuration values in component lifecycle hooks, or anywhere else outside of render.

These functions and are exactly the same as their counterparts available inside the `Config` component, the only thing you lose is the hot config reload.
5 changes: 2 additions & 3 deletions src/AdminConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,10 @@ export class AdminConfig<T> extends React.Component<AdminConfigProps<T> & Inject
};

/**
* Returns every config keys set in `window.{namespace}`
* Returns every config keys
*/
private getConfigKeys = (): Array<Extract<keyof T, string>> => {
// `slice(0, -1)`is for removing the trailing point
return Object.keys(get(window, this.props.namespace.slice(0, -1))) as any;
return Object.keys(this.props.getAllConfig()) as any;
};
}

Expand Down
8 changes: 7 additions & 1 deletion src/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,10 @@ describe("react-runtime-config", () => {
let children: jest.Mock<JSX.Element>;

beforeEach(() => {
config = createConfig<IConfig>({ namespace: "test", storage });
const defaultConfig = {
batman: "from-default",
};
config = createConfig<IConfig & typeof defaultConfig>({ namespace: "test", storage, defaultConfig });
children = jest.fn(() => <div />);

const { AdminConfig } = config;
Expand All @@ -265,6 +268,7 @@ describe("react-runtime-config", () => {
{ path: "loulou", storageValue: null, value: "d", windowValue: "d" },
{ path: "foo", storageValue: null, value: "from-window", windowValue: "from-window" },
{ path: "aBoolean", storageValue: null, value: true, windowValue: true },
{ path: "batman", storageValue: null, value: "from-default", windowValue: null },
];
expect(children.mock.calls[0][0].fields).toEqual(expectedFields);
});
Expand All @@ -279,6 +283,7 @@ describe("react-runtime-config", () => {
{ path: "loulou", storageValue: null, value: "d", windowValue: "d" },
{ path: "foo", storageValue: null, value: "from-window", windowValue: "from-window" },
{ path: "aBoolean", storageValue: null, value: true, windowValue: true },
{ path: "batman", storageValue: null, value: "from-default", windowValue: null },
];

expect(children.mock.calls[1][0].fields).toEqual(expectedFields);
Expand All @@ -304,6 +309,7 @@ describe("react-runtime-config", () => {
{ path: "loulou", storageValue: null, value: "d", windowValue: "d" },
{ path: "foo", storageValue: null, value: "from-window", windowValue: "from-window" },
{ path: "aBoolean", storageValue: null, value: true, windowValue: true },
{ path: "batman", storageValue: null, value: "from-default", windowValue: null },
];

expect(children.mock.calls[2][0].fields).toEqual(expectedFields);
Expand Down
20 changes: 20 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import get from "lodash/get";
import uniq from "lodash/uniq";
import React from "react";

import AdminConfigBase, { AdminConfigProps } from "./AdminConfig";
Expand Down Expand Up @@ -33,6 +34,7 @@ export interface InjectedProps<TConfig> {
path: K,
value: TConfig[K],
) => void;
getAllConfig: () => TConfig;
getWindowValue: (path: Extract<keyof TConfig, string>) => any;
getStorageValue: (path: Extract<keyof TConfig, string>) => string | boolean | null;
}
Expand Down Expand Up @@ -78,6 +80,10 @@ export function createConfig<TConfig>(options: ConfigOptions<TConfig>) {
return storageValue !== null ? storageValue : windowValue !== null ? windowValue : defaultValue;
}

/**
* Set a config value in the storage.
* This will also remove the value if the value is the same as the window one.
*/
function setConfig<K extends Extract<keyof TConfig, string> = Extract<keyof TConfig, string>>(
path: K,
value: TConfig[K],
Expand All @@ -90,11 +96,23 @@ export function createConfig<TConfig>(options: ConfigOptions<TConfig>) {
window.dispatchEvent(new Event("storage"));
}

/**
* Get all consolidate config values.
*/
function getAllConfig(): TConfig {
// `slice(0, -1)`is for removing the trailing point
const windowKeys = Object.keys(get(window, injected.namespace.slice(0, -1))) as any;
const defaultKeys = Object.keys(options.defaultConfig || {});

return uniq([...windowKeys, ...defaultKeys]).reduce((mem, key) => ({ ...mem, [key]: getConfig(key) }), {});
}

return {
Config(props: ConfigProps<TConfig>) {
return (
<ConfigBase
getConfig={getConfig}
getAllConfig={getAllConfig}
getStorageValue={getStorageValue}
getWindowValue={getWindowValue}
setConfig={setConfig}
Expand All @@ -107,6 +125,7 @@ export function createConfig<TConfig>(options: ConfigOptions<TConfig>) {
return (
<AdminConfigBase
getConfig={getConfig}
getAllConfig={getAllConfig}
getStorageValue={getStorageValue}
getWindowValue={getWindowValue}
setConfig={setConfig}
Expand All @@ -117,6 +136,7 @@ export function createConfig<TConfig>(options: ConfigOptions<TConfig>) {
},
getConfig,
setConfig,
getAllConfig,
};
}

Expand Down

0 comments on commit edd246e

Please sign in to comment.