This repository was archived by the owner on Feb 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdemo_class.ts
49 lines (39 loc) · 1.63 KB
/
demo_class.ts
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
import { DarkSky, DarkSkyOptions, Language, Units, WeekForecast } from 'darksky-api'
import * as yargs from 'yargs'
const args = yargs.argv
const KEY = (args.key as string) || process.env.DARKSKY_KEY
if (!KEY) throw Error('DARKSKY_KEY was not found in the env')
const latitude = (args.lat as number) || 42.984
const longitude = (args.long as number) || -81.245
// Optional Default options
const options: DarkSkyOptions = {
// Optional
// Anything set here can be overriden when making the request
units: Units.SI,
lang: Language.FRENCH
}
// Create the api wrapper class
const darksky = new DarkSky(KEY, options)
// Use the wrapper
/**
* Will get the weekly forecast using a helper function, it excludes all of the datablocks except
* for the `daily` one. If you need more than that you can use `DarkSky.forecast` and pass in
* an Exclude array.
*/
async function getWeeklyForecast(lat: number, lng: number): Promise<WeekForecast | undefined> {
try {
// You can pass options here to override the options set above
const result: WeekForecast = await darksky.week(lat, lng, { lang: Language.ENGLISH })
console.log(`Got forecast for ${result.latitude},${result.longitude}`)
return result
} catch (error) {
// If DarkSky API doesn't return a 'daily' data-block, then this function will throw
console.log('Unable to get the weekly forecast for the chosen location')
console.error(error)
}
}
(async () => {
const forecast = await getWeeklyForecast(latitude, longitude)
if (!forecast) return console.error('unable to get forecast')
console.log(`Forecast for tomorrow: ${forecast.daily.data[1].temperatureMax}`)
})()