Typecheck remaining files in src/api

This commit is contained in:
Alan Cohen
2015-07-29 14:23:26 -07:00
parent 7fffbe0c64
commit e583eb4592
4 changed files with 43 additions and 19 deletions

View File

@@ -1,4 +1,6 @@
/* @flow */
'use strict'; 'use strict';
const _ = require('lodash'); const _ = require('lodash');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
@@ -16,9 +18,10 @@ function isValidLedgerHash(ledgerHash) {
return core.UInt256.is_valid(ledgerHash); return core.UInt256.is_valid(ledgerHash);
} }
function loadSchema(filepath) { function loadSchema(filepath: string): {} {
try { try {
return JSON.parse(fs.readFileSync(filepath, 'utf8')); const schemaContent: any = fs.readFileSync(filepath, 'utf8');
return JSON.parse(schemaContent);
} catch (e) { } catch (e) {
throw new Error('Failed to parse schema: ' + filepath); throw new Error('Failed to parse schema: ' + filepath);
} }
@@ -43,7 +46,7 @@ function formatSchemaErrors(errors) {
return errors.map(formatSchemaError).join(', '); return errors.map(formatSchemaError).join(', ');
} }
function schemaValidate(schemaName, object) { function schemaValidate(schemaName: string, object: any): void {
const formats = {address: isValidAddress, const formats = {address: isValidAddress,
ledgerHash: isValidLedgerHash}; ledgerHash: isValidLedgerHash};
const options = {schemas: SCHEMAS, formats: formats, const options = {schemas: SCHEMAS, formats: formats,

View File

@@ -1,17 +1,20 @@
/* @flow */
'use strict'; 'use strict';
const BigNumber = require('bignumber.js'); const BigNumber = require('bignumber.js');
const core = require('../../core'); const core = require('../../core');
const errors = require('./errors'); const errors = require('./errors');
function dropsToXrp(drops) { type Amount = {currency: string, issuer: string, value: string}
function dropsToXrp(drops: string): string {
return (new BigNumber(drops)).dividedBy(1000000.0).toString(); return (new BigNumber(drops)).dividedBy(1000000.0).toString();
} }
function xrpToDrops(xrp) { function xrpToDrops(xrp: string): string {
return (new BigNumber(xrp)).times(1000000.0).floor().toString(); return (new BigNumber(xrp)).times(1000000.0).floor().toString();
} }
function toRippledAmount(amount) { function toRippledAmount(amount: Amount): string|Amount {
if (amount.currency === 'XRP') { if (amount.currency === 'XRP') {
return xrpToDrops(amount.value); return xrpToDrops(amount.value);
} }
@@ -22,7 +25,9 @@ function toRippledAmount(amount) {
}; };
} }
function wrapCatch(asyncFunction: () => void): () => void { type AsyncFunction = (...x: any) => void
function wrapCatch(asyncFunction: AsyncFunction): AsyncFunction {
return function() { return function() {
try { try {
asyncFunction.apply(this, arguments); asyncFunction.apply(this, arguments);
@@ -33,7 +38,10 @@ function wrapCatch(asyncFunction: () => void): () => void {
}; };
} }
function composeAsync(wrapper, callback) { type Callback = (err: any, data: any) => void
type Wrapper = (data: any) => any
function composeAsync(wrapper: Wrapper, callback: Callback): Callback {
return function(error, data) { return function(error, data) {
if (error) { if (error) {
callback(error); callback(error);
@@ -50,7 +58,7 @@ function composeAsync(wrapper, callback) {
}; };
} }
function convertExceptions(f) { function convertExceptions<T>(f: () => T): () => T {
return function() { return function() {
try { try {
return f.apply(this, arguments); return f.apply(this, arguments);

View File

@@ -1,3 +1,4 @@
/* @flow */
'use strict'; 'use strict';
const _ = require('lodash'); const _ = require('lodash');
const core = require('./utils').core; const core = require('./utils').core;
@@ -8,7 +9,7 @@ function error(text) {
return new ValidationError(text); return new ValidationError(text);
} }
function validateAddressAndSecret(obj) { function validateAddressAndSecret(obj: {address: string, secret: string}): void {
const address = obj.address; const address = obj.address;
const secret = obj.secret; const secret = obj.secret;
schemaValidate('address', address); schemaValidate('address', address);
@@ -22,7 +23,7 @@ function validateAddressAndSecret(obj) {
} }
} }
function validateSecret(secret) { function validateSecret(secret: string): void {
if (!secret) { if (!secret) {
throw error('Parameter missing: secret'); throw error('Parameter missing: secret');
} }

View File

@@ -1,3 +1,4 @@
/* @flow */
'use strict'; 'use strict';
const _ = require('lodash'); const _ = require('lodash');
const assert = require('assert'); const assert = require('assert');
@@ -5,19 +6,23 @@ const common = require('../common');
const dropsToXrp = common.dropsToXrp; const dropsToXrp = common.dropsToXrp;
const composeAsync = common.composeAsync; const composeAsync = common.composeAsync;
function clamp(value, min, max) { type Callback = (err: any, data: any) => void
function clamp(value: number, min: number, max: number): number {
assert(min <= max, 'Illegal clamp bounds'); assert(min <= max, 'Illegal clamp bounds');
return Math.min(Math.max(value, min), max); return Math.min(Math.max(value, min), max);
} }
function getXRPBalance(remote, address, ledgerVersion, callback) { function getXRPBalance(remote: any, address: string, ledgerVersion?: number, callback: Callback): void {
remote.requestAccountInfo({account: address, ledger: ledgerVersion}, remote.requestAccountInfo({account: address, ledger: ledgerVersion},
composeAsync((data) => dropsToXrp(data.account_data.Balance), callback)); composeAsync((data) => dropsToXrp(data.account_data.Balance), callback));
} }
type Getter = (marker: ?string, limit: number, callback: Callback) => void
// If the marker is omitted from a response, you have reached the end // If the marker is omitted from a response, you have reached the end
// getter(marker, limit, callback), callback(error, {marker, results}) // getter(marker, limit, callback), callback(error, {marker, results})
function getRecursiveRecur(getter, marker, limit, callback) { function getRecursiveRecur(getter: Getter, marker?: string, limit: number, callback: Callback): void {
getter(marker, limit, (error, data) => { getter(marker, limit, (error, data) => {
if (error) { if (error) {
return callback(error); return callback(error);
@@ -34,11 +39,13 @@ function getRecursiveRecur(getter, marker, limit, callback) {
}); });
} }
function getRecursive(getter, limit, callback) { function getRecursive(getter: Getter, limit?: number, callback: Callback) {
getRecursiveRecur(getter, undefined, limit || Infinity, callback); getRecursiveRecur(getter, undefined, limit || Infinity, callback);
} }
function renameCounterpartyToIssuer(amount) { type Amount = {counterparty?: string, issuer?: string, value: string}
function renameCounterpartyToIssuer(amount?: Amount): ?{issuer?: string} {
if (amount === undefined) { if (amount === undefined) {
return undefined; return undefined;
} }
@@ -48,7 +55,9 @@ function renameCounterpartyToIssuer(amount) {
return _.omit(withIssuer, 'counterparty'); return _.omit(withIssuer, 'counterparty');
} }
function renameCounterpartyToIssuerInOrder(order) { type Order = {taker_gets: Amount, taker_pays: Amount}
function renameCounterpartyToIssuerInOrder(order: Order) {
const taker_gets = renameCounterpartyToIssuer(order.taker_gets); const taker_gets = renameCounterpartyToIssuer(order.taker_gets);
const taker_pays = renameCounterpartyToIssuer(order.taker_pays); const taker_pays = renameCounterpartyToIssuer(order.taker_pays);
const changes = {taker_gets: taker_gets, taker_pays: taker_pays}; const changes = {taker_gets: taker_gets, taker_pays: taker_pays};
@@ -70,7 +79,9 @@ function signum(num) {
* @returns {Number} [-1, 0, 1] * @returns {Number} [-1, 0, 1]
*/ */
function compareTransactions(first, second) { type Outcome = {outcome: {ledgerVersion: string, indexInLedger: string}};
function compareTransactions(first: Outcome, second: Outcome): number {
if (first.outcome.ledgerVersion === second.outcome.ledgerVersion) { if (first.outcome.ledgerVersion === second.outcome.ledgerVersion) {
return signum(Number(first.outcome.indexInLedger) - return signum(Number(first.outcome.indexInLedger) -
Number(second.outcome.indexInLedger)); Number(second.outcome.indexInLedger));
@@ -79,7 +90,8 @@ function compareTransactions(first, second) {
Number(second.outcome.ledgerVersion) ? -1 : 1; Number(second.outcome.ledgerVersion) ? -1 : 1;
} }
function hasCompleteLedgerRange(remote, minLedgerVersion, maxLedgerVersion) { function hasCompleteLedgerRange(remote: any, minLedgerVersion: number,
maxLedgerVersion: number): boolean {
const firstLedgerVersion = 32570; // earlier versions have been lost const firstLedgerVersion = 32570; // earlier versions have been lost
return remote.getServer().hasLedgerRange( return remote.getServer().hasLedgerRange(
minLedgerVersion || firstLedgerVersion, minLedgerVersion || firstLedgerVersion,