mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 15:19:35 +02:00
move out request handling
This commit is contained in:
parent
79db55d99c
commit
e0d9c28ef2
3 changed files with 80 additions and 56 deletions
82
index.js
82
index.js
|
@ -1,75 +1,45 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const Promise = require('pinkie-promise')
|
|
||||||
const {fetch} = require('fetch-ponyfill')({Promise})
|
|
||||||
const {stringify} = require('query-string')
|
|
||||||
|
|
||||||
const parseLocation = require('./parse/location')
|
const parseLocation = require('./parse/location')
|
||||||
const parseLine = require('./parse/line')
|
const parseLine = require('./parse/line')
|
||||||
const parseRemark = require('./parse/remark')
|
const parseRemark = require('./parse/remark')
|
||||||
const parseOperator = require('./parse/operator')
|
const parseOperator = require('./parse/operator')
|
||||||
|
const request = require('./lib/request')
|
||||||
|
|
||||||
|
const id = x => x
|
||||||
|
|
||||||
|
const defaultProfile = {
|
||||||
const id = (x) => x
|
transformReqBody: id,
|
||||||
const defaults = {
|
transformReq: id,
|
||||||
onBody: id,
|
|
||||||
onReq: id,
|
|
||||||
parseLocation: parseLocation,
|
parseLocation: parseLocation,
|
||||||
parseLine: parseLine,
|
parseLine: parseLine,
|
||||||
parseRemark: parseRemark,
|
parseRemark: parseRemark,
|
||||||
parseOperator: parseOperator
|
parseOperator: parseOperator
|
||||||
}
|
}
|
||||||
|
|
||||||
const hafasError = (err) => {
|
const createClient = (profile) => {
|
||||||
err.isHafasError = true
|
profile = Object.assign({}, defaultProfile, profile)
|
||||||
return err
|
if ('function' !== profile.transformReqBody) {
|
||||||
}
|
throw new Error('profile.transformReqBody must be a function.')
|
||||||
|
}
|
||||||
const createRequest = (opt) => {
|
if ('function' !== profile.transformReq) {
|
||||||
opt = Object.assign({}, defaults, opt)
|
throw new Error('profile.transformReq must be a function.')
|
||||||
|
}
|
||||||
const request = (data) => {
|
if ('function' !== profile.parseLocation) {
|
||||||
const body = opt.onBody({lang: 'en', svcReqL: [data]})
|
throw new Error('profile.parseLocation must be a function.')
|
||||||
const req = opt.onReq({
|
}
|
||||||
method: 'post',
|
if ('function' !== profile.parseLine) {
|
||||||
body: JSON.stringify(body),
|
throw new Error('profile.parseLine must be a function.')
|
||||||
headers: {
|
}
|
||||||
'Content-Type': 'application/json',
|
if ('function' !== profile.parseRemark) {
|
||||||
'Accept-Encoding': 'gzip, deflate',
|
throw new Error('profile.parseRemark must be a function.')
|
||||||
'user-agent': 'https://github.com/derhuerst/hafas-client'
|
}
|
||||||
},
|
if ('function' !== profile.parseOperator) {
|
||||||
query: null
|
throw new Error('profile.parseOperator must be a function.')
|
||||||
})
|
|
||||||
const url = opt.endpoint + (req.query ? '?' + stringify(req.query) : '')
|
|
||||||
|
|
||||||
return fetch(url, req)
|
|
||||||
.then((res) => {
|
|
||||||
if (!res.ok) {
|
|
||||||
const err = new Error(res.statusText)
|
|
||||||
err.statusCode = res.status
|
|
||||||
throw hafasError(err)
|
|
||||||
}
|
|
||||||
return res.json()
|
|
||||||
})
|
|
||||||
.then((b) => {
|
|
||||||
if (b.err) throw hafasError(new Error(b.err))
|
|
||||||
if (!b.svcResL || !b.svcResL[0]) throw new Error('invalid response')
|
|
||||||
if (b.svcResL[0].err !== 'OK') {
|
|
||||||
throw hafasError(new Error(b.svcResL[0].errTxt))
|
|
||||||
}
|
|
||||||
const d = b.svcResL[0].res
|
|
||||||
const c = d.common || {}
|
|
||||||
|
|
||||||
if (Array.isArray(c.locL)) d.locations = c.locL.map(opt.parseLocation)
|
|
||||||
if (Array.isArray(c.prodL)) d.lines = c.prodL.map(opt.parseLine)
|
|
||||||
if (Array.isArray(c.remL)) d.remarks = c.remL.map(opt.parseRemark)
|
|
||||||
if (Array.isArray(c.opL)) d.operators = c.opL.map(opt.parseOperator)
|
|
||||||
return d
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return request
|
const client = data => request(profile, data)
|
||||||
|
return client
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = createRequest
|
module.exports = createRequest
|
||||||
|
|
53
lib/request.js
Normal file
53
lib/request.js
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const Promise = require('pinkie-promise')
|
||||||
|
const {fetch} = require('fetch-ponyfill')({Promise})
|
||||||
|
const {stringify} = require('query-string')
|
||||||
|
|
||||||
|
const hafasError = (err) => {
|
||||||
|
err.isHafasError = true
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
const request = (profile, data) => {
|
||||||
|
const body = profile.transformReqBody({lang: 'en', svcReqL: [data]})
|
||||||
|
const req = profile.transformReq({
|
||||||
|
method: 'post',
|
||||||
|
// todo: CORS? referrer policy?
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Accept-Encoding': 'gzip, deflate',
|
||||||
|
'user-agent': 'https://github.com/derhuerst/hafas-client'
|
||||||
|
},
|
||||||
|
query: null
|
||||||
|
})
|
||||||
|
const url = profile.endpoint + (req.query ? '?' + stringify(req.query) : '')
|
||||||
|
|
||||||
|
return fetch(url, req)
|
||||||
|
.then((res) => {
|
||||||
|
if (!res.ok) {
|
||||||
|
const err = new Error(res.statusText)
|
||||||
|
err.statusCode = res.status
|
||||||
|
throw hafasError(err)
|
||||||
|
}
|
||||||
|
return res.json()
|
||||||
|
})
|
||||||
|
.then((b) => {
|
||||||
|
if (b.err) throw hafasError(new Error(b.err))
|
||||||
|
if (!b.svcResL || !b.svcResL[0]) throw new Error('invalid response')
|
||||||
|
if (b.svcResL[0].err !== 'OK') {
|
||||||
|
throw hafasError(new Error(b.svcResL[0].errTxt))
|
||||||
|
}
|
||||||
|
const d = b.svcResL[0].res
|
||||||
|
const c = d.common || {}
|
||||||
|
|
||||||
|
if (Array.isArray(c.locL)) d.locations = c.locL.map(profile.parseLocation)
|
||||||
|
if (Array.isArray(c.prodL)) d.lines = c.prodL.map(profile.parseLine)
|
||||||
|
if (Array.isArray(c.remL)) d.remarks = c.remL.map(profile.parseRemark)
|
||||||
|
if (Array.isArray(c.opL)) d.operators = c.opL.map(profile.parseOperator)
|
||||||
|
return d
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = request
|
|
@ -5,6 +5,7 @@
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"files": [
|
"files": [
|
||||||
"index.js",
|
"index.js",
|
||||||
|
"lib",
|
||||||
"parse",
|
"parse",
|
||||||
"stringify.js"
|
"stringify.js"
|
||||||
],
|
],
|
||||||
|
|
Loading…
Add table
Reference in a new issue