Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeroen Peeters committed Sep 14, 2018
2 parents 1ead8e3 + e33d9e3 commit 910fb41
Show file tree
Hide file tree
Showing 24 changed files with 318 additions and 235 deletions.
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,48 @@ yarn add @nosplatform/api-functions
```

## Usage in react
Wrap your component with the higher-order components to provide fallbacks when running outside the
context of the [nOS client](https://github.com/nos/client). Specify `propTypes` provided by this
package.

```js
import React from 'react';
import { compose } from 'recompose';
import { injectNOS, injectAssets, nosProps, assetProps } from "@nosplatform/api-functions/lib/react";

class ShowBalance extends React.Component {
static propTypes = {
nos: nosProps.isRequired,
assets: assetProps.isRequired
}

render() {
return (
<button type="button" onClick={this.handleClick}>
Show NEO Balance
</button>
);
}

async handleClick = () => {
const { nos, assets } = this.props;
const balance = await nos.getBalance({ asset: assets.NEO });
console.log('NEO Balance:', balance);
}
};

export default compose(
injectNOS,
injectAssets
)(ShowBalance);
```
import { injectNOS, nosProps } from "@nosplatform/api-functions/lib/react";

In addition to automatically providing the NOS API function as a prop to your React component, the
api-functions package also provides the opportunity to specify a fallback implementation. This is
especially useful for building in the context of another browser if not wanting to use the nOS
client for any reason.

```js
const balance = await nos.getBalance({ asset: assets.NEO }, () => '23');
console.log('NEO Balance:', balance); // NEO Balance: 23
```
27 changes: 0 additions & 27 deletions es6/index.js

This file was deleted.

16 changes: 0 additions & 16 deletions es6/lib.js

This file was deleted.

4 changes: 0 additions & 4 deletions es6/react/index.js

This file was deleted.

9 changes: 0 additions & 9 deletions es6/react/inject.js

This file was deleted.

14 changes: 0 additions & 14 deletions es6/react/props.js

This file was deleted.

18 changes: 18 additions & 0 deletions lib/assets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;

var _exists = _interopRequireDefault(require("./exists"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/** @const {object} assets bind assets constant to variable */
var assets = _exists.default ? window.NOS.ASSETS : {
NEO: 'c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b',
GAS: '602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7'
};
var _default = assets;
exports.default = _default;
29 changes: 13 additions & 16 deletions lib/lib.js → lib/executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,29 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useFallback = exports.executor = exports.nos = exports.exists = void 0;
exports.default = void 0;

/** @const {boolean} exists check whether nOS is defined */
var exists = !!window.NOS && !!window.NOS.V1;
/** @const {object} nos bind nOS api to variable */

exports.exists = exists;
var nos = exists ? window.NOS.V1 : undefined;
/** @function executor executes the nOS API function */
var _exists = _interopRequireDefault(require("./exists"));

exports.nos = nos;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var executor = function executor(func, fallback, config) {
return exists ? nos[func](config) : useFallback(fallback, config);
};
/** @const {object} nos bind nOS api to variable */
var nos = _exists.default ? window.NOS.V1 : undefined;
/**
* @function useFallback
* @param {function} fallback checks if fallback function exists, otherwise return nothing
* @param {object} args potential arguments that are passed along
*/


exports.executor = executor;

var useFallback = function useFallback(fallback, args) {
return fallback ? fallback(args) : undefined;
};
/** @function executor executes the nOS API function */


var executor = function executor(func, fallback, config) {
return _exists.default ? nos[func](config) : useFallback(fallback, config);
};

exports.useFallback = useFallback;
var _default = executor;
exports.default = _default;
11 changes: 11 additions & 0 deletions lib/exists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;

/** @const {boolean} exists check whether nOS is defined */
var exists = !!window.NOS;
var _default = exists;
exports.default = _default;
34 changes: 15 additions & 19 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,21 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
Object.defineProperty(exports, "assets", {
enumerable: true,
get: function get() {
return _assets.default;
}
});
Object.defineProperty(exports, "nos", {
enumerable: true,
get: function get() {
return _nos.default;
}
});

var _bindings = require("./bindings");
var _assets = _interopRequireDefault(require("./assets"));

var _lib = require("./lib");
var _nos = _interopRequireDefault(require("./nos"));

/**
* @nosplatform/api-functions
* API bindings (with fallback) and types for nOS dApps
* @author Jeroen Peeters <[email protected]>
*/
var _default = {
claimGas: _bindings.claimGas,
exists: _lib.exists,
getAddress: _bindings.getAddress,
getBalance: _bindings.getBalance,
getStorage: _bindings.getStorage,
invoke: _bindings.invoke,
send: _bindings.send,
testInvoke: _bindings.testInvoke
};
exports.default = _default;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
58 changes: 36 additions & 22 deletions lib/bindings.js → lib/nos.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getStorage = exports.invoke = exports.testInvoke = exports.send = exports.claimGas = exports.getBalance = exports.getAddress = void 0;
exports.default = void 0;

var _lib = require("./lib");
var _exists = _interopRequireDefault(require("./exists"));

var _executor = _interopRequireDefault(require("./executor"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// Wallet Actions

Expand All @@ -14,7 +18,7 @@ var _lib = require("./lib");
* @param {function} fallback fallback function if outside of nOS browser
*/
var getAddress = function getAddress(fallback) {
return (0, _lib.executor)('getAddress', fallback);
return (0, _executor.default)("getAddress", fallback);
};
/**
* @function getBalance
Expand All @@ -23,21 +27,27 @@ var getAddress = function getAddress(fallback) {
*/


exports.getAddress = getAddress;

var getBalance = function getBalance(config, fallback) {
return (0, _lib.executor)('getBalance', fallback, config);
return (0, _executor.default)("getBalance", fallback, config);
};
/**
* @function claimGas
* @function getLastBlock
* @param {object} config All possible arguments and flags are passed as childs of this object
* @param {function} fallback fallback function if outside of nOS browser
*/


exports.getBalance = getBalance;
var getLastBlock = function getLastBlock(config, fallback) {
return (0, _executor.default)("getLastBlock", fallback, config);
};
/**
* @function claimGas
* @param {function} fallback fallback function if outside of nOS browser
*/


var claimGas = function claimGas(fallback) {
return (0, _lib.executor)('claimGas', fallback);
return (0, _executor.default)("claimGas", fallback);
};
/**
* @function send
Expand All @@ -46,10 +56,8 @@ var claimGas = function claimGas(fallback) {
*/


exports.claimGas = claimGas;

var send = function send(config, fallback) {
return (0, _lib.executor)('send', fallback, config);
return (0, _executor.default)("send", fallback, config);
}; // Smart Contract Actions

/**
Expand All @@ -59,10 +67,8 @@ var send = function send(config, fallback) {
*/


exports.send = send;

var testInvoke = function testInvoke(config, fallback) {
return (0, _lib.executor)('testInvoke', fallback, config);
return (0, _executor.default)("testInvoke", fallback, config);
};
/**
* @function invoke
Expand All @@ -71,10 +77,8 @@ var testInvoke = function testInvoke(config, fallback) {
*/


exports.testInvoke = testInvoke;

var invoke = function invoke(config, fallback) {
return (0, _lib.executor)('invoke', fallback, config);
return (0, _executor.default)("invoke", fallback, config);
};
/**
* @function getStorage
Expand All @@ -83,10 +87,20 @@ var invoke = function invoke(config, fallback) {
*/


exports.invoke = invoke;

var getStorage = function getStorage(config, fallback) {
return (0, _lib.executor)('getStorage', fallback, config);
return (0, _executor.default)("getStorage", fallback, config);
};

exports.getStorage = getStorage;
var nos = {
claimGas: claimGas,
exists: _exists.default,
getAddress: getAddress,
getBalance: getBalance,
getLastBlock: getLastBlock,
getStorage: getStorage,
invoke: invoke,
send: send,
testInvoke: testInvoke
};
var _default = nos;
exports.default = _default;
Loading

0 comments on commit 910fb41

Please sign in to comment.