Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Commit

Permalink
Transfer fixes (#68)
Browse files Browse the repository at this point in the history
- Throw if the info response isn't in the right shape
- Sometimes, responses don't include fields, and might not include types
- Make a new RC for these changes
  • Loading branch information
Morley Zhi authored Jun 25, 2019
1 parent 18fc61f commit 99309b9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
18 changes: 14 additions & 4 deletions src/transfers/parseInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,18 @@ import {
WithdrawInfo,
} from "../types";

export function parseInfo(info: RawInfoResponse) {
const { fee, transactions, transaction } = info;
function isValidInfoResponse(obj: any): obj is RawInfoResponse {
return (
(obj as RawInfoResponse).withdraw !== undefined &&
(obj as RawInfoResponse).deposit !== undefined
);
}

export function parseInfo(info: any) {
if (!isValidInfoResponse(info)) {
throw new Error("The endpoint didn't return a valid info response!");
}
const { fee, transactions, transaction } = info as RawInfoResponse;
return {
withdraw: parseWithdraw(info),
deposit: parseDeposit(info),
Expand Down Expand Up @@ -72,7 +82,7 @@ export function parseWithdraw(info: RawInfoResponse): WithdrawInfo {
fee,
minAmount: entry.min_amount,
maxAmount: entry.max_amount,
types: Object.entries(entry.types).map(parseType),
types: Object.entries(entry.types || {}).map(parseType),
};
return accum;
},
Expand All @@ -90,7 +100,7 @@ export function parseDeposit(info: RawInfoResponse): DepositInfo {
fee,
minAmount: entry.min_amount,
maxAmount: entry.max_amount,
fields: Object.entries(entry.fields).map(parseField),
fields: Object.entries(entry.fields || {}).map(parseField),
};
return accum;
},
Expand Down
4 changes: 2 additions & 2 deletions src/types/transfers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface RawInfoResponse {
fee_percent: number;
min_amount: number;
max_amount: number;
types: {
types?: {
[type: string]: RawType;
};
};
Expand All @@ -34,7 +34,7 @@ export interface RawInfoResponse {
fee_percent: number;
min_amount: number;
max_amount: number;
fields: {
fields?: {
[field: string]: RawField;
};
};
Expand Down

0 comments on commit 99309b9

Please sign in to comment.