mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 15:19:35 +02:00
validate profile
This commit is contained in:
parent
a2fa56edc1
commit
eacbd8ef01
3 changed files with 45 additions and 6 deletions
5
index.js
5
index.js
|
@ -3,14 +3,13 @@
|
||||||
const minBy = require('lodash/minBy')
|
const minBy = require('lodash/minBy')
|
||||||
const maxBy = require('lodash/maxBy')
|
const maxBy = require('lodash/maxBy')
|
||||||
|
|
||||||
|
const validateProfile = require('./lib/validate-profile')
|
||||||
const defaultProfile = require('./lib/default-profile')
|
const defaultProfile = require('./lib/default-profile')
|
||||||
const request = require('./lib/request')
|
const request = require('./lib/request')
|
||||||
|
|
||||||
const createClient = (profile) => {
|
const createClient = (profile) => {
|
||||||
profile = Object.assign({}, defaultProfile, profile)
|
profile = Object.assign({}, defaultProfile, profile)
|
||||||
if ('string' !== typeof profile.timezone) {
|
validateProfile(profile)
|
||||||
throw new Error('profile.timezone must be a string.')
|
|
||||||
}
|
|
||||||
|
|
||||||
const departures = (station, opt = {}) => {
|
const departures = (station, opt = {}) => {
|
||||||
if ('string' !== typeof station) throw new Error('station must be a string.')
|
if ('string' !== typeof station) throw new Error('station must be a string.')
|
||||||
|
|
|
@ -15,7 +15,6 @@ const parseStopover = require('../parse/stopover')
|
||||||
const formatAddress = require('../format/address')
|
const formatAddress = require('../format/address')
|
||||||
const formatCoord = require('../format/coord')
|
const formatCoord = require('../format/coord')
|
||||||
const formatDate = require('../format/date')
|
const formatDate = require('../format/date')
|
||||||
const filters = require('../format/filters')
|
|
||||||
const formatLocationFilter = require('../format/location-filter')
|
const formatLocationFilter = require('../format/location-filter')
|
||||||
const formatPoi = require('../format/poi')
|
const formatPoi = require('../format/poi')
|
||||||
const formatStation = require('../format/station')
|
const formatStation = require('../format/station')
|
||||||
|
@ -25,7 +24,6 @@ const formatRectangle = require('../format/rectangle')
|
||||||
|
|
||||||
const id = x => x
|
const id = x => x
|
||||||
|
|
||||||
// todo: find out which are actually necessary
|
|
||||||
const defaultProfile = {
|
const defaultProfile = {
|
||||||
transformReqBody: id,
|
transformReqBody: id,
|
||||||
transformReq: id,
|
transformReq: id,
|
||||||
|
@ -48,7 +46,6 @@ const defaultProfile = {
|
||||||
formatAddress,
|
formatAddress,
|
||||||
formatCoord,
|
formatCoord,
|
||||||
formatDate,
|
formatDate,
|
||||||
filters,
|
|
||||||
formatLocationFilter,
|
formatLocationFilter,
|
||||||
formatPoi,
|
formatPoi,
|
||||||
formatStation,
|
formatStation,
|
||||||
|
|
43
lib/validate-profile.js
Normal file
43
lib/validate-profile.js
Normal file
|
@ -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
|
Loading…
Add table
Reference in a new issue