From 126048bac9f52871b841d9898cabe7cfd265ebb6 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 15 Dec 2017 13:58:30 +0100 Subject: Fix connect unused vars --- packages/connect/src/utils/orderbook_channel_message_parsers.ts | 1 - packages/connect/src/ws_orderbook_channel.ts | 2 -- 2 files changed, 3 deletions(-) (limited to 'packages/connect/src') diff --git a/packages/connect/src/utils/orderbook_channel_message_parsers.ts b/packages/connect/src/utils/orderbook_channel_message_parsers.ts index 486a416ef..d9a84cca2 100644 --- a/packages/connect/src/utils/orderbook_channel_message_parsers.ts +++ b/packages/connect/src/utils/orderbook_channel_message_parsers.ts @@ -5,7 +5,6 @@ import * as _ from 'lodash'; import { OrderbookChannelMessage, OrderbookChannelMessageTypes, - SignedOrder, } from '../types'; import {typeConverters} from './type_converters'; diff --git a/packages/connect/src/ws_orderbook_channel.ts b/packages/connect/src/ws_orderbook_channel.ts index 6687025c0..f58593484 100644 --- a/packages/connect/src/ws_orderbook_channel.ts +++ b/packages/connect/src/ws_orderbook_channel.ts @@ -8,7 +8,6 @@ import { OrderbookChannelHandler, OrderbookChannelMessageTypes, OrderbookChannelSubscriptionOpts, - SignedOrder, WebsocketClientEventType, WebsocketConnectionEventType, } from './types'; @@ -99,7 +98,6 @@ export class WebSocketOrderbookChannel implements OrderbookChannel { try { const utf8Data = message.utf8Data; const parserResult = orderbookChannelMessageParsers.parser(utf8Data); - const type = parserResult.type; if (parserResult.requestId === requestId) { switch (parserResult.type) { case (OrderbookChannelMessageTypes.Snapshot): { -- cgit From cb11aec84df346d5180c7d5874859c1c34f0bf1c Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Wed, 20 Dec 2017 00:44:08 -0500 Subject: Add new underscore-privates rule to @0xproject/tslint-config and fix lint errors --- packages/connect/src/http_client.ts | 6 ++--- packages/connect/src/ws_orderbook_channel.ts | 34 ++++++++++++++-------------- 2 files changed, 20 insertions(+), 20 deletions(-) (limited to 'packages/connect/src') diff --git a/packages/connect/src/http_client.ts b/packages/connect/src/http_client.ts index 252c9e9dd..3bada2be0 100644 --- a/packages/connect/src/http_client.ts +++ b/packages/connect/src/http_client.ts @@ -31,7 +31,7 @@ BigNumber.config({ * that implement the standard relayer API v0 */ export class HttpClient implements Client { - private apiEndpointUrl: string; + private _apiEndpointUrl: string; /** * Instantiates a new HttpClient instance * @param url The relayer API base HTTP url you would like to interact with @@ -39,7 +39,7 @@ export class HttpClient implements Client { */ constructor(url: string) { assert.isHttpUrl('url', url); - this.apiEndpointUrl = url; + this._apiEndpointUrl = url; } /** * Retrieve token pair info from the API @@ -152,7 +152,7 @@ export class HttpClient implements Client { const stringifiedParams = queryString.stringify(params); query = `?${stringifiedParams}`; } - const url = `${this.apiEndpointUrl}/v0${path}${query}`; + const url = `${this._apiEndpointUrl}/v0${path}${query}`; const headers = new Headers({ 'content-type': 'application/json', }); diff --git a/packages/connect/src/ws_orderbook_channel.ts b/packages/connect/src/ws_orderbook_channel.ts index f58593484..a669d8094 100644 --- a/packages/connect/src/ws_orderbook_channel.ts +++ b/packages/connect/src/ws_orderbook_channel.ts @@ -18,10 +18,10 @@ import {orderbookChannelMessageParsers} from './utils/orderbook_channel_message_ * that implements the standard relayer API v0 */ export class WebSocketOrderbookChannel implements OrderbookChannel { - private apiEndpointUrl: string; - private client: WebSocket.client; - private connectionIfExists?: WebSocket.connection; - private subscriptionCounter = 0; + private _apiEndpointUrl: string; + private _client: WebSocket.client; + private _connectionIfExists?: WebSocket.connection; + private _subscriptionCounter = 0; /** * Instantiates a new WebSocketOrderbookChannel instance * @param url The relayer API base WS url you would like to interact with @@ -29,8 +29,8 @@ export class WebSocketOrderbookChannel implements OrderbookChannel { */ constructor(url: string) { assert.isUri('url', url); - this.apiEndpointUrl = url; - this.client = new WebSocket.client(); + this._apiEndpointUrl = url; + this._client = new WebSocket.client(); } /** * Subscribe to orderbook snapshots and updates from the websocket @@ -46,11 +46,11 @@ export class WebSocketOrderbookChannel implements OrderbookChannel { assert.isFunction('handler.onUpdate', _.get(handler, 'onUpdate')); assert.isFunction('handler.onError', _.get(handler, 'onError')); assert.isFunction('handler.onClose', _.get(handler, 'onClose')); - this.subscriptionCounter += 1; + this._subscriptionCounter += 1; const subscribeMessage = { type: 'subscribe', channel: 'orderbook', - requestId: this.subscriptionCounter, + requestId: this._subscriptionCounter, payload: subscriptionOpts, }; this._getConnection((error, connection) => { @@ -74,22 +74,22 @@ export class WebSocketOrderbookChannel implements OrderbookChannel { * Close the websocket and stop receiving updates */ public close() { - if (!_.isUndefined(this.connectionIfExists)) { - this.connectionIfExists.close(); + if (!_.isUndefined(this._connectionIfExists)) { + this._connectionIfExists.close(); } } private _getConnection(callback: (error?: Error, connection?: WebSocket.connection) => void) { - if (!_.isUndefined(this.connectionIfExists) && this.connectionIfExists.connected) { - callback(undefined, this.connectionIfExists); + if (!_.isUndefined(this._connectionIfExists) && this._connectionIfExists.connected) { + callback(undefined, this._connectionIfExists); } else { - this.client.on(WebsocketClientEventType.Connect, connection => { - this.connectionIfExists = connection; - callback(undefined, this.connectionIfExists); + this._client.on(WebsocketClientEventType.Connect, connection => { + this._connectionIfExists = connection; + callback(undefined, this._connectionIfExists); }); - this.client.on(WebsocketClientEventType.ConnectFailed, error => { + this._client.on(WebsocketClientEventType.ConnectFailed, error => { callback(error, undefined); }); - this.client.connect(this.apiEndpointUrl); + this._client.connect(this._apiEndpointUrl); } } private _handleWebSocketMessage(requestId: number, subscriptionOpts: OrderbookChannelSubscriptionOpts, -- cgit From 12dc8c0d158517b77ac548e0f102a45068f2f5e3 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Thu, 21 Dec 2017 14:44:09 -0500 Subject: Add missing calls to bigNumberConfigs in packages where we are instantiating BigNumbers --- packages/connect/src/http_client.ts | 5 ----- packages/connect/src/index.ts | 5 +++++ 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'packages/connect/src') diff --git a/packages/connect/src/http_client.ts b/packages/connect/src/http_client.ts index 3bada2be0..028a6c07b 100644 --- a/packages/connect/src/http_client.ts +++ b/packages/connect/src/http_client.ts @@ -21,11 +21,6 @@ import { } from './types'; import {typeConverters} from './utils/type_converters'; -// TODO: move this and bigNumberConfigs in the 0x.js package into one place -BigNumber.config({ - EXPONENTIAL_AT: 1000, -}); - /** * This class includes all the functionality related to interacting with a set of HTTP endpoints * that implement the standard relayer API v0 diff --git a/packages/connect/src/index.ts b/packages/connect/src/index.ts index ec369a606..c0f847d09 100644 --- a/packages/connect/src/index.ts +++ b/packages/connect/src/index.ts @@ -1,3 +1,8 @@ +import {bigNumberConfigs} from '@0xproject/utils'; + +// Customize our BigNumber instances +bigNumberConfigs.configure(); + export {HttpClient} from './http_client'; export {WebSocketOrderbookChannel} from './ws_orderbook_channel'; export { -- cgit From f8cbfea4a19bb5f55a7450e7817281b986365431 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Thu, 21 Dec 2017 15:40:09 -0500 Subject: Remove unused import --- packages/connect/src/http_client.ts | 1 - 1 file changed, 1 deletion(-) (limited to 'packages/connect/src') diff --git a/packages/connect/src/http_client.ts b/packages/connect/src/http_client.ts index 028a6c07b..924421f63 100644 --- a/packages/connect/src/http_client.ts +++ b/packages/connect/src/http_client.ts @@ -1,6 +1,5 @@ import {assert} from '@0xproject/assert'; import {schemas} from '@0xproject/json-schemas'; -import {BigNumber} from 'bignumber.js'; import 'isomorphic-fetch'; import * as _ from 'lodash'; import * as queryString from 'query-string'; -- cgit From 9f3acf8e2888b6105062e47664ecd5adaaf3c889 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Sun, 24 Dec 2017 19:02:01 -0500 Subject: Prevent getFeesAsync method on HttpClient from mutating input --- packages/connect/src/http_client.ts | 6 ------ packages/connect/src/utils/type_converters.ts | 5 ----- 2 files changed, 11 deletions(-) (limited to 'packages/connect/src') diff --git a/packages/connect/src/http_client.ts b/packages/connect/src/http_client.ts index 924421f63..da052ba3c 100644 --- a/packages/connect/src/http_client.ts +++ b/packages/connect/src/http_client.ts @@ -112,12 +112,6 @@ export class HttpClient implements Client { */ public async getFeesAsync(request: FeesRequest): Promise { assert.doesConformToSchema('request', request, schemas.relayerApiFeesPayloadSchema); - typeConverters.convertBigNumberFieldsToStrings(request, [ - 'makerTokenAmount', - 'takerTokenAmount', - 'expirationUnixTimestampSec', - 'salt', - ]); const requestOpts = { payload: request, }; diff --git a/packages/connect/src/utils/type_converters.ts b/packages/connect/src/utils/type_converters.ts index c136382fd..28810af1a 100644 --- a/packages/connect/src/utils/type_converters.ts +++ b/packages/connect/src/utils/type_converters.ts @@ -18,11 +18,6 @@ export const typeConverters = { 'salt', ]); }, - convertBigNumberFieldsToStrings(obj: object, fields: string[]): void { - _.each(fields, field => { - _.update(obj, field, (value: BigNumber) => value.toString()); - }); - }, convertStringsFieldsToBigNumbers(obj: object, fields: string[]): void { _.each(fields, field => { _.update(obj, field, (value: string) => new BigNumber(value)); -- cgit From 8fe81c9d090ce50496f3150602f19433e7aedd1e Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Tue, 19 Dec 2017 17:22:38 -0500 Subject: Refactor JSON parsing in HttpClient --- packages/connect/src/http_client.ts | 78 +++++++++++++--------- .../src/utils/orderbook_channel_message_parser.ts | 40 +++++++++++ .../src/utils/orderbook_channel_message_parsers.ts | 40 ----------- .../src/utils/relayer_response_json_parsers.ts | 42 ++++++++++++ packages/connect/src/utils/type_converters.ts | 22 +++--- packages/connect/src/ws_orderbook_channel.ts | 4 +- 6 files changed, 142 insertions(+), 84 deletions(-) create mode 100644 packages/connect/src/utils/orderbook_channel_message_parser.ts delete mode 100644 packages/connect/src/utils/orderbook_channel_message_parsers.ts create mode 100644 packages/connect/src/utils/relayer_response_json_parsers.ts (limited to 'packages/connect/src') diff --git a/packages/connect/src/http_client.ts b/packages/connect/src/http_client.ts index da052ba3c..f738c7e07 100644 --- a/packages/connect/src/http_client.ts +++ b/packages/connect/src/http_client.ts @@ -18,7 +18,7 @@ import { TokenPairsItem, TokenPairsRequest, } from './types'; -import {typeConverters} from './utils/type_converters'; +import {relayerResponseJsonParsers} from './utils/relayer_response_json_parsers'; /** * This class includes all the functionality related to interacting with a set of HTTP endpoints @@ -48,18 +48,13 @@ export class HttpClient implements Client { const requestOpts = { params: request, }; - const tokenPairs = await this._requestAsync('/token_pairs', HttpRequestType.Get, requestOpts); - assert.doesConformToSchema( - 'tokenPairs', tokenPairs, schemas.relayerApiTokenPairsResponseSchema); - _.each(tokenPairs, (tokenPair: object) => { - typeConverters.convertStringsFieldsToBigNumbers(tokenPair, [ - 'tokenA.minAmount', - 'tokenA.maxAmount', - 'tokenB.minAmount', - 'tokenB.maxAmount', - ]); - }); - return tokenPairs; + const result = await this._requestAsync( + '/token_pairs', + HttpRequestType.Get, + relayerResponseJsonParsers.parseTokenPairsJson, + requestOpts, + ); + return result; } /** * Retrieve orders from the API @@ -73,10 +68,13 @@ export class HttpClient implements Client { const requestOpts = { params: request, }; - const orders = await this._requestAsync(`/orders`, HttpRequestType.Get, requestOpts); - assert.doesConformToSchema('orders', orders, schemas.signedOrdersSchema); - _.each(orders, (order: object) => typeConverters.convertOrderStringFieldsToBigNumber(order)); - return orders; + const result = await this._requestAsync( + `/orders`, + HttpRequestType.Get, + relayerResponseJsonParsers.parseOrdersJson, + requestOpts, + ); + return result; } /** * Retrieve a specific order from the API @@ -85,10 +83,12 @@ export class HttpClient implements Client { */ public async getOrderAsync(orderHash: string): Promise { assert.doesConformToSchema('orderHash', orderHash, schemas.orderHashSchema); - const order = await this._requestAsync(`/order/${orderHash}`, HttpRequestType.Get); - assert.doesConformToSchema('order', order, schemas.signedOrderSchema); - typeConverters.convertOrderStringFieldsToBigNumber(order); - return order; + const result = await this._requestAsync( + `/order/${orderHash}`, + HttpRequestType.Get, + relayerResponseJsonParsers.parseOrderJson, + ); + return result; } /** * Retrieve an orderbook from the API @@ -100,10 +100,13 @@ export class HttpClient implements Client { const requestOpts = { params: request, }; - const orderBook = await this._requestAsync('/orderbook', HttpRequestType.Get, requestOpts); - assert.doesConformToSchema('orderBook', orderBook, schemas.relayerApiOrderBookResponseSchema); - typeConverters.convertOrderbookStringFieldsToBigNumber(orderBook); - return orderBook; + const result = await this._requestAsync( + '/orderbook', + HttpRequestType.Get, + relayerResponseJsonParsers.parseOrderbookResponseJson, + requestOpts, + ); + return result; } /** * Retrieve fee information from the API @@ -115,10 +118,13 @@ export class HttpClient implements Client { const requestOpts = { payload: request, }; - const fees = await this._requestAsync('/fees', HttpRequestType.Post, requestOpts); - assert.doesConformToSchema('fees', fees, schemas.relayerApiFeesResponseSchema); - typeConverters.convertStringsFieldsToBigNumbers(fees, ['makerFee', 'takerFee']); - return fees; + const result = await this._requestAsync( + '/fees', + HttpRequestType.Post, + relayerResponseJsonParsers.parseFeesResponseJson, + requestOpts, + ); + return result; } /** * Submit a signed order to the API @@ -129,10 +135,16 @@ export class HttpClient implements Client { const requestOpts = { payload: signedOrder, }; - await this._requestAsync('/order', HttpRequestType.Post, requestOpts); + await this._requestAsync( + '/order', + HttpRequestType.Post, + _.noop, + requestOpts, + ); } - private async _requestAsync(path: string, requestType: HttpRequestType, - requestOptions?: HttpRequestOptions): Promise { + private async _requestAsync(path: string, requestType: HttpRequestType, + jsonParser: (json: any) => T, + requestOptions?: HttpRequestOptions): Promise { const params = _.get(requestOptions, 'params'); const payload = _.get(requestOptions, 'payload'); let query = ''; @@ -154,6 +166,6 @@ export class HttpClient implements Client { throw Error(response.statusText); } const json = await response.json(); - return json; + return jsonParser(json); } } diff --git a/packages/connect/src/utils/orderbook_channel_message_parser.ts b/packages/connect/src/utils/orderbook_channel_message_parser.ts new file mode 100644 index 000000000..adc565d29 --- /dev/null +++ b/packages/connect/src/utils/orderbook_channel_message_parser.ts @@ -0,0 +1,40 @@ +import {assert} from '@0xproject/assert'; +import {schemas} from '@0xproject/json-schemas'; +import * as _ from 'lodash'; + +import { + OrderbookChannelMessage, + OrderbookChannelMessageTypes, +} from '../types'; + +import {relayerResponseJsonParsers} from './relayer_response_json_parsers'; + +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}`); + assert.isString('type', type); + switch (type) { + case (OrderbookChannelMessageTypes.Snapshot): { + assert.doesConformToSchema('message', messageObj, schemas.relayerApiOrderbookChannelSnapshotSchema); + const orderbookJson = messageObj.payload; + const orderbook = relayerResponseJsonParsers.parseOrderbookResponseJson(orderbookJson); + return _.assign(messageObj, {payload: orderbook}); + } + case (OrderbookChannelMessageTypes.Update): { + assert.doesConformToSchema('message', messageObj, schemas.relayerApiOrderbookChannelUpdateSchema); + const orderJson = messageObj.payload; + const order = relayerResponseJsonParsers.parseOrderJson(orderJson); + return _.assign(messageObj, {payload: order}); + } + default: { + return { + type: OrderbookChannelMessageTypes.Unknown, + requestId: 0, + payload: undefined, + }; + } + } + }, +}; diff --git a/packages/connect/src/utils/orderbook_channel_message_parsers.ts b/packages/connect/src/utils/orderbook_channel_message_parsers.ts deleted file mode 100644 index d9a84cca2..000000000 --- a/packages/connect/src/utils/orderbook_channel_message_parsers.ts +++ /dev/null @@ -1,40 +0,0 @@ -import {assert} from '@0xproject/assert'; -import {schemas} from '@0xproject/json-schemas'; -import * as _ from 'lodash'; - -import { - OrderbookChannelMessage, - OrderbookChannelMessageTypes, -} from '../types'; - -import {typeConverters} from './type_converters'; - -export const orderbookChannelMessageParsers = { - parser(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}`); - assert.isString('type', type); - switch (type) { - case (OrderbookChannelMessageTypes.Snapshot): { - assert.doesConformToSchema('message', messageObj, schemas.relayerApiOrderbookChannelSnapshotSchema); - const orderbook = messageObj.payload; - typeConverters.convertOrderbookStringFieldsToBigNumber(orderbook); - return messageObj; - } - case (OrderbookChannelMessageTypes.Update): { - assert.doesConformToSchema('message', messageObj, schemas.relayerApiOrderbookChannelUpdateSchema); - const order = messageObj.payload; - typeConverters.convertOrderStringFieldsToBigNumber(order); - return messageObj; - } - default: { - return { - type: OrderbookChannelMessageTypes.Unknown, - requestId: 0, - payload: undefined, - }; - } - } - }, -}; 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; }, }; diff --git a/packages/connect/src/ws_orderbook_channel.ts b/packages/connect/src/ws_orderbook_channel.ts index a669d8094..849fb9a4e 100644 --- a/packages/connect/src/ws_orderbook_channel.ts +++ b/packages/connect/src/ws_orderbook_channel.ts @@ -11,7 +11,7 @@ import { WebsocketClientEventType, WebsocketConnectionEventType, } from './types'; -import {orderbookChannelMessageParsers} from './utils/orderbook_channel_message_parsers'; +import {orderbookChannelMessageParser} from './utils/orderbook_channel_message_parser'; /** * This class includes all the functionality related to interacting with a websocket endpoint @@ -97,7 +97,7 @@ export class WebSocketOrderbookChannel implements OrderbookChannel { if (!_.isUndefined(message.utf8Data)) { try { const utf8Data = message.utf8Data; - const parserResult = orderbookChannelMessageParsers.parser(utf8Data); + const parserResult = orderbookChannelMessageParser.parse(utf8Data); if (parserResult.requestId === requestId) { switch (parserResult.type) { case (OrderbookChannelMessageTypes.Snapshot): { -- cgit From e744e4cd989bd3ae1070c59f7baa8097f18b8b06 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 22 Dec 2017 15:05:32 +0100 Subject: Apply prettier config --- packages/connect/src/http_client.ts | 18 +++++++------- packages/connect/src/index.ts | 6 ++--- .../src/schemas/relayer_fees_request_schema.ts | 4 ++-- .../schemas/relayer_orderbook_request_schema.ts | 4 ++-- .../src/schemas/relayer_orders_request_schema.ts | 20 ++++++++-------- .../schemas/relayer_token_pairs_request_schema.ts | 4 ++-- packages/connect/src/schemas/schemas.ts | 12 +++------- packages/connect/src/types.ts | 25 +++++++++++-------- .../src/utils/orderbook_channel_message_parsers.ts | 15 +++++------- packages/connect/src/utils/type_converters.ts | 2 +- packages/connect/src/ws_orderbook_channel.ts | 28 +++++++++++++++------- 11 files changed, 73 insertions(+), 65 deletions(-) (limited to 'packages/connect/src') diff --git a/packages/connect/src/http_client.ts b/packages/connect/src/http_client.ts index 924421f63..06b5d8ace 100644 --- a/packages/connect/src/http_client.ts +++ b/packages/connect/src/http_client.ts @@ -1,10 +1,10 @@ -import {assert} from '@0xproject/assert'; -import {schemas} from '@0xproject/json-schemas'; +import { assert } from '@0xproject/assert'; +import { schemas } from '@0xproject/json-schemas'; import 'isomorphic-fetch'; import * as _ from 'lodash'; import * as queryString from 'query-string'; -import {schemas as clientSchemas} from './schemas/schemas'; +import { schemas as clientSchemas } from './schemas/schemas'; import { Client, FeesRequest, @@ -18,7 +18,7 @@ import { TokenPairsItem, TokenPairsRequest, } from './types'; -import {typeConverters} from './utils/type_converters'; +import { typeConverters } from './utils/type_converters'; /** * This class includes all the functionality related to interacting with a set of HTTP endpoints @@ -49,8 +49,7 @@ export class HttpClient implements Client { params: request, }; const tokenPairs = await this._requestAsync('/token_pairs', HttpRequestType.Get, requestOpts); - assert.doesConformToSchema( - 'tokenPairs', tokenPairs, schemas.relayerApiTokenPairsResponseSchema); + assert.doesConformToSchema('tokenPairs', tokenPairs, schemas.relayerApiTokenPairsResponseSchema); _.each(tokenPairs, (tokenPair: object) => { typeConverters.convertStringsFieldsToBigNumbers(tokenPair, [ 'tokenA.minAmount', @@ -137,8 +136,11 @@ export class HttpClient implements Client { }; await this._requestAsync('/order', HttpRequestType.Post, requestOpts); } - private async _requestAsync(path: string, requestType: HttpRequestType, - requestOptions?: HttpRequestOptions): Promise { + private async _requestAsync( + path: string, + requestType: HttpRequestType, + requestOptions?: HttpRequestOptions, + ): Promise { const params = _.get(requestOptions, 'params'); const payload = _.get(requestOptions, 'payload'); let query = ''; diff --git a/packages/connect/src/index.ts b/packages/connect/src/index.ts index c0f847d09..b9fc3c568 100644 --- a/packages/connect/src/index.ts +++ b/packages/connect/src/index.ts @@ -1,10 +1,10 @@ -import {bigNumberConfigs} from '@0xproject/utils'; +import { bigNumberConfigs } from '@0xproject/utils'; // Customize our BigNumber instances bigNumberConfigs.configure(); -export {HttpClient} from './http_client'; -export {WebSocketOrderbookChannel} from './ws_orderbook_channel'; +export { HttpClient } from './http_client'; +export { WebSocketOrderbookChannel } from './ws_orderbook_channel'; export { Client, ECSignature, diff --git a/packages/connect/src/schemas/relayer_fees_request_schema.ts b/packages/connect/src/schemas/relayer_fees_request_schema.ts index 9408c94a0..f20e077ba 100644 --- a/packages/connect/src/schemas/relayer_fees_request_schema.ts +++ b/packages/connect/src/schemas/relayer_fees_request_schema.ts @@ -2,7 +2,7 @@ export const relayerOrderBookRequestSchema = { id: '/RelayerOrderBookRequest', type: 'object', properties: { - baseTokenAddress: {$ref: '/Address'}, - quoteTokenAddress: {$ref: '/Address'}, + baseTokenAddress: { $ref: '/Address' }, + quoteTokenAddress: { $ref: '/Address' }, }, }; diff --git a/packages/connect/src/schemas/relayer_orderbook_request_schema.ts b/packages/connect/src/schemas/relayer_orderbook_request_schema.ts index 9408c94a0..f20e077ba 100644 --- a/packages/connect/src/schemas/relayer_orderbook_request_schema.ts +++ b/packages/connect/src/schemas/relayer_orderbook_request_schema.ts @@ -2,7 +2,7 @@ export const relayerOrderBookRequestSchema = { id: '/RelayerOrderBookRequest', type: 'object', properties: { - baseTokenAddress: {$ref: '/Address'}, - quoteTokenAddress: {$ref: '/Address'}, + baseTokenAddress: { $ref: '/Address' }, + quoteTokenAddress: { $ref: '/Address' }, }, }; diff --git a/packages/connect/src/schemas/relayer_orders_request_schema.ts b/packages/connect/src/schemas/relayer_orders_request_schema.ts index c11bc77be..570238dae 100644 --- a/packages/connect/src/schemas/relayer_orders_request_schema.ts +++ b/packages/connect/src/schemas/relayer_orders_request_schema.ts @@ -2,15 +2,15 @@ export const relayerOrdersRequestSchema = { id: '/RelayerOrdersRequest', type: 'object', properties: { - exchangeContractAddress: {$ref: '/Address'}, - tokenAddress: {$ref: '/Address'}, - makerTokenAddress: {$ref: '/Address'}, - takerTokenAddress: {$ref: '/Address'}, - tokenA: {$ref: '/Address'}, - tokenB: {$ref: '/Address'}, - maker: {$ref: '/Address'}, - taker: {$ref: '/Address'}, - trader: {$ref: '/Address'}, - feeRecipient: {$ref: '/Address'}, + exchangeContractAddress: { $ref: '/Address' }, + tokenAddress: { $ref: '/Address' }, + makerTokenAddress: { $ref: '/Address' }, + takerTokenAddress: { $ref: '/Address' }, + tokenA: { $ref: '/Address' }, + tokenB: { $ref: '/Address' }, + maker: { $ref: '/Address' }, + taker: { $ref: '/Address' }, + trader: { $ref: '/Address' }, + feeRecipient: { $ref: '/Address' }, }, }; diff --git a/packages/connect/src/schemas/relayer_token_pairs_request_schema.ts b/packages/connect/src/schemas/relayer_token_pairs_request_schema.ts index 8013e1454..379232204 100644 --- a/packages/connect/src/schemas/relayer_token_pairs_request_schema.ts +++ b/packages/connect/src/schemas/relayer_token_pairs_request_schema.ts @@ -2,7 +2,7 @@ export const relayerTokenPairsRequestSchema = { id: '/RelayerTokenPairsRequest', type: 'object', properties: { - tokenA: {$ref: '/Address'}, - tokenB: {$ref: '/Address'}, + tokenA: { $ref: '/Address' }, + tokenB: { $ref: '/Address' }, }, }; diff --git a/packages/connect/src/schemas/schemas.ts b/packages/connect/src/schemas/schemas.ts index 97ac672bf..288d6969d 100644 --- a/packages/connect/src/schemas/schemas.ts +++ b/packages/connect/src/schemas/schemas.ts @@ -1,12 +1,6 @@ -import { - relayerOrderBookRequestSchema, -} from './relayer_orderbook_request_schema'; -import { - relayerOrdersRequestSchema, -} from './relayer_orders_request_schema'; -import { - relayerTokenPairsRequestSchema, -} from './relayer_token_pairs_request_schema'; +import { relayerOrderBookRequestSchema } from './relayer_orderbook_request_schema'; +import { relayerOrdersRequestSchema } from './relayer_orders_request_schema'; +import { relayerTokenPairsRequestSchema } from './relayer_token_pairs_request_schema'; export const schemas = { relayerOrderBookRequestSchema, diff --git a/packages/connect/src/types.ts b/packages/connect/src/types.ts index d02444a3e..1e6139bf9 100644 --- a/packages/connect/src/types.ts +++ b/packages/connect/src/types.ts @@ -1,4 +1,4 @@ -import {BigNumber} from 'bignumber.js'; +import { BigNumber } from 'bignumber.js'; // TODO: Consolidate Order, SignedOrder and ECSignature into a shared package instead of duplicating them from 0x.js export interface Order { @@ -57,19 +57,24 @@ export interface OrderbookChannelSubscriptionOpts { } export interface OrderbookChannelHandler { - onSnapshot: (channel: OrderbookChannel, subscriptionOpts: OrderbookChannelSubscriptionOpts, - snapshot: OrderbookResponse) => void; - onUpdate: (channel: OrderbookChannel, subscriptionOpts: OrderbookChannelSubscriptionOpts, - order: SignedOrder) => void; - onError: (channel: OrderbookChannel, subscriptionOpts: OrderbookChannelSubscriptionOpts, - err: Error) => void; + onSnapshot: ( + channel: OrderbookChannel, + subscriptionOpts: OrderbookChannelSubscriptionOpts, + snapshot: OrderbookResponse, + ) => void; + onUpdate: ( + channel: OrderbookChannel, + subscriptionOpts: OrderbookChannelSubscriptionOpts, + order: SignedOrder, + ) => void; + onError: (channel: OrderbookChannel, subscriptionOpts: OrderbookChannelSubscriptionOpts, err: Error) => void; onClose: (channel: OrderbookChannel, subscriptionOpts: OrderbookChannelSubscriptionOpts) => void; } export type OrderbookChannelMessage = - SnapshotOrderbookChannelMessage | - UpdateOrderbookChannelMessage | - UnknownOrderbookChannelMessage; + | SnapshotOrderbookChannelMessage + | UpdateOrderbookChannelMessage + | UnknownOrderbookChannelMessage; export enum OrderbookChannelMessageTypes { Snapshot = 'snapshot', diff --git a/packages/connect/src/utils/orderbook_channel_message_parsers.ts b/packages/connect/src/utils/orderbook_channel_message_parsers.ts index d9a84cca2..a4f22dc4b 100644 --- a/packages/connect/src/utils/orderbook_channel_message_parsers.ts +++ b/packages/connect/src/utils/orderbook_channel_message_parsers.ts @@ -1,13 +1,10 @@ -import {assert} from '@0xproject/assert'; -import {schemas} from '@0xproject/json-schemas'; +import { assert } from '@0xproject/assert'; +import { schemas } from '@0xproject/json-schemas'; import * as _ from 'lodash'; -import { - OrderbookChannelMessage, - OrderbookChannelMessageTypes, -} from '../types'; +import { OrderbookChannelMessage, OrderbookChannelMessageTypes } from '../types'; -import {typeConverters} from './type_converters'; +import { typeConverters } from './type_converters'; export const orderbookChannelMessageParsers = { parser(utf8Data: string): OrderbookChannelMessage { @@ -16,13 +13,13 @@ export const orderbookChannelMessageParsers = { assert.assert(!_.isUndefined(type), `Message is missing a type parameter: ${utf8Data}`); assert.isString('type', type); switch (type) { - case (OrderbookChannelMessageTypes.Snapshot): { + case OrderbookChannelMessageTypes.Snapshot: { assert.doesConformToSchema('message', messageObj, schemas.relayerApiOrderbookChannelSnapshotSchema); const orderbook = messageObj.payload; typeConverters.convertOrderbookStringFieldsToBigNumber(orderbook); return messageObj; } - case (OrderbookChannelMessageTypes.Update): { + case OrderbookChannelMessageTypes.Update: { assert.doesConformToSchema('message', messageObj, schemas.relayerApiOrderbookChannelUpdateSchema); const order = messageObj.payload; typeConverters.convertOrderStringFieldsToBigNumber(order); diff --git a/packages/connect/src/utils/type_converters.ts b/packages/connect/src/utils/type_converters.ts index c136382fd..31abdc95c 100644 --- a/packages/connect/src/utils/type_converters.ts +++ b/packages/connect/src/utils/type_converters.ts @@ -1,4 +1,4 @@ -import {BigNumber} from 'bignumber.js'; +import { BigNumber } from 'bignumber.js'; import * as _ from 'lodash'; // TODO: convert all of these to non-mutating, pure functions diff --git a/packages/connect/src/ws_orderbook_channel.ts b/packages/connect/src/ws_orderbook_channel.ts index a669d8094..399f97319 100644 --- a/packages/connect/src/ws_orderbook_channel.ts +++ b/packages/connect/src/ws_orderbook_channel.ts @@ -1,5 +1,5 @@ -import {assert} from '@0xproject/assert'; -import {schemas} from '@0xproject/json-schemas'; +import { assert } from '@0xproject/assert'; +import { schemas } from '@0xproject/json-schemas'; import * as _ from 'lodash'; import * as WebSocket from 'websocket'; @@ -11,7 +11,7 @@ import { WebsocketClientEventType, WebsocketConnectionEventType, } from './types'; -import {orderbookChannelMessageParsers} from './utils/orderbook_channel_message_parsers'; +import { orderbookChannelMessageParsers } from './utils/orderbook_channel_message_parsers'; /** * This class includes all the functionality related to interacting with a websocket endpoint @@ -41,7 +41,10 @@ export class WebSocketOrderbookChannel implements OrderbookChannel { */ public subscribe(subscriptionOpts: OrderbookChannelSubscriptionOpts, handler: OrderbookChannelHandler): void { assert.doesConformToSchema( - 'subscriptionOpts', subscriptionOpts, schemas.relayerApiOrderbookChannelSubscribePayload); + 'subscriptionOpts', + subscriptionOpts, + schemas.relayerApiOrderbookChannelSubscribePayload, + ); assert.isFunction('handler.onSnapshot', _.get(handler, 'onSnapshot')); assert.isFunction('handler.onUpdate', _.get(handler, 'onUpdate')); assert.isFunction('handler.onError', _.get(handler, 'onError')); @@ -92,25 +95,32 @@ export class WebSocketOrderbookChannel implements OrderbookChannel { this._client.connect(this._apiEndpointUrl); } } - private _handleWebSocketMessage(requestId: number, subscriptionOpts: OrderbookChannelSubscriptionOpts, - message: WebSocket.IMessage, handler: OrderbookChannelHandler): void { + private _handleWebSocketMessage( + requestId: number, + subscriptionOpts: OrderbookChannelSubscriptionOpts, + message: WebSocket.IMessage, + handler: OrderbookChannelHandler, + ): void { if (!_.isUndefined(message.utf8Data)) { try { const utf8Data = message.utf8Data; const parserResult = orderbookChannelMessageParsers.parser(utf8Data); if (parserResult.requestId === requestId) { switch (parserResult.type) { - case (OrderbookChannelMessageTypes.Snapshot): { + case OrderbookChannelMessageTypes.Snapshot: { handler.onSnapshot(this, subscriptionOpts, parserResult.payload); break; } - case (OrderbookChannelMessageTypes.Update): { + case OrderbookChannelMessageTypes.Update: { handler.onUpdate(this, subscriptionOpts, parserResult.payload); break; } default: { handler.onError( - this, subscriptionOpts, new Error(`Message has missing a type parameter: ${utf8Data}`)); + this, + subscriptionOpts, + new Error(`Message has missing a type parameter: ${utf8Data}`), + ); } } } -- cgit From 4dfa720f2dec2be0b0758633c087579a02e482bb Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 4 Jan 2018 12:38:19 +0100 Subject: Use configured version of bignumber in all packages --- packages/connect/src/index.ts | 5 ----- packages/connect/src/types.ts | 2 +- packages/connect/src/utils/type_converters.ts | 2 +- 3 files changed, 2 insertions(+), 7 deletions(-) (limited to 'packages/connect/src') diff --git a/packages/connect/src/index.ts b/packages/connect/src/index.ts index b9fc3c568..a492f5ae9 100644 --- a/packages/connect/src/index.ts +++ b/packages/connect/src/index.ts @@ -1,8 +1,3 @@ -import { bigNumberConfigs } from '@0xproject/utils'; - -// Customize our BigNumber instances -bigNumberConfigs.configure(); - export { HttpClient } from './http_client'; export { WebSocketOrderbookChannel } from './ws_orderbook_channel'; export { diff --git a/packages/connect/src/types.ts b/packages/connect/src/types.ts index 1e6139bf9..edb6c77a6 100644 --- a/packages/connect/src/types.ts +++ b/packages/connect/src/types.ts @@ -1,4 +1,4 @@ -import { BigNumber } from 'bignumber.js'; +import { BigNumber } from '@0xproject/utils'; // TODO: Consolidate Order, SignedOrder and ECSignature into a shared package instead of duplicating them from 0x.js export interface Order { diff --git a/packages/connect/src/utils/type_converters.ts b/packages/connect/src/utils/type_converters.ts index 31abdc95c..9d20154fd 100644 --- a/packages/connect/src/utils/type_converters.ts +++ b/packages/connect/src/utils/type_converters.ts @@ -1,4 +1,4 @@ -import { BigNumber } from 'bignumber.js'; +import { BigNumber } from '@0xproject/utils'; import * as _ from 'lodash'; // TODO: convert all of these to non-mutating, pure functions -- cgit From 262c34abac6a42edf7b6f3c463c9473740281296 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Tue, 9 Jan 2018 14:18:45 -0800 Subject: Update style --- packages/connect/src/http_client.ts | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) (limited to 'packages/connect/src') diff --git a/packages/connect/src/http_client.ts b/packages/connect/src/http_client.ts index 15c613923..d70e5ace4 100644 --- a/packages/connect/src/http_client.ts +++ b/packages/connect/src/http_client.ts @@ -48,13 +48,13 @@ export class HttpClient implements Client { const requestOpts = { params: request, }; - const result = await this._requestAsync( + const responseJson = await this._requestAsync( '/token_pairs', HttpRequestType.Get, - relayerResponseJsonParsers.parseTokenPairsJson, requestOpts, ); - return result; + const tokenPairs = relayerResponseJsonParsers.parseTokenPairsJson(responseJson); + return tokenPairs; } /** * Retrieve orders from the API @@ -68,13 +68,13 @@ export class HttpClient implements Client { const requestOpts = { params: request, }; - const result = await this._requestAsync( + const responseJson = await this._requestAsync( `/orders`, HttpRequestType.Get, - relayerResponseJsonParsers.parseOrdersJson, requestOpts, ); - return result; + const orders = relayerResponseJsonParsers.parseOrdersJson(responseJson); + return orders; } /** * Retrieve a specific order from the API @@ -83,12 +83,12 @@ export class HttpClient implements Client { */ public async getOrderAsync(orderHash: string): Promise { assert.doesConformToSchema('orderHash', orderHash, schemas.orderHashSchema); - const result = await this._requestAsync( + const responseJson = await this._requestAsync( `/order/${orderHash}`, HttpRequestType.Get, - relayerResponseJsonParsers.parseOrderJson, ); - return result; + const order = relayerResponseJsonParsers.parseOrderJson(responseJson); + return order; } /** * Retrieve an orderbook from the API @@ -100,13 +100,13 @@ export class HttpClient implements Client { const requestOpts = { params: request, }; - const result = await this._requestAsync( + const responseJson = await this._requestAsync( '/orderbook', HttpRequestType.Get, - relayerResponseJsonParsers.parseOrderbookResponseJson, requestOpts, ); - return result; + const orderbook = relayerResponseJsonParsers.parseOrderbookResponseJson(responseJson); + return orderbook; } /** * Retrieve fee information from the API @@ -118,13 +118,13 @@ export class HttpClient implements Client { const requestOpts = { payload: request, }; - const result = await this._requestAsync( + const responseJson = await this._requestAsync( '/fees', HttpRequestType.Post, - relayerResponseJsonParsers.parseFeesResponseJson, requestOpts, ); - return result; + const fees = relayerResponseJsonParsers.parseFeesResponseJson(responseJson); + return fees; } /** * Submit a signed order to the API @@ -138,13 +138,10 @@ export class HttpClient implements Client { await this._requestAsync( '/order', HttpRequestType.Post, - _.noop, requestOpts, ); } - private async _requestAsync(path: string, requestType: HttpRequestType, - jsonParser: (json: any) => T, - requestOptions?: HttpRequestOptions): Promise { + private async _requestAsync(path: string, requestType: HttpRequestType, requestOptions?: HttpRequestOptions): Promise { const params = _.get(requestOptions, 'params'); const payload = _.get(requestOptions, 'payload'); let query = ''; @@ -166,6 +163,6 @@ export class HttpClient implements Client { throw Error(response.statusText); } const json = await response.json(); - return jsonParser(json); + return json; } } -- cgit From eb760aa33f0e158b3d26058aa1e9906a1f400a14 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Tue, 9 Jan 2018 14:19:33 -0800 Subject: Prettier --- packages/connect/src/http_client.ts | 41 ++++++---------------- .../src/utils/relayer_response_json_parsers.ts | 13 +++---- 2 files changed, 15 insertions(+), 39 deletions(-) (limited to 'packages/connect/src') diff --git a/packages/connect/src/http_client.ts b/packages/connect/src/http_client.ts index d70e5ace4..5604a9607 100644 --- a/packages/connect/src/http_client.ts +++ b/packages/connect/src/http_client.ts @@ -48,11 +48,7 @@ export class HttpClient implements Client { const requestOpts = { params: request, }; - const responseJson = await this._requestAsync( - '/token_pairs', - HttpRequestType.Get, - requestOpts, - ); + const responseJson = await this._requestAsync('/token_pairs', HttpRequestType.Get, requestOpts); const tokenPairs = relayerResponseJsonParsers.parseTokenPairsJson(responseJson); return tokenPairs; } @@ -68,11 +64,7 @@ export class HttpClient implements Client { const requestOpts = { params: request, }; - const responseJson = await this._requestAsync( - `/orders`, - HttpRequestType.Get, - requestOpts, - ); + const responseJson = await this._requestAsync(`/orders`, HttpRequestType.Get, requestOpts); const orders = relayerResponseJsonParsers.parseOrdersJson(responseJson); return orders; } @@ -83,10 +75,7 @@ export class HttpClient implements Client { */ public async getOrderAsync(orderHash: string): Promise { assert.doesConformToSchema('orderHash', orderHash, schemas.orderHashSchema); - const responseJson = await this._requestAsync( - `/order/${orderHash}`, - HttpRequestType.Get, - ); + const responseJson = await this._requestAsync(`/order/${orderHash}`, HttpRequestType.Get); const order = relayerResponseJsonParsers.parseOrderJson(responseJson); return order; } @@ -100,11 +89,7 @@ export class HttpClient implements Client { const requestOpts = { params: request, }; - const responseJson = await this._requestAsync( - '/orderbook', - HttpRequestType.Get, - requestOpts, - ); + const responseJson = await this._requestAsync('/orderbook', HttpRequestType.Get, requestOpts); const orderbook = relayerResponseJsonParsers.parseOrderbookResponseJson(responseJson); return orderbook; } @@ -118,11 +103,7 @@ export class HttpClient implements Client { const requestOpts = { payload: request, }; - const responseJson = await this._requestAsync( - '/fees', - HttpRequestType.Post, - requestOpts, - ); + const responseJson = await this._requestAsync('/fees', HttpRequestType.Post, requestOpts); const fees = relayerResponseJsonParsers.parseFeesResponseJson(responseJson); return fees; } @@ -135,13 +116,13 @@ export class HttpClient implements Client { const requestOpts = { payload: signedOrder, }; - await this._requestAsync( - '/order', - HttpRequestType.Post, - requestOpts, - ); + await this._requestAsync('/order', HttpRequestType.Post, requestOpts); } - private async _requestAsync(path: string, requestType: HttpRequestType, requestOptions?: HttpRequestOptions): Promise { + private async _requestAsync( + path: string, + requestType: HttpRequestType, + requestOptions?: HttpRequestOptions, + ): Promise { const params = _.get(requestOptions, 'params'); const payload = _.get(requestOptions, 'payload'); let query = ''; diff --git a/packages/connect/src/utils/relayer_response_json_parsers.ts b/packages/connect/src/utils/relayer_response_json_parsers.ts index 02d88f26d..668461bf4 100644 --- a/packages/connect/src/utils/relayer_response_json_parsers.ts +++ b/packages/connect/src/utils/relayer_response_json_parsers.ts @@ -1,15 +1,10 @@ -import {assert} from '@0xproject/assert'; -import {schemas} from '@0xproject/json-schemas'; +import { assert } from '@0xproject/assert'; +import { schemas } from '@0xproject/json-schemas'; import * as _ from 'lodash'; -import { - FeesResponse, - OrderbookResponse, - SignedOrder, - TokenPairsItem, -} from '../types'; +import { FeesResponse, OrderbookResponse, SignedOrder, TokenPairsItem } from '../types'; -import {typeConverters} from './type_converters'; +import { typeConverters } from './type_converters'; export const relayerResponseJsonParsers = { parseTokenPairsJson(json: any): TokenPairsItem[] { -- cgit From c2c75124315f71c8a87ac83cbab84ba02115169d Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Thu, 11 Jan 2018 15:58:33 -0800 Subject: Improve HttpClient errors --- packages/connect/src/http_client.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'packages/connect/src') diff --git a/packages/connect/src/http_client.ts b/packages/connect/src/http_client.ts index 5604a9607..c30b499a2 100644 --- a/packages/connect/src/http_client.ts +++ b/packages/connect/src/http_client.ts @@ -20,6 +20,7 @@ import { } from './types'; import { relayerResponseJsonParsers } from './utils/relayer_response_json_parsers'; +const TRAILING_SLASHES_REGEX = /\/+$/; /** * This class includes all the functionality related to interacting with a set of HTTP endpoints * that implement the standard relayer API v0 @@ -33,7 +34,7 @@ export class HttpClient implements Client { */ constructor(url: string) { assert.isHttpUrl('url', url); - this._apiEndpointUrl = url; + this._apiEndpointUrl = url.replace(TRAILING_SLASHES_REGEX, ''); // remove trailing slashes } /** * Retrieve token pair info from the API @@ -134,16 +135,18 @@ export class HttpClient implements Client { const headers = new Headers({ 'content-type': 'application/json', }); - const response = await fetch(url, { method: requestType, body: JSON.stringify(payload), headers, }); + const json = await response.json(); if (!response.ok) { - throw Error(response.statusText); + const errorString = `${response.status} - ${response.statusText}\n${requestType} ${url}\n${JSON.stringify( + json, + )}`; + throw Error(errorString); } - const json = await response.json(); return json; } } -- cgit From fbcbf066cdc13b99d1d280e131c01bd5456d90ae Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Fri, 12 Jan 2018 11:53:14 -0800 Subject: Stop appending /v0 to HttpClient api endpoint urls --- packages/connect/src/http_client.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/connect/src') diff --git a/packages/connect/src/http_client.ts b/packages/connect/src/http_client.ts index c30b499a2..3df77b0f0 100644 --- a/packages/connect/src/http_client.ts +++ b/packages/connect/src/http_client.ts @@ -131,7 +131,7 @@ export class HttpClient implements Client { const stringifiedParams = queryString.stringify(params); query = `?${stringifiedParams}`; } - const url = `${this._apiEndpointUrl}/v0${path}${query}`; + const url = `${this._apiEndpointUrl}${path}${query}`; const headers = new Headers({ 'content-type': 'application/json', }); -- cgit