Created
September 9, 2019 18:01
-
-
Save suru-dissanaike/2b8fab7073b4e50397955991a12a82ec to your computer and use it in GitHub Desktop.
API to get your Medium posts made for Google Cloud Functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const functions = require('firebase-functions'); | |
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; | |
var xmlParser = require('fast-xml-parser'); | |
const POSTS_URL = 'https://medium.com/feed/@suru.dissanaike'; | |
const FetchData = (response) => { | |
var request = new XMLHttpRequest(); | |
request.onreadystatechange = () => { | |
if (request.readyState == 4 && request.status == 200) { | |
let xmlData = request.responseText; | |
if (xmlParser.validate(xmlData) === true) { //optional (it'll return an object in case it's not valid) | |
var jsonObj = xmlParser.parse(xmlData); | |
response.send(jsonObj.rss.channel.item) | |
} | |
} | |
} | |
request.open("GET", POSTS_URL, true); | |
request.send(); | |
} | |
exports.getMediumPosts = functions.https.onRequest((request, response) => { | |
response.set('Access-Control-Allow-Origin', "*") | |
response.set('Access-Control-Allow-Methods', 'GET, POST') | |
FetchData(response) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment