Skip to content

Lokavaluto/lokapi-browser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lokavaluto’s LokAPI for node and browsers

LokAPI is a JavaScript library intended to be used in mobile applications or web application to abstract all logics with lokavaluto’s server.

This packages holds implementation details that are usable for browser or node support and as such, this package brings you all functionality of the LokAPI on these platforms.

Adding @lokavaluto/lokapi-browser to your project

From the root of your project:

npm install --save @lokavaluto/lokapi-browser

Or better, as lokapi-browser is still in early release,

npm install --save Lokavaluto/lokapi-browser#master

To be sure to get the latest version, relaunch this last command whenever you want to update.

Setting up @lokavaluto/lokapi-browser

Subclassing LokAPIBrowserAbstract

To use @lokavaluto/lokapi, you’ll need to load backends. These manages the actual inner exchanges of a currency. Go check @lokavaluto/lokapi-backend-cyclos, or @lokavaluto/lokapi-backend-comchain for more information.

Once you’ve chosen your backends, you can instantiate the LokAPIBrowserAbstract. You’ll need to provide at least:

BackendFactories
A mapping of currency backends loaded
(optional) requestLogin
a function to trigger a login screen when automatic authorization fails or when no automatic autorization data exists. This will be triggered only if a protected request is made on the administration backend side.
(optional) requestLocalPassword
a function for backends to trigger a request to the user for a password that is meant to be kept locally on the device. This is typically used before payment with some backends (to see an example see package lokapi-backend-comchain), or administrative tasks. And takes usually the form of a popup.

Here’s an example using sweetalert2 as popup

import LokAPIBrowserAbstract from "@lokavaluto/lokapi-browser"

import comchain from '@lokavaluto/lokapi-backend-comchain'
import cyclos from '@lokavaluto/lokapi-backend-cyclos'

import Swal from "sweetalert2"


class LokAPI extends LokAPIBrowserAbstract {

  BackendFactories = {
    comchain,
    cyclos
  }

  requestLocalPassword = async function (state: string) {
    let text
    if (state === 'firstTry') {
      text = ''
    } else if (state === 'failedUnlock') {
      text = 'Failed to unlock ! Please retry...'
    }
    const ret = await Swal.fire({
      title: 'Enter your password',
      text,
      showCloseButton: true,
      input: 'password',
      inputLabel: 'wallet password',
      inputPlaceholder: 'your wallet password',
      inputAttributes: {
        maxlength: '32',
        autocapitalize: 'off',
        autocorrect: 'off'
      }
    })
    if (ret.isConfirmed) {
      return ret.value
    }
    throw new Error('User canceled the dialog box')
  }

  requestLogin() {
    // XXXvlab: for now `requestLogin` is not using the same
    // interface than `requestLocalPassword`. To make it work
    // meanwhile, use this callback to trigger a
    // `lokapi.login(..)`.
    console.log("Login requested !")
  }

}

Usage

Instantiating LokAPI

On instantiation time, LokAPI class will require you to provide:

host
as a string (example: “lokavaluto.fr”)
database
as a string (example: “myodoodb”)

For instance:

var lokAPI = new LokAPI('lokavaluto.fr', 'myodoodb')

Further usages

You can go check the documentation of the main package LokAPI

Developer

Building the project from fresh clone

npm install

Transpilation to JavaScript

npx tsc

or with watch support:

npx tsc -w