0.0.3 • Published 9 months ago

raments v0.0.3

Weekly downloads
-
License
ISC
Repository
github
Last release
9 months ago

Ramen - A micro framework for creating REST API in Nodejs

Ramen image i got from google lol

Just another ✨Backend API Framework✨ for typescript

The plan

Currently it's just me trying to create a javascript framework for making APIs. This is definitely inspired from the Express framework

the Server class in main.js file can be initialized which will create an HTTP server and you can use the listen function to listen to a specific port

the Request class in the Request.js file holds the request object which can be used to access all the properties for the http request.

How to use Ramen

  1. Using the annotations syntax
import Ramen, {Request, Response} from "ramen"
import { Route, Get } from "ramen/router"; 

const ramen = new Ramen();

@Route("/posts")
class PostRouter {
    @Get("/:id")
    fetchPostWithId(req: Request<{id: string}>, res: Response) {
        const postId = req.params.id;
        const post: object = fetchPost(postId);
        return res.send(post);
    }
}

ramen.appendRouter("/api", PostRouter);

ramen.listen(process.env.PORT || 3000, (_port) => {
    console.log(`[SERVER]: Running on port ${_port}`);
});
  1. Using the Router API
import Ramen, {Request, Response} from "ramen";
import Router from "ramen/router";

const ramen = new Ramen();
const postRouter = Router("/posts");

postRouter.append("/:id", "GET", (req: Request<{id: string}>, res: Response) => {
    const postId = req.params.id;
    const post: object = fetchPostById(postId);
    return res.send(post);
});

ramen.appendRouter("/posts", postRouter);
ramen.listen(process.env.PORT || 3000, (_port) => {
    console.log(`[SERVER]: Running on port ${_port}`);
});
  1. or you can simply add methods in the ramen object directly!
import Ramen, {Request, Response} from "ramen";
const ramen = new Ramen();

ramen.append("/posts/:id", "GET", (req: Request, res: Response) => {
    const postId = req.params.id;
    const post: object = fetchPostById(postId);
    return res.send(post);
});

ramen.listen(process.env.PORT || 3000, (_port) => {
    console.log(`[SERVER]: Running on port ${_port}`);
});
  • Request is a generic class with this definition

    class Request<RouteParameters = {[key: string]: string}, Body = {[key: string]: string}, QueryParams = {[key: string]: string}>

    this helps you to get good type definition in your IDE for yout request properties.

  • The Request class hold the following properties

    method: string;
    headers: IHeaders;
    _url: string;
    socket: Socket;
    queryParams: QueryParams | object;
    ip: string;
    body: Body | undefined;
    params: RouteParameters | undefined;
    cookies: { [k: string]: string };
    raw_body: string;
    locals?: { [k: string]: string };
    data_completed: boolean;
    logFunction?: (data: logInfo) => void;
    define: () => void;
  • Response class has the following properties

    cookies: (_cookies: { [k:string]: { val: string; path: string; [k:string]: string } }) => void;
    
    send: (data: number | string | object, status: number) => void;
    
    setStatus: (value: number) => void;
    
    sendFile: async (path: string) => void;
    
    render: (path: string, options: object = {}, status=200) => void;
  • The Ramen class has the following properties

    constructor: (isRouter: boolean = false, locals = {}) => new Ramen;
    
    defaultAppend: (cb: (req: Request, res: Response) => any) => void;
    
    append: (path: string, method: string = "GET", ...cb: Array<(req: Request, res: Response, next: Function) => any>) => void;
    
    appendRouter: (router: Router) => void;
    
    listen: (port: number, cb: (port: number) => void = (port: number) => {
        console.log(`SERVER RUNNING on port : ${port}`);
    }) => void;