# headersfactory

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

Latest version **1.0.3** (published 2017-11-07) · ISC license · 0 weekly downloads

## Install

```sh
npm install headersfactory
pnpm add headersfactory
yarn add headersfactory
bun add headersfactory
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2017-11-07 |
| First published | 2017-11-01 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Tyler Buchea |
| Maintainers | tylerbuchea |

## Links

- npm: https://www.npmjs.com/package/headersfactory
- Repository: https://github.com/tylerbuchea/headersfactory
- Homepage: https://github.com/tylerbuchea/headersfactory#readme
- Issues: https://github.com/tylerbuchea/headersfactory/issues
- npm.io page: https://npm.io/package/headersfactory

## Recent versions

- 1.0.3 (latest) — 2017-11-07
- 1.0.2 — 2017-11-01
- 1.0.1 — 2017-11-01

## README

# 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

```bash
# with npm
npm install --save headersfactory

# with yarn
yarn add headersfactory
```

## Use

`headersFactory(jwtToken)(method, data)`

```jsx
// 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.

```jsx
'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;
```

---
_Source: https://npm.io/package/headersfactory · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
