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

http-staror

v1.0.7

Published

Makes easy to use HTTP status and errors.

Downloads

9

Readme

Http-Staror

With Typescript

Http

import { Err, Http, Status } from "http-staror"
Http.setStatus("Accepted") // {status: 'Accepted',statusCode: 202,message: 'Accepted'}
Http.setStatus("Accepted").setStatusCode(500) // { status: 'Accepted', statusCode: 500, message: 'Accepted' }

// Note: If you don't set status code explicitly then the status code would be inferred from the status you set.

//Most of the time would not want to set the status code explicitly unless you are setting a custom status that does not exist in the package
Http.setStatus("Accepted").setStatusCode(500).setMessage("It is accepted") // { status: 'Accepted', statusCode: 500, message: 'It is accepted' }

// Note: If you don't set a message explicitly then the message would be the same as status
Http.setStatus("x-hello").setStatusCode(303).setMessage("custom status") // { status: 'hello', statusCode: 303, message: 'custom status' }

// Note: To set a custom status you must prefix with "x-" also you have to set a status code otherwise it will be set to 200

Err

/** Supported Functions ⬇⬇
 * setProduction()
 * setStatus()
 * setFilePath()
 * setWhere()
 * setIsOperational()
 * setLineNumber()
 * setMessage()
 * setNoStack()
 * setStatusCode()
 * setUniqueIdentifier()
 */
Err.setStatus("Ok").setMessage("custom status")

// Output ⬇
{
  filePath: null
  isOperational: true
  isProduction: false
  lineNumber: null
  message: "custom status""
  stack: "Error: custom status"\n    at _a2.setMessage (http://localhost:5173/node_modules/.vite/deps/http-staror.js?v=a66ceed3:146:39)\n    at http://localhost:5173/src/main.ts:2:39"
  status: "Ok"
  statusCode: 202
  uniqueIdentifier: null
  where: null
}
Err.setProduction().setStatus("Ok").setMessage("Something Went Wrong!")

// Output ⬇
{
  filePath: null
  isOperational: true
  isProduction: true
  lineNumber: null
  message: "Something Went Wrong!"
  stack: null
  status: "Ok"
  statusCode: 200
  uniqueIdentifier: null
  where: null
}

// Note: Only set production for testing. This package already contains some to code to detect if it is in production or development mode.

// It will look at process.env.NODE_ENV to detect the mode

Err.setProduction(false).setStatus("Ok").setMessage("Something Went Wrong!")

// Warning: You can also setProduction to false therefore it will will be set to development mode even though your application is running in production mode.
Err.setStatus("Ok").setMessage("Something Went Wrong!").setNoStack()

// Output ⬇
{
  filePath: null
  isOperational: true
  isProduction: true
  lineNumber: null
  message: "Something Went Wrong!"
  stack: null
  status: "Ok"
  statusCode: 200
  uniqueIdentifier: null
  where: null
}

// Note: You can set it to no stack mode therefore the stack will be null in development. In production stack is always null.
Err.setStatus("Ok")
  .setMessage("Something Went Wrong!")
  .setNoStack()
  .setIsOperational(false)

// Output ⬇
{
  filePath: null
  isOperational: false
  isProduction: true
  lineNumber: null
  message: "Something Went Wrong!"
  stack: null
  status: "Ok"
  statusCode: 200
  uniqueIdentifier: null
  where: null
}

// Note: You can set is operational to false. By default it is true. If there is any unhandled error in the application that error won't have is operational property.
// You can check if an error is instance your custom error
console.log(e.message instanceof Err) // boolean
Err.setStatus("Ok")
  .setMessage("Something Went Wrong!")
  .setFilePath("src/app.ts")
  .setWhere("inside main function")
  .setLineNumber(24)
  .setUniqueIdentifier("axdf")
  .setNoStack()

// Output ⬇
{
  filePath: "src/app.ts"
  isOperational: true
  isProduction: true
  lineNumber: 24
  message: "Something Went Wrong!"
  stack: null
  status: "Ok"
  statusCode: 200
  uniqueIdentifier: "axdf"
  where: "inside main function"
}
// you can throw error like this
try {
  //...do this
} catch (error) {
  throw Err.setStatus("InternalServerError").setWhere(
    "getVideosDurationString()"
  )
}

// This way structure of the error object is always same and predictable.

Status

console.log(Status.Ok)
// {value: "Ok", statusCode: 200}