# schlepp

> API client for authenticated requests using a JWT bearer token

Latest version **1.0.9** (published 2017-10-19) · MIT license · 0 weekly downloads

## Install

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

## 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.9 |
| Published | 2017-10-19 |
| First published | 2017-09-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 8 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | The Gnar Company |
| Maintainers | mikestone14 |
| Keywords | api-client, api, client, jwt, authentication |

## Links

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

## Dependencies (8)

- [lodash](https://npm.io/package/lodash.md) ^4.17.4
- [webpack](https://npm.io/package/webpack.md) ^3.5.5
- [babel-core](https://npm.io/package/babel-core.md) ^6.26.0
- [es6-promise](https://npm.io/package/es6-promise.md) ^4.1.1
- [babel-loader](https://npm.io/package/babel-loader.md) ^7.1.2
- [isomorphic-fetch](https://npm.io/package/isomorphic-fetch.md) ^2.2.1
- [babel-preset-es2015](https://npm.io/package/babel-preset-es2015.md) ^6.24.1
- [babel-preset-stage-0](https://npm.io/package/babel-preset-stage-0.md) ^6.24.1

## Alternatives

- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads
- [escher-request](https://npm.io/package/escher-request.md) — 545 weekly downloads

## Recent versions

- 1.0.9 (latest) — 2017-10-19
- 1.0.8 — 2017-10-17
- 1.0.7 — 2017-09-07
- 1.0.6 — 2017-09-07
- 1.0.5 — 2017-09-07
- 1.0.4 — 2017-09-01
- 1.0.3 — 2017-09-01
- 1.0.2 — 2017-09-01
- 1.0.1 — 2017-09-01
- 1.0.0 — 2017-09-01

## README

# Schlepp

[![CircleCI](https://circleci.com/gh/TheGnarCo/schlepp/tree/master.svg?style=svg)](https://circleci.com/gh/TheGnarCo/schlepp/tree/master)

[![Coverage Status](https://coveralls.io/repos/github/TheGnarCo/schlepp/badge.svg?branch=master)](https://coveralls.io/github/TheGnarCo/schlepp?branch=master)

API client used for making authenticated JSON requests using a JWT bearer token to a
specified endpoint. Unauthenticated requests are also supported.

This API client follows the convention of storing the bearer token in local
storage. When creating an instance of the API client, pass the function the name
of the key in local storage to use when retrieving the bearer token for
authenticated requests.

## Setup

Create an instance of Schlepp passing in your API host and local storage bearer token key

```js
import Schlepp from 'schlepp';

const api = new Schlepp(
  bearerTokenKeyInLocalStorage: 'auth_token', // assumes localStorage.getItem('auth_token') will return the bearer token
  headers: { Accept: 'application/json' }, // default is { 'Content-Type': 'application/json' }
  host: 'https://example.com',
);
```

## Usage

Use the `authenticated` and `unauthenticated` objects to send an HTTP request to
the server. Both objects expose the same HTTP method functions (`get`, `delete`,
`post`, `patch`).

The difference between the `authenticated` and `unauthenticated` request is that the `authenticated` requests include an `Authorization` header: (`{ Authorization: 'Bearer my-bearer-token' }`).


### Request methods

The following request methods can be performed as either authenticated or
unauthenticated requests

### #delete

```js
delete = (path: string, headers?: object = {}) => promise

// Example:
api.authenticated.delete('users/1', { Accept: 'application/json' });
api.unauthenticated.delete('users/1');
```

### #get

```js
get = (path: string, headers?: object = {}) => promise

// Example:
api.authenticated.get('users');
api.unauthenticated.get('users', { Accept: 'application/json' });
```

### #patch

```js
patch = (path: string, params: object, headers?: object = {}) => promise

// Example:
api.authenticated.patch('users/1', JSON.stringify({ first_name: 'Mike' }));
api.unauthenticated.patch(
  '/users/1',
  JSON.stringify({ first_name: 'Mike' }),
  { Accept: 'application/json' },
);
```

### #post

```js
post = (path: string, params: object, headers?: object = {}) => promise

// Example:
api.authenticated.post('comments', JSON.stringify({ comment: 'This is neat!' }));
api.unauthenticated.post(
  'comments',
  JSON.stringify({ comment: 'This is neat!' }),
  { Accept: 'application/json' },
);
```

## Examples

### Unauthenticated requests

```js
import Schlepp from 'schlepp';

const api = new Schlepp(
  bearerTokenKeyInLocalStorage: 'auth_token',
  host: 'https://example.com',
);

api.unauthenticated.get('posts?title=Unicorns');
api.unauthenticated.delete('users/1');
api.unauthenticated.post('users', JSON.stringify({ first_name: 'Charlie', last_name: 'Brown' }));
api.unauthenticated.patch('users/1', JSON.stringify{ last_name: 'Chaplin' }));
```

### Authenticated requests

```js
import Schlepp from 'schlepp';

const api = new Schlepp(
  bearerTokenKeyInLocalStorage: 'auth_token',
  host: 'https://example.com',
);

api.authenticated.get('posts?title=Unicorns');
api.authenticated.delete('users/1');
api.authenticated.post('users', JSON.stringify({ first_name: 'Charlie', last_name: 'Brown' }));
api.authenticated.patch('users/1', JSON.stringify({ last_name: 'Chaplin' }));
```

### Specifying headers

By default, request headers will be set to `{ 'Content-Type': 'application/json'
}`. Request headers can be amended/overridden in 2 ways.

**1) Instantiating the class with headers**

If you'd like to set headers to be used across all requests made by the API
client, you may want to set headers when creating an instance of the Schlepp
class.

```js
import Schlepp from 'schlepp';

const api = new Schlepp(
  bearerTokenKeyInLocalStorage: 'auth_token',
  headers: { Accept: 'application/json' },
  host: 'https://example.com',
);

// in the above example all requests send from the `api` constant will include the following headers:
// { Accept: 'application/json', 'Content-Type': 'application/json' }

import Schlepp from 'schlepp';

const api = new Schlepp(
  bearerTokenKeyInLocalStorage: 'auth_token',
  headers: { 'Content-Type': 'text/plain' },
  host: 'https://example.com',
);

// in the above example all requests send from the `api` constant will include the following headers:
// { 'Content-Type': 'text/plain' }
```

**2) Setting headers on each request**

To override or amend the default or instance headers, you can specify the
headers of an individual request.

```js
import Schlepp from 'schlepp';

const api = new Schlepp(
  bearerTokenKeyInLocalStorage: 'auth_token',
  headers: { Accept: 'text/plain' },
  host: 'https://example.com',
);

api.unauthenticated.get('users', { Accept: 'application/json' });

// in the above example the request will include the following headers:
// { Accept: 'application/json', 'Content-Type': 'application/json' }

import Schlepp from 'schlepp';

const api = new Schlepp(
  bearerTokenKeyInLocalStorage: 'auth_token',
  host: 'https://example.com',
);

api.unauthenticated.get('users', { 'Content-Type': 'text/plain' });

// in the above example the request will include the following headers:
// { 'Content-Type': 'text/plain' }
```

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