docs/Api.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Api - Documentation</title>
<script src="scripts/prettify/prettify.js"></script>
<script src="scripts/prettify/lang-css.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
<div class="navicon"></div>
</label>
<label for="nav-trigger" class="overlay"></label>
<nav>
<li class="nav-link nav-home-link"><a href="index.html">Home</a></li><li class="nav-heading">Classes</li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="Api.html">Api</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Api.html#_debug">_debug</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Api.html#authorize">authorize</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Api.html#base64decode">base64decode</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Api.html#base64encode">base64encode</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Api.html#baseUrl">baseUrl</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Api.html#buildUrl">buildUrl</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Api.html#callRoute">callRoute</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Api.html#delete">delete</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Api.html#get">get</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Api.html#handleEvents">handleEvents</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Api.html#handleMiddlewares">handleMiddlewares</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Api.html#hasValidToken">hasValidToken</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Api.html#head">head</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Api.html#init">init</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Api.html#isAuthorized">isAuthorized</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Api.html#patch">patch</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Api.html#post">post</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Api.html#put">put</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Api.html#sanitize">sanitize</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Api.html#use">use</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Api.html#validate">validate</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Api.html#varOrFn">varOrFn</a></span></li><li class="nav-heading"><a href="global.html">Globals</a></li><li class="nav-item"><span class="nav-item-type type-member">M</span><span class="nav-item-name"><a href="global.html#defaults">defaults</a></span></li>
</nav>
<div id="main">
<h1 class="page-title">Api</h1>
<section>
<header>
<h2>
Api
</h2>
<div class="class-description"><p>Api class to handle all interactions with backend</p></div>
</header>
<article>
<div class="container-overview">
<div class="section-method">
<h2>Constructor</h2>
<h4 class="name" id="Api"><span class="type-signature"></span>new Api<span class="signature">(settings)</span><span class="type-signature"></span></h4>
<div class="description">
<p>Constructor for an Api object</p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="Api.js.html">Api.js</a>, <a href="Api.js.html#line63">line 63</a>
</li></ul></dd>
</dl>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>settings</code></td>
<td class="type">
<span class="param-type"><code><a href="global.html#ApiSettings">ApiSettings</a></code></span>
</td>
<td class="description last">
<p>Settings to create api instance</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<h3 class="subsection-title">Methods</h3>
<div class="section-method">
<h4 class="name" id="_debug"><span class="type-signature"></span>_debug<span class="signature">(…args)</span><span class="type-signature"></span></h4>
<div class="description">
<p>Console log all arguments if verbose setting is true</p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="Api.js.html">Api.js</a>, <a href="Api.js.html#line564">line 564</a>
</li></ul></dd>
</dl>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Attributes</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>args</code></td>
<td class="type">
<span class="param-type"><code>*</code></span>
</td>
<td class="attributes">
<repeatable><br>
</td>
<td class="description last">
<p>All args passed to console.log</p>
</td>
</tr>
</tbody>
</table>
</div>
<div class="section-method">
<h4 class="name" id="authorize"><span class="type-signature"></span>authorize<span class="signature">(args)</span><span class="type-signature"></span></h4>
<div class="description">
<p>Authorize this API. If token is provided, it will be used for authorization.
If username/password are provided, <code>settings.authorization</code> must be set to 'Basic',
and a token will be generated using <a href="Api.html#base64encode"><code>Api#base64encode</code></a> with input: <code>${username}:${password}</code></p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="Api.js.html">Api.js</a>, <a href="Api.js.html#line277">line 277</a>
</li></ul></dd>
</dl>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>args</code></td>
<td class="type">
<span class="param-type"><code>object</code></span>
</td>
<td class="description last">
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Attributes</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>token</code></td>
<td class="type">
<span class="param-type"><code>string</code></span>
|
<span class="param-type"><code>function</code></span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="description last">
<p>A token string, or a function to obtain a token value</p>
</td>
</tr>
<tr>
<td class="name"><code>username</code></td>
<td class="type">
<span class="param-type"><code>string</code></span>
|
<span class="param-type"><code>function</code></span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="description last">
<p>A username string, or a function to obtain a username value</p>
</td>
</tr>
<tr>
<td class="name"><code>password</code></td>
<td class="type">
<span class="param-type"><code>string</code></span>
|
<span class="param-type"><code>function</code></span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="description last">
<p>A password string, or a function to obtain a password value</p>
</td>
</tr>
<tr>
<td class="name"><code>headers</code></td>
<td class="type">
<span class="param-type"><code>object</code></span>
|
<span class="param-type"><code>function</code></span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="description last">
<p>Custom authorization headers object, or a function to obtain them</p>
</td>
</tr>
<tr>
<td class="name"><code>validate</code></td>
<td class="type">
<span class="param-type"><code>function</code></span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="description last">
<p>Custom authorization validation function</p>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<div class="section-throws">
<h5>Throws:</h5>
<dl>
<dt>
<div class="param-desc">
<ul>
<li>Arguments MUST provide headers, token OR username & password</li>
</ul>
</div>
</dt>
<dd></dd>
<dt>
<dl>
<dt>
Type
</dt>
<dd>
<span class="param-type"><code>Error</code></span>
</dd>
</dl>
</dt>
<dd></dd>
</dl>
</div>
</div>
<div class="section-method">
<h4 class="name" id="base64decode"><span class="type-signature"></span>base64decode<span class="signature">(b64, format<span class="signature-attributes">opt</span>)</span><span class="type-signature"> → {string}</span></h4>
<div class="description">
<p>Decode given base64 string</p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="Api.js.html">Api.js</a>, <a href="Api.js.html#line242">line 242</a>
</li></ul></dd>
</dl>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Attributes</th>
<th>Default</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>b64</code></td>
<td class="type">
<span class="param-type"><code>string</code></span>
</td>
<td class="attributes">
</td>
<td class="default">
</td>
<td class="description last">
<p>Base64 string to be decoded</p>
</td>
</tr>
<tr>
<td class="name"><code>format</code></td>
<td class="type">
<span class="param-type"><code>string</code></span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="default">
'utf8'
</td>
<td class="description last">
<p>Target format for decoded string</p>
</td>
</tr>
</tbody>
</table>
<div class="section-returns">
<h5>Returns:</h5>
<dl class="param-type">
<dt>
Type:
</dt>
<dd>
<span class="param-type"><code>string</code></span>
</dd>
</dl>
<div class="param-desc">
<ul>
<li>Returns base64 decoded string</li>
</ul>
</div>
</div>
<div class="section-examples">
<h5>Example</h5>
<p class="code-caption">Example usage of base64decode.</p>
<pre class="prettyprint"><code>api.base64encode('hello world'); // returns 'aGVsbG8gd29ybGQ='</code></pre>
</div>
</div>
<div class="section-method">
<h4 class="name" id="base64encode"><span class="type-signature"></span>base64encode<span class="signature">(str)</span><span class="type-signature"> → {string}</span></h4>
<div class="description">
<p>Encode given string as base64</p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="Api.js.html">Api.js</a>, <a href="Api.js.html#line226">line 226</a>
</li></ul></dd>
</dl>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>str</code></td>
<td class="type">
<span class="param-type"><code>string</code></span>
</td>
<td class="description last">
<p>Input string to be encoded</p>
</td>
</tr>
</tbody>
</table>
<div class="section-returns">
<h5>Returns:</h5>
<dl class="param-type">
<dt>
Type:
</dt>
<dd>
<span class="param-type"><code>string</code></span>
</dd>
</dl>
<div class="param-desc">
<ul>
<li>Returns base64 encoded string</li>
</ul>
</div>
</div>
<div class="section-examples">
<h5>Example</h5>
<p class="code-caption">Example usage of base64encode.</p>
<pre class="prettyprint"><code>api.base64encode('aGVsbG8gd29ybGQ='); // returns 'hello world'</code></pre>
</div>
</div>
<div class="section-method">
<h4 class="name" id="baseUrl"><span class="type-signature"></span>baseUrl<span class="signature">()</span><span class="type-signature"> → {string}</span></h4>
<div class="description">
<p>Construct the base URL for this API (using other member variables)</p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="Api.js.html">Api.js</a>, <a href="Api.js.html#line312">line 312</a>
</li></ul></dd>
</dl>
<div class="section-returns">
<h5>Returns:</h5>
<dl class="param-type">
<dt>
Type:
</dt>
<dd>
<span class="param-type"><code>string</code></span>
</dd>
</dl>
<div class="param-desc">
<ul>
<li>Returns the constructed BaseUrl (root + stage + prefix + version)</li>
</ul>
</div>
</div>
</div>
<div class="section-method">
<h4 class="name" id="buildUrl"><span class="type-signature"></span>buildUrl<span class="signature">(path)</span><span class="type-signature"> → {string}</span></h4>
<div class="description">
<p>Build a request URL for a specific path</p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="Api.js.html">Api.js</a>, <a href="Api.js.html#line327">line 327</a>
</li></ul></dd>
</dl>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>path</code></td>
<td class="type">
<span class="param-type"><code>string</code></span>
</td>
<td class="description last">
<p>Build full request url for path</p>
</td>
</tr>
</tbody>
</table>
<div class="section-returns">
<h5>Returns:</h5>
<dl class="param-type">
<dt>
Type:
</dt>
<dd>
<span class="param-type"><code>string</code></span>
</dd>
</dl>
<div class="param-desc">
<ul>
<li>Fully constructed request url</li>
</ul>
</div>
</div>
</div>
<div class="section-method">
<h4 class="name" id="callRoute"><span class="type-signature">(async) </span>callRoute<span class="signature">(args)</span><span class="type-signature"> → {Promise.<object>}</span></h4>
<div class="description">
<p>Generic request execution method. For a GET request, only the "path" parameter is required</p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="Api.js.html">Api.js</a>, <a href="Api.js.html#line345">line 345</a>
</li></ul></dd>
</dl>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>args</code></td>
<td class="type">
<span class="param-type"><code>object</code></span>
</td>
<td class="description last">
<p>Arguments to call api route</p>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Attributes</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>path</code></td>
<td class="type">
<span class="param-type"><code>string</code></span>
</td>
<td class="attributes">
</td>
<td class="description last">
<p>Relative api path to fetch</p>
</td>
</tr>
<tr>
<td class="name"><code>options</code></td>
<td class="type">
<span class="param-type"><code><a href="global.html#FetchOptions">FetchOptions</a></code></span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="description last">
<p>Additional fetch options (will be assigned on top of <a href="global.html#defaults"><code>defaults</code></a>)</p>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<div class="section-returns">
<h5>Returns:</h5>
<dl class="param-type">
<dt>
Type:
</dt>
<dd>
<span class="param-type"><code>Promise.<object></code></span>
</dd>
</dl>
<div class="param-desc">
<ul>
<li>Returns promise with response data</li>
</ul>
</div>
</div>
</div>
<div class="section-method">
<h4 class="name" id="delete"><span class="type-signature">(async) </span>delete<span class="signature">(path, options<span class="signature-attributes">opt</span>)</span><span class="type-signature"> → {Promise.<object>}</span></h4>
<div class="description">
<p>Query API route for <code>path</code> using the DELETE method with a body</p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="Api.js.html">Api.js</a>, <a href="Api.js.html#line522">line 522</a>
</li></ul></dd>
</dl>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Attributes</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>path</code></td>
<td class="type">
<span class="param-type"><code>string</code></span>
</td>
<td class="attributes">
</td>
<td class="description last">
<p>Request path</p>
</td>
</tr>
<tr>
<td class="name"><code>options</code></td>
<td class="type">
<span class="param-type"><code>object</code></span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="description last">
<p>Additional fetch options</p>
</td>
</tr>
</tbody>
</table>
<div class="section-returns">
<h5>Returns:</h5>
<dl class="param-type">
<dt>
Type:
</dt>
<dd>
<span class="param-type"><code>Promise.<object></code></span>
</dd>
</dl>
<div class="param-desc">
<ul>
<li>Returns promise with response data</li>
</ul>
</div>
</div>
<div class="section-examples">
<h5>Example</h5>
<p class="code-caption">Example usage of delete.</p>
<pre class="prettyprint"><code>var api = new Api({ root: 'api.example.com' })
api.delete('todos/1')
.then(resp => {
// resp = response from DELETE https://api.example.com/todos/1
})
.catch(err => {
// Something went wrong
})</code></pre>
</div>
</div>
<div class="section-method">
<h4 class="name" id="get"><span class="type-signature">(async) </span>get<span class="signature">(options<span class="signature-attributes">opt</span>)</span><span class="type-signature"> → {Promise.<object>}</span></h4>
<div class="description">
<p>Helper for simple GET requests (only need to call "api.get('something')")</p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="Api.js.html">Api.js</a>, <a href="Api.js.html#line404">line 404</a>
</li></ul></dd>
</dl>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Attributes</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>options</code></td>
<td class="type">
<span class="param-type"><code>object</code></span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="description last">
<p>Additional fetch options</p>
</td>
</tr>
</tbody>
</table>
<div class="section-returns">
<h5>Returns:</h5>
<dl class="param-type">
<dt>
Type:
</dt>
<dd>
<span class="param-type"><code>Promise.<object></code></span>
</dd>
</dl>
<div class="param-desc">
<ul>
<li>Returns promise with response data</li>
</ul>
</div>
</div>
<div class="section-examples">
<h5>Example</h5>
<p class="code-caption">Example usage of get.</p>
<pre class="prettyprint"><code>var api = new Api({ root: 'api.example.com' })
api.get('todos')
.then(resp => {
// resp = response from GET https://api.example.com/todos
})
.catch(err => {
// Something went wrong
})</code></pre>
</div>
</div>
<div class="section-method">
<h4 class="name" id="handleEvents"><span class="type-signature"></span>handleEvents<span class="signature">(response)</span><span class="type-signature"></span></h4>
<div class="description">
<p>Process event emitters for response</p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="Api.js.html">Api.js</a>, <a href="Api.js.html#line142">line 142</a>
</li></ul></dd>
</dl>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>response</code></td>
<td class="type">
<span class="param-type"><code>object</code></span>
</td>
<td class="description last">
<p>Fetch response to trigger events</p>
</td>
</tr>
</tbody>
</table>
</div>
<div class="section-method">
<h4 class="name" id="handleMiddlewares"><span class="type-signature"></span>handleMiddlewares<span class="signature">(response)</span><span class="type-signature"></span></h4>
<div class="description">
<p>Process middleware chain</p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="Api.js.html">Api.js</a>, <a href="Api.js.html#line132">line 132</a>
</li></ul></dd>
</dl>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>response</code></td>
<td class="type">
<span class="param-type"><code>object</code></span>
</td>
<td class="description last">
<p>Fetch response to be passed through middleware chain</p>
</td>
</tr>
</tbody>
</table>
</div>
<div class="section-method">
<h4 class="name" id="hasValidToken"><span class="type-signature"></span>hasValidToken<span class="signature">()</span><span class="type-signature"> → {boolean}</span></h4>
<div class="description">
<p>Check if this API instance has a valid token</p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="Api.js.html">Api.js</a>, <a href="Api.js.html#line201">line 201</a>
</li></ul></dd>
</dl>
<div class="section-returns">
<h5>Returns:</h5>
<dl class="param-type">
<dt>
Type:
</dt>
<dd>
<span class="param-type"><code>boolean</code></span>
</dd>
</dl>
<div class="param-desc">
<ul>
<li>Whether or not this API instance has a valid token</li>
</ul>
</div>
</div>
</div>
<div class="section-method">
<h4 class="name" id="head"><span class="type-signature">(async) </span>head<span class="signature">(path, options<span class="signature-attributes">opt</span>)</span><span class="type-signature"> → {Promise.<object>}</span></h4>
<div class="description">
<p>Query API route for <code>path</code> using the HEAD method (no response body)</p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="Api.js.html">Api.js</a>, <a href="Api.js.html#line553">line 553</a>
</li></ul></dd>
</dl>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Attributes</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>path</code></td>
<td class="type">
<span class="param-type"><code>string</code></span>
</td>
<td class="attributes">
</td>
<td class="description last">
<p>Request path</p>
</td>
</tr>
<tr>
<td class="name"><code>options</code></td>
<td class="type">
<span class="param-type"><code>object</code></span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="description last">
<p>Additional fetch options</p>
</td>
</tr>
</tbody>
</table>
<div class="section-returns">
<h5>Returns:</h5>
<dl class="param-type">
<dt>
Type:
</dt>
<dd>
<span class="param-type"><code>Promise.<object></code></span>
</dd>
</dl>
<div class="param-desc">
<ul>
<li>Returns promise with response data</li>
</ul>
</div>
</div>
<div class="section-examples">
<h5>Example</h5>
<p class="code-caption">Example usage of head.</p>
<pre class="prettyprint"><code>var api = new Api({ root: 'api.example.com' })
api.head('todos', payload)
.then(resp => {
// response from HEAD https://api.example.com/todos (no response body)
// resp = {
// url: 'https://api.example.com/todos',
// status: 200,
// statusText: 'OK',
// headers: { ... }
// }
})
.catch(err => {
// Something went wrong
})</code></pre>
</div>
</div>
<div class="section-method">
<h4 class="name" id="init"><span class="type-signature"></span>init<span class="signature">()</span><span class="type-signature"></span></h4>
<div class="description">
<p>Initialize API instance</p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="Api.js.html">Api.js</a>, <a href="Api.js.html#line88">line 88</a>
</li></ul></dd>
</dl>
<div class="section-throws">
<h5>Throws:</h5>
<dl>
<dt>
<div class="param-desc">
<p>Unable to load fetch library via NodeJS or window.fetch</p>
</div>
</dt>
<dd></dd>
<dt>
<dl>
<dt>
Type
</dt>
<dd>
<span class="param-type"><code>Error</code></span>
</dd>
</dl>
</dt>
<dd></dd>
</dl>
</div>
</div>
<div class="section-method">
<h4 class="name" id="isAuthorized"><span class="type-signature"></span>isAuthorized<span class="signature">()</span><span class="type-signature"> → {boolean}</span></h4>
<div class="description">
<p>Check if this API instance is authorized</p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="Api.js.html">Api.js</a>, <a href="Api.js.html#line211">line 211</a>
</li></ul></dd>
</dl>
<div class="section-returns">
<h5>Returns:</h5>
<dl class="param-type">
<dt>
Type:
</dt>
<dd>
<span class="param-type"><code>boolean</code></span>
</dd>
</dl>
<div class="param-desc">
<ul>
<li>If this API instance has <code>settings.authorization</code> set and a valid token</li>
</ul>
</div>
</div>
</div>
<div class="section-method">
<h4 class="name" id="patch"><span class="type-signature">(async) </span>patch<span class="signature">(path, body<span class="signature-attributes">opt</span>, options<span class="signature-attributes">opt</span>)</span><span class="type-signature"> → {Promise.<object>}</span></h4>
<div class="description">
<p>Query API route for <code>path</code> using the PATCH method with a body</p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="Api.js.html">Api.js</a>, <a href="Api.js.html#line497">line 497</a>
</li></ul></dd>
</dl>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Attributes</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>path</code></td>
<td class="type">
<span class="param-type"><code>string</code></span>
</td>
<td class="attributes">
</td>
<td class="description last">
<p>Request path</p>
</td>
</tr>
<tr>
<td class="name"><code>body</code></td>
<td class="type">
<span class="param-type"><code>object</code></span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="description last">
<p>Request payload</p>
</td>
</tr>
<tr>
<td class="name"><code>options</code></td>
<td class="type">
<span class="param-type"><code>object</code></span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="description last">
<p>Additional fetch options</p>
</td>
</tr>
</tbody>
</table>
<div class="section-returns">
<h5>Returns:</h5>
<dl class="param-type">
<dt>
Type:
</dt>
<dd>
<span class="param-type"><code>Promise.<object></code></span>
</dd>
</dl>
<div class="param-desc">
<ul>
<li>Returns promise with response data</li>
</ul>
</div>
</div>
<div class="section-examples">
<h5>Example</h5>
<p class="code-caption">Example usage of patch.</p>
<pre class="prettyprint"><code>var api = new Api({ root: 'api.example.com' })
var payload = {
todo: {
name: 'new name (patched)'
}
}
api.patch('todos/1', payload)
.then(resp => {
// resp = response from PATCH https://api.example.com/todos/1
})
.catch(err => {
// Something went wrong
})</code></pre>
</div>
</div>
<div class="section-method">
<h4 class="name" id="post"><span class="type-signature">(async) </span>post<span class="signature">(path, body<span class="signature-attributes">opt</span>, options<span class="signature-attributes">opt</span>)</span><span class="type-signature"> → {Promise.<object>}</span></h4>
<div class="description">
<p>Query API route for <code>path</code> using the POST method with a body</p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="Api.js.html">Api.js</a>, <a href="Api.js.html#line433">line 433</a>
</li></ul></dd>
</dl>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Attributes</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>path</code></td>
<td class="type">
<span class="param-type"><code>string</code></span>
</td>
<td class="attributes">
</td>
<td class="description last">
<p>Request path</p>
</td>
</tr>
<tr>
<td class="name"><code>body</code></td>
<td class="type">
<span class="param-type"><code>object</code></span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="description last">
<p>Request payload</p>
</td>
</tr>
<tr>
<td class="name"><code>options</code></td>
<td class="type">
<span class="param-type"><code>object</code></span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="description last">
<p>Additional fetch options</p>
</td>
</tr>
</tbody>
</table>
<div class="section-returns">
<h5>Returns:</h5>
<dl class="param-type">
<dt>
Type:
</dt>
<dd>
<span class="param-type"><code>Promise.<object></code></span>
</dd>
</dl>
<div class="param-desc">
<ul>
<li>Returns promise with response data</li>
</ul>
</div>
</div>
<div class="section-examples">
<h5>Example</h5>
<p class="code-caption">Example usage of post.</p>
<pre class="prettyprint"><code>var api = new Api({ root: 'api.example.com' })
var payload = {
todo: {
name: 'foobar'
}
}
api.post('todos', payload)
.then(resp => {
// resp = response from POST https://api.example.com/todos
})
.catch(err => {
// Something went wrong
})</code></pre>
</div>
</div>
<div class="section-method">
<h4 class="name" id="put"><span class="type-signature">(async) </span>put<span class="signature">(path, body<span class="signature-attributes">opt</span>, options<span class="signature-attributes">opt</span>)</span><span class="type-signature"> → {Promise.<object>}</span></h4>
<div class="description">
<p>Query API route for <code>path</code> using the PUT method with a body</p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="Api.js.html">Api.js</a>, <a href="Api.js.html#line465">line 465</a>
</li></ul></dd>
</dl>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Attributes</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>path</code></td>
<td class="type">
<span class="param-type"><code>string</code></span>
</td>
<td class="attributes">
</td>
<td class="description last">
<p>Request path</p>
</td>
</tr>
<tr>
<td class="name"><code>body</code></td>
<td class="type">
<span class="param-type"><code>object</code></span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="description last">
<p>Request payload</p>
</td>
</tr>
<tr>
<td class="name"><code>options</code></td>
<td class="type">
<span class="param-type"><code>object</code></span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="description last">
<p>Additional fetch options</p>
</td>
</tr>
</tbody>
</table>
<div class="section-returns">
<h5>Returns:</h5>
<dl class="param-type">
<dt>
Type:
</dt>
<dd>
<span class="param-type"><code>Promise.<object></code></span>
</dd>
</dl>
<div class="param-desc">
<ul>
<li>Returns promise with response data</li>
</ul>
</div>
</div>
<div class="section-examples">
<h5>Example</h5>
<p class="code-caption">Example usage of put.</p>
<pre class="prettyprint"><code>var api = new Api({ root: 'api.example.com' })
var payload = {
todo: {
name: 'new name'
}
}
api.put('todos/1', payload)
.then(resp => {
// resp = response from PUT https://api.example.com/todos/1
})
.catch(err => {
// Something went wrong
})</code></pre>
</div>
</div>
<div class="section-method">
<h4 class="name" id="sanitize"><span class="type-signature"></span>sanitize<span class="signature">()</span><span class="type-signature"></span></h4>
<div class="description">
<p>Sanitize all API variables for this API instance</p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="Api.js.html">Api.js</a>, <a href="Api.js.html#line164">line 164</a>
</li></ul></dd>
</dl>
<div class="section-throws">
<h5>Throws:</h5>
<dl>
<dt>
<div class="param-desc">
<p>API root must be HTTPS if secureOnly is true in the <a href="global.html#ApiSettings"><code>ApiSettings</code></a></p>
</div>
</dt>
<dd></dd>
<dt>
<dl>
<dt>
Type
</dt>
<dd>
<span class="param-type"><code>Error</code></span>
</dd>
</dl>
</dt>
<dd></dd>
</dl>
</div>
</div>
<div class="section-method">
<h4 class="name" id="use"><span class="type-signature"></span>use<span class="signature">(fn)</span><span class="type-signature"></span></h4>
<div class="description">
<p>Register middleware</p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="Api.js.html">Api.js</a>, <a href="Api.js.html#line124">line 124</a>
</li></ul></dd>
</dl>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>fn</code></td>
<td class="type">
<span class="param-type"><code>function</code></span>
</td>
<td class="description last">
<p>Middleware function (must resolve with response)</p>
</td>
</tr>
</tbody>
</table>
</div>
<div class="section-method">
<h4 class="name" id="validate"><span class="type-signature"></span>validate<span class="signature">()</span><span class="type-signature"></span></h4>
<div class="description">
<p>Validate this API instance</p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="Api.js.html">Api.js</a>, <a href="Api.js.html#line296">line 296</a>
</li></ul></dd>
</dl>
<div class="section-throws">
<h5>Throws:</h5>
<dl>
<dt>
<div class="param-desc">
<ul>
<li>If required schema is invalid</li>
</ul>
</div>
</dt>
<dd></dd>
<dt>
<dl>
<dt>
Type
</dt>
<dd>
<span class="param-type"><code>Error</code></span>
</dd>
</dl>
</dt>
<dd></dd>
</dl>
</div>
</div>
<div class="section-method">
<h4 class="name" id="varOrFn"><span class="type-signature"></span>varOrFn<span class="signature">(ref, …args)</span><span class="type-signature"> → {*}</span></h4>
<div class="description">
<p>If ref is a function, execute with optional params.
Otherwise, return value of ref</p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="Api.js.html">Api.js</a>, <a href="Api.js.html#line257">line 257</a>
</li></ul></dd>
</dl>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Attributes</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>ref</code></td>
<td class="type">
<span class="param-type"><code>*</code></span>
</td>
<td class="attributes">
</td>
<td class="description last">
</td>
</tr>
<tr>
<td class="name"><code>args</code></td>
<td class="type">
<span class="param-type"><code>*</code></span>
</td>
<td class="attributes">
<repeatable><br>
</td>
<td class="description last">
</td>
</tr>
</tbody>
</table>
<div class="section-returns">
<h5>Returns:</h5>
<dl class="param-type">
<dt>
Type:
</dt>
<dd>
<span class="param-type"><code>*</code></span>
</dd>
</dl>
<div class="param-desc">
<ul>
<li>Return value of ref(...args) or ref</li>
</ul>
</div>
</div>
</div>
</article>
</section>
</div>
<br class="clear">
<footer>
Generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Wed Oct 09 2019 19:05:25 GMT-0600 (MDT) using the Minami theme.
</footer>
<script>prettyPrint();</script>
<script src="scripts/linenumber.js"></script>
</body>
</html>