Update some API methods to use api.request() internally (#896)

* use any instead of object
This commit is contained in:
Fred K. Schott
2018-07-26 12:23:07 -07:00
committed by Elliot Lee
parent 4f40b5cb6d
commit b94698df0b
4 changed files with 39 additions and 51 deletions

View File

@@ -131,19 +131,16 @@ class RippleAPI extends EventEmitter {
}
/**
* Makes a request to the API with the given command and
* additional request body parameters.
*/
async request(command: 'account_info', params: AccountInfoRequest):
Promise<AccountInfoResponse>
async request(command: 'account_lines', params: AccountLinesRequest):
Promise<AccountLinesResponse>
/**
* Returns objects owned by an account.
* For an account's trust lines and balances,
* see `getTrustlines` and `getBalances`.
*/
async request(command: 'account_objects', params: AccountObjectsRequest):
Promise<AccountObjectsResponse>
async request(command: 'account_offers', params: AccountOffersRequest):
Promise<AccountOffersResponse>
async request(command: 'book_offers', params: BookOffersRequest):
@@ -156,15 +153,9 @@ class RippleAPI extends EventEmitter {
Promise<LedgerEntryResponse>
async request(command: 'server_info', params?: ServerInfoRequest):
Promise<ServerInfoResponse>
async request(command: string, params: object):
Promise<object>
/**
* Makes a request to the API with the given command and
* additional request body parameters.
*/
async request(command: string, params: object = {}): Promise<object> {
async request(command: string, params: any):
Promise<any>
async request(command: string, params: any = {}): Promise<any> {
return this.connection.request({
...params,
command

View File

@@ -18,7 +18,7 @@ type TransactionResponse = FormattedTransactionType & {
function attachTransactionDate(connection: Connection, tx: any
): Promise<FormattedTransactionType> {
): Promise<TransactionResponse> {
if (tx.date) {
return Promise.resolve(tx)
}
@@ -88,26 +88,20 @@ function formatResponse(options: TransactionOptions, tx: TransactionResponse
return parseTransaction(tx)
}
function getTransaction(id: string, options: TransactionOptions = {}
async function getTransaction(id: string, options: TransactionOptions = {}
): Promise<FormattedTransactionType> {
validate.getTransaction({id, options})
const request = {
command: 'tx',
transaction: id,
binary: false
const _options = await utils.ensureLedgerVersion.call(this, options)
try {
const tx = await this.request('tx', {
transaction: id,
binary: false
})
const txWithDate = await attachTransactionDate(this.connection, tx)
return formatResponse(_options, txWithDate)
} catch (error) {
throw (await convertError(this.connection, _options, error))
}
return utils.ensureLedgerVersion.call(this, options).then(_options => {
return this.connection.request(request).then((tx: TransactionResponse) =>
attachTransactionDate(this.connection, tx)
).then(_.partial(formatResponse, _options))
.catch(error => {
return convertError(this.connection, _options, error).then(_error => {
throw _error
})
})
})
}
export default getTransaction

View File

@@ -1,7 +1,12 @@
import * as _ from 'lodash'
import * as utils from './utils'
import {validate} from '../common'
import {Submit} from './types'
import {RippleAPI} from '..'
export interface FormattedSubmitResponse {
resultCode: string,
resultMessage: string
}
function isImmediateRejection(engineResult: string): boolean {
// note: "tel" errors mean the local server refused to process the
@@ -13,7 +18,7 @@ function isImmediateRejection(engineResult: string): boolean {
return _.startsWith(engineResult, 'tem')
}
function formatSubmitResponse(response) {
function formatSubmitResponse(response): FormattedSubmitResponse {
const data = {
resultCode: response.engine_result,
resultMessage: response.engine_result_message
@@ -24,14 +29,15 @@ function formatSubmitResponse(response) {
return data
}
function submit(signedTransaction: string): Promise<Submit> {
async function submit(
this: RippleAPI, signedTransaction: string
): Promise<FormattedSubmitResponse> {
// 1. Validate
validate.submit({signedTransaction})
const request = {
command: 'submit',
tx_blob: signedTransaction
}
return this.connection.request(request).then(formatSubmitResponse)
// 2. Make Request
const response = await this.request('submit', {tx_blob: signedTransaction})
// 3. Return Formatted Response
return formatSubmitResponse(response)
}
export default submit

View File

@@ -94,19 +94,16 @@ function prepareTransaction(txJSON: any, api: RippleAPI,
})
}
function prepareSequence(): Promise<Object> {
async function prepareSequence(): Promise<Object> {
if (instructions.sequence !== undefined) {
txJSON.Sequence = instructions.sequence
return Promise.resolve(txJSON)
}
const request = {
command: 'account_info',
account: account
}
return api.connection.request(request).then(response => {
txJSON.Sequence = response.account_data.Sequence
return txJSON
const response = await api.request('account_info', {
account: account as string
})
txJSON.Sequence = response.account_data.Sequence
return txJSON
}
return Promise.all([