stellar-sdk
Advanced tools
Comparing version 0.2.3 to 0.2.5
@@ -81,2 +81,3 @@ --- | ||
var StellarSdk = require('js-stellar-sdk') | ||
// create the server connection object | ||
@@ -136,3 +137,3 @@ var server = new StellarSdk.Server({hostname:'example-horizon-server.com', secure: true, port: 443}); | ||
Let's say you want to look at an account's transaction history. You can use the `accounts()` command and pass in `transactions` as the resource you're interested in. | ||
Let's say you want to look at an account's transaction history. You can use the `transactions()` command and pass in the account address to `forAccount` as the resource you're interested in. | ||
@@ -143,3 +144,5 @@ ```javascript | ||
server.accounts("GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ", "transactions") | ||
server.transactions() | ||
.forAccount(accountAddress) | ||
.call() | ||
.then(function (page) { | ||
@@ -170,7 +173,6 @@ // page 1 | ||
var es = server.accounts("GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ", "transactions", | ||
{ | ||
streaming: { | ||
onmessage: streamingMessageHandler | ||
} | ||
var es = server.transactions() | ||
.forAccount(accountAddress) | ||
.stream({ | ||
onmessage: streamingMessageHandler | ||
}) | ||
@@ -177,0 +179,0 @@ ``` |
@@ -22,4 +22,6 @@ "use strict"; | ||
var NotFoundError = NetworkError.subclass("NotFoundError"); | ||
exports.NotFoundError = NotFoundError; | ||
var BadRequestError = Error.subclass("BadRequestError"); | ||
exports.NotFoundError = NotFoundError; | ||
exports.BadRequestError = BadRequestError; | ||
/** | ||
@@ -26,0 +28,0 @@ * From: https://github.com/joyent/node/blob/master/lib/util.js |
"use strict"; | ||
var _applyConstructor = function (Constructor, args) { var instance = Object.create(Constructor.prototype); var result = Constructor.apply(instance, args); return result != null && (typeof result == "object" || typeof result == "function") ? result : instance; }; | ||
var _createClass = (function () { function defineProperties(target, props) { for (var key in props) { var prop = props[key]; prop.configurable = true; if (prop.value) prop.writable = true; } Object.defineProperties(target, props); } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | ||
@@ -17,3 +19,20 @@ | ||
var NetworkError = _errors.NetworkError; | ||
var BadRequestError = _errors.BadRequestError; | ||
var AccountCallBuilder = require("./account_call_builder").AccountCallBuilder; | ||
var LedgerCallBuilder = require("./ledger_call_builder").LedgerCallBuilder; | ||
var TransactionCallBuilder = require("./transaction_call_builder").TransactionCallBuilder; | ||
var OperationCallBuilder = require("./operation_call_builder").OperationCallBuilder; | ||
var OfferCallBuilder = require("./offer_call_builder").OfferCallBuilder; | ||
var OrderbookCallBuilder = require("./orderbook_call_builder").OrderbookCallBuilder; | ||
var PaymentCallBuilder = require("./payment_call_builder").PaymentCallBuilder; | ||
var EffectCallBuilder = require("./effect_call_builder").EffectCallBuilder; | ||
var _stellarBase = require("stellar-base"); | ||
@@ -27,3 +46,3 @@ | ||
var URI = require("URIjs"); | ||
var URITemplate = require("URIjs/src/URITemplate"); | ||
var URITemplate = require("URIjs").URITemplate; | ||
@@ -85,152 +104,51 @@ var EventSource = typeof window === "undefined" ? require("eventsource") : window.EventSource; | ||
accounts: { | ||
/** | ||
* <p>Returns account resources. For a list of all accounts, don't pass an address | ||
* or resource parameters. For a specific transaction, pass only an address. For a list of | ||
* account sub resources, pass the account address and the type of sub resources.</p> | ||
* | ||
* <p>A configuration object can be passed to each call. Calls that return a collection | ||
* can be streamed by passing a streaming object in the config object.</p>@param {string} [address] - Returns the given account. | ||
* @param {string} [resource] - Return a specific resource associated with an account. Can be | ||
* {"transactions", "operations", "effects", "payments", "offers"}. | ||
* @param {object} [opts] - Optional configuration for the request. | ||
* @param {string} [opts.after] - Return only resources after the given paging token. | ||
* @param {number} [opts.limit] - Limit the number of returned resources to the given amount. | ||
* @param {string} [opts.order] - Order the returned collection in "asc" or "desc" order. | ||
* @param {object} [opts.streaming] - Streaming handlers. If not null, will turn this request | ||
* into a streaming request. | ||
* @param {function} [opts.streaming.onmessage] - If given an onmessage handler, turns this query | ||
* into a streaming query. | ||
* @param {function} [opts.streaming.onerror] - If this query is a streaming query, will use the | ||
* given handler for error messages. | ||
* @returns {Promise|EventSource} If this is a normal request (non streaming) will return a promise | ||
* that will fulfill when the request completes. Otherwise, it will | ||
* return the EventSource object. | ||
* @throws {NotFoundError} - If the account does not exist. | ||
* @throws {NetworkError} - For any other errors thrown by horizon | ||
*/ | ||
value: function accounts(address, resource, opts) { | ||
if (!address || typeof address === "object") { | ||
opts = address; | ||
address = null; | ||
resource = null; | ||
} else if (!resource || typeof resource === "object") { | ||
opts = resource; | ||
resource = null; | ||
} | ||
return this._sendResourceRequest("accounts", address, resource, opts); | ||
value: function accounts() { | ||
return new AccountCallBuilder(URI(this.serverURL)); | ||
} | ||
}, | ||
ledgers: { | ||
/** | ||
* <p>Returns ledger resources. For a list of all ledgers, don't pass sequence | ||
* or resource parameters. For a specific transaction, pass only a sequence. For a list of | ||
* ledger sub resources, pass the sequence and the type of sub resources.</p> | ||
* | ||
* <p>A configuration object can be passed to each call. Calls that return a collection | ||
* can be streamed by passing a streaming object in the config object.</p>@param {number} [sequence] - Returns the given ledger. | ||
* @param {string} [resource] - Return a specific resource associated with a ledger. Can be | ||
* {"transactions", "operations", "effects", "payments"}. | ||
* @param {object} [opts] - Optional configuration for the request. | ||
* @param {string} [opts.after] - Return only resources after the given paging token. | ||
* @param {number} [opts.limit] - Limit the number of returned resources to the given amount. | ||
* @param {string} [opts.order] - Order the returned collection in "asc" or "desc" order. | ||
* @param {object} [opts.streaming] - Streaming handlers. If not null, will turn this request | ||
* into a streaming request. | ||
* @param {function} [opts.streaming.onmessage] - If given an onmessage handler, turns this query | ||
* into a streaming query. | ||
* @param {function} [opts.streaming.onerror] - If this query is a streaming query, will use the | ||
* given handler for error messages. | ||
* @returns {Promise|EventSource} If this is a normal request (non streaming) will return a promise | ||
* that will fulfill when the request completes. Otherwise, it will | ||
* return the EventSource object. | ||
* @throws {NotFoundError} - If the ledger does not exist. | ||
* @throws {NetworkError} - For any other errors thrown by horizon | ||
*/ | ||
value: function ledgers(sequence, resource, opts) { | ||
if (!sequence || typeof sequence === "object") { | ||
opts = sequence; | ||
sequence = null; | ||
resource = null; | ||
} else if (!resource || typeof resource === "object") { | ||
opts = resource; | ||
resource = null; | ||
} | ||
return this._sendResourceRequest("ledgers", sequence, resource, opts); | ||
value: function ledgers() { | ||
return new LedgerCallBuilder(URI(this.serverURL)); | ||
} | ||
}, | ||
transactions: { | ||
value: function transactions() { | ||
return new TransactionCallBuilder(URI(this.serverURL)); | ||
} | ||
}, | ||
offers: { | ||
/** | ||
* <p>Returns transaction resources. For a list of all transactions, don't pass hash | ||
* or resource parameters. For a specific transaction, pass a hash. For a list of | ||
* transaction sub resources, pass the transaction hash and the type of sub resources.</p> | ||
* | ||
* <p>A configuration object can be passed to each call. Calls that return a collection | ||
* can be streamed by passing a streaming object in the config object.</p> | ||
* @param {string} [hash] - Returns the given transaction. | ||
* @param {string} [resource] - Return a specific resource associated with a transaction. Can be | ||
* {"operations", "effects", "payments"}. | ||
* @param {object} [opts] - Optional configuration for the request. | ||
* @param {string} [opts.after] - Return only resources after the given paging token. | ||
* @param {number} [opts.limit] - Limit the number of returned resources to the given amount. | ||
* @param {string} [opts.order] - Order the returned collection in "asc" or "desc" order. | ||
* @param {object} [opts.streaming] - Streaming handlers. If not null, will turn this request | ||
* into a streaming request. | ||
* @param {function} [opts.streaming.onmessage] - If given an onmessage handler, turns this query | ||
* into a streaming query. | ||
* @param {function} [opts.streaming.onerror] - If this query is a streaming query, will use the | ||
* given handler for error messages. | ||
* @returns {Promise|EventSource} If this is a normal request (non streaming) will return a promise | ||
* that will fulfill when the request completes. Otherwise, it will | ||
* return the EventSource object. | ||
* @throws {NotFoundError} - If the transaction does not exist. | ||
* @throws {NetworkError} - For any other errors thrown by horizon | ||
/* | ||
* Should be | ||
* offers('accounts', accountID) or | ||
*/ | ||
value: function transactions(hash, resource, opts) { | ||
if (!hash || typeof hash === "object") { | ||
opts = hash; | ||
hash = null; | ||
resource = null; | ||
} else if (!resource || typeof resource === "object") { | ||
opts = resource; | ||
resource = null; | ||
value: function offers(resource) { | ||
for (var _len = arguments.length, resourceParams = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
resourceParams[_key - 1] = arguments[_key]; | ||
} | ||
return this._sendResourceRequest("transactions", hash, resource, opts); | ||
return _applyConstructor(OfferCallBuilder, [URI(this.serverURL), resource].concat(resourceParams)); | ||
} | ||
}, | ||
orderbook: { | ||
value: function orderbook(selling, buying) { | ||
return new OrderbookCallBuilder(URI(this.serverURL), selling, buying); | ||
} | ||
}, | ||
operations: { | ||
/** | ||
* Operation's firehose. | ||
* TODO: support for operation/:id [blocked on horizon] | ||
* TODO: support for operation/:id/effects [blocked on horizon] | ||
*/ | ||
value: function operations(id, resource, opts) { | ||
if (!id || typeof id === "object") { | ||
opts = id; | ||
id = null; | ||
resource = null; | ||
} else if (!resource || typeof resource === "object") { | ||
opts = resource; | ||
resource = null; | ||
} | ||
return this._sendResourceRequest("operations", id, resource, opts); | ||
value: function operations() { | ||
return new OperationCallBuilder(URI(this.serverURL)); | ||
} | ||
}, | ||
payments: { | ||
/** | ||
* Payments firehose. | ||
*/ | ||
value: function payments(opts) { | ||
return this._sendResourceRequest("payments", null, null, opts); | ||
value: function payments() { | ||
return new PaymentCallBuilder(URI(this.serverURL)); | ||
} | ||
}, | ||
effects: { | ||
value: function effects() { | ||
return new EffectCallBuilder(URI(this.serverURL)); | ||
} | ||
}, | ||
loadAccount: { | ||
@@ -248,3 +166,3 @@ | ||
var self = this; | ||
return this.accounts(address).then(function (res) { | ||
return this.accounts().address(address).call().then(function (res) { | ||
return new Account(address, res.sequence); | ||
@@ -260,37 +178,2 @@ }); | ||
}, | ||
_sendResourceRequest: { | ||
/** | ||
* Sends a request for a resource or collection of resources. For a non streaming | ||
* request, will return a promise. This will be fulfilled either with the specific | ||
* resource response, or a collection of responses. Otherwise, for a streaming request, | ||
* will return the EventSource object. | ||
* @param {string} type - {"accounts", "ledgers", "transactions"} | ||
*/ | ||
value: function _sendResourceRequest(type, id, resource, opts) { | ||
var url = this._buildEndpointPath(type, id, resource, opts); | ||
if (opts && opts.streaming) { | ||
return this._sendStreamingRequest(url, opts.streaming); | ||
} else { | ||
var promise = this._sendNormalRequest(url).then(this._parseResponse.bind(this)); | ||
return promise; | ||
} | ||
} | ||
}, | ||
_buildEndpointPath: { | ||
value: function _buildEndpointPath(type, id, resource, opts) { | ||
if (id && typeof id !== "string") { | ||
id = id.toString(); | ||
} | ||
var argArray = [type, id, resource].filter(function (x) { | ||
return x !== null; | ||
}); | ||
var url = URI(this.serverURL).segment(argArray); | ||
if (opts) { | ||
url = this._appendResourceCollectionConfiguration(url, opts); | ||
} | ||
return url; | ||
} | ||
}, | ||
_sendNormalRequest: { | ||
@@ -305,95 +188,2 @@ value: function _sendNormalRequest(url) { | ||
} | ||
}, | ||
_handleNetworkError: { | ||
value: function _handleNetworkError(response) { | ||
if (response instanceof Error) { | ||
return Promise.reject(response); | ||
} else { | ||
switch (response.status) { | ||
case 404: | ||
return Promise.reject(new NotFoundError(response.data, response)); | ||
default: | ||
return Promise.reject(new NetworkError(response.status, response)); | ||
} | ||
} | ||
} | ||
}, | ||
_sendStreamingRequest: { | ||
value: function _sendStreamingRequest(url, streaming) { | ||
var es = new EventSource(url.toString()); | ||
es.onmessage = function (message) { | ||
var result = message.data ? JSON.parse(message.data) : message; | ||
streaming.onmessage(result); | ||
}; | ||
es.onerror = streaming.onerror; | ||
return es; | ||
} | ||
}, | ||
_appendResourceCollectionConfiguration: { | ||
value: function _appendResourceCollectionConfiguration(url, opts) { | ||
if (opts.after) { | ||
url.addQuery("after", opts.after); | ||
} | ||
if (opts.limit) { | ||
url.addQuery("limit", opts.limit); | ||
} | ||
if (opts.order) { | ||
url.addQuery("order", opts.order); | ||
} | ||
return url; | ||
} | ||
}, | ||
_parseResponse: { | ||
value: function _parseResponse(json) { | ||
if (json._embedded && json._embedded.records) { | ||
return this._toCollectionPage(json); | ||
} else { | ||
return this._parseRecord(json); | ||
} | ||
} | ||
}, | ||
_toCollectionPage: { | ||
value: function _toCollectionPage(json) { | ||
var self = this; | ||
for (var i = 0; i < json._embedded.records.length; i++) { | ||
json._embedded.records[i] = this._parseRecord(json._embedded.records[i]); | ||
} | ||
return { | ||
records: json._embedded.records, | ||
next: function next() { | ||
return self._sendNormalRequest(URI(json._links.next.href)).then(self._toCollectionPage.bind(self)); | ||
}, | ||
prev: function prev() { | ||
return self._sendNormalRequest(URI(json._links.prev.href)).then(self._toCollectionPage.bind(self)); | ||
} | ||
}; | ||
} | ||
}, | ||
_parseRecord: { | ||
/** | ||
* Convert each link into a function on the response object. | ||
*/ | ||
value: function _parseRecord(json) { | ||
if (!json._links) { | ||
return json; | ||
} | ||
var self = this; | ||
var linkFn = function linkFn(link) { | ||
return function (opts) { | ||
if (link.template) { | ||
var template = URITemplate(link.href); | ||
return self._sendNormalRequest(URI(template.expand(opts))); | ||
} else { | ||
return self._sendNormalRequest(URI(link.href)); | ||
} | ||
}; | ||
}; | ||
Object.keys(json._links).map(function (value, index) { | ||
var link = json._links[value]; | ||
json[value] = linkFn(link); | ||
}); | ||
return json; | ||
} | ||
} | ||
@@ -400,0 +190,0 @@ }); |
{ | ||
"name": "stellar-sdk", | ||
"version": "0.2.3", | ||
"version": "0.2.5", | ||
"description": "stellar-sdk is a library for working with the Stellar Horizon server.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -16,2 +16,3 @@ Error.subclass = function(errorName) { | ||
export var NotFoundError = NetworkError.subclass("NotFoundError"); | ||
export var BadRequestError = Error.subclass("BadRequestError"); | ||
@@ -18,0 +19,0 @@ /** |
import {TransactionResult} from "./transaction_result"; | ||
import {NotFoundError, NetworkError} from "./errors"; | ||
import {NotFoundError, NetworkError, BadRequestError} from "./errors"; | ||
import {AccountCallBuilder} from "./account_call_builder"; | ||
import {LedgerCallBuilder} from "./ledger_call_builder"; | ||
import {TransactionCallBuilder} from "./transaction_call_builder"; | ||
import {OperationCallBuilder} from "./operation_call_builder"; | ||
import {OfferCallBuilder} from "./offer_call_builder"; | ||
import {OrderbookCallBuilder} from "./orderbook_call_builder"; | ||
import {PaymentCallBuilder} from "./payment_call_builder"; | ||
import {EffectCallBuilder} from "./effect_call_builder"; | ||
import {xdr, Account} from "stellar-base"; | ||
@@ -9,3 +17,3 @@ | ||
let URI = require("URIjs"); | ||
let URITemplate = require('URIjs/src/URITemplate'); | ||
let URITemplate = require("URIjs").URITemplate; | ||
@@ -58,138 +66,39 @@ var EventSource = (typeof window === 'undefined') ? require('eventsource') : window.EventSource; | ||
/** | ||
* <p>Returns account resources. For a list of all accounts, don't pass an address | ||
* or resource parameters. For a specific transaction, pass only an address. For a list of | ||
* account sub resources, pass the account address and the type of sub resources.</p> | ||
* | ||
* <p>A configuration object can be passed to each call. Calls that return a collection | ||
* can be streamed by passing a streaming object in the config object.</p>@param {string} [address] - Returns the given account. | ||
* @param {string} [resource] - Return a specific resource associated with an account. Can be | ||
* {"transactions", "operations", "effects", "payments", "offers"}. | ||
* @param {object} [opts] - Optional configuration for the request. | ||
* @param {string} [opts.after] - Return only resources after the given paging token. | ||
* @param {number} [opts.limit] - Limit the number of returned resources to the given amount. | ||
* @param {string} [opts.order] - Order the returned collection in "asc" or "desc" order. | ||
* @param {object} [opts.streaming] - Streaming handlers. If not null, will turn this request | ||
* into a streaming request. | ||
* @param {function} [opts.streaming.onmessage] - If given an onmessage handler, turns this query | ||
* into a streaming query. | ||
* @param {function} [opts.streaming.onerror] - If this query is a streaming query, will use the | ||
* given handler for error messages. | ||
* @returns {Promise|EventSource} If this is a normal request (non streaming) will return a promise | ||
* that will fulfill when the request completes. Otherwise, it will | ||
* return the EventSource object. | ||
* @throws {NotFoundError} - If the account does not exist. | ||
* @throws {NetworkError} - For any other errors thrown by horizon | ||
*/ | ||
accounts(address, resource, opts) { | ||
if (!address || typeof(address) === "object") { | ||
opts = address; | ||
address = null; | ||
resource = null; | ||
} else if (!resource || typeof resource === "object") { | ||
opts = resource; | ||
resource = null; | ||
} | ||
return this._sendResourceRequest("accounts", address, resource, opts); | ||
accounts() { | ||
return new AccountCallBuilder(URI(this.serverURL)); | ||
} | ||
/** | ||
* <p>Returns ledger resources. For a list of all ledgers, don't pass sequence | ||
* or resource parameters. For a specific transaction, pass only a sequence. For a list of | ||
* ledger sub resources, pass the sequence and the type of sub resources.</p> | ||
* | ||
* <p>A configuration object can be passed to each call. Calls that return a collection | ||
* can be streamed by passing a streaming object in the config object.</p>@param {number} [sequence] - Returns the given ledger. | ||
* @param {string} [resource] - Return a specific resource associated with a ledger. Can be | ||
* {"transactions", "operations", "effects", "payments"}. | ||
* @param {object} [opts] - Optional configuration for the request. | ||
* @param {string} [opts.after] - Return only resources after the given paging token. | ||
* @param {number} [opts.limit] - Limit the number of returned resources to the given amount. | ||
* @param {string} [opts.order] - Order the returned collection in "asc" or "desc" order. | ||
* @param {object} [opts.streaming] - Streaming handlers. If not null, will turn this request | ||
* into a streaming request. | ||
* @param {function} [opts.streaming.onmessage] - If given an onmessage handler, turns this query | ||
* into a streaming query. | ||
* @param {function} [opts.streaming.onerror] - If this query is a streaming query, will use the | ||
* given handler for error messages. | ||
* @returns {Promise|EventSource} If this is a normal request (non streaming) will return a promise | ||
* that will fulfill when the request completes. Otherwise, it will | ||
* return the EventSource object. | ||
* @throws {NotFoundError} - If the ledger does not exist. | ||
* @throws {NetworkError} - For any other errors thrown by horizon | ||
*/ | ||
ledgers(sequence, resource, opts) { | ||
if (!sequence || typeof(sequence) === "object") { | ||
opts = sequence; | ||
sequence = null; | ||
resource = null; | ||
} else if (!resource || typeof resource === "object") { | ||
opts = resource; | ||
resource = null; | ||
} | ||
return this._sendResourceRequest("ledgers", sequence, resource, opts); | ||
ledgers() { | ||
return new LedgerCallBuilder(URI(this.serverURL)); | ||
} | ||
/** | ||
* <p>Returns transaction resources. For a list of all transactions, don't pass hash | ||
* or resource parameters. For a specific transaction, pass a hash. For a list of | ||
* transaction sub resources, pass the transaction hash and the type of sub resources.</p> | ||
* | ||
* <p>A configuration object can be passed to each call. Calls that return a collection | ||
* can be streamed by passing a streaming object in the config object.</p> | ||
* @param {string} [hash] - Returns the given transaction. | ||
* @param {string} [resource] - Return a specific resource associated with a transaction. Can be | ||
* {"operations", "effects", "payments"}. | ||
* @param {object} [opts] - Optional configuration for the request. | ||
* @param {string} [opts.after] - Return only resources after the given paging token. | ||
* @param {number} [opts.limit] - Limit the number of returned resources to the given amount. | ||
* @param {string} [opts.order] - Order the returned collection in "asc" or "desc" order. | ||
* @param {object} [opts.streaming] - Streaming handlers. If not null, will turn this request | ||
* into a streaming request. | ||
* @param {function} [opts.streaming.onmessage] - If given an onmessage handler, turns this query | ||
* into a streaming query. | ||
* @param {function} [opts.streaming.onerror] - If this query is a streaming query, will use the | ||
* given handler for error messages. | ||
* @returns {Promise|EventSource} If this is a normal request (non streaming) will return a promise | ||
* that will fulfill when the request completes. Otherwise, it will | ||
* return the EventSource object. | ||
* @throws {NotFoundError} - If the transaction does not exist. | ||
* @throws {NetworkError} - For any other errors thrown by horizon | ||
*/ | ||
transactions(hash, resource, opts) { | ||
if (!hash || typeof(hash) === "object") { | ||
opts = hash; | ||
hash = null; | ||
resource = null; | ||
} else if (!resource || typeof resource === "object") { | ||
opts = resource; | ||
resource = null; | ||
} | ||
return this._sendResourceRequest("transactions", hash, resource, opts); | ||
transactions() { | ||
return new TransactionCallBuilder(URI(this.serverURL)); | ||
} | ||
/** | ||
* Operation's firehose. | ||
* TODO: support for operation/:id [blocked on horizon] | ||
* TODO: support for operation/:id/effects [blocked on horizon] | ||
/* | ||
* Should be | ||
* offers('accounts', accountID) or | ||
*/ | ||
operations(id, resource, opts) { | ||
if (!id || typeof(id) === "object") { | ||
opts = id; | ||
id = null; | ||
resource = null; | ||
} else if (!resource || typeof resource === "object") { | ||
opts = resource; | ||
resource = null; | ||
} | ||
return this._sendResourceRequest("operations", id, resource, opts); | ||
offers(resource, ...resourceParams) { | ||
return new OfferCallBuilder(URI(this.serverURL), resource, ...resourceParams); | ||
} | ||
/** | ||
* Payments firehose. | ||
*/ | ||
payments(opts) { | ||
return this._sendResourceRequest("payments", null, null, opts); | ||
orderbook(selling, buying) { | ||
return new OrderbookCallBuilder(URI(this.serverURL), selling, buying); | ||
} | ||
operations() { | ||
return new OperationCallBuilder(URI(this.serverURL)); | ||
} | ||
payments() { | ||
return new PaymentCallBuilder(URI(this.serverURL)); | ||
} | ||
effects() { | ||
return new EffectCallBuilder(URI(this.serverURL)); | ||
} | ||
/** | ||
@@ -204,3 +113,5 @@ * Fetches an account's most current state in the ledger and then creates and returns | ||
var self = this; | ||
return this.accounts(address) | ||
return this.accounts() | ||
.address(address) | ||
.call() | ||
.then(function (res) { | ||
@@ -216,32 +127,2 @@ return new Account(address, res.sequence); | ||
/** | ||
* Sends a request for a resource or collection of resources. For a non streaming | ||
* request, will return a promise. This will be fulfilled either with the specific | ||
* resource response, or a collection of responses. Otherwise, for a streaming request, | ||
* will return the EventSource object. | ||
* @param {string} type - {"accounts", "ledgers", "transactions"} | ||
*/ | ||
_sendResourceRequest(type, id, resource, opts) { | ||
var url = this._buildEndpointPath(type, id, resource, opts); | ||
if (opts && opts.streaming) { | ||
return this._sendStreamingRequest(url, opts.streaming); | ||
} else { | ||
var promise = this._sendNormalRequest(url) | ||
.then(this._parseResponse.bind(this)); | ||
return promise; | ||
} | ||
} | ||
_buildEndpointPath(type, id, resource, opts) { | ||
if (id && typeof id !== 'string') { | ||
id = id.toString(); | ||
} | ||
let argArray = [type, id, resource].filter(x => x !== null); | ||
let url = URI(this.serverURL).segment(argArray); | ||
if (opts) { | ||
url = this._appendResourceCollectionConfiguration(url, opts); | ||
} | ||
return url; | ||
} | ||
_sendNormalRequest(url) { | ||
@@ -255,89 +136,4 @@ // To fix: #15 Connection Stalled when making multiple requests to the same resource | ||
} | ||
} | ||
_handleNetworkError(response) { | ||
if (response instanceof Error) { | ||
return Promise.reject(response); | ||
} else { | ||
switch (response.status) { | ||
case 404: | ||
return Promise.reject(new NotFoundError(response.data, response)); | ||
default: | ||
return Promise.reject(new NetworkError(response.status, response)); | ||
} | ||
} | ||
} | ||
_sendStreamingRequest(url, streaming) { | ||
var es = new EventSource(url.toString()); | ||
es.onmessage = function (message) { | ||
var result = message.data ? JSON.parse(message.data) : message; | ||
streaming.onmessage(result); | ||
}; | ||
es.onerror = streaming.onerror; | ||
return es; | ||
} | ||
_appendResourceCollectionConfiguration(url, opts) { | ||
if (opts.after) { | ||
url.addQuery("after", opts.after); | ||
} | ||
if (opts.limit) { | ||
url.addQuery("limit", opts.limit); | ||
} | ||
if (opts.order) { | ||
url.addQuery("order", opts.order); | ||
} | ||
return url; | ||
} | ||
_parseResponse(json) { | ||
if (json._embedded && json._embedded.records) { | ||
return this._toCollectionPage(json); | ||
} else { | ||
return this._parseRecord(json); | ||
} | ||
} | ||
_toCollectionPage(json) { | ||
var self = this; | ||
for (var i = 0; i < json._embedded.records.length; i++) { | ||
json._embedded.records[i] = this._parseRecord(json._embedded.records[i]); | ||
} | ||
return { | ||
records: json._embedded.records, | ||
next: function () { | ||
return self._sendNormalRequest(URI(json._links.next.href)) | ||
.then(self._toCollectionPage.bind(self)); | ||
}, | ||
prev: function () { | ||
return self._sendNormalRequest(URI(json._links.prev.href)) | ||
.then(self._toCollectionPage.bind(self)); | ||
} | ||
}; | ||
} | ||
/** | ||
* Convert each link into a function on the response object. | ||
*/ | ||
_parseRecord(json) { | ||
if (!json._links) { | ||
return json; | ||
} | ||
var self = this; | ||
var linkFn = function (link) { | ||
return function (opts) { | ||
if (link.template) { | ||
let template = URITemplate(link.href); | ||
return self._sendNormalRequest(URI(template.expand(opts))); | ||
} else { | ||
return self._sendNormalRequest(URI(link.href)); | ||
} | ||
}; | ||
}; | ||
Object.keys(json._links).map(function(value, index) { | ||
var link = json._links[value]; | ||
json[value] = linkFn(link); | ||
}); | ||
return json; | ||
} | ||
} | ||
@@ -52,2 +52,3 @@ describe("server.js tests", function () { | ||
server.ledgers() | ||
.call() | ||
.then(function () { | ||
@@ -73,3 +74,7 @@ done(); | ||
it("requests the correct endpoint", function () { | ||
return server.ledgers({limit: 1, after: "b", order: "asc"}); | ||
return server.ledgers() | ||
.limit("1") | ||
.after("b") | ||
.order("asc") | ||
.call(); | ||
}); | ||
@@ -79,3 +84,7 @@ | ||
return server | ||
.ledgers({limit: 1, after: "b", order: "asc"}) | ||
.ledgers() | ||
.limit("1") | ||
.after("b") | ||
.order("asc") | ||
.call() | ||
.then(function(page) { | ||
@@ -98,10 +107,9 @@ return page.next() | ||
it("attaches onmessage handler to an EventSource", function (done) { | ||
var es = server.ledgers({ | ||
streaming: { | ||
onmessage: function (res) { | ||
expect(res.test).to.be.equal("body"); | ||
done(); | ||
var es = server.ledgers() | ||
.stream( | ||
{ onmessage: function (res) { | ||
expect(res.test).to.be.equal("body"); | ||
done(); | ||
} | ||
} | ||
}); | ||
}) | ||
}); | ||
@@ -122,3 +130,5 @@ }); | ||
it("throws a NotFoundError", function (done) { | ||
server.ledgers(1) | ||
server.ledgers() | ||
.ledger("1") | ||
.call() | ||
.then(function () { | ||
@@ -145,3 +155,5 @@ done("didn't throw an error"); | ||
it("requests the correct endpoint", function (done) { | ||
server.ledgers(1) | ||
server.ledgers() | ||
.ledger("1") | ||
.call() | ||
.then(function () { | ||
@@ -165,3 +177,8 @@ done(); | ||
it("requests the correct endpoint", function (done) { | ||
server.ledgers(1, {limit: 1, after: "b", order: "asc"}) | ||
server.ledgers() | ||
.ledger("1") | ||
.limit("1") | ||
.after("b") | ||
.order("asc") | ||
.call() | ||
.then(function () { | ||
@@ -188,3 +205,5 @@ done(); | ||
it("requests the correct endpoint", function (done) { | ||
server.ledgers(1, "transactions") | ||
server.transactions() | ||
.forLedger("1") | ||
.call() | ||
.then(function () { | ||
@@ -208,3 +227,8 @@ done(); | ||
it("requests the correct endpoint", function (done) { | ||
server.ledgers(1, "transactions", {limit: 1, after: "b", order: "asc"}) | ||
server.transactions() | ||
.forLedger("1") | ||
.after("b") | ||
.limit("1") | ||
.order("asc") | ||
.call() | ||
.then(function () { | ||
@@ -229,4 +253,5 @@ done(); | ||
it("attaches onmessage handler to an EventSource", function (done) { | ||
var es = server.ledgers(1, "transactions", { | ||
streaming: { | ||
var es = server.transactions() | ||
.forLedger("1") | ||
.stream({ | ||
onmessage: function (res) { | ||
@@ -237,4 +262,3 @@ expect(res.test).to.be.equal("body"); | ||
} | ||
} | ||
}); | ||
}); | ||
}); | ||
@@ -259,3 +283,4 @@ }); | ||
it("creates link functions", function () { | ||
var json = server._parseResponse({ | ||
var callBuilder = server.ledgers(); | ||
var json = callBuilder._parseResponse({ | ||
"_links": { | ||
@@ -270,2 +295,146 @@ "test": function () { | ||
}); | ||
describe("Smoke tests for the rest of the builders", function() { | ||
describe("AccountCallBuilder", function() { | ||
beforeEach(function (done) { | ||
// instruct the dev server to except the correct request | ||
return this.setFixtures({ | ||
request: "/accounts/GSDEF?limit=4", | ||
response: {status: 200, body: FAKE_COLLECTION_RESPONSE} | ||
}).then(function () { done() }); | ||
}); | ||
it("requests the correct endpoint", function (done) { | ||
server.accounts() | ||
.address("GSDEF") | ||
.limit("4") | ||
.call() | ||
.then(function () { | ||
done(); | ||
}) | ||
.catch(function (err) { | ||
done(err); | ||
}) | ||
}); | ||
}); | ||
describe("OfferCallBuilder", function() { | ||
beforeEach(function (done) { | ||
// instruct the dev server to except the correct request | ||
return this.setFixtures({ | ||
request: "/accounts/GSDEF/offers?order=asc", | ||
response: {status: 200, body: FAKE_COLLECTION_RESPONSE} | ||
}).then(function () { done() }); | ||
}); | ||
it("requests the correct endpoint", function (done) { | ||
server.offers('accounts', "GSDEF") | ||
.order("asc") | ||
.call() | ||
.then(function () { | ||
done(); | ||
}) | ||
.catch(function (err) { | ||
done(err); | ||
}) | ||
}); | ||
it("rejects the wrong resource", function(done) { | ||
expect(() => server.offers('ledgers', '123').call()).to.throw(/Bad resource specified/); | ||
done(); | ||
}); | ||
}); | ||
describe("OrderbookCallBuilder", function() { | ||
beforeEach(function (done) { | ||
// instruct the dev server to except the correct request | ||
return this.setFixtures({ | ||
request: "/order_book?selling_asset_type=native&buying_asset_type=credit_alphanum4&buying_asset_code=USD&buying_asset_issuer=GSEDF", | ||
response: {status: 200, body: FAKE_COLLECTION_RESPONSE} | ||
}).then(function () { done() }); | ||
}); | ||
it("requests the correct endpoint", function (done) { | ||
server.orderbook(new StellarSdk.Asset.native(), new StellarSdk.Asset('USD', "GSEDF")) | ||
.call() | ||
.then(function () { | ||
done(); | ||
}) | ||
.catch(function (err) { | ||
done(err); | ||
}) | ||
}); | ||
}); | ||
describe("EffectCallBuilder", function() { | ||
beforeEach(function (done) { | ||
// instruct the dev server to except the correct request | ||
return this.setFixtures({ | ||
request: "/effects?after=b", | ||
response: {status: 200, body: FAKE_COLLECTION_RESPONSE} | ||
}).then(function () { done() }); | ||
}); | ||
it("requests the correct endpoint", function (done) { | ||
server.effects() | ||
.after("b") | ||
.call() | ||
.then(function () { | ||
done(); | ||
}) | ||
.catch(function (err) { | ||
done(err); | ||
}) | ||
}); | ||
it("rejects two filters", function (done) { | ||
expect(() => server.effects().forOperation("blah").forLedger('234').call()).to.throw(/Too many filters/); | ||
done(); | ||
}); | ||
}) | ||
describe("OperationCallBuilder", function() { | ||
beforeEach(function (done) { | ||
// instruct the dev server to except the correct request | ||
return this.setFixtures({ | ||
request: "/transactions/blah/operations", | ||
response: {status: 200, body: FAKE_COLLECTION_RESPONSE} | ||
}).then(function () { done() }); | ||
}); | ||
it("requests the correct endpoint", function (done) { | ||
server.operations() | ||
.forTransaction("blah") | ||
.call() | ||
.then(function () { | ||
done(); | ||
}) | ||
.catch(function (err) { | ||
done(err); | ||
}) | ||
}); | ||
}); | ||
describe("PaymentCallBuilder", function() { | ||
beforeEach(function (done) { | ||
// instruct the dev server to except the correct request | ||
return this.setFixtures({ | ||
request: "/accounts/GDEFS/payments", | ||
response: {status: 200, body: FAKE_COLLECTION_RESPONSE} | ||
}).then(function () { done() }); | ||
}); | ||
it("requests the correct endpoint", function (done) { | ||
server.payments() | ||
.forAccount("GDEFS") | ||
.call() | ||
.then(function () { | ||
done(); | ||
}) | ||
.catch(function (err) { | ||
done(err); | ||
}) | ||
}); | ||
}); | ||
}) | ||
}); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
2129925
50
46297