Skip to content

Instantly share code, notes, and snippets.

@Ramko9999
Last active June 2, 2020 05:15
Show Gist options
  • Select an option

  • Save Ramko9999/975f0cfc2d87611098c5285d5a53aa80 to your computer and use it in GitHub Desktop.

Select an option

Save Ramko9999/975f0cfc2d87611098c5285d5a53aa80 to your computer and use it in GitHub Desktop.
import fetch from "node-fetch";
const url = "http://localhost:5000/"
function getComments(commentIds){
let requestUrl = url + "comments";
let response = await fetch(requestUrl,
{
body: JSON.stringify(
{comments: commentIds}
)
});
let json = await response.json();
if(json.error){
throw Error("Failed to retrieve the comments");
}
return json;
}
function getPosts(postIds){
let requestUrl = url + "posts";
let response = await fetch(requestUrl,
{
body: JSON.stringify(
{posts: postIds}
)
});
let json = await response.json();
if(json.error){
throw Error("Failed to retrieve the posts");
}
return json;
}
function toggleLikeOnPost(post, user, action){
let requestUrl = url + "toggle-like-post";
let response = await fetch(requestUrl,
{
method: "POST",
body: JSON.stringify(
{post: post, user:user, action:action}
)
});
let json = await response.json();
if(json.error){
throw Error("Failed to toggle like on post");
}
return json;
}
function toggleLikeOnComment(comment, user, action){
let requestUrl = url + "toggle-like-comment";
let response = await fetch(requestUrl,
{
method: "POST",
body: JSON.stringify(
{comment: comment, user:user, action:action}
)
});
let json = await response.json();
if(json.error){
throw Error("Failed to toggle like on comment");
}
return json;
}
function createPost(user, post){
let requestUrl = url + "create-post";
let response = await fetch(requestUrl,
{
method:"POST",
body: JSON.stringify(
{user:user, post:post}
)
});
let json = await response.json();
if(json.error){
throw Error("Failed to create post");
}
return json;
}
function createChild(user, parent, comment, type){
let requestUrl = url + "create-child";
let response = await fetch(requestUrl,
{
method: "POST",
body: JSON.stringify(
{user:user, parent:parent, comment:comment,type:type.toUpperCase()}
)
});
let json = await response.json();
if(json.error){
throw Error("Failed to create comment");
}
return json;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment