-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
161 lines (140 loc) · 3.65 KB
/
index.js
File metadata and controls
161 lines (140 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
'use strict'
import stringifyQuery from 'qs/lib/stringify.js'
import ky from 'ky-universal'
import {parse as parseContentType} from 'content-type'
import createDebug from 'debug'
const debug = createDebug('hafas-rest-api-client')
const RESPONSE = Symbol('Response')
const HEADERS = Symbol('Response.headers')
const SERVER_TIMING = Symbol('Server-Timing header')
const CACHE = Symbol('X-Cache header')
const createClient = (endpoint, opt = {}) => {
new URL(endpoint); // throws if endpoint URL is invalid
const {
userAgent,
} = {
userAgent: 'hafas-rest-api-client',
}
const request = async (path, query = {}, opt = {}) => {
const url = new URL(path, endpoint)
const cfg = {
mode: 'cors',
redirect: 'follow',
searchParams: stringifyQuery(Object.fromEntries([
...url.searchParams.entries(),
...Object.entries(query),
]), {allowDots: true}),
...opt,
headers: {
'Accept': 'application/json',
'User-Agent': userAgent,
...(opt.headers || {}),
},
}
let res
try {
res = await ky.get(url.href, cfg)
debug(res.status, path, query, opt)
} catch (err) {
// parse JSON body, attach to error object
try {
const headers = err.response && err.response.headers
const cType = headers && headers.get('content-type')
if (cType && parseContentType(cType).type === 'application/json') {
err.body = await err.response.json()
if (err.body.msg) err.message += ' – ' + err.body.msg
}
// eslint-disable-next-line no-empty
} catch (_) {}
throw err
}
const body = await res.json()
Object.defineProperty(body, RESPONSE, {value: res})
Object.defineProperty(body, HEADERS, {value: res.headers})
Object.defineProperty(body, SERVER_TIMING, {
value: res.headers.get('Server-Timing') || null,
})
Object.defineProperty(body, CACHE, {
value: res.headers.get('X-Cache') || null,
})
return body
}
const locations = async (query, opt = {}) => {
return await request('/locations', {
query,
...opt,
})
}
const nearby = async (loc, opt = {}) => {
return await request('/stops/nearby', {
...loc,
...opt,
})
}
const stations = async (query, opt = {}) => {
return await request('/stations', {
...opt,
query,
})
}
const reachableFrom = async (loc, opt = {}) => {
return await request('/stops/reachable-from', {
...loc,
...opt,
})
}
const stop = async (id, opt = {}) => {
if (!id) throw new TypeError('invalid id')
return await request('/stops/' + encodeURIComponent(id), opt)
}
const _stationBoard = (type) => async (stop, opt = {}) => {
if (!stop) throw new TypeError('invalid stop')
if (stop.id) stop = stop.id
else if ('string' !== typeof stop) throw new TypeError('invalid stop')
return await request(`/stops/${encodeURIComponent(stop)}/departures`, opt)
}
const departures = _stationBoard('departures')
const arrivals = _stationBoard('arrivals')
const journeys = async (from, to, opt = {}) => {
return await request('/journeys', {
from, to,
...opt,
})
}
const refreshJourney = async (ref, opt = {}) => {
if (!ref) throw new TypeError('invalid ref')
return await request('/journeys/' + encodeURIComponent(ref), opt)
}
const trip = async (id, lineName, opt = {}) => {
if (!id) throw new TypeError('invalid id')
return await request('/trips/' + encodeURIComponent(id), {
lineName,
...opt,
})
}
const radar = async (bbox, opt = {}) => {
return await request('/radar', {
...bbox,
...opt,
})
}
return {
locations,
nearby,
stations,
reachableFrom,
stop,
departures, arrivals,
journeys,
refreshJourney,
trip,
radar,
}
}
export default createClient
export {
RESPONSE,
HEADERS,
SERVER_TIMING,
CACHE,
}