mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 15:19: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
33
index.js
33
index.js
|
@ -1,6 +1,9 @@
|
||||||
'use strict'
|
'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')
|
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) => {
|
const createRequest = (opt) => {
|
||||||
opt = Object.assign({}, defaults, opt)
|
opt = Object.assign({}, defaults, opt)
|
||||||
|
|
||||||
const request = (data) => {
|
const request = (data) => {
|
||||||
const body = opt.onBody({lang: 'en', svcReqL: [data]})
|
const body = opt.onBody({lang: 'en', svcReqL: [data]})
|
||||||
const req = opt.onReq({
|
const req = opt.onReq({
|
||||||
json: true, body: body,
|
method: 'post',
|
||||||
|
body: JSON.stringify(body),
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Accept-Encoding': 'gzip, deflate',
|
'Accept-Encoding': 'gzip, deflate',
|
||||||
'user-agent': 'https://github.com/derhuerst/hafas-client'
|
'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) => {
|
.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.err) throw hafasError(b.err)
|
||||||
if (!b.svcResL || !b.svcResL[0]) throw new Error('invalid response')
|
if (!b.svcResL || !b.svcResL[0]) throw new Error('invalid response')
|
||||||
if (b.svcResL[0].err !== 'OK') throw hafasError(b.svcResL[0].errTxt)
|
if (b.svcResL[0].err !== 'OK') throw hafasError(b.svcResL[0].errTxt)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "hafas-client",
|
"name": "hafas-client",
|
||||||
"description": "JavaScript client for HAFAS mobile APIs.",
|
"description": "JavaScript client for HAFAS mobile APIs.",
|
||||||
"version": "1.2.2",
|
"version": "1.2.5",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"files": [
|
"files": [
|
||||||
"index.js",
|
"index.js",
|
||||||
|
@ -24,12 +24,14 @@
|
||||||
"node": ">=6"
|
"node": ">=6"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"got": "^7.1.0",
|
"fetch-ponyfill": "^4.1.0",
|
||||||
"moment-timezone": "^0.5.13",
|
"moment-timezone": "^0.5.13",
|
||||||
|
"pinkie-promise": "^2.0.1",
|
||||||
|
"query-string": "^5.0.0",
|
||||||
"slugg": "^1.2.0"
|
"slugg": "^1.2.0"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "node -e \"require('.')\"",
|
"test": "node -e \"require('.')\"",
|
||||||
"prepublish": "npm test"
|
"prepublishOnly": "npm test"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue