npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rest-api-framework

v0.2.0

Published

Framework for building REST API with TypeScript support

Downloads

11

Readme

Introduction

Simple REST API framework for Node.js

npm version npms score github_actions status codecov Codacy Badge Known Vulnerabilities dependencies Status License: MIT

Highlights

  • No need to set app as global variable

    Will provide a route method, route.get, route.use,_ etc.

  • No need to use deprecated domain-context for error handler

    Will handle it in Promise

  • Extend server for more use

    Use route.for method

  • Load config in app

    Use app.set and app.get method

  • Zero dependency

    No need to bother about vulnerabilities

  • TypeScript support

    Added required types for TypeScript

  • Can easily execute deferred/delayed jobs

    Added support to execute your deferred functions after request end


How to use

const { app, route, requestListener } = require('rest-api-framework');

app.set('foo', 'bar');
app.get('foo');
// Output: bar

app.set('config', {
    'foo': 1,
    'bar': {
        'test': 2
    }
});

app.get('config.foo'); 
// Output: 1
app.get('config.bar.test');
// Output: 2

Register middlewares

  • Instead of callback, we expect Promise in return.
  • If you are doing an asynchronous task then return Promise else no need to return anything
  • Also, you can set and get data in request context using req.set and req.get methods

For asynchronous task:

route.use((req, res) => {
    // set data
    req.set('foo', 'bar');
    
    // for asynchronous return Promise
    return new Promise((resolve, reject) => {
        setTimeout(resolve, 100));
    });
    
});

For synchronous task:

route.use((req, res) => {
    // get data
    let foo = req.get('foo');
    
    // do some sync
});

Route

  • :page - (:) required url parameter (similar to express)
  • :limit? - (:?) optional url paramater
route.get('/', (req, res) => {
    res.send({test: 1});
});

route.get('/page/:page/:limit?', (req, res) => {
    res.send(req.params);
});

Extended Route

let test = route.for('/test');

test.use((req, res) => {
    // middleware for /test and /test/*
})

test.get('/', (req, res) => {
    // route for /test
    res.send('test');
});

test.post('/:uid', (req, res) => {
    // route for /test/1 OR /test/abc OR etc.
    res.send('test');
});

How to use other middlewares?

For example: Body parser

const util = require('util');
const bodyParser = require('body-parser');

// can use Node.js's utils module to convert callback function into Promise
const jsonBodyParser = util.promisify(bodyParser.json());
const urlEncodedBodyParser = util.promisify(bodyParser.urlencoded({extended: true}))

route.use(jsonBodyParser);
route.use(urlEncodedBodyParser);

How to execute deferred jobs?

const { route } = require('rest-api-framework')

// Register deferred jobs
route.deferred('LOG', (data) => {
    console.log(data)
})

route.deferred('ANALYTICS', (data) => {
    //TODO: send data to analytics server and return promise
})

// Execute registered deferred jobs by passing data
route.get('/foo', (req, res) => {
    // NOTE: registration of deferred jobs matters for sequential execution
    req.defer('LOG_REQUEST', {})
    // execute request and send response 
    res.send('foo is ended')
    req.defer('ANALYTICS', {})
})

Error Handler

A new way to handle errors and exceptions

// Throwing error
route.get('/error', (req, res) => {
    // throw custom error and set code
    return Promise.reject({ code: 'NOT_AUTHORIZED' })
});

//Catching error
route.error('NOT_AUTHORIZED', (err, req, res) => {
    // Also, you can use ###err### for error detail
    res.status(401).send({ message: 'You are not authorized.'});
});

Uncaught Errors

Common error handler method for all errors

route.error((err, req, res) => {
    // common error handler
    res.status(500).send({ message: "Internal Server Error" });
});

start server

const {requestListener} = require('rest-api-framework')

http.createServer(requestListener).listen(3000);

TypeScript Example

import http from 'http'
import {app, route, requestListener, rest} from 'rest-api-framework'

app.set('ENV', 'development')

route.use((req: rest.Request): void => {
    req.set('flag', 1)
})

route.get('/', (req: rest.Request, res: rest.Response): void => {
    res.send('Hello, World!')
})

route.get('/error', (req: rest.Request, res: rest.Response) : Promise<void> => {
    return Promise.reject(new Error('custom error'))
})

route.error('NOT_FOUND', (err: Error, req: rest.Request, res: rest.Response) => {
    // status 404
})

http.createServer(requestListener).listen(3000)

Sponsors