aboutsummaryrefslogtreecommitdiffstats
path: root/packages/connect/src/utils
diff options
context:
space:
mode:
authorBrandon Millman <brandon.millman@gmail.com>2017-12-20 06:22:38 +0800
committerBrandon Millman <brandon.millman@gmail.com>2017-12-25 10:01:28 +0800
commit8fe81c9d090ce50496f3150602f19433e7aedd1e (patch)
tree7e569cf16cd3a9ad9b09b5e64c5101d0c477f8fe /packages/connect/src/utils
parent9f3acf8e2888b6105062e47664ecd5adaaf3c889 (diff)
downloaddexon-0x-contracts-8fe81c9d090ce50496f3150602f19433e7aedd1e.tar.gz
dexon-0x-contracts-8fe81c9d090ce50496f3150602f19433e7aedd1e.tar.zst
dexon-0x-contracts-8fe81c9d090ce50496f3150602f19433e7aedd1e.zip
Refactor JSON parsing in HttpClient
Diffstat (limited to 'packages/connect/src/utils')
-rw-r--r--packages/connect/src/utils/orderbook_channel_message_parser.ts (renamed from packages/connect/src/utils/orderbook_channel_message_parsers.ts)18
-rw-r--r--packages/connect/src/utils/relayer_response_json_parsers.ts42
-rw-r--r--packages/connect/src/utils/type_converters.ts22
3 files changed, 64 insertions, 18 deletions
diff --git a/packages/connect/src/utils/orderbook_channel_message_parsers.ts b/packages/connect/src/utils/orderbook_channel_message_parser.ts
index d9a84cca2..adc565d29 100644
--- a/packages/connect/src/utils/orderbook_channel_message_parsers.ts
+++ b/packages/connect/src/utils/orderbook_channel_message_parser.ts
@@ -7,10 +7,10 @@ import {
OrderbookChannelMessageTypes,
} from '../types';
-import {typeConverters} from './type_converters';
+import {relayerResponseJsonParsers} from './relayer_response_json_parsers';
-export const orderbookChannelMessageParsers = {
- parser(utf8Data: string): OrderbookChannelMessage {
+export const orderbookChannelMessageParser = {
+ parse(utf8Data: string): OrderbookChannelMessage {
const messageObj = JSON.parse(utf8Data);
const type: string = _.get(messageObj, 'type');
assert.assert(!_.isUndefined(type), `Message is missing a type parameter: ${utf8Data}`);
@@ -18,15 +18,15 @@ export const orderbookChannelMessageParsers = {
switch (type) {
case (OrderbookChannelMessageTypes.Snapshot): {
assert.doesConformToSchema('message', messageObj, schemas.relayerApiOrderbookChannelSnapshotSchema);
- const orderbook = messageObj.payload;
- typeConverters.convertOrderbookStringFieldsToBigNumber(orderbook);
- return messageObj;
+ const orderbookJson = messageObj.payload;
+ const orderbook = relayerResponseJsonParsers.parseOrderbookResponseJson(orderbookJson);
+ return _.assign(messageObj, {payload: orderbook});
}
case (OrderbookChannelMessageTypes.Update): {
assert.doesConformToSchema('message', messageObj, schemas.relayerApiOrderbookChannelUpdateSchema);
- const order = messageObj.payload;
- typeConverters.convertOrderStringFieldsToBigNumber(order);
- return messageObj;
+ const orderJson = messageObj.payload;
+ const order = relayerResponseJsonParsers.parseOrderJson(orderJson);
+ return _.assign(messageObj, {payload: order});
}
default: {
return {
diff --git a/packages/connect/src/utils/relayer_response_json_parsers.ts b/packages/connect/src/utils/relayer_response_json_parsers.ts
new file mode 100644
index 000000000..02d88f26d
--- /dev/null
+++ b/packages/connect/src/utils/relayer_response_json_parsers.ts
@@ -0,0 +1,42 @@
+import {assert} from '@0xproject/assert';
+import {schemas} from '@0xproject/json-schemas';
+import * as _ from 'lodash';
+
+import {
+ FeesResponse,
+ OrderbookResponse,
+ SignedOrder,
+ TokenPairsItem,
+} from '../types';
+
+import {typeConverters} from './type_converters';
+
+export const relayerResponseJsonParsers = {
+ parseTokenPairsJson(json: any): TokenPairsItem[] {
+ assert.doesConformToSchema('tokenPairs', json, schemas.relayerApiTokenPairsResponseSchema);
+ return json.map((tokenPair: any) => {
+ return typeConverters.convertStringsFieldsToBigNumbers(tokenPair, [
+ 'tokenA.minAmount',
+ 'tokenA.maxAmount',
+ 'tokenB.minAmount',
+ 'tokenB.maxAmount',
+ ]);
+ });
+ },
+ parseOrdersJson(json: any): SignedOrder[] {
+ assert.doesConformToSchema('orders', json, schemas.signedOrdersSchema);
+ return json.map((order: object) => typeConverters.convertOrderStringFieldsToBigNumber(order));
+ },
+ parseOrderJson(json: any): SignedOrder {
+ assert.doesConformToSchema('order', json, schemas.signedOrderSchema);
+ return typeConverters.convertOrderStringFieldsToBigNumber(json);
+ },
+ parseOrderbookResponseJson(json: any): OrderbookResponse {
+ assert.doesConformToSchema('orderBook', json, schemas.relayerApiOrderBookResponseSchema);
+ return typeConverters.convertOrderbookStringFieldsToBigNumber(json);
+ },
+ parseFeesResponseJson(json: any): FeesResponse {
+ assert.doesConformToSchema('fees', json, schemas.relayerApiFeesResponseSchema);
+ return typeConverters.convertStringsFieldsToBigNumbers(json, ['makerFee', 'takerFee']);
+ },
+};
diff --git a/packages/connect/src/utils/type_converters.ts b/packages/connect/src/utils/type_converters.ts
index 28810af1a..5ba1b8291 100644
--- a/packages/connect/src/utils/type_converters.ts
+++ b/packages/connect/src/utils/type_converters.ts
@@ -1,15 +1,17 @@
import {BigNumber} from 'bignumber.js';
import * as _ from 'lodash';
-// TODO: convert all of these to non-mutating, pure functions
export const typeConverters = {
- convertOrderbookStringFieldsToBigNumber(orderbook: object): void {
- _.each(orderbook, (orders: object[]) => {
- _.each(orders, (order: object) => this.convertOrderStringFieldsToBigNumber(order));
- });
+ convertOrderbookStringFieldsToBigNumber(orderbook: any): any {
+ const bids = _.get(orderbook, 'bids', []);
+ const asks = _.get(orderbook, 'asks', []);
+ return {
+ bids: bids.map((order: any) => this.convertOrderStringFieldsToBigNumber(order)),
+ asks: asks.map((order: any) => this.convertOrderStringFieldsToBigNumber(order)),
+ };
},
- convertOrderStringFieldsToBigNumber(order: object): void {
- this.convertStringsFieldsToBigNumbers(order, [
+ convertOrderStringFieldsToBigNumber(order: any): any {
+ return this.convertStringsFieldsToBigNumbers(order, [
'makerTokenAmount',
'takerTokenAmount',
'makerFee',
@@ -18,9 +20,11 @@ export const typeConverters = {
'salt',
]);
},
- convertStringsFieldsToBigNumbers(obj: object, fields: string[]): void {
+ convertStringsFieldsToBigNumbers(obj: any, fields: string[]): any {
+ const result = _.assign({}, obj);
_.each(fields, field => {
- _.update(obj, field, (value: string) => new BigNumber(value));
+ _.update(result, field, (value: string) => new BigNumber(value));
});
+ return result;
},
};