Skip to content

TechmongersNL/coders-network-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

The Techmongers Coders Network API

Introduction

The Techmongers Coders Network API is just a simple REST API. Sadly, it does not have a UI yet. Will you help us make it? :)

How-to

We've documented all the available endpoints below. Each endpoint has two examples, one that you can use with HTTPie in your terminal, and one that you can use directly in your DevTools. We encourage you to do this!

Making requests

We recommend you use axios to make your requests. Some request examples are:

// inside an async function
try {
  // Simple GET request
  const response = await axios.get(
    "https://coders-network-api.onrender.com/hello"
  );
  console.log(response.data);
} catch (error) {
  console.log(error.message);
}

// All these should also be wrapped in async functions and try/catch blocks:
// POST request
const response = await axios.post(
  "https://coders-network-api.onrender.com/login",
  { email: "[email protected]", password: "somepassword" } // these are the body parameters
);

// Authorized post request
const response = await axios.post(
  "https://coders-network-api.onrender.com/posts",
  { title: "My new post", content: "lorem ipsum" },
  {
    headers: {
      Authorization: "Bearer <yourJWTtoken>", // Setting the JWT header
    },
  }
);

If we want to avoid having to repeat the first part of the URL many times (what is sometimes called the baseURL) we can create a preconfigured axios instance. To do this you can create an axios.js file, maybe inside the /store folder in your frontend. In this file you can do the following:

// /src/store/axios.js
import axios from "axios";

export default axios.create({
  baseURL: "https://coders-network-api.onrender.com",
});

Then from another file (like the actions files):

import axios from "../axios"; // Import axios from our file, not the library

export const fetchPosts = () => async (dispatch, getState) => {
  try {
    const response = await axios.get("/posts");
    console.log(response.data);
  } catch (e) {
    console.log(e.message);
  }
};

Hello world

The simplest endpoint of all, just to see if everything's still working.

  • Example

  • HTTPie:

    http -v GET https://coders-network-api.onrender.com/hello

  • JavaScript:

    axios
      .get("https://coders-network-api.onrender.com/hello")
      .then((response) => console.log("data", response.data))
      .catch((err) => console.log("err", err));
  • Response:

    {
      message: "Hello world!";
    }

Entity types

The API knows of the following entity types:

  • Developers
    • Technologies
  • Posts
    • Tags
    • Comments
    • Likes

To give you a feeling of what these objects look like, here are some TypeScript definitions for them:

interface Developer_Meta {
  id: number;
  name: string;
  email: string;
}

interface Developer extends Developer_Meta {
  github_username: null | string;
  website: null | string;
  posts: Post_Meta[];
  technologies: Technology[];
  createdAt: string;
}

interface Technology {
  id: number;
  title: string;
}

interface Post_Meta {
  id: number;
  title: string;
  tags: Tag[];
  post_likes: Like[];
  createdAt: string;
  updatedAt: string;
}

interface Post extends Post_Meta {
  content: string;
}

interface Like {
  developer: Developer_Meta;
  createdAt: string;
}

interface Comment {
  id: number;
  text: string;
  developer: Developer_Meta;
  createdAt: string;
  updatedAt: string;
}

Signup, login, authentication

We use JSON Web Tokens for authentication. Some endpoints are authenticated, which means you can only get data from them if you're logged in.

  • To login, you want to POST to /login with your email and password, and then you get a JWT back.
  • To access an authenticated endpoint after you've logged in, you have to send the JWT along as a header. We have a simple test authenticated endpoint at /me.
  • To start off, for testing purposes only, you can use the "fake" (and misformed) JWT faketokenforkelley238765293, which will always let you login as user #1.

Signup POST /signup

  • HTTPie:

    http -v POST https://coders-network-api.onrender.com/signup name="Kelley van Evert" [email protected] password=abcd

  • JavaScript:

    axios
      .post("/signup", {
        name: "Kelley van Evert",
        email: "[email protected]",
        password: "abcd",
      })
      .then((data) => console.log("data", data))
      .catch((err) => console.log("err", err));
  • Response:

    {
      jwt: string;
    }

Login POST /login

  • HTTPie:

    http -v POST https://coders-network-api.onrender.com/login [email protected] password=abcd

  • JavaScript:

    axios
      .post("/login", {
        email: "[email protected]",
        password: "abcd",
      })
      .then((data) => console.log("data", data))
      .catch((err) => console.log("err", err));
  • Response:

    {
      jwt: string;
    }

Check whether authenticated / get own profile GET /me

  • HTTPie:

    http -v GET https://coders-network-api.onrender.com/me Authorization:"Bearer JWT"

  • JavaScript:

    axios
      .get("/me", { headers: { Authorization: "Bearer <yourJWTtoken>" } })
      .then((data) => console.log("data", data))
      .catch((err) => console.log("err", err));
  • Response:

    Developer

Posts

Get a single post GET /posts/:id

  • Example

  • HTTPie:

    http -v GET https://coders-network-api.onrender.com/posts/1

  • JavaScript:

    axios
      .get(`/posts/1`)
      .then((data) => console.log("data", data))
      .catch((err) => console.log("err", err));
  • Response:

    Post

Like a post POST /posts/:id/likes

This is an authenticated API endpoint.

  • HTTPie:

    http -v POST https://coders-network-api.onrender.com/posts/1/likes Authorization:"Bearer JWT"

  • JavaScript:

    axios
      .post(
        `/posts/1/likes`,
        {}, // empty body object, no body parameters are sent.
        { headers: { Authorization: "Bearer <yourJWTtoken>" } }
      )
      .then((data) => console.log("data", data))
      .catch((err) => console.log("err", err));
  • Response:

    {
      ok: true;
    }

Unlike a post DELETE /posts/:id/likes

This is an authenticated API endpoint.

  • HTTPie:

    http -v DELETE https://coders-network-api.onrender.com/posts/1/likes Authorization:"Bearer JWT"

  • JavaScript:

    axios
      .delete(`/posts/1/likes`, {
        headers: { Authorization: "Bearer <yourJWTtoken>" },
      })
      .then((data) => console.log("data", data))
      .catch((err) => console.log("err", err));
  • Response:

    {
      ok: true;
    }

Get a post's comments GET /posts/:id/comments

  • Example

  • HTTPie:

    http -v GET https://coders-network-api.onrender.com/posts/1/comments

  • JavaScript:

    axios
      .get(`/posts/1/comments`)
      .then((data) => console.log("data", data))
      .catch((err) => console.log("err", err));
  • Response:

    {
      count: number;
      rows: Comment[];
    }

Add a comment POST /posts/:id/comments

This is an authenticated API endpoint. The new comment is made in the name of the user currently logged in.

  • HTTPie:

    http -v POST https://coders-network-api.onrender.com/posts/1/comments Authorization:"Bearer JWT" text="Love it!"

  • JavaScript:

    axios
      .post(
        `/posts/1/comments`,
        {
          text: "Love it!",
        },
        {
          headers: {
            Authorization: "Bearer <yourJWTtoken>",
          },
        }
      )
      .then((data) => console.log("data", data))
      .catch((err) => console.log("err", err));
  • Response:

    Comment

Get a list of posts GET /posts

Paginated with the optional offset and limit query parameters.

  • Example

  • HTTPie:

    http -v GET "https://coders-network-api.onrender.com/posts?offset=1&limit=2"

  • JavaScript:

    axios
      .get(`/posts?offset=1&limit=2`)
      .then((data) => console.log("data", data))
      .catch((err) => console.log("err", err));
  • Response:

    {
      count: number;
      rows: Post[];
    }

Get a list of posts (by tag) GET /posts?tag=XYZ

Paginated with the optional offset and limit query parameters.

  • Example

  • HTTPie:

    http -v GET "https://coders-network-api.onrender.com/posts?tag=github"

  • JavaScript:

    axios
      .get(`/posts?tag=github`)
      .then((data) => console.log("data", data))
      .catch((err) => console.log("err", err));
  • Response:

    {
      count: number;
      rows: Post[];
    }

Get a list of posts (by author) GET /posts?author=ID

Paginated with the optional offset and limit query parameters.

  • Example

  • HTTPie:

    http -v GET "https://coders-network-api.onrender.com/posts?author=1"

  • JavaScript:

    axios
      .get(`/posts?author=2`)
      .then((data) => console.log("data", data))
      .catch((err) => console.log("err", err));
  • Response:

    {
      count: number;
      rows: Post[];
    }

Create a new post POST /posts

This is an authenticated API endpoint. The new post is made in the name of the user currently logged in.

  • HTTPie:

    http -v POST https://coders-network-api.onrender.com/posts Authorization:"Bearer JWT" title="ABC" content="bla bla bla"

  • JavaScript:

    axios
      .post(
        "/posts",
        {
          title: "ABC",
          content: "bla bla bla",
        },
        {
          headers: { Authorization: "Bearer <yourJWTtoken>" },
        }
      )
      .then((data) => console.log("data", data))
      .catch((err) => console.log("err", err));
  • Response:

    Post

Update a post PUT /posts/:id

This is an authenticated API endpoint. The new post is made in the name of the user currently logged in.

You don't have to send all post fields. Only the included fields will be updated.

  • HTTPie:

    http -v PUT https://coders-network-api.onrender.com/posts Authorization:"Bearer JWT" title="DEF"

  • JavaScript:

    axios
      .put(
        "/posts/1",
        {
          title: "DEF",
        },
        {
          headers: { Authorization: "Bearer <yourJWTtoken>" },
        }
      )
      .then((data) => console.log("data", data))
      .catch((err) => console.log("err", err));
  • Response:

    Post

Delete a post DELETE /posts/:id

This is an authenticated API endpoint. The post must be owned by the user currently logged in.

  • HTTPie:

    http -v DELETE https://coders-network-api.onrender.com/posts/1 Authorization:"Bearer JWT"

  • JavaScript:

    axios
      .delete("/posts/1", {
        headers: { Authorization: "Bearer <yourJWTtoken>" },
      })
      .then((data) => console.log("data", data))
      .catch((err) => console.log("err", err));
  • Response:

    {
      id: number;
    }

Developers

Create a new developer's profile

Refer to the /signup endpoint above.

Get a single developer's profile GET /developers/:id

  • Example

  • HTTPie:

    http -v GET https://coders-network-api.onrender.com/developers/1

  • JavaScript:

    axios
      .get(`/developers/1`)
      .then((data) => console.log("data", data))
      .catch((err) => console.log("err", err));
  • Response:

    Developer

Get a list of developers GET /developers

Paginated with the optional offset and limit query parameters.

  • Example

  • HTTPie:

    http -v GET "https://coders-network-api.onrender.com/developers?offset=1&limit=2"

  • JavaScript:

    axios
      .get(`/developers?offset=1&limit=2`)
      .then((data) => console.log("data", data))
      .catch((err) => console.log("err", err));
  • Response:

    {
      count: number;
      rows: Developer[];
    }

Update your profile PUT /developers/:id

This is an authenticated API endpoint. You can of course only edit your own profile.

  • HTTPie:

    http -v PUT https://coders-network-api.onrender.com/developers/1 Authorization:"Bearer JWT" name="Bla" github_username="blabla"

  • JavaScript:

    axios
      .put(
        `/developers/1`,
        {
          name: "Bla",
          github_username: "blabla",
        },
        {
          headers: { Authorization: "Bearer <yourJWTtoken>" },
        }
      )
      .then((data) => console.log("data", data))
      .catch((err) => console.log("err", err));
  • Response:

    Developer

Delete your account DELETE /developers/:id

This is an authenticated API endpoint. You can of course only delete your own account.

  • HTTPie:

    http -v DELETE https://coders-network-api.onrender.com/developers/1 Authorization:"Bearer JWT"

  • JavaScript:

    axios
      .delete(`/developers/1`, {
        headers: { Authorization: "Bearer <yourJWTtoken>" },
      })
      .then((data) => console.log("data", data))
      .catch((err) => console.log("err", err));
  • Response:

    {
      id: number;
    }

About

The Official Codaisseur Coders Network API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published