0.1.0 • Published 1 year ago

@lokavaluto/lokapi-browser v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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

Changelog

0.0.3-alpha.202304230054

New

Changes

  • Switch to browser implementation. Valentin Lab

    The browser implementation uses fetch API and is more adapted and more compatible than previous one based on node interface.

    In particular, some incompatibility on iOS platform where found when loading package local files.

  • Chg: fix: compatibility with new exception location in @lokavaluto/lokapi !api. Valentin Lab

    Note that we broke also client API. The main class is now exported with RestExc, types and is as a consequence not anymore the default import.

Fix

  • Add missing timeout option support for correct polling. Valentin Lab

    This was used by lokapi-backend-comchain thanks to the jsc3l and could potentially freeze despite the timeout being specified.

  • Prevent double slash at beginning of path. Valentin Lab

  • Fill response headers as before. Valentin Lab

    In @0k.io/browser-request version 0.0.1, the response headers were not filled up correctly.