dark-sky-skeleton
Advanced tools
Comparing version 0.1.22 to 0.1.41
@@ -1,7 +0,29 @@ | ||
import queryString from 'query-string'; | ||
import fetchJsonp from 'fetch-jsonp'; | ||
import fetch from 'whatwg-fetch'; | ||
'use strict'; | ||
class DarkSkySkeleton { | ||
constructor(apiKey, proxyUrl) { | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _queryString = require('query-string'); | ||
var _queryString2 = _interopRequireDefault(_queryString); | ||
var _fetchJsonp = require('fetch-jsonp'); | ||
var _fetchJsonp2 = _interopRequireDefault(_fetchJsonp); | ||
var _whatwgFetch = require('whatwg-fetch'); | ||
var _whatwgFetch2 = _interopRequireDefault(_whatwgFetch); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var DarkSkySkeleton = function () { | ||
function DarkSkySkeleton(apiKey, proxyUrl) { | ||
_classCallCheck(this, DarkSkySkeleton); | ||
this.proxyUrl = proxyUrl || ''; | ||
@@ -15,77 +37,95 @@ this.apiKey = apiKey || ''; | ||
longitude(long) { | ||
!long ? null : this._longitude = long; | ||
return this; | ||
} | ||
_createClass(DarkSkySkeleton, [{ | ||
key: 'longitude', | ||
value: function longitude(long) { | ||
!long ? null : this._longitude = long; | ||
return this; | ||
} | ||
}, { | ||
key: 'latitude', | ||
value: function latitude(lat) { | ||
!lat ? null : this._latitude = lat; | ||
return this; | ||
} | ||
latitude(lat) { | ||
!lat ? null : this._latitude = lat; | ||
return this; | ||
} | ||
/** | ||
* @param {string} time - 'YYYY-MM-DDTHH:mm:ss' | ||
*/ | ||
/** | ||
* @param {string} time - 'YYYY-MM-DDTHH:mm:ss' | ||
*/ | ||
time(time) { | ||
this._time = time; | ||
return this; | ||
} | ||
}, { | ||
key: 'time', | ||
value: function time(_time) { | ||
this._time = _time; | ||
return this; | ||
} | ||
}, { | ||
key: 'units', | ||
value: function units(unit) { | ||
!unit ? null : this.query.units = unit; | ||
return this; | ||
} | ||
}, { | ||
key: 'language', | ||
value: function language(lang) { | ||
!lang ? null : this.query.lang = lang; | ||
return this; | ||
} | ||
}, { | ||
key: 'exclude', | ||
value: function exclude(blocks) { | ||
this.query.exclude = blocks; | ||
return this; | ||
} | ||
units(unit) { | ||
!unit ? null : this.query.units = unit; | ||
return this; | ||
} | ||
// not on currently requests | ||
language(lang) { | ||
!lang ? null : this.query.lang = lang; | ||
return this; | ||
} | ||
}, { | ||
key: 'extendHourly', | ||
value: function extendHourly(param) { | ||
this.query.extend = !!param; | ||
return this; | ||
} | ||
}, { | ||
key: 'generateReqUrl', | ||
value: function generateReqUrl() { | ||
var baseUrl = this.proxyUrl ? this.proxyUrl : 'https://api.darksky.net/forecast/' + this.apiKey; | ||
this.url = baseUrl + '/' + this._latitude + ',' + this._longitude; | ||
this._time ? this.url += ',' + this._time : this.url; | ||
!this.isEmpty(this.query) ? this.url += '?' + _queryString2.default.stringify(this.query) : this.url; | ||
} | ||
}, { | ||
key: 'get', | ||
value: function get() { | ||
if (!this._latitude || !this._longitude) { | ||
return new Promise(function (resolve, reject) { | ||
reject('Request not sent. ERROR: Longitute or Latitude is missing.'); | ||
}); | ||
} | ||
this.generateReqUrl(); | ||
exclude(blocks) { | ||
this.query.exclude = blocks; | ||
return this; | ||
} | ||
var query = this.proxyUrl ? (0, _whatwgFetch2.default)(this.url) : (0, _fetchJsonp2.default)(this.url); | ||
// not on currently requests | ||
extendHourly(param) { | ||
this.query.extend = !!param; | ||
return this; | ||
} | ||
generateReqUrl() { | ||
const baseUrl = this.proxyUrl ? this.proxyUrl : `https://api.darksky.net/forecast/${this.apiKey}`; | ||
this.url = `${baseUrl}/${this._latitude},${this._longitude}`; | ||
this._time ? this.url += `,${this._time}` : this.url; | ||
!this.isEmpty(this.query) ? this.url += `?${queryString.stringify(this.query)}` : this.url; | ||
} | ||
get() { | ||
if (!this._latitude || !this._longitude) { | ||
return new Promise((resolve, reject) => { | ||
reject('Request not sent. ERROR: Longitute or Latitude is missing.'); | ||
return query.then(function (response) { | ||
return response.json(); | ||
}).then(function (json) { | ||
return json; | ||
}).catch(function (ex) { | ||
return ex; | ||
}); | ||
} | ||
this.generateReqUrl(); | ||
const query = this.proxyUrl ? fetch(this.url) : fetchJsonp(this.url); | ||
return query.then(function (response) { | ||
return response.json(); | ||
}).then(function (json) { | ||
return json; | ||
}).catch(function (ex) { | ||
return ex; | ||
}); | ||
} | ||
isEmpty(obj) { | ||
for (let key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
return false; | ||
}, { | ||
key: 'isEmpty', | ||
value: function isEmpty(obj) { | ||
for (var key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
return true; | ||
} | ||
} | ||
}]); | ||
export default DarkSkySkeleton; | ||
return DarkSkySkeleton; | ||
}(); | ||
exports.default = DarkSkySkeleton; |
{ | ||
"name": "dark-sky-skeleton", | ||
"version": "0.1.22", | ||
"description": "barebones dark sky weather api for client side js - supports api key and proxy urls", | ||
"version": "0.1.41", | ||
"description": "barebones dark sky weather api - for client or server side js", | ||
"main": "index.js", | ||
@@ -23,3 +23,4 @@ "scripts": { | ||
"forecast.io", | ||
"forecast" | ||
"forecast", | ||
"isomorphic" | ||
], | ||
@@ -34,11 +35,10 @@ "author": "Dean Verleger", | ||
"babel-cli": "^6.24.0", | ||
"babel-preset-env": "^1.3.2", | ||
"babel-preset-latest": "^6.24.1", | ||
"babel-preset-stage-1": "^6.24.1" | ||
"babel-plugin-add-module-exports": "^0.2.1", | ||
"babel-preset-env": "^1.3.2" | ||
}, | ||
"dependencies": { | ||
"fetch-jsonp": "^1.0.6", | ||
"query-string": "^4.3.2", | ||
"whatwg-fetch": "^2.0.3" | ||
"isomorphic-fetch": "^2.2.1", | ||
"query-string": "^4.3.2" | ||
} | ||
} | ||
} |
@@ -5,7 +5,7 @@ # dark-sky-skeleton | ||
A barebones wrapper library for Dark Sky API (previously known as Forecast.io). See Dark Sky developer docs: [https://darksky.net/dev/docs](https://darksky.net/dev/docs). | ||
An isomorphic barebones js wrapper library for Dark Sky API (previously known as Forecast.io). See Dark Sky developer docs: [https://darksky.net/dev/docs](https://darksky.net/dev/docs). | ||
For a more robust solution see [dark-sky-api](https://github.com/deanbot/dark-sky-api). | ||
### Install it | ||
## Install it | ||
@@ -16,6 +16,8 @@ ``` | ||
### Require it | ||
## Client Side Setup | ||
### Import it | ||
```javascript | ||
import darkSkySkeleton from 'dark-sky-skeleton'; | ||
import DarkSkySkeleton from 'dark-sky-skeleton'; | ||
``` | ||
@@ -25,14 +27,39 @@ | ||
While dark-sky-skeleton allows embedding api keys through use of jsonp on the backend using a proxy to make the api call is highly suggested as this hides the API key from client side requests [[ref](https://darksky.net/dev/docs/faq#cross-origin)]. | ||
`DarkSkySkeleton(apiKey, proxy)` | ||
* proxy url is optional | ||
* pass an empty string or false for api key if using proxy url | ||
```javascript | ||
const api = new DarkSkySkeleton('your-dark-sky-api-key'); | ||
``` | ||
#### Experimental (untested - help wanted) | ||
The above is simple and great for testing, but it exposes your api key in client side requests. Using a server-side proxy to make the actual api call to dark sky and is highly suggested as this hides the API key from client side requests [[ref](https://darksky.net/dev/docs/faq#cross-origin)]. | ||
The proxy in this would receive a request issued by dark-sky-api and attach this query to a base uri (like the following: `https://api.darksky.net/forecast/your-api-key`) prior to returning a final request. | ||
```javascript | ||
const darkSky = new darkSkySkeleton('your-dark-sky-api-key', '//base-url-to-proxy/service'); | ||
import DarkSkySkeleton from 'dark-sky-skeleton'; | ||
const api = new DarkSkySkeleton(false, '//base-url-to-proxy/service'); | ||
``` | ||
### Use it | ||
## Server Side Setup | ||
### Import it | ||
```javascript | ||
const DarkSkySkeleton = require('dark-sky-skeleton'); | ||
``` | ||
### Initialize it | ||
`DarkSkySkeleton(apiKey, proxy)` | ||
```javascript | ||
const api = new DarkSkySkeleton('your-dark-sky-api-key', true); | ||
``` | ||
Passing true as the proxy parameter indicates that the caller is server-side (_and essentially a proxy_). | ||
## Use it | ||
```javascript | ||
darkSky.latitude(lat) | ||
@@ -51,7 +78,8 @@ .longitude(long) | ||
```javascript | ||
const darkSky = new darkSkyApi('your-dark-sky-api-key'); | ||
darkSky.latitude(lat) | ||
.longitude(long); | ||
.longitude(long) | ||
.get() | ||
.then(data => console.log(data)); | ||
darkSky.get(); | ||
darkSky.get().then(data => console.log(data)); | ||
``` | ||
@@ -71,8 +99,2 @@ | ||
.then(data => console.log(data)); | ||
``` | ||
## TODO | ||
* reset query params when appropriate | ||
* add testing | ||
``` |
import queryString from 'query-string'; | ||
import fetchJsonp from 'fetch-jsonp'; | ||
import fetch from 'whatwg-fetch'; | ||
import fetch from 'isomorphic-fetch'; | ||
class DarkSkySkeleton { | ||
constructor(apiKey, proxyUrl) { | ||
this.proxyUrl = proxyUrl || ''; | ||
constructor(apiKey, proxy) { | ||
this.proxy = proxy || ''; | ||
this.apiKey = apiKey || ''; | ||
@@ -55,3 +55,3 @@ this._longitude = null; | ||
generateReqUrl() { | ||
const baseUrl = this.proxyUrl ? this.proxyUrl : `https://api.darksky.net/forecast/${this.apiKey}`; | ||
const baseUrl = this.proxy && this.proxy !== true ? this.proxy : `https://api.darksky.net/forecast/${this.apiKey}`; | ||
this.url = `${baseUrl}/${this._latitude},${this._longitude}`; | ||
@@ -74,3 +74,3 @@ this._time | ||
const query = this.proxyUrl ? fetch(this.url) : fetchJsonp(this.url); | ||
const query = this.proxy ? fetch(this.url) : fetchJsonp(this.url); | ||
@@ -95,3 +95,2 @@ return query.then(function (response) { | ||
} | ||
export default DarkSkySkeleton; |
Sorry, the diff of this file is not supported yet
11624
3
230
95
1
+ Addedisomorphic-fetch@^2.2.1
+ Addedencoding@0.1.13(transitive)
+ Addediconv-lite@0.6.3(transitive)
+ Addedis-stream@1.1.0(transitive)
+ Addedisomorphic-fetch@2.2.1(transitive)
+ Addednode-fetch@1.7.3(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedwhatwg-fetch@3.6.20(transitive)
- Removedwhatwg-fetch@^2.0.3
- Removedwhatwg-fetch@2.0.4(transitive)