dark-sky-api
A simple and robust wrapper library for Dark Sky API (previously known as Forecast.io).
Features:
- Simple to use.
- Promise based (es6-promises).
- Lightweight browser location checking (by default).
- Versatile - use it statically or instantiate it.
- Dates returned as moments.
- Excludes are used automatically to reduce latency and save cache space (see 'Request Parameters').
See Dark Sky developer docs: https://darksky.net/dev/docs.
Need something even smaller? Dark sky api uses dark-sky-skeleton.
Install it
npm install dark-sky-api
Require it
import DarkSkyApi from 'dark-sky-api';
Configure it statically (suggested)
Configuring dark-sky-api with an api key is supported but each request will expose said api key (for anyone to capture).
For this reason Dark Sky strongly suggests hiding your API key through use of a proxy [ref].
DarkSkyApi.apiKey = 'your-dark-sky-api-key';
DarkSkyApi.proxyUrl = '//base-url-to-proxy/service';
DarkSkyApi.units = 'si';
DarkSkyApi.language = 'de';
DarkSkyApi.postProcessor = (item) => {
item.day = item.dateTime.format('ddd');
return item;
}
Use it
Today's weather:
DarkSkyApi.loadCurrent()
.then(result => console.log(result));
Forecasted week of weather:
DarkSkyApi.loadForecast()
.then(result => console.log(result));
Specific time request:
DarkSkyApi.loadTime('2000-04-06T12:20:05')
.then(result => console.log(result));
What about geo location?
By default dark-sky-api will use Geolocation.getCurrentPosition to grab the current browser location automatically.
To manually set geolocation position pass along a position object:
const position = {
latitude: 43.075284,
longitude: -89.384318
};
DarkSkyApi.loadCurrent(position)
.then(result => console.log(result));
Retrieve the current location from the browser
let position;
DarkSkyApi.loadPosition()
.then(pos => {
position = pos;
});
Response units
To get the units used in dark sky api responses per configured unit type (default is 'us') use GetResponseUnits
after configuration. Keep in mind that the units would need to be retrieved again if you changed the api units.
const responseUnits = DarkSkyApi.getResponseUnits();
DarkSkyAPi.loadCurrent()
.then((data) => {
console.log(`The temperature is ${data.temperature} degrees ${responseUnits.temperature}`);
console.log(`The wind speed is ${data.windSpeed} ${responseUnits.windSpeed}`);
});
Extend Hourly
Use extendHourly
to return hour-by-hour data for the next 168 hours, instead of the next 48.
DarkSkyApi.extendHourly(true);
DarkSkyApi.loadForecast()
.then(console.log);
DarkSkyApi.extendHourly(false);
Post Processor
The post processor method is mapped to all weather items. It's an easy way to add or manipulate responses for an app.
import DarkSkyApi from 'dark-sky-api';
DarkSkyApi.apiKey = 'my-api-key';
DarkSkyApi.postProcessor = (item) => {
item.dayNice = item.dateTime.calendar(null, {
sameDay: '[Today]',
nextDay: 'ddd',
nextWeek: 'ddd',
lastDay: '[Yesterday]',
lastWeek: '[Last] ddd',
sameElse: 'ddd'
});
item.units = DarkSkyApi.getResponseUnits();
return item;
};
DarkSkyApi.loadCurrent()
.then(data => console.log(data.dayNice));
Time Machine request
To retrieve weather data for a specfic point in time use loadTime
. See docs for more info.
DarkSkyApi.loadTime(time, position);
Time can be a moment or a formatted date string.
const time = moment().year(2000);
DarkSkyApi.loadTime(time)
.then(result => console.log(result));
use setTime
and call without time parameter
DarkSkyApi.setTime('2000-04-06T12:20:05');
DarkSkyApi.loadTime()
.then(result => console.log(result));
Hourly, Minutely, Alerts, and Flags
To retrieve any of these results use loadItAll with optional excludesBlock. ExcludesBlock indicates which data points to omit.
DarkSkyApi.loadItAll(excludes, position);
DarkSkyApi.loadItAll()
.then(console.log);
DarkSkyApi.loadItAll('daily,hourly,minutely,flags')
Initialization / Configuration
Tldr: Initialization of the api is automatic, but configure before making api calls.
Static configuration settings such as apiKey, proxyUrl, units, language, and postProcessor are set prior to initialization (configuration phase), locked in during initalization (implicit or explicit), and can be changed after initialization.
Implicit (suggested)
This happens automatically when making a method call such as loadCurrent, loadForecast or loadItAll. Remember to configure beforehand.
DarkSkyApi.apiKey = 'my-api-key';
DarkSkyApi.loadCurrent();
Explicit
DarkSkyApi.apiKey = 'my-api-key';
DarkSkyApi.initialize();
or
DarkSkyApi.initialize(apiKey, proxyUrl, units, language, postProcessor);
Change/set configuration after initialization/use
It's possible to change units, language, postProcessor, extendHourly, and time after initialization. Note: calling any of the static set[Config]
methods will initialize the api so make sure you've added a proxy url or api key before using them.
DarkSkyApi.apiKey = 'my-api-key';
DarkSkyApi.loadCurrent();
DarkSkyApi.setUnits('auto');
DarkSkyApi.setLanguage('x-pig-latin');
DarkSkyApi.setPostProcessor((item) => {
return {
temperature: item.temperatureMax || item.temperature,
icon: item.icon
};
});
DarkSkyApi.setTime('2000-04-06T12:20:05');
DarkSkyApi.extendHourly(true);
Creating an instance
If you need to maintain multiple instances (configurations) of dark-sky-api create an instance.
import DarkSkyApi from 'dark-sky-api';
const api = new DarkSkyApi(apiKey, proxyUrl, units, language, processor);
api.units('us')
.language('en')
.postProcessor(item => {
item.newProp = val;
return item;
})
.loadCurrent()
.then(console.log);
api.extendHourly(true)
.loadForecast()
.then(console.log);
api.extendHourly(false)
.loadForecast()
.then(console.log);
position = {
latitude: 43.075284,
longitude: -89.384318
};
api.position(position)
.loadCurrent()
.then(console.log);
api.loadPositionAsync()
.then(position => api.position(position));
api.time('2000-04-06T12:20:05')
.loadTime()
.then(console.log);
api.loadTime('2000-04-06T12:20:05')
.then(console.log)
To Do
- add position validation
- add hourly and minutely api methods
- add flags and alerts api methods
- add tests