medium-json-feed
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -9,5 +9,9 @@ const https = require('https'); | ||
module.exports = (endpoint = '', callback) => { | ||
const url = `https://medium.com${endpoint || '/'}${endpoint.indexOf('?') === -1 ? '?' : '&' }format=json`; | ||
module.exports = (endpoint = '/', callback) => { | ||
if (endpoint.charAt(0) !== '/') { | ||
endpoint = '/' + endpoint; | ||
} | ||
const url = `https://medium.com${endpoint}${endpoint.indexOf('?') === -1 ? '?' : '&' }format=json`; | ||
return new Promise((resolve, reject) => https.get(url, res => { | ||
@@ -14,0 +18,0 @@ if (callback && callback.write instanceof Function) { |
{ | ||
"name": "medium-json-feed", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Get Medium latest articles in JSON format", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -26,18 +26,21 @@ # Medium JSON Feed | ||
mediumJSONFeed('@myUserName') // Usernames start with '@' | ||
// Usernames start with '@' | ||
mediumJSONFeed('@my-user-name') | ||
.then(data => ...) | ||
.catch(data => ...); | ||
mediumJSONFeed('myPublicationName', data => ...); // Publication names without '@' | ||
// Publication names without '@' | ||
mediumJSONFeed('my-publication-name', data => ...); | ||
mediumJSONFeed('/', data => ...); // Medium top page (trending posts) | ||
// Medium's top page (trending posts) | ||
mediumJSONFeed().then(data => ...); | ||
``` | ||
Other endpoint examples are `@user-name/latest`, `publicationName/latest` or `publication-name/trending`. | ||
Other endpoint examples are `@user-name/latest`, `publication-name/latest` or `publication-name/trending`. | ||
The `data` response contains: | ||
* `data.status`: HTTP status code (number). | ||
* `data.error`: Error message if exists (string). | ||
* `data.response`: List of found articles (Array). The format is the one returned by Medium. Inspect `data.response[...].content` and `data.response[...].virtuals` for useful information. | ||
* `data.status`: HTTP status code (**number**). | ||
* `data.error`: Error message if exists (**string**). | ||
* `data.response`: List of found articles (**Array**). The format is the one returned by Medium. Inspect `data.response[...].content` and `data.response[...].virtuals` for useful information. | ||
@@ -47,7 +50,10 @@ To get the full raw response given by Medium, provide a stream: | ||
```javascript | ||
mediumJSONFeed('@myUserName', process.stdout); // Raw stream pipe to stdout | ||
mediumJSONFeed('@myUserName', response); // Raw stream pipe to server's response | ||
// Raw stream pipe to stdout | ||
mediumJSONFeed('@my-user-name', process.stdout); | ||
// Raw stream pipe to server's response | ||
mediumJSONFeed('@my-user-name', response); | ||
``` | ||
*Note: the raw output will likely contain random chacters at the beginning of the string that break JSON format.* | ||
***Note**: the raw output will likely contain random chacters at the beginning of the string that break JSON format.* | ||
@@ -65,2 +71,3 @@ For a full example, see `server.js` file. | ||
3. Choose GitHub deploy and select your fork. | ||
4. Hit 'deploy'. | ||
4. Set environment variables `PORT` and `ORIGIN` (for `Access-Control-Allow-Origin` header). | ||
5. Hit 'deploy'. |
@@ -5,7 +5,5 @@ const http = require('http'); | ||
const port = process.env.PORT || 3000; | ||
const respond = (res, data = {}) => { | ||
if (!res.finished) { | ||
res.writeHead(data.status || 500, { 'Content-Type': 'application/json' }); | ||
res.end(JSON.stringify(data, null, 2), 'utf-8'); | ||
} | ||
const headers = { | ||
'Content-Type': 'application/json', | ||
'Access-Control-Allow-Origin': process.env.ORIGIN || '*' | ||
}; | ||
@@ -21,5 +19,6 @@ | ||
mediumJSONFeed(req.url) | ||
.then(data => respond(res, data)) | ||
.catch(error => respond(res, error)); | ||
mediumJSONFeed(req.url, data => { | ||
res.writeHead(data.status || 500, headers); | ||
res.end(JSON.stringify(data, null, 2), 'utf-8'); | ||
}); | ||
@@ -26,0 +25,0 @@ }).listen(port); |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
6369
59
71
2