Giter VIP home page Giter VIP logo

fhttp's Introduction

fhttp

Functions to deal with Request/Response using expressjs. Why ? "Just" to improve the testability, it's too bad to use directly the req.xxx functions

API

Request

Options

You need to call a function to registrate your options, and then create our Request object.

import { requestt } from 'fhttp';

const options = {};
const request = requestt(options);

app.get('/route', (req, res) => {
  const Request = request(req); // Okay, work on this request
  // Now, you can use functions on it to make your life easier

  Request.getHeader('headerName'); // -> req.headers.headerName
});

getBody(name)

Request.getBody(); // req.body
Request.getBody("name"); // req.body.name

getHeaders / getHeader(name)

Request.getHeaders(); // -> req.headers
Request.getHeader('headerName'); // -> req.headers.headerName
Request.getHeaders('headerName'); // -> req.headers.headerName

getParams / getParam(name)

Request.getParams(); // -> req.params
Request.getParam('paramName'); // -> req.params.paramName
Request.getParams('paramName'); // -> req.params.paramName

getQueries / getQuery(name)

Request.getQueries(); // -> req.query
Request.getQuery("queryName"); // -> req.query.queryName
Request.getQueries("queryName"); // -> req.query.queryName

getCookies / getCookie(name)

Request.getCookies(); // -> req.cookies
Request.getCookie("cookieName"); // -> req.cookies.cookieName
Request.getCookies("cookieName"); // -> req.cookies.cookieName

getMethod

Request.getMethod(); // -> req.method

Add shortcut

const options = {
  bind: {
    getHeader: [
      { name: 'getCsrfHeader', value: 'CSRF-TOKEN' },
    ],
  }
};
const request = requestt(options);

app.get('/', (req, res) => {
  const Request = request(req);

  Request.getCsrfHeader(); // -> req.headers['CSRF-TOKEN']
});

Response

Options

You need to call a function to registrate your options, and then create our Response object.

import { response } from 'fhttp';
const responsee = response(options);

app.get('/route', (req, res) => {
  const Response = responsee(req); // Okay, work on this response
  // Now, you can use functions on it to make your life easier
});

cookies

setCookies

Response.setCookie('cookieName', 'cookieValue'); // Not options provided, defaults options will be applied.

The options parameter is an object that can have the same properties as express res.cookies.

createCookieBag

const opts = {
  secure: true,
  httpOnly: true,
  cookieName: 'JWT-TOKEN',
  maxAge: 55555656555,
};

const jwtCookieBag = createCookieBag(opts);

app.get('', (req, res) => {
  const JwtCookie = jwtCookieBag(response);
  JwtCookie.setCookie('jwtValue');
});

fhttp's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.