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

@samislam/sendres

v1.0.1

Published

SendRes is a small and simple utility for sending formatted responses to the clients, it internally handles the `status: success | fail | error` based on the status code as well as other fields.

Downloads

14

Readme

SendRes is a small and simple utility for sending formatted responses to the clients, it internally handles the status: success | fail | error based on the status code as well as other fields.

API

sendRes(statusCode: number, res [, body: object , options: object])

  • statusCode: The status code.
  • res: The response object which is given to you by express.
  • body: optional, an object, which is the main response.body you want.
  • options: optional, options to configure how sendRes() or sendResMw() will behave.

Example using the sendRes() function:

app.route("/").get((req, res, next) => {
  // some code
  sendRes(200, res, { message: "Hello" });
  //   { # 200
  //     "status": "success",
  //     "message": "Hello"
  //   }
});

sendRes as an ExpressJs middleware:

sendResMw(statusCode: number [, body: object | function , options: object])

  • statusCode: The status code.
  • body: optional, an object or a function, which is the main response.body you want.
    • when using this argument as a function, it will be called with the req object as the first and only arguemnt. Your function must return an object.
  • options: optional, options to configure how sendRes() or sendResMw() will behave.

Example using the sendResMw() middleware:

app.route("/").get(sendResMw(200, { message: "Hello" }));
//   { # 200
//     "status": "success",
//     "message": "Hello"
//   }

Available options

  • statusField: boolean, weither to add the status property to the response or not (default: true).
  • magicalOperators: boolean, weither to disable the magical operators or to allow them (default: true).
  • statusFieldValue: function, this function will be called with the arguemnt statusCode, the return value determines the value of the status field.
  • tidy: boolean, weither to organize the order for the body of the response or not (default: false).
    • sendRes internally orders your json object, this can be disabled using this option.
  • resultsFieldName: string, the name you want for the results property which gets added when you use the $$data operator if you want to change it (default: 'results').
  • statusFieldName: string, the name you want for the status property which gets added automatically if you want to change it (default: 'status').

Example of using the options arguemnt:

app.route("/").get((req, res, next) => {
  // some code...
  sendRes(
    404,
    res,
    {},
    {
      statusFieldValue(statusCode) {
        if (statusCode >= 200 && statusCode <= 205) return "OK";
        else if (statusCode.toString().startsWith("4")) return "FAIL";
        else return "ERR";
      },
    }
  );
  //   { # 404
  //     "status": "FAIL"
  //   }
});

About the status property:

The status field depends on the status code you chose:

  • If the statusCode was >= 200 and <= 205, the status field will be 'success'.
  • If the statusCode starts with the number 4, the status field will be 'fail'.
  • otherwise, the status field will be 'error'.

Tip: You can change the status field value using the statusFieldValue option in the options argument object.

Magical Operators

Magical operators are the fields that you add within the body parameter. All the magical operators names start with $$, ex: $$data. They're used to manipulate the response body programatically.

  1. $$data: adds the results property to the response object, and converts its name in the response from $$data to data.
    • this property should hold an array.
    • the results property is the length of the $$data operator entries.

- Currently, as of v1.0.x the only magical operator available is the $$data operator.

Example without using the $$data magical operator:

app.route('/').get(
  async (req, res, next) => {
    // ... some code
    const dbRes = await axios.get('https://jsonplaceholder.typicode.com/users') // [{name: Omer}, {name: Nadia}, {name: Boyd}]
    req.allUsers = dbRes.data
    next()
  },
  sendResMw(200, (req) => ({
    message: 'Welcome, here are all the users',
    data: req.allUsers,
  }))
  // { # 200
  //     status: 'success',
  //     message: 'Welcome, here are all the users',
  //     data: [
  //         { name: Omer },
  //         { name: Nadia },
  //         { name: Boyd },
  //     ]
  // }
)

Example using the $$data magical operator:

app.route('/').get(
  async (req, res, next) => {
    // ... some code
    const dbRes = await axios.get('https://jsonplaceholder.typicode.com/users') // [{name: Omer}, {name: Nadia}, {name: Boyd}]
    req.allUsers = dbRes.data
    next()
  },
  sendResMw(200, (req) => ({
    message: 'Welcome, here are all the users',
    $$data: req.allUsers,
  }))
  // { # 200
  //     status: 'success',
  //     message: 'Welcome, here are all the users',
  //     results: 3, // <-- notice
  //     data: [
  //         { name: Omer },
  //         { name: Nadia },
  //         { name: Boyd },
  //     ]
  // }
)

version: 1.0 30 - June - 2022