Giter Club home page Giter Club logo

notesapi's Introduction

Notes API Framework

This API framework serves as my entry for a code challenge I joined in. It's a simple Notes API framework with CRUD functionality, which is commonly the usual initial work in projects.

Getting Started

Pre-requisites

This project needs NodeJS to be installed: https://nodejs.org/en/download

Installation

Clone the repository using the command below:

git clone https://github.com/swingspringer/NotesAPI.git

Go to the project directory

  cd NotesAPI
  npm install

Running Locally

This will run the app with hot-reload functionality using nodemon.

npm run dev

API Documentation

POST /notes

Create a new note.

Request

{
    "note": "sample note message"
}

Response (201 Created)

{
    "noteId": 1
}

Response (400 Bad Request)

Will be returned if note is not present in the request body

{
    "errorMessage": "'note' is a required field for this request"
}

GET /notes

Retrieve all notes.

Response (200 Success)

[
    {
        "noteId": 1,
        "note": "sample note message 1"
    },
    {
        "noteId": 2,
        "note": "sample note message 2"
    }
]

GET /notes/<id>

Retrieve a specific note by ID.

Parameters

Name Value
id Represents the noteId. Can be retrieved from POST /notes or GET /notes

Response (200)

{
    "noteId": 1,
    "note": "sample note message 1"
}

Response (404)

Will return if the noteId is not found

PUT /notes/<id>

Update a specific note.

Parameters

Name Value
id Represents the noteId. Can be retrieved from POST /notes or GET /notes

Request

{
    "note": "Updated: sample note message 1"
}

Response (204 No Content)

Would return empty body upon successful update

Based on: https://http.dev/put

Response (404 Not Found)

Will return if the noteId is not found

Response (400 Bad Request)

Will be returned if note is not present in the request body

{
    "errorMessage": "'note' is a required field for this request"
}

DELETE /notes/:id

Delete a specific note.

Parameters

Name Value
id Represents the noteId. Can be retrieved from POST /notes or GET /notes

Response (204 No Content)

Would return empty body upon successful delete

Based on: https://http.dev/delete

Response (404)

Will return if the noteId is not found

Architecture Decisions and Assumptions

This is an approach with a mixture of my experience with Java and Typescript, but of course following the standards of coding in NodeJS at the same time the design pattern of functional-singleton approach.

The project contains the following folders:

@Types

Contains the types to be used in the project.

Controllers

This will contain the major controller of the business logic for our APIs. In Java usually this is called the Service Layer. It seems the best approach for this is to use Class with static methods. Implementing an interface otherwise will require a Java-like approach where dependency must be injected and each class needs to be injected, so this is more of a functional programming approach.

DB

Will contain the approach for the pseudo-database. It is an in-memory approach where everything is stored in a JSON object, or in DSA-terms, in a dictionary.

In this case an interface is used. This approach requires an instance to be created, and it is declared inside DB/index.ts.

This pseudo-database will allow us to easily change the implementation once we need to push it to a database. We simply need to implement the INotesService and setup the methods required.

Handlers

Will contain the handlers for converting headers, params, and request bodies into a model we can easily pass around the application. This could be easily done in the controller but dividing the chore into a different handler will improve separation of concerns and will improve readability and maintanability over time.

Middleware

Equivalent to Interceptors/Filters in Java. Requests will pass through the middleware first before going into the controllers. There were no hard requirements for request body in this challenge so I created a simple NoteValidator middleware where it will simply return a 400 Bad Request if the payload is incorrect.

Routes

Will contain the routes for express. An extensible BaseRouter.ts is created so further routes will simply need to extend from this and the router object will automatically be created from scratch.

notesapi's People

Contributors

jbiglangawa avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.