diff options
author | Brandon Millman <brandon@0xproject.com> | 2017-11-28 06:42:26 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-28 06:42:26 +0800 |
commit | 4a770dee84ee49e8038da7dd32ce26338176bf36 (patch) | |
tree | 5d3e9f2749411ba91fa567f96ce81d1206d273be | |
parent | 4a17f5e82074b01e74ae6982e82419a037eebdb4 (diff) | |
parent | 426a412ba1a0e3cd955d11bbe6d7426a61e5dd63 (diff) | |
download | dexon-0x-contracts-4a770dee84ee49e8038da7dd32ce26338176bf36.tar dexon-0x-contracts-4a770dee84ee49e8038da7dd32ce26338176bf36.tar.gz dexon-0x-contracts-4a770dee84ee49e8038da7dd32ce26338176bf36.tar.bz2 dexon-0x-contracts-4a770dee84ee49e8038da7dd32ce26338176bf36.tar.lz dexon-0x-contracts-4a770dee84ee49e8038da7dd32ce26338176bf36.tar.xz dexon-0x-contracts-4a770dee84ee49e8038da7dd32ce26338176bf36.tar.zst dexon-0x-contracts-4a770dee84ee49e8038da7dd32ce26338176bf36.zip |
Merge pull request #241 from 0xProject/fix/moveConnectTypes
Move all connect types into types.ts
-rw-r--r-- | packages/connect/src/http_client.ts | 27 | ||||
-rw-r--r-- | packages/connect/src/types.ts | 43 | ||||
-rw-r--r-- | packages/connect/src/ws_orderbook_channel.ts | 23 |
3 files changed, 49 insertions, 44 deletions
diff --git a/packages/connect/src/http_client.ts b/packages/connect/src/http_client.ts index 85dc83c61..18855693f 100644 --- a/packages/connect/src/http_client.ts +++ b/packages/connect/src/http_client.ts @@ -9,6 +9,8 @@ import { Client, FeesRequest, FeesResponse, + HttpRequestOptions, + HttpRequestType, OrderbookRequest, OrderbookResponse, OrdersRequest, @@ -23,16 +25,6 @@ BigNumber.config({ EXPONENTIAL_AT: 1000, }); -interface RequestOptions { - params?: object; - payload?: object; -} - -enum RequestType { - Get = 'GET', - Post = 'POST', -} - /** * This class includes all the functionality related to interacting with a set of HTTP endpoints * that implement the standard relayer API v0 @@ -61,7 +53,7 @@ export class HttpClient implements Client { const requestOpts = { params: request, }; - const tokenPairs = await this._requestAsync('/token_pairs', RequestType.Get, requestOpts); + const tokenPairs = await this._requestAsync('/token_pairs', HttpRequestType.Get, requestOpts); assert.doesConformToSchema( 'tokenPairs', tokenPairs, schemas.relayerApiTokenPairsResponseSchema); _.each(tokenPairs, (tokenPair: object) => { @@ -86,7 +78,7 @@ export class HttpClient implements Client { const requestOpts = { params: request, }; - const orders = await this._requestAsync(`/orders`, RequestType.Get, requestOpts); + const orders = await this._requestAsync(`/orders`, HttpRequestType.Get, requestOpts); assert.doesConformToSchema('orders', orders, schemas.signedOrdersSchema); _.each(orders, (order: object) => typeConverters.convertOrderStringFieldsToBigNumber(order)); return orders; @@ -98,7 +90,7 @@ export class HttpClient implements Client { */ public async getOrderAsync(orderHash: string): Promise<SignedOrder> { assert.doesConformToSchema('orderHash', orderHash, schemas.orderHashSchema); - const order = await this._requestAsync(`/order/${orderHash}`, RequestType.Get); + const order = await this._requestAsync(`/order/${orderHash}`, HttpRequestType.Get); assert.doesConformToSchema('order', order, schemas.signedOrderSchema); typeConverters.convertOrderStringFieldsToBigNumber(order); return order; @@ -113,7 +105,7 @@ export class HttpClient implements Client { const requestOpts = { params: request, }; - const orderBook = await this._requestAsync('/orderbook', RequestType.Get, requestOpts); + const orderBook = await this._requestAsync('/orderbook', HttpRequestType.Get, requestOpts); assert.doesConformToSchema('orderBook', orderBook, schemas.relayerApiOrderBookResponseSchema); typeConverters.convertOrderbookStringFieldsToBigNumber(orderBook); return orderBook; @@ -134,7 +126,7 @@ export class HttpClient implements Client { const requestOpts = { payload: request, }; - const fees = await this._requestAsync('/fees', RequestType.Post, requestOpts); + const fees = await this._requestAsync('/fees', HttpRequestType.Post, requestOpts); assert.doesConformToSchema('fees', fees, schemas.relayerApiFeesResponseSchema); typeConverters.convertStringsFieldsToBigNumbers(fees, ['makerFee', 'takerFee']); return fees; @@ -148,9 +140,10 @@ export class HttpClient implements Client { const requestOpts = { payload: signedOrder, }; - await this._requestAsync('/order', RequestType.Post, requestOpts); + await this._requestAsync('/order', HttpRequestType.Post, requestOpts); } - private async _requestAsync(path: string, requestType: RequestType, requestOptions?: RequestOptions): Promise<any> { + private async _requestAsync(path: string, requestType: HttpRequestType, + requestOptions?: HttpRequestOptions): Promise<any> { const params = _.get(requestOptions, 'params'); const payload = _.get(requestOptions, 'payload'); let query = ''; diff --git a/packages/connect/src/types.ts b/packages/connect/src/types.ts index 75b6b8020..eb6df3ba9 100644 --- a/packages/connect/src/types.ts +++ b/packages/connect/src/types.ts @@ -15,6 +15,19 @@ export interface OrderbookChannel { close: () => void; } +/* + * baseTokenAddress: The address of token designated as the baseToken in the currency pair calculation of price + * quoteTokenAddress: The address of token designated as the quoteToken in the currency pair calculation of price + * snapshot: If true, a snapshot of the orderbook will be sent before the updates to the orderbook + * limit: Maximum number of bids and asks in orderbook snapshot + */ +export interface OrderbookChannelSubscriptionOpts { + baseTokenAddress: string; + quoteTokenAddress: string; + snapshot: boolean; + limit: number; +} + export interface OrderbookChannelHandler { onSnapshot: (channel: OrderbookChannel, snapshot: OrderbookResponse) => void; onUpdate: (channel: OrderbookChannel, order: SignedOrder) => void; @@ -48,17 +61,15 @@ export interface UnknownOrderbookChannelMessage { payload: undefined; } -/* - * baseTokenAddress: The address of token designated as the baseToken in the currency pair calculation of price - * quoteTokenAddress: The address of token designated as the quoteToken in the currency pair calculation of price - * snapshot: If true, a snapshot of the orderbook will be sent before the updates to the orderbook - * limit: Maximum number of bids and asks in orderbook snapshot - */ -export interface OrderbookChannelSubscriptionOpts { - baseTokenAddress: string; - quoteTokenAddress: string; - snapshot: boolean; - limit: number; +export enum WebsocketConnectionEventType { + Close = 'close', + Error = 'error', + Message = 'message', +} + +export enum WebsocketClientEventType { + Connect = 'connect', + ConnectFailed = 'connectFailed', } export interface TokenPairsRequest { @@ -118,3 +129,13 @@ export interface FeesResponse { makerFee: BigNumber; takerFee: BigNumber; } + +export interface HttpRequestOptions { + params?: object; + payload?: object; +} + +export enum HttpRequestType { + Get = 'GET', + Post = 'POST', +} diff --git a/packages/connect/src/ws_orderbook_channel.ts b/packages/connect/src/ws_orderbook_channel.ts index 78b823dbe..769a52237 100644 --- a/packages/connect/src/ws_orderbook_channel.ts +++ b/packages/connect/src/ws_orderbook_channel.ts @@ -8,20 +8,11 @@ import { OrderbookChannelHandler, OrderbookChannelMessageTypes, OrderbookChannelSubscriptionOpts, + WebsocketClientEventType, + WebsocketConnectionEventType, } from './types'; import {orderbookChannelMessageParsers} from './utils/orderbook_channel_message_parsers'; -enum ConnectionEventType { - Close = 'close', - Error = 'error', - Message = 'message', -} - -enum ClientEventType { - Connect = 'connect', - ConnectFailed = 'connectFailed', -} - /** * This class includes all the functionality related to interacting with a websocket endpoint * that implements the standard relayer API v0 @@ -63,13 +54,13 @@ export class WebSocketOrderbookChannel implements OrderbookChannel { if (!_.isUndefined(error)) { handler.onError(this, error); } else if (!_.isUndefined(connection) && connection.connected) { - connection.on(ConnectionEventType.Error, wsError => { + connection.on(WebsocketConnectionEventType.Error, wsError => { handler.onError(this, wsError); }); - connection.on(ConnectionEventType.Close, () => { + connection.on(WebsocketConnectionEventType.Close, () => { handler.onClose(this); }); - connection.on(ConnectionEventType.Message, message => { + connection.on(WebsocketConnectionEventType.Message, message => { this._handleWebSocketMessage(message, handler); }); connection.sendUTF(JSON.stringify(subscribeMessage)); @@ -88,11 +79,11 @@ export class WebSocketOrderbookChannel implements OrderbookChannel { if (!_.isUndefined(this.connectionIfExists) && this.connectionIfExists.connected) { callback(undefined, this.connectionIfExists); } else { - this.client.on(ClientEventType.Connect, connection => { + this.client.on(WebsocketClientEventType.Connect, connection => { this.connectionIfExists = connection; callback(undefined, this.connectionIfExists); }); - this.client.on(ClientEventType.ConnectFailed, error => { + this.client.on(WebsocketClientEventType.ConnectFailed, error => { callback(error, undefined); }); this.client.connect(this.apiEndpointUrl); |