Skip to content

Commit bb36408

Browse files
committed
Config validation was moved to util.
1 parent 863ef3b commit bb36408

File tree

3 files changed

+40
-21
lines changed

3 files changed

+40
-21
lines changed

src/compose.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Firebase from 'firebase'
22
import { defaultConfig } from './constants'
3+
import { validateConfig } from './utils'
34
import { authActions, queryActions, storageActions } from './actions'
45
let firebaseInstance
56

@@ -43,25 +44,20 @@ let firebaseInstance
4344
* // Use Function later to create store
4445
* const store = createStoreWithFirebase(rootReducer, initialState)
4546
*/
46-
export default (config, otherConfig) => next =>
47+
export default (fbConfig, otherConfig) => next =>
4748
(reducer, initialState, middleware) => {
4849
const store = next(reducer, initialState, middleware)
4950
const { dispatch } = store
5051

51-
const { apiKey, authDomain, databaseURL } = config
52-
53-
// Throw for missing Firebase Data
54-
if (!databaseURL) throw new Error('Firebase databaseURL is required')
55-
if (!authDomain) throw new Error('Firebase authDomain is required')
56-
if (!apiKey) throw new Error('Firebase apiKey is required')
57-
5852
// Combine all configs
59-
const configs = Object.assign({}, defaultConfig, config, otherConfig)
53+
const configs = Object.assign({}, defaultConfig, fbConfig, otherConfig)
54+
55+
validateConfig(configs)
6056

6157
// Initialize Firebase
6258
try {
63-
Firebase.initializeApp(config)
64-
} catch (err) {}
59+
Firebase.initializeApp(fbConfig)
60+
} catch (err) {} // silence reinitialize warning (hot-reloading)
6561

6662
// Enable Logging based on config
6763
if (configs.enableLogging) {

src/utils/index.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,39 @@
11
import { isFunction } from 'lodash'
2-
import { getEventsFromInput } from './events'
2+
export { getEventsFromInput } from './events'
33

44
/**
5+
* @private
56
* @description Create a function if not already one
67
* @param {Function|Object|Array|String} Callable function or value of return for new function
78
*/
89
export const createCallable = f => isFunction(f) ? f : () => f
910

10-
export {
11-
getEventsFromInput
12-
}
11+
/**
12+
* @private
13+
* @description Validate config input
14+
* @param {Object} Config object containing all combined configs
15+
*/
16+
export const validateConfig = (config) => {
17+
// require needed Firebase config
18+
const requiredProps = [
19+
'databaseURL',
20+
'authDomain',
21+
'apiKey'
22+
]
23+
requiredProps.forEach((p) => {
24+
if (!config[p]) {
25+
throw new Error(`${p} is a required config parameter for react-redux-firebase.`)
26+
}
27+
})
1328

14-
export default {
15-
createCallable,
16-
getEventsFromInput
29+
// Check that some certain config are functions if they exist
30+
const functionProps = [
31+
'fileMetadataFactory',
32+
'profileDecorator'
33+
]
34+
functionProps.forEach((p) => {
35+
if (!!config[p] && !isFunction(config[p])) {
36+
throw new Error(`${p} parameter in react-redux-firebase config must be a function. check your compose function.`)
37+
}
38+
})
1739
}

test/unit/compose.spec.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,18 @@ describe('Compose', () => {
104104
})
105105

106106
describe('throws for missing fbConfig parameters', () => {
107+
const errorSuffix = 'is a required config parameter for react-redux-firebase.'
107108
it('databaseURL', () => {
108109
expect(() => generateCreateStore('databaseURL')(reducer))
109-
.to.throw('Firebase databaseURL is required')
110+
.to.throw(`databaseURL ${errorSuffix}`)
110111
})
111112
it('authDomain', () => {
112113
expect(() => generateCreateStore('authDomain')(reducer))
113-
.to.throw('Firebase authDomain is required')
114+
.to.throw(`authDomain ${errorSuffix}`)
114115
})
115116
it('apiKey', () => {
116117
expect(() => generateCreateStore('apiKey')(reducer))
117-
.to.throw('Firebase apiKey is required')
118+
.to.throw(`apiKey ${errorSuffix}`)
118119
})
119120
})
120121

0 commit comments

Comments
 (0)