From e0d9c28ef23efcd9d8cadd0f400ddaeb57f91dbf Mon Sep 17 00:00:00 2001 From: Jannis R Date: Sat, 11 Nov 2017 22:49:04 +0100 Subject: [PATCH] move out request handling --- index.js | 82 ++++++++++++++++---------------------------------- lib/request.js | 53 ++++++++++++++++++++++++++++++++ package.json | 1 + 3 files changed, 80 insertions(+), 56 deletions(-) create mode 100644 lib/request.js diff --git a/index.js b/index.js index 0953a860..3d7abf43 100644 --- a/index.js +++ b/index.js @@ -1,75 +1,45 @@ 'use strict' -const Promise = require('pinkie-promise') -const {fetch} = require('fetch-ponyfill')({Promise}) -const {stringify} = require('query-string') - const parseLocation = require('./parse/location') const parseLine = require('./parse/line') const parseRemark = require('./parse/remark') const parseOperator = require('./parse/operator') +const request = require('./lib/request') +const id = x => x - -const id = (x) => x -const defaults = { - onBody: id, - onReq: id, +const defaultProfile = { + transformReqBody: id, + transformReq: id, parseLocation: parseLocation, parseLine: parseLine, parseRemark: parseRemark, parseOperator: parseOperator } -const hafasError = (err) => { - 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({ - 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 fetch(url, req) - .then((res) => { - if (!res.ok) { - const err = new Error(res.statusText) - err.statusCode = res.status - throw hafasError(err) - } - return res.json() - }) - .then((b) => { - if (b.err) throw hafasError(new Error(b.err)) - if (!b.svcResL || !b.svcResL[0]) throw new Error('invalid response') - if (b.svcResL[0].err !== 'OK') { - throw hafasError(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.parseLocation) - if (Array.isArray(c.prodL)) d.lines = c.prodL.map(opt.parseLine) - if (Array.isArray(c.remL)) d.remarks = c.remL.map(opt.parseRemark) - if (Array.isArray(c.opL)) d.operators = c.opL.map(opt.parseOperator) - return d - }) +const createClient = (profile) => { + profile = Object.assign({}, defaultProfile, profile) + if ('function' !== profile.transformReqBody) { + throw new Error('profile.transformReqBody must be a function.') + } + if ('function' !== profile.transformReq) { + throw new Error('profile.transformReq must be a function.') + } + if ('function' !== profile.parseLocation) { + throw new Error('profile.parseLocation must be a function.') + } + if ('function' !== profile.parseLine) { + throw new Error('profile.parseLine must be a function.') + } + if ('function' !== profile.parseRemark) { + throw new Error('profile.parseRemark must be a function.') + } + if ('function' !== profile.parseOperator) { + throw new Error('profile.parseOperator must be a function.') } - return request + const client = data => request(profile, data) + return client } module.exports = createRequest diff --git a/lib/request.js b/lib/request.js new file mode 100644 index 00000000..6affd575 --- /dev/null +++ b/lib/request.js @@ -0,0 +1,53 @@ +'use strict' + +const Promise = require('pinkie-promise') +const {fetch} = require('fetch-ponyfill')({Promise}) +const {stringify} = require('query-string') + +const hafasError = (err) => { + err.isHafasError = true + return err +} + +const request = (profile, data) => { + const body = profile.transformReqBody({lang: 'en', svcReqL: [data]}) + const req = profile.transformReq({ + method: 'post', + // todo: CORS? referrer policy? + 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 = profile.endpoint + (req.query ? '?' + stringify(req.query) : '') + + return fetch(url, req) + .then((res) => { + if (!res.ok) { + const err = new Error(res.statusText) + err.statusCode = res.status + throw hafasError(err) + } + return res.json() + }) + .then((b) => { + if (b.err) throw hafasError(new Error(b.err)) + if (!b.svcResL || !b.svcResL[0]) throw new Error('invalid response') + if (b.svcResL[0].err !== 'OK') { + throw hafasError(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(profile.parseLocation) + if (Array.isArray(c.prodL)) d.lines = c.prodL.map(profile.parseLine) + if (Array.isArray(c.remL)) d.remarks = c.remL.map(profile.parseRemark) + if (Array.isArray(c.opL)) d.operators = c.opL.map(profile.parseOperator) + return d + }) +} + +module.exports = request diff --git a/package.json b/package.json index 9ea12e46..720b6374 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "main": "index.js", "files": [ "index.js", + "lib", "parse", "stringify.js" ],