@toorieaa/httpacceptable

2.0.3 • Public • Published

1. Install

npm i @toorieaa/httpacceptable

2. Import

import HttpAcceptable from "@toorieaa/httpacceptable";

3. How is it implemented?

import SingletonClassError from "@toorieaa/singletonclasserror";
export default class HttpAcceptable {
  static ANYTHING_IS_FINE = "*/*";
  static APPLICATION_JSON = "application/json";
  static TEXT_HTML = "text/html";

  constructor() {
    throw new SingletonClassError();
  }
}

4. Use cases

Sometimes when building an application endpoint, you only need to serve just text/html and application/json and anything for the case of web forms

5. Examples

Examples of where we may use this

const errorMessage = `username / password should be specified and greater than ${usernameShouldBeAtLeastLength} / ${passwordShouldBeAtLeastLength} respectively`;
const errorObjectForJsonResponses = {
  status: HttpStatus.getStatusText(HttpStatus.BAD_REQUEST),
  message: errorMessage,
};

if (headers._acceptableResponse === HttpAcceptable.ANYTHING_IS_FINE) {
  response.status(HttpStatus.BAD_REQUEST).json(errorObjectForJsonResponses);
  return;
}

if (headers._acceptableResponse === HttpAcceptable.TEXT_HTML) {
  response.status(HttpStatus.BAD_REQUEST).send(errorMessage);
  return;
}

if (headers._acceptableResponse === HttpAcceptable.APPLICATION_JSON) {
  response.status(HttpStatus.BAD_REQUEST).json(errorObjectForJsonResponses);
  return;
}

5.1. License

[MIT]

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
2.0.30latest

Version History

VersionDownloads (Last 7 Days)Published
2.0.30
2.0.10
2.0.00
1.0.40
1.0.20
1.0.10
1.0.00

Package Sidebar

Install

npm i @toorieaa/httpacceptable

Weekly Downloads

0

Version

2.0.3

License

ISC

Unpacked Size

7.37 kB

Total Files

4

Last publish

Collaborators

  • toorieaa