hk-bus-eta 1.2.2 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Ctb.js CHANGED
@@ -1,10 +1,8 @@
1
- const utils = require('./utils')
2
-
3
1
  module.exports = {
4
2
  co: 'ctb',
5
3
  fetchEtas: ({stopId, route, bound }) => (
6
4
  fetch(`https://rt.data.gov.hk//v1/transport/citybus-nwfb/eta/CTB/${stopId}/${route}`, {
7
- cache: utils.isSafari ? 'default' : 'no-store'
5
+ cache: "reload"
8
6
  }).then(
9
7
  response => response.json()
10
8
  ).then(({data}) => data.filter(eta => eta.eta && eta.dir === bound).map(e => ({
@@ -19,7 +17,7 @@ module.exports = {
19
17
  ),
20
18
  fetchStopEtas: ( stopId ) => (
21
19
  fetch(`https://rt.data.gov.hk/v1/transport/batch/stop-eta/CTB/${stopId}`, {
22
- cache: utils.isSafari ? 'default' : 'no-store'
20
+ cache: "reload"
23
21
  }).then(
24
22
  response => response.json()
25
23
  ).then(({data}) => data.map( e => ({
@@ -37,4 +35,4 @@ module.exports = {
37
35
  co: 'ctb'
38
36
  })))
39
37
  )
40
- }
38
+ }
package/Kmb.js CHANGED
@@ -1,11 +1,9 @@
1
- const utils = require('./utils')
2
-
3
1
  module.exports = {
4
2
  co: 'kmb',
5
3
  fetchEtas: ({stopId, route, seq, serviceType, bound}) => (
6
4
  fetch(
7
5
  `https://data.etabus.gov.hk/v1/transport/kmb/eta/${stopId}/${route}/${serviceType}`,
8
- { cache: utils.isSafari ? 'default' : 'no-store' }
6
+ { cache: "reload" }
9
7
  ).then( response => response.json() )
10
8
  .then(({data}) => data.filter(e =>
11
9
  e.dir === bound
@@ -22,7 +20,7 @@ module.exports = {
22
20
  ),
23
21
  fetchStopEtas: ( stopId ) => (
24
22
  fetch(`https://data.etabus.gov.hk/v1/transport/kmb/stop-eta/${stopId}`, {
25
- cache: utils.isSafari ? 'default' : 'no-store'
23
+ cache: "reload"
26
24
  })
27
25
  .then(response => response.json())
28
26
  .then(({data}) => data.map( e => ({
@@ -42,4 +40,4 @@ module.exports = {
42
40
  co: 'kmb'
43
41
  })))
44
42
  )
45
- }
43
+ }
package/Lrtfeeder.js ADDED
@@ -0,0 +1,34 @@
1
+ module.exports = {
2
+ co: 'lrtfeeder',
3
+ fetchEtas: ({stopId, route, language}) => (
4
+ fetch(`https://rt.data.gov.hk/v1/transport/mtr/bus/getSchedule`, {
5
+ method: "POST",
6
+ cache: "reload",
7
+ headers: {
8
+ 'Content-Type': 'application/json'
9
+ },
10
+ body: JSON.stringify({
11
+ 'language': language,
12
+ 'routeName': route
13
+ })
14
+ }).then( response => response.json() )
15
+ .then(({busStop}) =>
16
+ busStop.filter(({busStopId}) => busStopId === stopId)
17
+ .reduce((ret, {bus: buses}) => ([...ret, ...buses.reduce( (etas, bus) => {
18
+ const etaDate = new Date(Date.now() + parseInt(bus.arrivalTimeInSecond, 10) * 1000)
19
+ if ( bus.arrivalTimeInSecond === "108000" ) return [...etas];
20
+ return [
21
+ ...etas,
22
+ {
23
+ eta: etaDate.toISOString(),
24
+ remark: {
25
+ [language]: bus.busRemark
26
+ },
27
+ co: 'lrtfeeder'
28
+ }
29
+ ]
30
+ }, [])]), [])
31
+ )
32
+ ),
33
+ fetchStopEtas: ( stopId ) => []
34
+ }
package/Nlb.js CHANGED
@@ -1,5 +1,3 @@
1
- const utils = require('./utils')
2
-
3
1
  module.exports = {
4
2
  co: 'nlb',
5
3
  fetchEtas: ({stopId, nlbId }) => (
@@ -30,7 +28,7 @@ module.exports = {
30
28
  ),
31
29
  fetchStopEtas: ( stopId ) => (
32
30
  fetch(`https://rt.data.gov.hk/v1/transport/batch/stop-eta/NLB/${stopId}`, {
33
- cache: utils.isSafari ? 'default' : 'no-store'
31
+ cache: "reload"
34
32
  }).then( response => {
35
33
  if (response.ok)
36
34
  return response.json()
package/Nwfb.js CHANGED
@@ -1,10 +1,8 @@
1
- const utils = require('./utils')
2
-
3
1
  module.exports = {
4
2
  co: 'nwfb',
5
3
  fetchEtas: ({stopId, route, bound }) => (
6
4
  fetch(`https://rt.data.gov.hk//v1/transport/citybus-nwfb/eta/NWFB/${stopId}/${route}`, {
7
- cache: utils.isSafari ? 'default' : 'no-store'
5
+ cache: "reload"
8
6
  }).then(
9
7
  response => response.json()
10
8
  ).then(({data}) => data.filter(eta => eta.eta && eta.dir === bound).map(e => ({
@@ -19,7 +17,7 @@ module.exports = {
19
17
  ),
20
18
  fetchStopEtas: ( stopId ) => (
21
19
  fetch(`https://rt.data.gov.hk/v1/transport/batch/stop-eta/NWFB/${stopId}`, {
22
- cache: utils.isSafari ? 'default' : 'no-store'
20
+ cache: "reload"
23
21
  }).then(
24
22
  response => response.json()
25
23
  ).then(({data}) => data.map( e => ({
@@ -37,4 +35,4 @@ module.exports = {
37
35
  co: 'nwfb'
38
36
  })))
39
37
  )
40
- }
38
+ }
package/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export type Company = "kmb" | "nlb" | "ctb" | "nwfb"
1
+ export type Company = "kmb" | "nlb" | "ctb" | "nwfb" | 'lrtfeeder'
2
2
 
3
3
  export type Terminal = {
4
4
  en: string,
@@ -21,6 +21,7 @@ export type RouteListEntry = {
21
21
  readonly fares: string[] | null,
22
22
  readonly faresHoliday: string[] | null,
23
23
  readonly freq: Freq | null,
24
+ readonly jt: string | null,
24
25
  readonly seq: number,
25
26
  readonly serviceType: string,
26
27
  readonly stops: {
@@ -69,7 +70,7 @@ export type Eta = {
69
70
  co: Company
70
71
  }
71
72
 
72
- export declare function fetchEtas(route: Route): Promise<Eta[]>
73
+ export declare function fetchEtas(route: any): Promise<Eta[]>
73
74
 
74
75
  export declare function fetchEtaObj(): Promise<BusDb>
75
76
 
package/index.js CHANGED
@@ -2,29 +2,29 @@ const KmbApi = require('./Kmb')
2
2
  const NwfbApi = require('./Nwfb')
3
3
  const CtbApi = require('./Ctb')
4
4
  const NlbApi = require('./Nlb')
5
+ const LrtfeederApi = require('./Lrtfeeder')
5
6
 
6
7
  module.exports = {
7
8
  // seq is 0-based here
8
- fetchEtas: async ( {route, stops, bound, seq, serviceType, co, nlbId }) => {
9
+ fetchEtas: async ( {route, stops, bound, seq, serviceType, co, nlbId, language }) => {
9
10
  let _etas = []
10
11
  for ( const company_id of co ) {
11
- if (company_id === 'kmb' && stops.kmb ){
12
- _etas = _etas.concat( await KmbApi.fetchEtas({
13
- route,
14
- stopId: stops.kmb[seq],
15
- seq: (seq + 1 === stops.kmb.length ? 1000 : seq),
16
- serviceType, bound: bound[company_id]})
17
- )
18
- }
19
- else if ( company_id === 'ctb' && stops.ctb ) {
20
- _etas = _etas.concat( await CtbApi.fetchEtas({stopId: stops.ctb[seq], route, bound: bound[company_id] }))
21
- }
22
- else if ( company_id === 'nwfb' && stops.nwfb ) {
23
- _etas = _etas.concat( await NwfbApi.fetchEtas({stopId: stops.nwfb[seq], route, bound: bound[company_id] }))
24
- }
25
- else if ( company_id === 'nlb' && stops.nlb ) {
26
- _etas = _etas.concat( await NlbApi.fetchEtas({stopId: stops.nlb[seq], nlbId}) )
27
- }
12
+ if (company_id === 'kmb' && stops.kmb ){
13
+ _etas = _etas.concat( await KmbApi.fetchEtas({
14
+ route,
15
+ stopId: stops.kmb[seq],
16
+ seq,
17
+ serviceType, bound: bound[company_id]})
18
+ )
19
+ } else if ( company_id === 'ctb' && stops.ctb ) {
20
+ _etas = _etas.concat( await CtbApi.fetchEtas({stopId: stops.ctb[seq], route, bound: bound[company_id] }))
21
+ } else if ( company_id === 'nwfb' && stops.nwfb ) {
22
+ _etas = _etas.concat( await NwfbApi.fetchEtas({stopId: stops.nwfb[seq], route, bound: bound[company_id] }))
23
+ } else if ( company_id === 'nlb' && stops.nlb ) {
24
+ _etas = _etas.concat( await NlbApi.fetchEtas({stopId: stops.nlb[seq], nlbId}) )
25
+ } else if ( company_id === 'lrtfeeder' && stops.lrtfeeder ) {
26
+ _etas = _etas.concat( await LrtfeederApi.fetchEtas({stopId: stops.lrtfeeder[seq], route, language}))
27
+ }
28
28
  }
29
29
 
30
30
  return _etas.sort((a,b) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hk-bus-eta",
3
- "version": "1.2.2",
3
+ "version": "1.3.1",
4
4
  "description": "Query the ETA (Estimated Time of Arrival) of HK Bus",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/utils.js DELETED
@@ -1,3 +0,0 @@
1
- module.exports = {
2
- isSafari: navigator && navigator.userAgent && navigator.userAgent.includes('Safari/') && !(navigator.userAgent.includes('Chrome/') || navigator.userAgent.includes('Chromium/'))
3
- }