# @lokavaluto/lokapi-browser

> Lokavaluto API

Latest version **0.1.0** (published 2023-05-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install @lokavaluto/lokapi-browser
pnpm add @lokavaluto/lokapi-browser
yarn add @lokavaluto/lokapi-browser
bun add @lokavaluto/lokapi-browser
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2023-05-08 |
| First published | 2021-11-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 30 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Valentin LAB |
| Maintainers | vaab |

## Links

- npm: https://www.npmjs.com/package/@lokavaluto/lokapi-browser
- Repository: git@github.com:Lokavaluto/lokapi-browser
- npm.io page: https://npm.io/package/@lokavaluto/lokapi-browser

## Dependencies (3)

- [@0k/types-request](https://npm.io/package/@0k/types-request.md) ^1.0.1
- [@lokavaluto/lokapi](https://npm.io/package/@lokavaluto/lokapi.md) 0.1.0 || >=0.1.1-alpha
- [@0k/browser-request](https://npm.io/package/@0k/browser-request.md) ^1.0.0

## Recent versions

- 0.1.0 (latest) — 2023-05-08
- 0.0.3-alpha.202304230054 — 2023-04-22
- 0.0.3-alpha.202303021556 — 2023-03-02
- 0.0.3-alpha.202209291633 — 2022-09-29
- 0.0.3-alpha.202206151833 — 2022-06-15
- 0.0.3-alpha.202206021232 — 2022-06-02
- 0.0.3-alpha.202205021026 — 2022-05-02
- 0.0.3-alpha.202202050825 — 2022-02-05
- 0.0.3-alpha.202202021805 — 2022-02-02
- 0.0.3-alpha.202111271557 — 2021-11-27
- 0.0.3-alpha.202111261926 — 2021-11-26
- 0.0.2 — 2021-11-24

## README

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:

```sh
npm install --save @lokavaluto/lokapi-browser
```

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

```sh
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

```typescript
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:

```typescript
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

```sh
npm install
```

## Transpilation to JavaScript

```sh
npx tsc
```

or with watch support:

```sh
npx tsc -w
```
# Changelog


## 0.1.0 (2023-05-08)

### New

* Export ``LocalStore`` for convenience. [Valentin Lab]

### 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.

---
_Source: https://npm.io/package/@lokavaluto/lokapi-browser · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
