Skip to content

Instantly share code, notes, and snippets.

@apricote
Last active January 10, 2018 13:20
Show Gist options
  • Save apricote/a09d4e7f4fd74225f5173be0b19fdf27 to your computer and use it in GitHub Desktop.
Save apricote/a09d4e7f4fd74225f5173be0b19fdf27 to your computer and use it in GitHub Desktop.
Tweeter Sample API

Tweeter Nodejs Project

Steps

Inspect the API Specification

You can find the specification in the file api.json, though I would recommend to use a UI for inspecting it: OpenApiRenderer

Sketch the Database schema

Based on the API Specification, define a database schema to save the data permanently.

Setup a Nodejs Project

Use the npm init Command to initialize a nodejs project in this folder.

Develop the API

The most standard way to develop an API in node these days is the express framework. Google for a guide and start hacking on the API.

I would recommand you to start with the simple Level 1 routes, and advance after that.

For storing the data you have multiple options:

  1. Relational Database (mysql/mariadb)
  2. Nosql Database (mongo)

You may use whatever you feel most comfortable with.

{
"openapi": "3.0.0",
"servers": [
{
"url": "https://tweeter.dev.narando.com/"
}
],
"info": {
"title": "tweeter API",
"description": "a simple twitter clone",
"version": "1.0.0"
},
"paths": {
"/tweets": {
"get": {
"summary": "returns all tweets",
"tags": ["Tweets", "Level 1"],
"responses": {
"200": {
"description": "Tweets could be returned",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Tweet"
}
}
}
}
}
}
},
"post": {
"summary": "create a new tweet",
"tags": ["Tweets", "Level 1"],
"responses": {
"201": {
"description": "Tweet created",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Tweet"
}
}
}
}
}
}
},
"/tweets/{id}": {
"get": {
"summary": "return detailed information for a single tweet",
"tags": ["Tweets", "Level 2"],
"responses": {
"200": {
"description": "Tweet could be retrieved",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Tweet"
}
}
}
},
"404": {
"description": "Tweet could not be found"
}
}
},
"delete": {
"summary": "delete a tweet",
"tags": ["Tweets", "Level 2"],
"responses": {
"204": {
"description": "Tweet got deleted"
}
}
}
},
"/users/{id}": {
"get": {
"summary": "return detailed information for a single user",
"tags": ["Users", "Level 3"],
"responses": {
"200": {
"description": "User could be retrieved",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/User"
}
}
}
},
"404": {
"description": "User could not be found"
}
}
}
},
"/users/{id}/follow": {
"post": {
"summary": "follow this user",
"tags": ["Users", "Level 3"],
"responses": {
"201": {
"description":
"Following was successful. Returns the users you are currently following",
"content": {
"application/json": {
"schema": {
"type": "array",
"schema": "#/components/schemas/User"
}
}
}
},
"403": {
"description": "There is no logged in user"
},
"404": {
"description": "User could not be found"
}
}
},
"delete": {
"summary": "unfollow this user",
"tags": ["Users", "Level 3"],
"responses": {
"204": {
"description": "Unfollowing was successful"
},
"403": {
"description": "There is no logged in user"
},
"404": {
"description": "User could not be found"
}
}
}
},
"/dashboard": {
"get": {
"summary":
"shows tweets from the users you follow. If there is no logged in user, show most recent tweets",
"tags": ["Users", "Tweets", "Level 4"],
"responses": {
"default": {
"description": "Default response"
}
}
}
}
},
"components": {
"schemas": {
"User": {
"type": "object",
"properties": {
"id": {
"type": "number"
},
"name": {
"type": "string"
},
"username": {
"type": "string"
},
"tweets": {
"type": "array",
"optional": true,
"items": {
"$ref": "#/components/schemas/Tweet"
}
}
}
},
"Tweet": {
"type": "object",
"properties": {
"id": {
"type": "number"
},
"author": {
"$ref": "#/components/schemas/User"
},
"content": {
"type": "string"
},
"time": {
"type": "string",
"format": "date-time"
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment