-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.mjs
35 lines (31 loc) · 1014 Bytes
/
fetch.mjs
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
import { ENDPOINTS, STREAMS } from './constants.mjs'
import fetch from 'isomorphic-unfetch'
import { get } from 'datagarrison'
import { encode } from 'querystring'
export const fetchNoaaData = () => {
console.log('Fetching NOAA data')
const { url, query } = ENDPOINTS.noaaCurrent
const source = `${url}?${encode(query)}`
return fetch(source, { method: 'GET' })
.then(response => {
if (response.ok) return response.json()
throw new Error(`Request rejected with status ${response.status}`)
})
.then(json => {
if (json.error && json.error.message) throw new Error(json.error.message)
return json
})
.then(json => ({
...json,
source,
data: json.data?.map(datum => ({ ...datum, t: `${datum.t} GMT` }))
}))
}
export const fetchPier17Data = () => {
console.log('Fetching Pier 17 data')
return get(STREAMS.pier17)
}
export const fetchCentralParkData = () => {
console.log('Fetching Central Park data')
return get(STREAMS.centralPark)
}