diff --git a/index.js b/index.js index 69ff8c21..d60098dc 100644 --- a/index.js +++ b/index.js @@ -3,14 +3,13 @@ const minBy = require('lodash/minBy') const maxBy = require('lodash/maxBy') +const validateProfile = require('./lib/validate-profile') const defaultProfile = require('./lib/default-profile') const request = require('./lib/request') const createClient = (profile) => { profile = Object.assign({}, defaultProfile, profile) - if ('string' !== typeof profile.timezone) { - throw new Error('profile.timezone must be a string.') - } + validateProfile(profile) const departures = (station, opt = {}) => { if ('string' !== typeof station) throw new Error('station must be a string.') diff --git a/lib/default-profile.js b/lib/default-profile.js index 8d673536..418be358 100644 --- a/lib/default-profile.js +++ b/lib/default-profile.js @@ -15,7 +15,6 @@ const parseStopover = require('../parse/stopover') const formatAddress = require('../format/address') const formatCoord = require('../format/coord') const formatDate = require('../format/date') -const filters = require('../format/filters') const formatLocationFilter = require('../format/location-filter') const formatPoi = require('../format/poi') const formatStation = require('../format/station') @@ -25,7 +24,6 @@ const formatRectangle = require('../format/rectangle') const id = x => x -// todo: find out which are actually necessary const defaultProfile = { transformReqBody: id, transformReq: id, @@ -48,7 +46,6 @@ const defaultProfile = { formatAddress, formatCoord, formatDate, - filters, formatLocationFilter, formatPoi, formatStation, diff --git a/lib/validate-profile.js b/lib/validate-profile.js new file mode 100644 index 00000000..7d8b595b --- /dev/null +++ b/lib/validate-profile.js @@ -0,0 +1,43 @@ +'use strict' + +const types = { + locale: 'string', + timezone: 'string', + transformReq: 'function', + transformReqBody: 'function', + transformJourneysQuery: 'function', + + parseDateTime: 'function', + parseDeparture: 'function', + parseJourneyPart: 'function', + parseJourney: 'function', + parseLine: 'function', + parseStationName: 'function', + parseLocation: 'function', + parseMovement: 'function', + parseNearby: 'function', + parseOperator: 'function', + parseRemark: 'function', + parseStopover: 'function', + + formatAddress: 'function', + formatCoord: 'function', + formatDate: 'function', + formatLocationFilter: 'function', + formatPoi: 'function', + formatStation: 'function', + formatTime: 'function', + formatLocation: 'function', + formatRectangle: 'function' +} + +const validateProfile = (profile) => { + for (let key of Object.keys(types)) { + const type = types[key] + if (type !== typeof profile[key]) { + throw new Error(`profile.${key} must be a ${type}.`) + } + } +} + +module.exports = validateProfile