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

feat(stateConfig): make state config more flexible #38

Merged
merged 5 commits into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep validating the type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we want to only explicitly accept string, number, or boolean? The reason I did this was it puts the complexity to 13. I talked with @JAdshead and we came up with this solution.

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