@serverless_api/router

0.0.1 • Public • Published

Serverless Api Router

This package is part of Serverless Api.


The router implements its routing based on the concept of a radix tree (trie).

Contents

Installation

npm install @serverless_api/router

Creating routes

import { Router, Request, Response } from '@serverless_api/router';

const router = new Router();

router.get('/api/users', (req: Request, res: Response) => {
    res.sendStauts(200);
});

router.post('/api/users/:id', (req: Request, res: Response) => {
    res.status(200).json({
        id: req.params.id,
    });
});

router.put('/api/users/:id', (req: Request, res: Response) => {
    res.status(200).json({
        id: req.params.id,
    });
});

router.patch('/api/users/:id', (req: Request, res: Response) => {
    res.status(200).json({
        id: req.params.id,
    });
});

router.delete('/api/users/:id', (req: Request, res: Response) => {
    res.status(200).json({
        id: req.params.id,
    });
});

const route = router.lookup('GET', '/api/users/1');

if (route) route.handle(req, res);

Groups

import { Router, Request, Response } from '@serverless_api/router';

const router = new Router();

router.group('/api', (api) => {
    api.group('blog', (blog) => {
        blog.get('', (req: Request, res: Response) => {
            res.status(200).json([]);
        });

        blog.get(':slug', (req: Request, res: Response) => {
            res.status(200).json({
                slug: req.params.slug,
            });
        });
    });

    api.group('admin', (admin) => {
        admin.group('blog', (blogAdmin) => {
            blogAdmin.get('', (req: Request, res: Response) => {
                res.status(200).json([]);
            });

            blogAdmin.get(':id', (req: Request, res: Response) => {
                res.status(200).json({
                    id: req.params.id,
                });
            });
        });
    });
});

Middlewares

function authMiddleware(req: Request, res: Response) {
    // Returning false breaks the chain of middlewares
    if (!req.hasHeader('token')) {
        res.sendStatus(401);
        return false;
    }
}

function userMiddleware(req: Request, res: Response) {
    const token = req.getHeader('token');

    // token validation
    // ....
    const id = 'foo';

    if (id) {
        req.setHeader('user', id);
    }
}

// For a single route
router.get('/api/users/:id', (req: Request, res: Response) => {
    res.status(200).json({
        id: req.params.id,
    });
}, authMiddleware, userMiddleware);

// For groups
router.group('/api', (api) => {
    api.group('admin', (admin) => {
        admin.group('blog', (blogAdmin) => {
            blogAdmin.get('', (req: Request, res: Response) => {
                res.status(200).json([]);
            });

            blogAdmin.get(':id', (req: Request, res: Response) => {
                res.status(200).json({
                    id: req.params.id,
                });
            });
        });
    }, authMiddleware, userMiddleware);
});

Readme

Keywords

none

Package Sidebar

Install

npm i @serverless_api/router

Weekly Downloads

1

Version

0.0.1

License

none

Unpacked Size

4.16 kB

Total Files

3

Last publish

Collaborators

  • edwin-luijten