newsapi-wrapper
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -10,12 +10,12 @@ const request = require('request'); | ||
const categories = { | ||
'business': true, | ||
'entertainment': true, | ||
'general': true, | ||
'health': true, | ||
'science': true, | ||
'sports': true, | ||
'technology': true | ||
business: true, | ||
entertainment: true, | ||
general: true, | ||
health: true, | ||
science: true, | ||
sports: true, | ||
technology: true | ||
}; | ||
const isAllowedCategory = (cat) => { | ||
const isAllowedCategory = cat => { | ||
return categories[cat] === true; | ||
@@ -25,6 +25,6 @@ }; | ||
const settings = { | ||
'apiKey': true, | ||
'country': true, | ||
'pageSize': true, | ||
'category': true | ||
apiKey: true, | ||
country: true, | ||
pageSize: true, | ||
category: true | ||
}; | ||
@@ -42,7 +42,11 @@ | ||
} else { | ||
throw new Error(`Unknown setting "${settingName}". Known setting names: ${Object.keys(settings)}`); | ||
throw new Error( | ||
`Unknown setting "${settingName}". Known setting names: ${Object.keys( | ||
settings | ||
)}` | ||
); | ||
} | ||
}; | ||
const setDefaults = (obj) => { | ||
const setDefaults = obj => { | ||
for (let p of Object.keys(obj)) { | ||
@@ -58,3 +62,2 @@ setDefault(p, obj[p]); | ||
class Request { | ||
constructor(opts) { | ||
@@ -94,3 +97,5 @@ this.options = {}; | ||
if (!this.options.apiKey) { | ||
throw new Error('Request should be sent but no API key given for News API. Please use setKey() to provide it.'); | ||
throw new Error( | ||
"Request should be sent but no API key given for News API. Please use setApiKey(key) or setDefault('apiKey', key) to provide it." | ||
); | ||
} | ||
@@ -112,4 +117,4 @@ | ||
cb(error, response, body); | ||
} | ||
} | ||
let obj; | ||
@@ -122,3 +127,3 @@ try { | ||
} | ||
if (response.statusCode === 200) { | ||
@@ -128,7 +133,18 @@ if (obj.status === STATUS_OK) { | ||
} else { | ||
reject(new Error('News API request failed. Reason: ' + obj.message)); | ||
reject( | ||
new Error( | ||
'News API request failed. Reason: ' + | ||
obj.message | ||
) | ||
); | ||
} | ||
} else { | ||
let msg = 'News API request failed. HTTP code: ' + response.statusCode + ', message: '; | ||
msg += obj && obj.message ? obj.message : response.statusMessage; | ||
let msg = | ||
'News API request failed. HTTP code: ' + | ||
response.statusCode + | ||
', message: '; | ||
msg += | ||
obj && obj.message | ||
? obj.message | ||
: response.statusMessage; | ||
reject(new Error(msg)); | ||
@@ -139,13 +155,13 @@ } | ||
} | ||
}; | ||
} | ||
const createRequest = (options) => { | ||
const createRequest = options => { | ||
return new Request(options); | ||
}; | ||
const setApiKey = apiKey => createRequest().setApiKey(apiKey); | ||
const setApiKey = apiKey => createRequest().setApiKey(apiKey); | ||
const setCategory = category => createRequest().setCategory(category); | ||
const setPageSize = pageSize => createRequest().setPageSize(pageSize); | ||
const setCountry = country => createRequest().setCountry(country); | ||
const send = options => createRequest(options).send(); | ||
const setCountry = country => createRequest().setCountry(country); | ||
const send = options => createRequest(options).send(); | ||
@@ -152,0 +168,0 @@ module.exports = { |
{ | ||
"name": "newsapi-wrapper", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "A wrapper around the News API (https://newsapi.org/) that exposes a Promise-like interface", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -10,15 +10,16 @@ # newsapi-wrapper | ||
newsapi | ||
.setCountry('ca') | ||
.setCategory('sports') | ||
.setPageSize(25) | ||
.send() | ||
.then(response => { | ||
console.log(`Results: ${response.totalResults}`); | ||
response.articles.forEach(article => { | ||
console.log(article.title); | ||
}); | ||
}) | ||
.catch(err => { | ||
console.log(err); | ||
}); | ||
.setApiKey('xxxxyyyzzz') | ||
.setCountry('ca') | ||
.setCategory('sports') | ||
.setPageSize(25) | ||
.send() | ||
.then(response => { | ||
console.log(`Results: ${response.totalResults}`); | ||
response.articles.forEach(article => { | ||
console.log(article.title); | ||
}); | ||
}) | ||
.catch(err => { | ||
console.log(err); | ||
}); | ||
@@ -57,17 +58,2 @@ ## Installation | ||
### request (deprecated) | ||
Returns an object you can use to build a request, send it, and process the result. Intended to used as a chain of method calls: | ||
newsapi.request() | ||
.setCategory('health') | ||
.setPageSize(15) | ||
.send(); | ||
request() is now optional and considered deprecated, because you can call all the request methods on newsapi directly. The above example would then look like this: | ||
newsapi.setCategory('health') | ||
.setPageSize(15) | ||
.send(); | ||
### setCategory | ||
@@ -111,1 +97,16 @@ | ||
### request (deprecated) | ||
Returns an object you can use to build a request, send it, and process the result. Intended to used as a chain of method calls: | ||
newsapi.request() | ||
.setCategory('health') | ||
.setPageSize(15) | ||
.send(); | ||
request() is now optional and considered deprecated, because you can call all the request methods on newsapi directly. The above example would then look like this: | ||
newsapi.setCategory('health') | ||
.setPageSize(15) | ||
.send(); | ||
9748
7
177
110