mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 07:09:35 +02:00
more granular overriding of defaults
This commit is contained in:
parent
5b0a4034cf
commit
09866a9a08
1 changed files with 38 additions and 27 deletions
65
index.js
65
index.js
|
@ -5,37 +5,48 @@ const parse = require('./parse')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const onData = (d) => {
|
const id = (x) => x
|
||||||
if (!d.common) return d
|
const defaults = {
|
||||||
const c = d.common
|
onBody: id,
|
||||||
if (Array.isArray(c.locL)) d.locations = c.locL.map(parse.location)
|
onReq: id,
|
||||||
if (Array.isArray(c.prodL)) d.products = c.prodL.map(parse.product)
|
onLocation: parse.location,
|
||||||
if (Array.isArray(c.remL)) d.remarks = c.remL.map(parse.remark)
|
onProduct: parse.product,
|
||||||
if (Array.isArray(c.opL)) d.agencies = c.opL.map(parse.agency)
|
onRemark: parse.remark,
|
||||||
return d
|
onAgency: parse.agency
|
||||||
}
|
}
|
||||||
|
|
||||||
const request = (cfg) => (data) => {
|
|
||||||
let body = {lang: 'en', svcReqL: [data]}
|
|
||||||
if (cfg.onBody) body = cfg.onBody(body)
|
|
||||||
|
|
||||||
let req = {
|
|
||||||
json: true, body: JSON.stringify(body),
|
const request = (opt) => {
|
||||||
headers: {
|
opt = Object.assign({}, defaults, opt)
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'Accept-Encoding': 'gzip, deflate'
|
return (data) => {
|
||||||
}
|
const body = opt.onBody({lang: 'en', svcReqL: [data]})
|
||||||
|
const req = opt.onReq({
|
||||||
|
json: true, body: JSON.stringify(body),
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Accept-Encoding': 'gzip, deflate'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return got.post(opt.endpoint, req)
|
||||||
|
.then((res) => {
|
||||||
|
const b = res.body
|
||||||
|
|
||||||
|
if (b.err) return new Error(b.err)
|
||||||
|
if (!b.svcResL || !b.svcResL[0]) return new Error('invalid response')
|
||||||
|
if (b.svcResL[0].err !== 'OK') return 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.onLocation)
|
||||||
|
if (Array.isArray(c.prodL)) d.products = c.prodL.map(opt.onProduct)
|
||||||
|
if (Array.isArray(c.remL)) d.remarks = c.remL.map(opt.onRemark)
|
||||||
|
if (Array.isArray(c.opL)) d.agencies = c.opL.map(opt.onAgency)
|
||||||
|
return d
|
||||||
|
}).catch((err) => err)
|
||||||
}
|
}
|
||||||
if (cfg.onReq) req = cfg.onReq(req)
|
|
||||||
|
|
||||||
return got.post(cfg.endpoint, req)
|
|
||||||
.then((res) => {
|
|
||||||
if (res.body.err) return new Error(res.body.err)
|
|
||||||
if (!res.body.svcResL || !res.body.svcResL[0]) return new Error('invalid response')
|
|
||||||
const data = res.body.svcResL[0]
|
|
||||||
if (data.err !== 'OK') return new Error(data.errTxt)
|
|
||||||
return cfg.onData || onData(data.res)
|
|
||||||
}).catch(console.error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = request
|
module.exports = request
|
||||||
|
|
Loading…
Add table
Reference in a new issue