1.0.0 • Published 8 months ago

colored-status-code v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
8 months ago

Colored Status Code

!NOTE

  • Color your HTTP status code in the console, terminal, etc.
  • Improve the readability of your log.
  • Easy to use & customize.
  • Also it's type safe & tested by 100% coverage.

GitHub npm version install size npm downloads

boilerplate from modern-npm-package

Get Started

šŸŽØ Colored Status Code šŸ”¢

npm install colored-status-code
import { coloredStatusCode } from "colored-status-code";

console.log(coloredStatusCode(101)); // 101 in grey
console.log(coloredStatusCode(200)); // 200 in green
console.log(coloredStatusCode(302)); // 302 in cyan
console.log(coloredStatusCode(404)); // 404 in red
console.log(coloredStatusCode(500)); // 500 in red

Examples

You can use it in the try-catch statement

// try-catch example
try {
  // ...
} catch (error) {
  if (error instanceof HTTPError) {
    console.error(
      `Error fetching data [${coloredStatusCode(error.statusCode)}] ${
        error.message
      }`
    );
  }
}

or as an Axios interceptor.

// Axios interceptor example
axios.interceptors.response.use(
  (response) => response,
  (error) => {
    console.error(
      `Error fetching data [${coloredStatusCode(error.response.status)}] ${
        error.message
      }`
    );
    return Promise.reject(error);
  }
);

or anywhere you want!


Features

  • 100-599 status code range is supported.
  • Customize the color of the status code.
import { changeStatusColorSettings } from "colored-status-code";

changeStatusColorSettings(100, "yellow"); // now 100 is yellow.
  • supports the color code of ansi-colors.
COLOR_CODES = {
  grey: "\x1b[90m",
  red: "\x1b[31m",
  green: "\x1b[32m",
  yellow: "\x1b[33m",
  blue: "\x1b[34m",
  purple: "\x1b[35m",
  cyan: "\x1b[36m",
  white: "\x1b[37m",
  reset: "\x1b[0m",
} as const;
1.0.0

8 months ago