0.0.3 • Published 6 years ago

bootic_client_js v0.0.3

Weekly downloads
-
License
ISC
Repository
-
Last release
6 years ago

Javascript client for the Bootic API

API docs: https://developers.bootic.net/

Usage in the browser

<script src="dist/bootic_client.js"></script>
<script>
  var client = new bootic.Client({
    accessToken: "abc"
  })

  // the root
  client.root().then(function(data){
    console.log('the root', data)
  })
</script>

Following links

Client#run(link, params) takes a link object and optional parameters

var link = {
  href: "https://api.bootic.net/v1/products",
  method: "get"
}

client.run(link, {q: "sale"})
  .then(function(data){
    console.log('product data', data)
  })

So you can follow links contained in API responses by chaining promises:

var myShop = function(root) { return root.shops[0] }
var myProducts = function(shop) { return client.run(shop._links["btc:products"]) }

client.root().then(myShop).then(myProducts).then(function(prods) {
  console.log('products', prods)
})

Refreshing access token

Bootic access tokens are short-lived. You can configure the client to run a function if the API returns a status 401 and re-authorize the client.

// tell the client what to do when the access token expires
client.onUnauthorized(function(client, next) {
  // get a new token from your server
  fetch("/token").then(function(r) {
    return r.json()
  }).then(function (data) {
    // authorize the client
    client.authorize(data.token)
    // don't forget to call next()
    next()
  })
})

When encountering an unauthorized response, the client will run this function and try again, transparently.

var client = new bootic.Client({accessToken: "nope"}).onUnauthorized(function(client, next){ ... })

// this will fail with a 401 the first time, fetch a new token and try again
client.root().then(function(root) {
  console.log('success!', root)
})

Handling Forbidden responses

You can optionally attach an onForbidden handler.

var client = client.onForbidden(function (cl, next) {
  window.location = "/login"
  next()
})

import

import {Client} from `bootic_client_js`

Development

npm install
npm test

Build with webpack