Skip to content

Commit

Permalink
Merge pull request #144 from neaps/kevee/remove-promises
Browse files Browse the repository at this point in the history
chore: Cleanup node fetch. Part of async in #137
  • Loading branch information
kevee authored Jun 30, 2022
2 parents 4c436c8 + 8cfe48a commit fc162dc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 57 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"jest": "^27.2.0",
"jest-junit": "^11.0.1",
"kind-of": "^6.0.3",
"node-fetch": "^2.6.0",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-node-resolve": "^5.2.0"
},
Expand Down
78 changes: 47 additions & 31 deletions src/__tests__/noaa.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs'
import fetch from 'node-fetch'
import https from 'https'
import tidePrediction from '..'

// Create a directory for test cache
Expand All @@ -9,6 +9,26 @@ if (!fs.existsSync('./.test-cache')) {

const stations = ['9413450', '9411340', '2695535', '8761724', '8410140']

const makeRequest = (url) =>
new Promise((resolve, reject) => {
https.get(url, (response) => {
const data = []

response.on('data', (fragment) => {
data.push(fragment)
})

response.on('end', () => {
const body = Buffer.concat(data)
resolve(JSON.parse(body.toString()))
})

response.on('error', (error) => {
reject(error)
})
})
})

const getStation = (station, callback) => {
const filePath = `./.test-cache/${station}.json`
if (fs.existsSync(filePath)) {
Expand All @@ -21,39 +41,35 @@ const getStation = (station, callback) => {
return
}
const stationData = {}
fetch(
`https://api.tidesandcurrents.noaa.gov/mdapi/prod/webapi/stations/${station}/harcon.json?units=metric`
)
.then((response) => {
return response.json()
})
.then((harmonics) => {
stationData.harmonics = harmonics
return fetch(
`https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?date=recent&station=${station}&product=predictions&datum=MTL&time_zone=gmt&units=metric&format=json`
)
})
.then((response) => {
return response.json()
})
.then((levels) => {
stationData.levels = levels
return fetch(
`https://api.tidesandcurrents.noaa.gov/mdapi/prod/webapi/stations/${station}/datums.json?units=metric`
)
const tasks = []

tasks.push(
makeRequest(
`https://api.tidesandcurrents.noaa.gov/mdapi/prod/webapi/stations/${station}/harcon.json?units=metric`
).then((data) => {
stationData.harmonics = data
})
.then((response) => {
return response.json()
)

tasks.push(
makeRequest(
`https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?date=recent&station=${station}&product=predictions&datum=MTL&time_zone=gmt&units=metric&format=json`
).then((data) => {
stationData.levels = data
})
.then((info) => {
stationData.info = info
fs.writeFile(filePath, JSON.stringify(stationData), (error) => {
if (error) {
throw new Error('Cannot write to test cache')
}
callback(stationData)
})
)

tasks.push(
makeRequest(
`https://api.tidesandcurrents.noaa.gov/mdapi/prod/webapi/stations/${station}/datums.json?units=metric`
).then((data) => {
stationData.info = data
})
)

Promise.all(tasks).then(() => {
callback(stationData)
})
}

describe('Results compare to NOAA', () => {
Expand Down
25 changes: 0 additions & 25 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4491,13 +4491,6 @@ natural-compare@^1.4.0:
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
integrity sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=

node-fetch@^2.6.0:
version "2.6.7"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad"
integrity sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==
dependencies:
whatwg-url "^5.0.0"

node-int64@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"
Expand Down Expand Up @@ -5563,11 +5556,6 @@ tr46@^2.1.0:
dependencies:
punycode "^2.1.1"

tr46@~0.0.3:
version "0.0.3"
resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a"
integrity sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=

tsconfig-paths@^3.11.0:
version "3.11.0"
resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-3.11.0.tgz#954c1fe973da6339c78e06b03ce2e48810b65f36"
Expand Down Expand Up @@ -5775,11 +5763,6 @@ walker@^1.0.7:
dependencies:
makeerror "1.0.x"

webidl-conversions@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871"
integrity sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE=

webidl-conversions@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-5.0.0.tgz#ae59c8a00b121543a2acc65c0434f57b0fc11aff"
Expand Down Expand Up @@ -5816,14 +5799,6 @@ whatwg-mimetype@^2.3.0:
resolved "https://registry.yarnpkg.com/whatwg-mimetype/-/whatwg-mimetype-2.3.0.tgz#3d4b1e0312d2079879f826aff18dbeeca5960fbf"
integrity sha512-M4yMwr6mAnQz76TbJm914+gPpB/nCwvZbJU28cUD6dR004SAxDLOOSUaB1JDRqLtaOV/vi0IC5lEAGFgrjGv/g==

whatwg-url@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-5.0.0.tgz#966454e8765462e37644d3626f6742ce8b70965d"
integrity sha1-lmRU6HZUYuN2RNNib2dCzotwll0=
dependencies:
tr46 "~0.0.3"
webidl-conversions "^3.0.0"

whatwg-url@^8.0.0, whatwg-url@^8.5.0:
version "8.7.0"
resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-8.7.0.tgz#656a78e510ff8f3937bc0bcbe9f5c0ac35941b77"
Expand Down

0 comments on commit fc162dc

Please sign in to comment.