mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-15 01:55:48 +00:00
61 lines
2.3 KiB
Plaintext
61 lines
2.3 KiB
Plaintext
## Boilerplate
|
|
|
|
Use the following [boilerplate code](https://en.wikipedia.org/wiki/Boilerplate_code) to wrap your custom code using RippleAPI.
|
|
|
|
```javascript
|
|
const {RippleAPI} = require('ripple-lib');
|
|
|
|
const api = new RippleAPI({
|
|
server: 'wss://s1.ripple.com' // Public rippled server hosted by Ripple, Inc.
|
|
});
|
|
api.on('error', (errorCode, errorMessage) => {
|
|
console.log(errorCode + ': ' + errorMessage);
|
|
});
|
|
api.connect().then(() => {
|
|
/* insert code here */
|
|
}).then(() => {
|
|
return api.disconnect();
|
|
}).catch(console.error);
|
|
```
|
|
|
|
RippleAPI is designed to work in [NodeJS](https://nodejs.org) (version `0.12.0` or greater) using [Babel](https://babeljs.io/) for [ECMAScript 6](https://babeljs.io/docs/learn-es2015/) support.
|
|
|
|
The code samples in this documentation are written in ES6, but `RippleAPI` will work with ES5 also. Regardless of whether you use ES5 or ES6, the methods that return promises will return [ES6-style promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise).
|
|
|
|
<aside class="notice">
|
|
All the code snippets in this documentation assume that you have surrounded them with this boilerplate.
|
|
</aside>
|
|
|
|
<aside class="notice">
|
|
If you omit the "catch" section, errors may not be visible.
|
|
</aside>
|
|
|
|
<aside class="notice">
|
|
The "error" event is emitted whenever an error occurs that cannot be associated with a specific request. If the listener is not registered, an exception will be thrown whenever the event is emitted.
|
|
</aside>
|
|
|
|
### Parameters
|
|
|
|
The RippleAPI constructor optionally takes one argument, an object with the following options:
|
|
|
|
<%- renderSchema('input/api-options.json') %>
|
|
|
|
If you omit the `server` parameter, RippleAPI operates [offline](#offline-functionality).
|
|
|
|
|
|
### Installation ###
|
|
|
|
1. Install [NodeJS](https://nodejs.org) and the Node Package Manager (npm). Most Linux distros have a package for NodeJS, but make sure you have version `0.12.0` or higher.
|
|
2. Use npm to install [Babel](https://babeljs.io/) globally:
|
|
`npm install -g babel-cli`
|
|
3. Use npm to install RippleAPI:
|
|
`npm install ripple-lib`
|
|
|
|
After you have installed ripple-lib, you can create scripts using the [boilerplate](#boilerplate) and run them using babel-node:
|
|
`babel-node script.js`
|
|
|
|
<aside class="notice">
|
|
Instead of using babel-node in production, we recommend using Babel to transpile to ECMAScript 5 first.
|
|
</aside>
|
|
|