dark-sky-api
Advanced tools
Comparing version 0.6.2 to 0.6.21
@@ -42,7 +42,6 @@ 'use strict'; | ||
var DarkSkyApi = function () { | ||
// darkSkyApi; instance of dark sky skeleton | ||
// initialized; weather the instance of dark sky api has lat and long set | ||
// darkSkyApi; instance of dark-sky-skeleton | ||
// initialized; whether the instance of dark-sky-api has lat and long set | ||
// _units; | ||
// _language; | ||
// _time | ||
// _extendHourly | ||
@@ -174,5 +173,6 @@ // _postProcessor | ||
return val != 'currently'; | ||
}).join(',')).time(false).get(); | ||
// .then(res => console.log(result)) | ||
// .then(({ currently }) => this.processWeatherItem(currently)); | ||
}).join(',')).time(false).get().then(function (_ref2) { | ||
var currently = _ref2.currently; | ||
return _this.processWeatherItem(currently); | ||
}); | ||
} | ||
@@ -179,0 +179,0 @@ |
{ | ||
"name": "dark-sky-api", | ||
"version": "0.6.2", | ||
"version": "0.6.21", | ||
"description": "a simple and robust dark sky api service for client-side js", | ||
@@ -41,2 +41,2 @@ "main": "index.js", | ||
} | ||
} | ||
} |
179
README.md
# dark-sky-api | ||
A simple and robust wrapper library for Dark Sky API (previously known as Forecast.io). | ||
A simple and robust isomorphic js wrapper library for Dark Sky API (previously known as Forecast.io). | ||
@@ -10,2 +10,3 @@ Features: | ||
* Lightweight browser location checking (by default). | ||
* Isomorphic - use it client-side or server-side. | ||
* Versatile - use it statically or instantiate it. | ||
@@ -17,6 +18,8 @@ * Dates returned as [moments](https://momentjs.com/). | ||
Need something even smaller? Dark sky api uses [dark-sky-skeleton](https://github.com/deanbot/dark-sky-skeleton). | ||
Need something even smaller? dark-sky-api uses [dark-sky-skeleton](https://github.com/deanbot/dark-sky-skeleton). | ||
### Install it | ||
You can use dark-sky-api client-side __OR__ server-side. Note: an example of a server side proxy used with client side dark-sky-api is forthecoming... | ||
## Install it | ||
``` | ||
@@ -26,3 +29,3 @@ npm install dark-sky-api | ||
### Require it | ||
## Import it | ||
```javascript | ||
@@ -32,3 +35,4 @@ import DarkSkyApi from 'dark-sky-api'; | ||
or server side | ||
or Common JS | ||
```javascript | ||
@@ -38,14 +42,38 @@ const DarkSkyApi = require('dark-sky-api'); | ||
### Configure it statically (suggested) | ||
## Configure it | ||
Configuring dark-sky-api with an api key is supported but each request will expose said api key (for anyone to capture). | ||
Static configuration is suggested. | ||
For this reason Dark Sky strongly suggests hiding your API key through use of a proxy [[ref](https://darksky.net/dev/docs/faq#cross-origin)]. | ||
```javascript | ||
DarkSkyApi.apiKey = 'your-dark-sky-api-key'; | ||
``` | ||
### Proxy URL - Client-side be warned! | ||
The above is simple and great for testing, but your api key is exposed in every request (when running in client-side). Using a separate server-side proxy to make the actual api call to dark sky is highly suggested as this hides the api key. [[ref](https://darksky.net/dev/docs/faq#cross-origin)]. | ||
To use a proxy set your api-key to false or an empty string, and pass the URL of the proxy service as the proxy (second) param. | ||
```javascript | ||
// one of the two is required | ||
DarkSkyApi.proxy = '//base-url-to-proxy/service'; | ||
``` | ||
#### Experimental (help wanted) | ||
dark-sky-api theoretically supports a proxy service (aka untested). A proxy service would receive a request issued by dark-sky-api, attach this query to a base URI (like the following: `https://api.darksky.net/forecast/your-api-key`), and return a final request. | ||
### Running Server Side | ||
Along with your api key, set proxy to true. | ||
```javascript | ||
DarkSkyApi.apiKey = 'your-dark-sky-api-key'; | ||
DarkSkyApi.proxyUrl = '//base-url-to-proxy/service'; // or set to true if running server side | ||
DarkSkyApi.proxy = true; | ||
``` | ||
// optional configuration | ||
Passing true as the proxy parameter indicates that the caller is server-side. Awesome! | ||
### Optional Configuration | ||
```javascript | ||
DarkSkyApi.units = 'si'; // default 'us' | ||
@@ -59,3 +87,3 @@ DarkSkyApi.language = 'de'; // default 'en' | ||
### Use it | ||
## Use it | ||
@@ -77,2 +105,3 @@ Today's weather: | ||
Specific time request: | ||
```javascript | ||
@@ -83,6 +112,6 @@ DarkSkyApi.loadTime('2000-04-06T12:20:05') | ||
### What about geo location? | ||
By default dark-sky-api will use [Geolocation.getCurrentPosition](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition) to grab the current browser location automatically. | ||
## What about geo location? | ||
By default dark-sky-api will use [Geolocation.getCurrentPosition](https://developer.mozilla.org/en-US/docs/Web/api/Geolocation/getCurrentPosition) to grab the current browser location automatically. | ||
To manually set geolocation position pass along a position object: | ||
To manually set geolocation position pass along a position object. __This is mandatory when running dark-sky-api server-side!__ | ||
@@ -108,3 +137,3 @@ ```javascript | ||
### Response units | ||
## Response units | ||
@@ -122,3 +151,3 @@ 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. | ||
### Extend Hourly | ||
## Extend Hourly | ||
@@ -137,3 +166,3 @@ Use `extendHourly` to return hour-by-hour data for the next 168 hours, instead of the next 48. | ||
### Post Processor | ||
## Post Processor | ||
@@ -172,3 +201,3 @@ The post processor method is mapped to all weather items. It's an easy way to add or manipulate responses for an app. | ||
### Time Machine request | ||
## Time Machine request | ||
@@ -187,3 +216,3 @@ To retrieve weather data for a specfic point in time use `loadTime`. See [docs](https://darksky.net/dev/docs/time-machine) for more info. | ||
### Hourly, Minutely, Alerts, and Flags | ||
## Hourly, Minutely, Alerts, and Flags | ||
@@ -201,11 +230,60 @@ To retrieve any of these results use loadItAll with optional excludesBlock. ExcludesBlock indicates which data points to omit. | ||
### Initialization / Configuration | ||
## Creating an instance | ||
If you need to maintain multiple instances (configurations) of dark-sky-api create an instance. | ||
```javascript | ||
// import | ||
import DarkSkyApi from 'dark-sky-api'; | ||
// instantiate | ||
const api = new DarkSkyApi(apiKey, proxy, units, language, processor); // only apiKey or proxy are required | ||
// instance config methods support method chaining | ||
api.units('us') | ||
.language('en') | ||
.postProcessor(item => { | ||
item.newProp = val; | ||
return item; | ||
}) | ||
.loadCurrent() | ||
.then(console.log); | ||
// extend hourly available for forecasts | ||
api.extendHourly(true) | ||
.loadForecast() | ||
.then(console.log); | ||
// turn off extend hourly | ||
api.extendHourly(false) | ||
.loadForecast() | ||
.then(console.log); | ||
// change position | ||
position = { | ||
latitude: 43.075284, | ||
longitude: -89.384318 | ||
}; | ||
api.position(position) | ||
.loadCurrent() | ||
.then(console.log); | ||
// change back | ||
api.loadPositionAsync() // get current position | ||
.then(position => api.position(position)); | ||
// time machine request | ||
api.loadTime('2000-04-06T12:20:05') | ||
.then(console.log) | ||
``` | ||
## 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. | ||
Static configuration settings such as apiKey, proxy, 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. | ||
This happens automatically when making a method call such as loadCurrent, loadForecast, loadTime or loadItAll. Remember to configure beforehand. | ||
@@ -230,8 +308,8 @@ ```javascript | ||
```javascript | ||
DarkSkyApi.initialize(apiKey, proxyUrl, units, language, postProcessor); // only apiKey or proxyUrl are required | ||
DarkSkyApi.initialize(apiKey, proxy, units, language, postProcessor); // only apiKey or proxy are required | ||
``` | ||
#### Change/set configuration after initialization/use | ||
### 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. | ||
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 an api key or proxy before using them. | ||
@@ -256,51 +334,2 @@ ```javascript | ||
### Creating an instance | ||
If you need to maintain multiple instances (configurations) of dark-sky-api create an instance. | ||
```javascript | ||
// import | ||
import DarkSkyApi from 'dark-sky-api'; | ||
// instantiate | ||
const api = new DarkSkyApi(apiKey, proxyUrl, units, language, processor); // only apiKey or proxyUrl are required | ||
// instance config methods support method chaining | ||
api.units('us') | ||
.language('en') | ||
.postProcessor(item => { | ||
item.newProp = val; | ||
return item; | ||
}) | ||
.loadCurrent() | ||
.then(console.log); | ||
// extend hourly available for forecasts | ||
api.extendHourly(true) | ||
.loadForecast() | ||
.then(console.log); | ||
// turn off extend hourly | ||
api.extendHourly(false) | ||
.loadForecast() | ||
.then(console.log); | ||
// change position | ||
position = { | ||
latitude: 43.075284, | ||
longitude: -89.384318 | ||
}; | ||
api.position(position) | ||
.loadCurrent() | ||
.then(console.log); | ||
// change back | ||
api.loadPositionAsync() // get current position | ||
.then(position => api.position(position)); | ||
// time machine request | ||
api.loadTime('2000-04-06T12:20:05') | ||
.then(console.log) | ||
``` | ||
#### To Do | ||
@@ -307,0 +336,0 @@ |
@@ -26,7 +26,6 @@ import darkSkySkeleton from 'dark-sky-skeleton'; | ||
class DarkSkyApi { | ||
// darkSkyApi; instance of dark sky skeleton | ||
// initialized; weather the instance of dark sky api has lat and long set | ||
// darkSkyApi; instance of dark-sky-skeleton | ||
// initialized; whether the instance of dark-sky-api has lat and long set | ||
// _units; | ||
// _language; | ||
// _time | ||
// _extendHourly | ||
@@ -132,5 +131,4 @@ // _postProcessor | ||
.time(false) | ||
.get(); | ||
// .then(res => console.log(result)) | ||
// .then(({ currently }) => this.processWeatherItem(currently)); | ||
.get() | ||
.then(({ currently }) => this.processWeatherItem(currently)); | ||
} | ||
@@ -137,0 +135,0 @@ |
155642
324
1076