## Resource

Create a base resource.

```javascript
var api = new resti('http://swapi.co/api/');
```

Build further resources using the one, many and endpoint methods. All three methods return new resources with the headers and middleware previously defined on their parent.

```javascript
starships = api.many('starships');
// http://swapi.co/api/starships

starships.one(10);
// http://swapi.co/api/starships/10

starships.one(10).endpoint('crew');
// http://swapi.co/api/starships/10/crew
```

## Methods

Get, Post, Put, Patch, Head, Delete and Options requests can be sent via their corresponding, lowercase methods on the resource. Remove is used in place of Delete.

```javascript
starships.get(function(res) {
	if (res.ok) {
		console.log(res.body);
	}
});
```

## Headers

By default, all resources are created with `Content-Type` and `Accept` headers set to `application/json`.

### Set

```javascript
starships.header('Content-Type', 'application/json');

starships.header({
	'Content-Type': 'application/json',
	'Accept': 'application/json'
});
```

### Get

```javascript
const type = starships.header('Content-Type');
```

### Unset

```javascript
starships.header('Content-Type', false);
```

### Using

```javascript
// returns bool
starships.using('Content-Type');

starships.using('Content-Type', 'application/json');

starships.using({
	'Content-Type': 'application/json',
	'Accept': 'application/json'
});
```

### Convenience methods

```javascript
// Get 'Content-Type' header.
starships.type();
// Set 'Content-Type' header.
starships.type('application/json');
```

```javascript
// Get 'Accept' header.
starships.accept();
// Set 'Accept' header.
starships.accept('application/json');
```

### Shorthands

```javascript
starships.header('Content-Type', 'json');
// for 'application/json'

starships.type('html'); 
// for 'text/html'

starships.accept('xml'); 
// for 'application/xml'

starships.using('Content-Type', 'form'); 
// for 'application/x-www-form-urlencoded'
```

### X-Rquested-With header

Set/unset `X-Requested-With` header to `XMLHttpRequest`.

```javascript
starships.enableReqWithHeader();

starships.disableReqWithHeader();
```

## Params

Add url parameters.

```javascript
starships.params('property', 'value');

starships.params({
	'first': 'pair',
	'second': 'pair'
});
```

## Parse

Add a parser function to determine which part of the XHR's responseText is set on the response object's `body` property.

```javascript
starships.parse(function(responseText) {
	return responseText.result;
});
```

## Middleware

Connect/express-like middleware for different stages in the request/response cycle. 

### Before

```javascript
starships.before(function(req, next) {
	const token = cookie.load('token');
    // only send request if we have a token
    if (token) {
        req.header('Authorization', token);
        next();
    } else {
        // user's token has expired and must sign in again
        window.location.href = '/login';
    }
});
```

Bail out of a request early by ommitting a call to `next()`. 

### Use

```javascript
starships.use(function(res, next) {
	// code
});
```

## Options

Toggle trailing slashes on request URLs.

```javascript
api.enableTrailingSlashes();

api.disableTrailingSlashes();
```