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

handlin

v1.1.4

Published

NodeJS API response generator and handler.

Downloads

11

Readme

banner

handlin

Handlin allows to generate and handle NodeJS API responses. By embracing the router-controller-service API model, handling standardizes the API data return format.

Example of the API response with a help of handlin

{
  "success": true,
  "code": 200,
  "data": {
    "_id": "63dd72562dae3971744d65b1",
    "isActive": false,
    "balance": "$2,693.75",
    "name": "Floyd Kerr",
    "gender": "male",
    "email": "[email protected]"
  },
  "status": "OK",
  "message": "Standard response for successful HTTP requests."
}

Prerequisites

This project requires NodeJS (version 18 or later) and NPM. Node and NPM are really easy to install. To make sure you have them available on your machine, try running the following command.

$ npm -v && node -v
8.13.2
v18.0.0

Table of contents

Installation

To install and set up the library, run:

$ npm install handlin

RCS

The handlin package relies on Router Controller Service API design pattern.

In this pattern, a Router component is responsible for mapping incoming requests to the appropriate Controller.

The Controller component then calls the appropriate Service component to execute the requested business logic, and finally returns the result to the Router component, which returns it to the client.

The Service component encapsulates the application's business logic and communicates with data storage or other external systems as necessary. banner

Usage

Generating responses

To generate response with handlin, you first need to import the generateResponse method to your service:

import { generateResponse } from "handlin";

It takes as a parameter an object with the following fields:

| Property | Type | Required | Description | | ----------- | ------ | -------- | ------------------------------------------------------------------------------------------------------------------------------------ | | code | number | Yes | A http response code that should be sent | | status | string | No | By default it attaches statuses correcsponsing to status codes from http-status library | | message | string | No | By default it attaches messages correcsponsing to status codes from http-status library | | redirectUrl | string | No | A url to a destination where the user will be redirected with the response | | data | object | No | Data attached to the response |

return generateResponse({ code: 200, data: sampleObject });

OR more detailed:

return generateResponse({
  code: 200,
  message: "Data retrieved succesfully!",
  success: true,
  data: sampleObject,
});

Full sample of the code from sample.service.ts:

import { generateResponse } from "handlin";

export const getSampleData = async () => {
  const sampleObject = {
    _id: "63dd72562dae3971744d65b1",
    isActive: false,
    balance: "$2,693.75",
    name: "Floyd Kerr",
    gender: "male",
    email: "[email protected]",
  };

  return generateResponse({
    code: 200,
    message: "Data retrieved succesfully!",
    success: true,
    data: sampleObject,
  });
};

Handling responses

To handle response with handlin, you first need to import the handleResponse method to your controller:

import { handleResponse } from "handlin";

Then, use it by providing response from service and request, response from express as parameters:

const response = await SampleService.getSampleData();

return handleResponse(response, req, res);

Full sample of the code from sample.controller.ts:

import { Request, Response } from "express";
import { handleResponse, generateResponse } from "handlin";
import * as SampleService from "../services/sample.service";

export const getSampleData = async (req: Request, res: Response) => {
  try {
    //Retrieve response from the service
    const response = await SampleService.getSampleData();

    //Handle the response - return data to the user
    return handleResponse(response, req, res);
  } catch (e) {
    //In case of a potential error, catch it;
    //generate error response and handle it immediately

    if (e instanceof Error) {
      return handleResponse(
        generateResponse({ code: 500, message: e.message }),
        req,
        res
      );
    }
  }
};

Sample Express Project using handlin

You can check out a demo express project utilizing handlin package here:

MRCS-express-boilerplate

Contributing

As an open source project, handlin depends on the contributions and support of its community. Whether you're a seasoned developer or just starting out, there's a place for you in the handlin project. We welcome bug reports, bug fixes, new features, and documentation improvements.

If you're interested in getting involved, please check out our GitHub repository and read our contribution guidelines.

Authors

  • bas0N - Initial work - bas0N

License

MIT License

Readme template credits

Andrea SonnY