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

response-body-builder

v1.0.2

Published

A smart response builder used to build response bodies.

Downloads

7

Readme

response-body-builder

Response Builder is a middleware that helps build you response bodies for APIs.

Installation

npm install response-body-builder

Usage

Basic Usage

const { ResponseBuilder, ResponseBuilderMiddleware } = require("response-body-builder");
const express = require("express");
const app = express();

app.use(ResponseBuilderMiddleware);

app.get("/ping", (req, res) => {
  const response = new ResponseBuilder()
    .set("ping", "Pong!");
  res.sendResponse(response);
  // {
  //   "ping": "Pong!",
  //   "httpCode": 200,
  //   "httpCodeMessage": "Successful."
  // }
});

Advanced Usage Example

const { ResponseBuilder } = require("response-body-builder");
const wait = require("util").promisify(setTimeout);

(async function () {
  const response = await (
    new ResponseBuilder()
      .set("test", "hello")
      .setPromise("bool", fakePromiseValue(500))
      .mergePromise(fakePromiseObject(200))
      .setCode(500)
      .cast()
  );

  const syncResponse = new ResponseBuilder()
    .set("test", "hello")
    .setPromise("bool", fakePromiseValue(500))
    .mergePromise(fakePromiseObject(200))
    .setCode(500)
    .castSync();

  console.log(response);
  console.log(syncResponse); // Ignore everything promise-related.
  
  // Output:
  // {
  //   test: 'hello',
  //   httpCode: 500,
  //   httpCodeMessage: 'Internal server error.',
  //   bool: false,
  //   two: 2,
  //   four: 4
  // }
  // {
  //   test: 'hello',
  //   httpCode: 500,
  //   httpCodeMessage: 'Internal server error.'
  // }
}());

async function fakePromiseValue (time) {
  await wait(time);
  return (Math.random() * 100 < 50 ? true : false);
}

async function fakePromiseObject (time) {
  await wait(time);
  return {
    two: 2,
    four: 4
  }
}

Documentation

  1. .set(key, value) (Returns this)

Adds key-value pair to the body object.

  1. .setAsync(key, promise) (Returns this)

Starts to resolve the promise right away ,without adding it to queue, once done, it adds it to the body object as a key-value pair.

If promise doesn't resolve until .cast() or .castSync() are called, the result of the promise will not be included into the body object, not recommended to use.

  1. .mergePromise(promise) (Returns this)

Queues the promise into the mergePromises queue. (Upon .cast() all the promises queued are resolved and their value properties are merged into the body object.)

  1. .setPromise(key, promise) (Returns this)

Queues the promise into the keyPromises queue. (Upon .cast() all promises queued are resolved and added to the body object as a key-resovled value pair.)

  1. .merge(object) (Returns this)

Copies all of the properties of object into the body object.

  1. .cast([useCode] = true) (Returns Promise<Object>)

useCode whether or not the status code and the message relevant to it to be inserted into the body object.

Resolves all of the promise queues into the body object and returns it.

  1. .castSync([useCode = true]) (Return Object)

useCode whether or not the status code and the message relevant to it to be inserted into the body object.

Returns the body object, skipping to resolve any of the queues.

  1. .setCode(httpCode[, message])

Sets the status code of the request. If message is not provided, the default message for the will be used for supported codes (or undefined for non-supported status codes).

  1. Supported Http Code Messages
"HTTP_200": "Successful.",
"HTTP_204": "No content.",
"HTTP_400": "Bad request.",
"HTTP_401": "Unauthorized.",
"HTTP_403": "Forbidden.",
"HTTP_404": "Not found.",
"HTTP_429": "Too many requests.",
"HTTP_500": "Internal server error.",
"HTTP_503": "Service unavailable."