2016-06-22 01:39:04 +02:00
|
|
|
'use strict'
|
|
|
|
|
2017-10-04 22:15:01 +02:00
|
|
|
const Promise = require('pinkie-promise')
|
|
|
|
const {fetch} = require('fetch-ponyfill')({Promise})
|
|
|
|
const {stringify} = require('query-string')
|
|
|
|
|
2016-06-22 02:09:02 +02:00
|
|
|
const parse = require('./parse')
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-06-26 18:14:41 +02:00
|
|
|
const id = (x) => x
|
|
|
|
const defaults = {
|
|
|
|
onBody: id,
|
|
|
|
onReq: id,
|
|
|
|
onLocation: parse.location,
|
2017-05-16 01:30:43 +02:00
|
|
|
onLine: parse.line,
|
2016-06-26 18:14:41 +02:00
|
|
|
onRemark: parse.remark,
|
2017-05-09 16:39:26 +02:00
|
|
|
onOperator: parse.operator
|
2016-06-22 02:09:02 +02:00
|
|
|
}
|
2016-06-22 01:39:59 +02:00
|
|
|
|
2016-06-22 02:09:02 +02:00
|
|
|
|
2016-06-26 18:14:41 +02:00
|
|
|
|
2017-10-03 17:36:42 +02:00
|
|
|
const createRequest = (opt) => {
|
2016-06-26 18:14:41 +02:00
|
|
|
opt = Object.assign({}, defaults, opt)
|
|
|
|
|
2017-10-03 17:36:42 +02:00
|
|
|
const request = (data) => {
|
2016-06-26 18:14:41 +02:00
|
|
|
const body = opt.onBody({lang: 'en', svcReqL: [data]})
|
|
|
|
const req = opt.onReq({
|
2017-10-04 22:15:01 +02:00
|
|
|
method: 'post',
|
|
|
|
body: JSON.stringify(body),
|
2016-06-26 18:14:41 +02:00
|
|
|
headers: {
|
2017-10-04 22:15:01 +02:00
|
|
|
'Content-Type': 'application/json',
|
2017-05-09 16:39:26 +02:00
|
|
|
'Accept-Encoding': 'gzip, deflate',
|
|
|
|
'user-agent': 'https://github.com/derhuerst/hafas-client'
|
2017-10-04 22:15:01 +02:00
|
|
|
},
|
|
|
|
query: null
|
2016-06-26 18:14:41 +02:00
|
|
|
})
|
2017-10-04 22:15:01 +02:00
|
|
|
const url = opt.endpoint + (req.query ? '?' + stringify(req.query) : '')
|
2016-06-26 18:14:41 +02:00
|
|
|
|
2017-10-04 22:15:01 +02:00
|
|
|
return fetch(url, req)
|
2016-06-26 18:14:41 +02:00
|
|
|
.then((res) => {
|
2017-10-04 22:15:01 +02:00
|
|
|
if (!res.ok) {
|
|
|
|
const err = new Error(res.statusText)
|
|
|
|
err.statusCode = res.status
|
|
|
|
err.isHafasError = true
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
return res.json()
|
|
|
|
})
|
|
|
|
.then((b) => {
|
2017-07-24 14:25:43 +02:00
|
|
|
if (b.err) throw hafasError(b.err)
|
2016-07-18 19:21:31 +02:00
|
|
|
if (!b.svcResL || !b.svcResL[0]) throw new Error('invalid response')
|
2017-07-24 14:25:43 +02:00
|
|
|
if (b.svcResL[0].err !== 'OK') throw hafasError(b.svcResL[0].errTxt)
|
2016-06-26 18:14:41 +02:00
|
|
|
const d = b.svcResL[0].res
|
|
|
|
const c = d.common || {}
|
|
|
|
|
|
|
|
if (Array.isArray(c.locL)) d.locations = c.locL.map(opt.onLocation)
|
2017-05-16 01:30:43 +02:00
|
|
|
if (Array.isArray(c.prodL)) d.lines = c.prodL.map(opt.onLine)
|
2016-06-26 18:14:41 +02:00
|
|
|
if (Array.isArray(c.remL)) d.remarks = c.remL.map(opt.onRemark)
|
2017-05-09 16:39:26 +02:00
|
|
|
if (Array.isArray(c.opL)) d.operators = c.opL.map(opt.onOperator)
|
2016-06-26 18:14:41 +02:00
|
|
|
return d
|
2017-10-03 17:36:42 +02:00
|
|
|
})
|
2016-06-22 01:39:59 +02:00
|
|
|
}
|
2017-10-03 17:36:42 +02:00
|
|
|
|
|
|
|
return request
|
2016-06-22 01:39:59 +02:00
|
|
|
}
|
|
|
|
|
2017-10-03 17:36:42 +02:00
|
|
|
module.exports = createRequest
|