Last active
June 1, 2020 23:51
-
-
Save Ramko9999/0776f123f05e9204b2091572924c2c88 to your computer and use it in GitHub Desktop.
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 express = require("express"); | |
| const app = express(); | |
| const config = require("./config"); | |
| app.use(express.json()); | |
| /* | |
| All the parameters to the endpoints below will simply be passed as a object in the body. | |
| This is easily customizable. | |
| */ | |
| app.get("/comments", async (req, res)=>{ | |
| try{ | |
| let body = req.body; | |
| let url = config.url + "getCommentMetadata"; | |
| let response = await _makeCall(url, body); | |
| res.status(200).send(response); | |
| } | |
| catch(error){ | |
| handleError(res, error.message); | |
| } | |
| }); | |
| app.get("/posts", async (req, res) => { | |
| try{ | |
| let body = req.body; | |
| let url = config.url + "getPostMetadata"; | |
| let response = await _makeCall(url, body); | |
| res.status(200).send(response); | |
| } | |
| catch(error){ | |
| handleError(res, error.message); | |
| } | |
| }); | |
| app.post("/toggle-like-post", async (req, res) => { | |
| try{ | |
| let body = req.body; | |
| let url = config.url + "toggleLikesOnPost"; | |
| let response = await _makeCall(url, body); | |
| res.status(200).send(response); | |
| } | |
| catch(error){ | |
| handleError(res, error.message); | |
| } | |
| }); | |
| app.post("/toggle-like-comment", async (req, res) =>{ | |
| try{ | |
| let body = req.body; | |
| let url = config.url + "toggleLikesOnComment"; | |
| let response = await _makeCall(url, body); | |
| res.status(200).send(response); | |
| } | |
| catch(error){ | |
| handleError(res, error.message); | |
| } | |
| }); | |
| app.post("/create-child", async (req, res) => { | |
| try{ | |
| let body = req.body; | |
| let url = config.url; | |
| if("post" in body){ | |
| url += "createCommentOnPost"; | |
| } | |
| else{ | |
| url += "createReplyOnComment"; | |
| } | |
| let response = await _makeCall(url, body); | |
| res.status(200).send(response); | |
| } | |
| catch(error){ | |
| handleError(res, error.message); | |
| } | |
| }); | |
| app.post("/create-post", async (req, res)=>{ | |
| try{ | |
| let body = req.body; | |
| let url = config.url + "createPost"; | |
| let response = await _makeCall(url, body); | |
| res.status(200).send(response); | |
| } | |
| catch(error){ | |
| handleError(res, error.message); | |
| } | |
| }); | |
| /* | |
| This is where the magic happens. Each of the end points will pass in a query url | |
| with an object of parameters. This function will construct the url and make the call. | |
| */ | |
| async function _makeCall(url, params){ | |
| try{ | |
| const keys = Object.keys(params); | |
| if (keys.length > 0) { | |
| url += "?" | |
| for (let index = 0; index < keys.length; index++) { | |
| const key = keys[index]; | |
| if (Array.isArray(params[key])) { //It was an array of values | |
| let heading = `${key}=`; | |
| if (index > 0) { | |
| heading = `&${key}=`; | |
| } | |
| url += heading; | |
| url += (params[key]).join(`&${key}=`); | |
| } else { //It was just a single value | |
| if (index === 0) { | |
| url += key + "=" + params[key] | |
| } else { | |
| url += "&" + key + "=" + params[key] | |
| } | |
| } | |
| } | |
| } | |
| const token = config.token; | |
| const response = await fetch(url, { | |
| method: 'GET', | |
| headers: { | |
| 'Authorization': 'Bearer ' + token, | |
| 'Accept': 'application/json', | |
| 'Content-Type': 'application/json', | |
| } | |
| }); | |
| const json = await response.json(); | |
| if(json.error){ | |
| throw Error("TigerGraphError:" + json.message); | |
| } | |
| return json; | |
| } | |
| catch(error){ | |
| throw Error(error.message); | |
| } | |
| } | |
| function handleError(res, errorMessage){ | |
| if(errorMessage.indexOf("TigerGraphError") > 0){ | |
| res.status(500).send({error:true, message:errorMessage}) | |
| } | |
| else{ | |
| res.status(400).send({error:true, message:errorMessage}) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment