bowtie-co/node-api

View on GitHub
docs/index.html

Summary

Maintainability
Test Coverage
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Home - 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">
    

    



    









    


    <section class="readme">
        <article><p><a href="https://codeclimate.com/github/bowtie-co/node-api/maintainability"><img src="https://api.codeclimate.com/v1/badges/974bf224f3870818bc16/maintainability" alt="Maintainability"></a>
<a href="https://codeclimate.com/github/bowtie-co/node-api/test_coverage"><img src="https://api.codeclimate.com/v1/badges/974bf224f3870818bc16/test_coverage" alt="Test Coverage"></a></p>
<p><a href="https://travis-ci.org/bowtie-co/node-api"><img src="https://img.shields.io/travis/bowtie-co/node-api.svg?style=flat-square" alt="build status"></a>
<a href="https://www.npmjs.com/package/@bowtie/api"><img src="https://img.shields.io/npm/v/@bowtie/api.svg?style=flat-square" alt="npm version"></a>
<a href="https://nodejs.org"><img src="https://img.shields.io/node/v/@bowtie/api.svg?style=flat-square" alt="node version"></a>
<a href="https://www.npmjs.com/package/@bowtie/api"><img src="https://img.shields.io/npm/dt/@bowtie/api.svg?style=flat-square" alt="npm downloads"></a>
<a href="https://github.com/bowtie-co/node-api/graphs/contributors"><img src="https://img.shields.io/github/contributors/bowtie-co/node-api.svg?style=flat-square" alt="GitHub contributors"></a>
<a href="https://github.com/bowtie-co/node-api/blob/master/LICENSE"><img src="https://img.shields.io/npm/l/@bowtie/api.svg?style=flat-square" alt="license"></a></p>
<p><a href="https://github.com/bowtie-co/node-api/pulls"><img src="https://img.shields.io/github/issues-pr/bowtie-co/node-api.svg?style=flat-square" alt="GitHub pull requests"></a>
<a href="https://github.com/bowtie-co/node-api/pulls?utf8=%E2%9C%93&amp;q=is%3Apr+is%3Aclosed+"><img src="https://img.shields.io/github/issues-pr-closed/bowtie-co/node-api.svg?style=flat-square" alt="GitHub closed pull requests"></a>
<a href="https://github.com/bowtie-co/node-api/issues"><img src="https://img.shields.io/github/issues/bowtie-co/node-api.svg?style=flat-square" alt="GitHub issues"></a>
<a href="https://github.com/bowtie-co/node-api/issues?utf8=%E2%9C%93&amp;q=is%3Aissue+is%3Aclosed+"><img src="https://img.shields.io/github/issues-closed/bowtie-co/node-api.svg?style=flat-square" alt="GitHub closed issues"></a></p>
<h1>@bowtie/api</h1><p>JavaScript utilities and helpers</p>
<ul>
<li><a href="#installation">Installation</a></li>
<li><a href="#documentation">Documentation</a></li>
<li><a href="#basic-usage">Basic Usage</a><ul>
<li><a href="#get-example">GET Example</a></li>
<li><a href="#post-example">POST Example</a></li>
<li><a href="#put-example">PUT Example</a></li>
<li><a href="#delete-example">DELETE Example</a></li>
</ul>
</li>
<li><a href="#base-url-config">Base URL Config</a></li>
<li><a href="#authorization">Authorization</a><ul>
<li><a href="#basic-auth">Basic Auth</a></li>
<li><a href="#bearer-token">Bearer Token</a></li>
<li><a href="#custom-auth">Custom Auth</a></li>
</ul>
</li>
<li><a href="#ignore-security">Ignore Security</a></li>
<li><a href="#enable-debug-logs">Enable Debug Logs</a></li>
</ul>
<h2>Installation</h2><pre class="prettyprint source lang-bash"><code>npm install --save @bowtie/api</code></pre><h2>Documentation</h2><p><a href="https://bowtie-co.github.io/node-api">Documentation</a></p>
<h2>Basic Usage</h2><pre class="prettyprint source lang-javascript"><code>// Require @bowtie/api class definition
const Api = require('@bowtie/api')

// Create api instance
const api = new Api({
  root: 'api.example.com' // Will default to https:// if no protocol is provided here
})</code></pre><h4>GET Example</h4><pre class="prettyprint source lang-javascript"><code>// REST GET /todos - Get all todos
api.get('todos')
  .then(resp => {
    // resp = response from GET https://api.example.com/todos
  })
  .catch(err => {
    // Something went wrong
  })</code></pre><h4>POST Example</h4><pre class="prettyprint source lang-javascript"><code>// REST POST /todos - Create new todo
api.post('todos', { name: 'new todo' })
  .then(resp => {
    // resp = response from POST https://api.example.com/todos
  })
  .catch(err => {
    // Something went wrong
  })</code></pre><h4>PUT Example</h4><pre class="prettyprint source lang-javascript"><code>// REST PUT /todos/1 - Update todo with id=1
api.put('todos/1', { name: 'changed name' })
  .then(resp => {
    // resp = response from PUT https://api.example.com/todos/1
  })
  .catch(err => {
    // Something went wrong
  })</code></pre><h4>DELETE Example</h4><pre class="prettyprint source lang-javascript"><code>// REST DELETE /todos/1 - Delete todo with id=1
api.delete('todos/1')
  .then(resp => {
    // resp = response from DELETE https://api.example.com/todos/1
  })
  .catch(err => {
    // Something went wrong
  })</code></pre><h2>Base URL Config</h2><pre class="prettyprint source lang-javascript"><code>// Create api instance
const api = new Api({
  root: 'api.example.com', // Set beginning of the baseUrl to https://api.example.com
  stage: 'test',           // Append &quot;/test&quot; to baseUrl (i.e. https://api.example.com/test)
  prefix: 'api',           // Append &quot;/api&quot; to baseUrl  (i.e. https://api.example.com/test/api)
  version: 'v1'            // Append &quot;/v1&quot; to baseUrl   (i.e. https://api.example.com/test/api/v1)
})

// baseUrl is constructed as follows: (only api.root is required, any combination of stage/prefix/version is allowed)
baseUrl = `${api.root}/${api.stage}/${api.prefix}/${api.version}`;</code></pre><h2>Authorization</h2><h4>Basic Auth</h4><pre class="prettyprint source lang-javascript"><code>// Create api instance
const api = new Api({
  root: 'api.example.com', // Will default to https:// if no protocol is provided here
  authorization: 'Basic'   // Specify Basic authorization type for this api
})

// Authorize api for basic auth with username & password
api.authorize({
  username: 'user',
  password: 'pass'
})</code></pre><h4>Bearer Token</h4><pre class="prettyprint source lang-javascript"><code>// Create api instance
const api = new Api({
  root: 'api.example.com', // Will default to https:// if no protocol is provided here
  authorization: 'Bearer'  // Specify Bearer authorization type for this api
})

// Authorize with static token value
api.authorize({
  token: 'abc123'
})

// Authorize with dynamic token function
api.authorize({
  token: () => localStorage.getItem('access_token')
})</code></pre><h4>Custom Auth</h4><pre class="prettyprint source lang-javascript"><code>// Create api instance
const api = new Api({
  root: 'api.example.com', // Will default to https:// if no protocol is provided here
  authorization: 'Custom'  // Specify Custom authorization type for this api
})

// Authorize with static headers
api.authorize({
  headers: {
    'token': 'abc123'
  },
  validate: () => {
    // Function to validate the custom auth, returns true/false
    return true
  }
})

// Authorize with dynamic headers
api.authorize({
  token: () => ({
    'token': localStorage.getItem('access_token'),
    'uid': localStorage.getItem('uid')
  }),
  validate: () => {
    // Function to validate the custom auth, returns true/false
    return true
  }
})</code></pre><p>Example using the <a href="https://github.com/lynndylanhurley/j-toker">j-toker</a> package with headers from <a href="https://github.com/lynndylanhurley/devise_token_auth">devise_token_auth</a></p>
<pre class="prettyprint source lang-javascript"><code>const Auth = require('j-toker')

// Configure j-toker apiUrl to match api.baseUrl()
Auth.configure({
  apiUrl: api.baseUrl()
})

// Authorize with custom headers that retrieve authHeaders from j-toker &quot;Auth&quot; instance
api.authorize({
  headers: () => Auth.retrieveData('authHeaders'),
  validate: () => Auth.user.signedIn
})</code></pre><h2>Ignore security</h2><pre class="prettyprint source lang-javascript"><code>// Create api instance
const api = new Api({
  root: 'http://localhost:3000', // Use a local development API over HTTP (not secure HTTPS)
  secureOnly: false              // Disable secure HTTPS requirement
})</code></pre><h2>Enable Debug logs</h2><pre class="prettyprint source lang-javascript"><code>// Create api instance
const api = new Api({
  root: 'api.example.com', // Will default to https:// if no protocol is provided here
  verbose: true            // Set verbose=true to enable debug log output
})</code></pre></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>