Giter VIP home page Giter VIP logo

headersfactory's Introduction

Headers Factory

Opinionated headers factory for easy use with fetch and other AJAX things too I guess.

99% of the time I'm hitting a JSON API in CORS mode, getting or posting some data, and maybe sending up a JWT token.

So I've designed this module to make that use case super easy.

Install

# with npm
npm install --save headersfactory

# with yarn
yarn add headersfactory

Use

headersFactory(jwtToken)(method, data)

// with es6
import headersFactory from 'headersfactory';

// with CommonJS
const headersFactory = require('headersfactory');

// Example get with no JWT token
const githubUrl = 'https://api.github.com/users/tylerbuchea/repos?sort=updated';
const githubHeaders = headersFactory()('get');

fetch(url, githubHeaders)
    .then(response => response.json())
    .then(console.log)
    .then(console.error);

// Example post and get with JWT token
const madeupUrl = 'https://api.example.com/users';
const jwtToken = 'IMAJWTTOKEN'; // no need add "Bearer " it is added automatically

// No we can use headers() to generate new headers with the JWT token automatically connected
const headers = headersFactory(jwtToken);

const headersPost = headers('post', { age: 27 }); // JSON is automatically strinigified and attached to the body of the request
fetch(madeupUrl, headersPost)
    .then(response => response.json())
    .then(console.log)
    .then(console.error);

// We can use headers() again and the JWT is still automatically attached even though we're performing a different call
const headersGet = headers('get');
fetch(madeupUrl, headersGet)
    .then(response => response.json())
    .then(console.log)
    .then(console.error);

The actual code

This whole module is super tiny feel free to just copy paste it into your own file locally and modify as opposed to npm install-ing it.

'use strict';

function headersFactory(jwtToken) {
  return function headers(method, data, headers) {
    var header = {
      mode: 'cors',
      method: method,
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
      },
    };
    if (jwtToken) header.headers.Authorization = 'Bearer ' + jwtToken;
    if (data) header.body = JSON.stringify(data);
    return header;
  };
}

module.exports = headersFactory;

headersfactory's People

Contributors

tylerbuchea avatar

Stargazers

 avatar

Watchers

 avatar  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.