default-http

0.0.2 • Public • Published

Coverage Status

Default-http

This is just a repository to factor classes that I always use in my web projects as an abstraction for every "req" and "res" object in Restify, Koa, Hapi and Express.

/**
 * This is a standard HttpRequest object that aims to be used in any web server framework
 */
export default class HttpRequest {
 
  /**
   * This creates need a new HttpRequest
   * @param  {string} method   The is the method attribute
   * @param  {Object} body     The is the body of the request
   * @param  {Object[]} cookies  The is the cookie array of the request
   * @param  {string} hostname This is hostname
   * @param  {string} ip       The sending ip
   * @param  {string} url      The request url
   * @param  {Object} params   The url params of the request
   * @param  {string} path     The request path
   * @param  {string} protocol The request protocol
   * @param  {Object} query    The query params of the request
   * @param  {boolean} secure   Is the request secure ?
   * @param  {Object} headers  The request headers
   */
  constructor (method, body, cookies, hostname, ip, url, params, path, protocol, query, secure, headers, req) {
    /**
     * The is the method attribute
     * @type {string} 
     */
    this.method = method
    /**
     * The is the body of the request
     * @type {Object} 
     */
    this.body = body
    /**
     * The is the cookie array of the request
     * @type {Object} 
     */
    this.cookies = cookies
    /**
     * This is hostname
     * @type {string} 
     */
    this.hostname = hostname
    /**
     * The sending ip
     * @type {string} 
     */
    this.ip = ip
    /**
     * The request url
     * @type {string} 
     */
    this.url = url
    /**
     * The url params of the request
     * @type {Object} 
     */
    this.params = params
    /**
     * The request path
     * @type {string} 
     */
    this.path = path
    /**
     * The request protocol
     * @type {string} 
     */
    this.protocol = protocol
    /**
     * The query params of the request
     * @type {Object} 
     */
    this.query = query
    /**
     * Is the request secure ?
     * @type {boolean} 
     */
    this.secure = secure
    /**
     * The request headers
     * @type {Object} 
     */
    this.headers = headers
    /**
     * The default request from the server
     * @type {Object} 
     */
    this.req = req
  }
}
 
import Constants from './constants'
import Errors from './errors'
/**
 * This is a standard HttpResponse object that aims to be used in any web server framework
 */
export default class HttpResponse {
 
  /**
   * Create a new standard HttpResponse
   * @param  {Object} content Object that could have both content or page property
   * @param  {number} code    The http code of the request
   * @param  {Object} headers The response headers
   */
  constructor (content, code, headers) {
    if (content && !content.content && !content.page) throw new Error(Errors.NO_CONTENT)
    /**
     * The http code of the request
     * @type {number} 
     */
    this.code = code ? code : 200 // eslint-disable-line no-unneeded-ternary
    /**
     * Object that could have both content or page property
     * @type {Object} 
     */
    this.content = content ? content : {content: Constants[this.code]} // eslint-disable-line no-unneeded-ternary
    /**
     * The response headers
     * @type {Object} 
     */
    this.headers = headers
  }
}
 

Readme

Keywords

none

Package Sidebar

Install

npm i default-http

Weekly Downloads

2

Version

0.0.2

License

MIT

Last publish

Collaborators

  • skahrz