All Projects → bukalapak → bukalapak.js

bukalapak / bukalapak.js

Licence: other
Javascript API wrapper

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to bukalapak.js

the-movie-db
Promised based Javascript API wrapper for https://www.themoviedb.org/ that works in the browser and node.js.
Stars: ✭ 35 (-2.78%)
Mutual labels:  javascript-api-wrapper
faceapp.js
JavaScript API wrapper for the FaceApp tool for Android and iOS
Stars: ✭ 59 (+63.89%)
Mutual labels:  javascript-api-wrapper

bukalapak.js

Build Status Code Climate Test Coverage npm version

Bukalapak API javascript wrapper.

Usage

// initialization
let client = new Bukalapak({ baseUrl: 'https://api.bukalapak.com/', storage: localStorage });

// use auth adapter and api adapter
client.useAdapter('auth', { clientId: 'abcdef', clientSecret: '1234567', baseUrl: 'https://accounts.bukalapak.com' });
client.useAdapter('api');

// read-only operation, return promise and auto include `Authorization` header with token from client_credentials
client.get('/products', { query: { keywords: 'thinkpad' } });
client.api.products({ keywords: 'thinkpad' }); // shortcut

// client, now have `auth` method
client.auth.login('[email protected]', 's3cr3t-p4ssw0rd');

// accessing endpoint, return promise and auto include `Authorization` header with token from resource_owner_password
// it will auto-refresh token when it's expired.
client.get('/me');
client.api.me(); // shortcut

// remove username and password pair, and use client_credentials token instead
client.auth.logout();

// That's it!

// Bonus: full example on how to handle promise based response
function logResponse (response) {
  console.log(response.status);
  console.log(response.statusText);
  console.log(response.headers.get('Content-Type'));
  console.log('---------------------------');

  return response;
}

function checkStatus (response) {
  if (response.status >= 200 && response.status < 300) {
    return response;
  } else {
    var error = new Error(response.statusText);
    error.response = response;
    throw error;
  }
}

function parseJSON (response) {
  return response.json();
}

client.api.products({ keywords: 'thinkpad' })
  .then(logResponse)
  .then(checkStatus)
  .then(parseJSON)
  .then(function (data) { console.log(data); })
  .catch(function (error) { console.log('ERROR: ', error); });

There are two optional dependencies depends on your usage:

  • You must bring your own ES2015 Promise compatible polyfill if you need to support older browsers
  • You must provide localStorage compatible when your environment does not support, or you want to use more advanced storage (see below)

Storage Support

If you want to have storage support on node, then you can use node-localstorage

let LocalStorage = require('node-localstorage').LocalStorage;
let localStorage = new LocalStorage('./local_storage');
let client = new Bukalapak({ baseUrl: 'https://api.bukalapak.com/', storage: localStorage });

Or if you want to use more advanced storage like localForage, then you can do:

let storage = localforage.createInstance({ name: 'bukalapak' });
let client = new Bukalapak({ baseUrl: 'https://api.bukalapak.com/', storage: storage, storageOptions: { serialize: false } });
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].