Skip to content

Commit

Permalink
Config validation was moved to util.
Browse files Browse the repository at this point in the history
  • Loading branch information
prescottprue committed Dec 6, 2016
1 parent 863ef3b commit bb36408
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
18 changes: 7 additions & 11 deletions src/compose.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Firebase from 'firebase'
import { defaultConfig } from './constants'
import { validateConfig } from './utils'
import { authActions, queryActions, storageActions } from './actions'
let firebaseInstance

Expand Down Expand Up @@ -43,25 +44,20 @@ let firebaseInstance
* // Use Function later to create store
* const store = createStoreWithFirebase(rootReducer, initialState)
*/
export default (config, otherConfig) => next =>
export default (fbConfig, otherConfig) => next =>
(reducer, initialState, middleware) => {
const store = next(reducer, initialState, middleware)
const { dispatch } = store

const { apiKey, authDomain, databaseURL } = config

// Throw for missing Firebase Data
if (!databaseURL) throw new Error('Firebase databaseURL is required')
if (!authDomain) throw new Error('Firebase authDomain is required')
if (!apiKey) throw new Error('Firebase apiKey is required')

// Combine all configs
const configs = Object.assign({}, defaultConfig, config, otherConfig)
const configs = Object.assign({}, defaultConfig, fbConfig, otherConfig)

validateConfig(configs)

// Initialize Firebase
try {
Firebase.initializeApp(config)
} catch (err) {}
Firebase.initializeApp(fbConfig)
} catch (err) {} // silence reinitialize warning (hot-reloading)

// Enable Logging based on config
if (configs.enableLogging) {
Expand Down
36 changes: 29 additions & 7 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
import { isFunction } from 'lodash'
import { getEventsFromInput } from './events'
export { getEventsFromInput } from './events'

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

export {
getEventsFromInput
}
/**
* @private
* @description Validate config input
* @param {Object} Config object containing all combined configs
*/
export const validateConfig = (config) => {
// require needed Firebase config
const requiredProps = [
'databaseURL',
'authDomain',
'apiKey'
]
requiredProps.forEach((p) => {
if (!config[p]) {
throw new Error(`${p} is a required config parameter for react-redux-firebase.`)
}
})

export default {
createCallable,
getEventsFromInput
// Check that some certain config are functions if they exist
const functionProps = [
'fileMetadataFactory',
'profileDecorator'
]
functionProps.forEach((p) => {
if (!!config[p] && !isFunction(config[p])) {
throw new Error(`${p} parameter in react-redux-firebase config must be a function. check your compose function.`)
}
})
}
7 changes: 4 additions & 3 deletions test/unit/compose.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,18 @@ describe('Compose', () => {
})

describe('throws for missing fbConfig parameters', () => {
const errorSuffix = 'is a required config parameter for react-redux-firebase.'
it('databaseURL', () => {
expect(() => generateCreateStore('databaseURL')(reducer))
.to.throw('Firebase databaseURL is required')
.to.throw(`databaseURL ${errorSuffix}`)
})
it('authDomain', () => {
expect(() => generateCreateStore('authDomain')(reducer))
.to.throw('Firebase authDomain is required')
.to.throw(`authDomain ${errorSuffix}`)
})
it('apiKey', () => {
expect(() => generateCreateStore('apiKey')(reducer))
.to.throw('Firebase apiKey is required')
.to.throw(`apiKey ${errorSuffix}`)
})
})

Expand Down

0 comments on commit bb36408

Please sign in to comment.