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

Commit

Permalink
feat(stateConfig): make state config more flexible (#38)
Browse files Browse the repository at this point in the history
This allows the provideStateConfig to accept more than an object
or a string.

Closes #20
  • Loading branch information
James Singleton committed Feb 26, 2020
1 parent 4952d28 commit 72539a0
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 29 deletions.
24 changes: 24 additions & 0 deletions __tests__/server/utils/__snapshots__/stateConfig.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,30 @@ exports[`stateConfig methods stateConfig with module config should not set confi

exports[`stateConfig methods stateConfig with module config should not set config if provided not a string or object 2`] = `Object {}`;

exports[`stateConfig methods stateConfig with module config should set config if provided a boolean 1`] = `
Object {
"enableTest": true,
}
`;

exports[`stateConfig methods stateConfig with module config should set config if provided a boolean 2`] = `
Object {
"enableTest": true,
}
`;

exports[`stateConfig methods stateConfig with module config should set config if provided a number 1`] = `
Object {
"timeout": 600000,
}
`;

exports[`stateConfig methods stateConfig with module config should set config if provided a number 2`] = `
Object {
"timeout": 600000,
}
`;

exports[`stateConfig methods stateConfig with module config should set config if provided a string 1`] = `
Object {
"someApiUrl": "https://internet-origin-string.example.com/some-api/v1",
Expand Down
22 changes: 22 additions & 0 deletions __tests__/server/utils/stateConfig.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,28 @@ describe('stateConfig methods', () => {
expect(getClientStateConfig()).toMatchSnapshot();
expect(getServerStateConfig()).toMatchSnapshot();
});
it('should set config if provided a number', () => {
provideStateConfig = {
timeout: {
client: 600000,
server: 600000,
},
};
setStateConfig(provideStateConfig);
expect(getClientStateConfig()).toMatchSnapshot();
expect(getServerStateConfig()).toMatchSnapshot();
});
it('should set config if provided a boolean', () => {
provideStateConfig = {
enableTest: {
client: true,
server: true,
},
};
setStateConfig(provideStateConfig);
expect(getClientStateConfig()).toMatchSnapshot();
expect(getServerStateConfig()).toMatchSnapshot();
});
it('should not set config if provided not a string or object', () => {
provideStateConfig = {
someApiUrl: {
Expand Down
63 changes: 38 additions & 25 deletions docs/api/modules/App-Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,32 @@ Node Bundle (e.g.`mymodule.node.js`) rather than the Browser Bundles (e.g.
security and bundle size considerations.

**Contents**
* [provideStateConfig](#providestateconfig)
* [csp](#csp)
* [corsOrigins](#corsorigins)
* [configureRequestLog](#configurerequestlog)
* [extendSafeRequestRestrictedAttributes](#extendsaferequestrestrictedattributes)
* [createSsrFetch](#createssrfetch)
* [validateStateConfig](#validatestateconfig)
* [requiredSafeRequestRestrictedAttributes](#requiredsaferequestrestrictedattributes)
* [appCompatibility](#appcompatibility)
- [`provideStateConfig`](#providestateconfig)
- [`csp`](#csp)
- [`corsOrigins`](#corsorigins)
- [`configureRequestLog`](#configurerequestlog)
- [`extendSafeRequestRestrictedAttributes`](#extendsaferequestrestrictedattributes)
- [`createSsrFetch`](#createssrfetch)
- [`validateStateConfig`](#validatestateconfig)
- [`requiredSafeRequestRestrictedAttributes`](#requiredsaferequestrestrictedattributes)
- [`appCompatibility`](#appcompatibility)

## `provideStateConfig`
**Module Type**
* ✅ Root Module
* 🚫 Child Module

**Shape**

```js
if (!global.BROWSER) {
Module.appConfig = {
provideStateConfig: {
server: {
[settingName]: {
[settingName]: {
client: {
[environmentLevel]: String,
},
},
client: {
[settingName]: {
server: {
[environmentLevel]: String,
},
},
Expand All @@ -77,18 +76,32 @@ In practice, the state config supplied by a Root Module may look like this shape
if (!global.BROWSER) {
Module.appConfig = {
provideStateConfig: {
server: {
myApiHostname: {
development: 'dev.api.intranet.example.com',
qa: 'qa.api.intranet.example.com',
production: 'prod.api.intranet.example.com',
someApiUrl: {
client: {
development: 'https://internet-origin-dev.example.com/some-api/v1',
qa: 'https://internet-origin-qa.example.com/some-api/v1',
production: 'https://internet-origin.example.com/some-api/v1',
},
server: {
development: 'https://intranet-origin-dev.example.com/some-api/v1',
qa: 'https://intranet-origin-qa.example.com/some-api/v1',
production: 'https://intranet-origin.example.com/some-api/v1',
},
},
client: {
myApiHostname: {
development: 'dev.api.external.example.com',
qa: 'qa.api.external.example.com',
production: 'prod.api.external.example.com',
someBooleanValue: {
client: true,
server: true,
},
someNumberValue: {
client: {
development: 480000,
qa: 480000,
production: 480000,
},
server: {
development: 480000,
qa: 480000,
production: 480000,
},
},
},
Expand Down Expand Up @@ -357,4 +370,4 @@ If the One App version fails a Module's `appCompatibility` check, the Server wil
**📘 More Information**
* [Node Semver](https://github.com/npm/node-semver)

[☝️ Return To Top](#app-configuration)
[☝️ Return To Top](#app-configuration)
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export default {
production: 'https://intranet-origin.example.com/some-api/v1',
},
},
someBooleanValue: {
client: true,
server: true,
},
},
extendSafeRequestRestrictedAttributes: {
cookies: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ if (!global.BROWSER) {
},
},
},
someBooleanValue: {
client: {
validate(value) {
if (typeof value !== 'boolean') {
throw new TypeError('Failed to pass correct boolean on client');
}
},
},
server: {
validate(value) {
if (typeof value !== 'boolean') {
throw new TypeError('Failed to pass correct boolean on server');
}
},
},
},
},
};
}
Expand Down
6 changes: 2 additions & 4 deletions src/server/utils/stateConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ export const setStateConfig = (providedStateConfig) => {
throw makeConfigEnvError();
}
acc.client[key] = client[configEnv];
}
if (typeof client === 'string') {
} else {
acc.client[key] = client;
}
}
Expand All @@ -134,8 +133,7 @@ export const setStateConfig = (providedStateConfig) => {
throw makeConfigEnvError();
}
acc.server[key] = server[configEnv];
}
if (typeof server === 'string') {
} else {
acc.server[key] = server;
}
}
Expand Down

0 comments on commit 72539a0

Please sign in to comment.