mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-22 22:59:35 +02:00
ability to fetch data
This commit is contained in:
parent
df398966dc
commit
844780d805
2 changed files with 32 additions and 2 deletions
29
index.js
29
index.js
|
@ -1,3 +1,30 @@
|
|||
'use strict'
|
||||
|
||||
// todo
|
||||
const got = require('got')
|
||||
|
||||
const request = (cfg) => (data) => {
|
||||
let req = {
|
||||
json: true,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept-Encoding': 'gzip, deflate'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
client: cfg.client, ext: cfg.ext
|
||||
, ver: cfg.version, auth: cfg.auth
|
||||
, lang: 'en', svcReqL: [data]
|
||||
})
|
||||
}
|
||||
if (cfg.req) req = cfg.req(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 data.res
|
||||
}).catch(console.error)
|
||||
}
|
||||
|
||||
module.exports = request
|
||||
|
|
|
@ -10,5 +10,8 @@
|
|||
"bugs": "https://github.com/derhuerst/hafas-client/issues",
|
||||
"license": "ISC",
|
||||
"keywords": ["hafas", "public", "transport", "api"],
|
||||
"engines" : {"node": ">=4"}
|
||||
"engines" : {"node": ">=4"},
|
||||
"dependencies": {
|
||||
"got": "^6.3"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue