mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 07:09:35 +02:00
got -> fetch-ponyfill, 1.2.5
This commit is contained in:
parent
f58cac93b7
commit
d72fdc8092
2 changed files with 25 additions and 16 deletions
31
index.js
31
index.js
|
@ -1,6 +1,9 @@
|
|||
'use strict'
|
||||
|
||||
const got = require('got')
|
||||
const Promise = require('pinkie-promise')
|
||||
const {fetch} = require('fetch-ponyfill')({Promise})
|
||||
const {stringify} = require('query-string')
|
||||
|
||||
const parse = require('./parse')
|
||||
|
||||
|
||||
|
@ -17,30 +20,34 @@ const defaults = {
|
|||
|
||||
|
||||
|
||||
const hafasError = (msg) => {
|
||||
const err = new Error(msg)
|
||||
err.isHafasError = true
|
||||
return err
|
||||
}
|
||||
|
||||
const createRequest = (opt) => {
|
||||
opt = Object.assign({}, defaults, opt)
|
||||
|
||||
const request = (data) => {
|
||||
const body = opt.onBody({lang: 'en', svcReqL: [data]})
|
||||
const req = opt.onReq({
|
||||
json: true, body: body,
|
||||
method: 'post',
|
||||
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 = opt.endpoint + (req.query ? '?' + stringify(req.query) : '')
|
||||
|
||||
return got.post(opt.endpoint, req)
|
||||
return fetch(url, req)
|
||||
.then((res) => {
|
||||
const b = res.body
|
||||
|
||||
if (!res.ok) {
|
||||
const err = new Error(res.statusText)
|
||||
err.statusCode = res.status
|
||||
err.isHafasError = true
|
||||
throw err
|
||||
}
|
||||
return res.json()
|
||||
})
|
||||
.then((b) => {
|
||||
if (b.err) throw hafasError(b.err)
|
||||
if (!b.svcResL || !b.svcResL[0]) throw new Error('invalid response')
|
||||
if (b.svcResL[0].err !== 'OK') throw hafasError(b.svcResL[0].errTxt)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "hafas-client",
|
||||
"description": "JavaScript client for HAFAS mobile APIs.",
|
||||
"version": "1.2.2",
|
||||
"version": "1.2.5",
|
||||
"main": "index.js",
|
||||
"files": [
|
||||
"index.js",
|
||||
|
@ -24,12 +24,14 @@
|
|||
"node": ">=6"
|
||||
},
|
||||
"dependencies": {
|
||||
"got": "^7.1.0",
|
||||
"fetch-ponyfill": "^4.1.0",
|
||||
"moment-timezone": "^0.5.13",
|
||||
"pinkie-promise": "^2.0.1",
|
||||
"query-string": "^5.0.0",
|
||||
"slugg": "^1.2.0"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "node -e \"require('.')\"",
|
||||
"prepublish": "npm test"
|
||||
"prepublishOnly": "npm test"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue