diff --git a/index.js b/index.js index 10948c67..a353f7de 100644 --- a/index.js +++ b/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', + '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) diff --git a/package.json b/package.json index 014f7b06..4f9cb205 100644 --- a/package.json +++ b/package.json @@ -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" } }