# ctjs-fetch

> A small fetch library for the browser with built in JWT support written in ES6.

Latest version **1.0.2** (published 2017-08-22) · 0 weekly downloads

## Install

```sh
npm install ctjs-fetch
pnpm add ctjs-fetch
yarn add ctjs-fetch
bun add ctjs-fetch
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2017-08-22 |
| First published | 2017-02-10 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | chrisjasp |
| Keywords | fetch, rest, http, client, api |

## Links

- npm: https://www.npmjs.com/package/ctjs-fetch
- npm.io page: https://npm.io/package/ctjs-fetch

## Dependencies (2)

- [bluebird](https://npm.io/package/bluebird.md) ^3.4.7
- [ctjs-util](https://npm.io/package/ctjs-util.md) 1.0.2

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2017-08-22
- 1.0.1 — 2017-02-23
- 1.0.0 — 2017-02-10

## README

# ctjs-fetch
A small fetch library for the browser with built in JWT support written in ES6.

## Getting Started
Install ctjs-fetch
```
npm install --save ctjs-fetch
```

Each send() request returns a Promise with the Response object as the resolved value.
```javascript
import{FetchClient} from 'ctjs-fetch';

let client = new FetchClient(baseUrl);

client.get('path').send()
  .then(response => {
    // Do something with the response
  });

client.get('path').query({}).send()
  .then(response => {
    // Do something with the response
  });

client.post('path').jsonBody({}).send()
  .then(response => {
    // Do something with the response
  });

client.post('path').body(FormData).send()
  .then(response => {
    // Do something with the response
  });

client.put('path').jsonBody({}).send()
  .then(response => {
    // Do something with the response
  });

client.delete('path').send()
  .then(response => {
    // Do something with the response
  });
```

## Authentication
There is an Auth class to help with JWT using local storage. You can store, remove, verify, and read the token that 
is returned from your API login.
```javascript
import {Auth} from 'ctjs-fetch';

let config = {
  tokenName: "<Token name. Default: token>",
  authToken: "<Bearer Name. Default: Token>",
  storageType: "<Storage Type. Default: LocalStorage>",
  authHeader: "<Request Header. Default: Authorization>",
};

let auth = new Auth(config);
// returns bool
auth.isAuthenticated();
// stores the resulting jwt token
auth.loginSuccess(token);
// destroy the stored token
auth.logout();
```
## Interceptors
Interceptors provide a way to modify the request or response. The most common Interceptor is the 
AddAuthHeader. Some others are 
* AddAuthHeader
* AcceptJson
* ReadJson
* ThrowOnErrorResponse
* WorkingOverlay
```javascript
import {Auth, AddAuthHeader, FetchClient} from 'ctjs-fetch';

let config = {};
let auth = new Auth(config);

let fetchClient = new FetchClient(baseUrl).withInterceptor(new AddAuthHeader(auth));
```

### Who do I talk to?
chrisjasp@gmail.com

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