1.0.14 • Published 5 years ago

@poppinss/request v1.0.14

Weekly downloads
33
License
MIT
Repository
github
Last release
5 years ago

Request

circleci-image npm-image license-image

Wrapper over Node.js req object to standardize and ease the process of reading data from HTTP requests.

Table of contents

Features

  1. Support for reading plain and signed cookies (only when signed via @poppinss/response)
  2. Handy methods for content negotiation.
  3. Handles inconsistencies between certain headers like referer and referrer.
  4. Reliably reads ip address of proxied requests.
  5. Assigns distributed unique x-request-id to each request.

Usage

Install the package from npm as follows:

npm i @poppinss/request

# yarn
yarn add @poppinss/request

and then use it as follows

import { Request, RequestConfigContract } from '@poppinss/request'
import { createServer } from 'http'

const config: RequestConfigContract = {
  allowMethodSpoofing: false,
  subdomainOffset: 2,
  trustProxy: require('proxy-addr').compile('loopback'),
}

createServer((req, res) => {
  const request = new Request(req, res, config)
  res.end(`${request.id()} ${request.url()}`)
})

Config

Typescript support

The module is written in Typescript and exports following classes, types and interfaces.

import { Request, RequestContract, RequestConfigContract} from '@poppinss/request'

RequestContract is the interface that Request class adheres too. Since, you cannot extend concrete implementations in Typescript, you may need the interface to have a lossely typed flow.

Request.macro('cartValue', function () {
  return Number(this.cookie('cart')) || 0
})

then, you need to add cartValue to the interface

import { RequestContract as BaseContract } from '@poppinss/request'

interface RequestContract extends BaseContract {
  cartValue (): number
}

const request = new Request(req, res, config) as unknown as RequestContract

API

Following are the autogenerated files via Typedoc

Maintainers

Harminder virk

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago