aboutsummaryrefslogtreecommitdiffstats
path: root/packages/connect
diff options
context:
space:
mode:
authorfragosti <francesco.agosti93@gmail.com>2018-08-15 05:59:53 +0800
committerfragosti <francesco.agosti93@gmail.com>2018-08-15 07:06:09 +0800
commit5c40c466f6e9f690afa86f23dd5ba240691dceb8 (patch)
treec7eda62370f6dff4f4fd253dc1484705ea6f3cb9 /packages/connect
parentce88086e080e05ed71c1ef4e31f2d0ce530af67f (diff)
downloaddexon-0x-contracts-5c40c466f6e9f690afa86f23dd5ba240691dceb8.tar.gz
dexon-0x-contracts-5c40c466f6e9f690afa86f23dd5ba240691dceb8.tar.zst
dexon-0x-contracts-5c40c466f6e9f690afa86f23dd5ba240691dceb8.zip
Update json-schemas, update HTTPClient types
Diffstat (limited to 'packages/connect')
-rw-r--r--packages/connect/package.json2
-rw-r--r--packages/connect/src/http_client.ts36
-rw-r--r--packages/connect/src/index.ts2
-rw-r--r--packages/connect/src/types.ts6
-rw-r--r--packages/connect/src/utils/orderbook_channel_message_parser.ts10
-rw-r--r--packages/connect/src/utils/relayer_response_json_parsers.ts8
-rw-r--r--packages/connect/src/ws_orderbook_channel.ts8
-rw-r--r--packages/connect/test/fixtures/standard_relayer_api/token_pairs.ts4
-rw-r--r--packages/connect/test/http_client_test.ts4
-rw-r--r--packages/connect/test/orderbook_channel_message_parsers_test.ts20
10 files changed, 44 insertions, 56 deletions
diff --git a/packages/connect/package.json b/packages/connect/package.json
index 4ab5cec64..39376a9a6 100644
--- a/packages/connect/package.json
+++ b/packages/connect/package.json
@@ -52,7 +52,7 @@
"homepage": "https://github.com/0xProject/0x-monorepo/packages/connect/README.md",
"dependencies": {
"@0xproject/assert": "^0.2.14",
- "@0xproject/json-schemas": "^0.8.3",
+ "@0xproject/json-schemas": "^1.0.1-rc.4",
"@0xproject/types": "^0.8.2",
"@0xproject/typescript-typings": "^1.0.4",
"@0xproject/utils": "^1.0.5",
diff --git a/packages/connect/src/http_client.ts b/packages/connect/src/http_client.ts
index b2e15775e..20a2f9cc0 100644
--- a/packages/connect/src/http_client.ts
+++ b/packages/connect/src/http_client.ts
@@ -7,17 +7,19 @@ import * as queryString from 'query-string';
import { schemas as clientSchemas } from './schemas/schemas';
import {
+ APIOrder,
+ AssetPairsItem,
AssetPairsRequestOpts,
Client,
- OrderConfigRequest,
- OrderConfigResponse,
HttpRequestOptions,
HttpRequestType,
OrderbookRequest,
OrderbookResponse,
+ OrderConfigRequest,
+ OrderConfigResponse,
OrdersRequestOpts,
PagedRequestOpts,
- TokenPairsItem,
+ PaginatedCollection,
} from './types';
import { relayerResponseJsonParsers } from './utils/relayer_response_json_parsers';
@@ -26,12 +28,6 @@ const DEFAULT_PAGED_REQUEST_OPTS: PagedRequestOpts = {
page: 1,
perPage: 100,
};
-/**
- * This mapping defines how an option property name gets converted into an HTTP request query field
- */
-const OPTS_TO_QUERY_FIELD_MAP = {
- perPage: 'per_page',
-};
/**
* This class includes all the functionality related to interacting with a set of HTTP endpoints
@@ -47,12 +43,8 @@ export class HttpClient implements Client {
if (_.isUndefined(params) || _.isEmpty(params)) {
return '';
}
- // format params into a form the api expects
- const formattedParams = _.mapKeys(params, (_value: any, key: string) => {
- return _.get(OPTS_TO_QUERY_FIELD_MAP, key, key);
- });
// stringify the formatted object
- const stringifiedParams = queryString.stringify(formattedParams);
+ const stringifiedParams = queryString.stringify(params);
return `?${stringifiedParams}`;
}
/**
@@ -67,9 +59,9 @@ export class HttpClient implements Client {
/**
* Retrieve assetData pair info from the API
* @param requestOpts Options specifying assetData information to retrieve and page information, defaults to { page: 1, perPage: 100 }
- * @return The resulting TokenPairsItems that match the request
+ * @return The resulting AssetPairsItems that match the request
*/
- public async getAssetPairsAsync(requestOpts?: AssetPairsRequestOpts & PagedRequestOpts): Promise<TokenPairsItem[]> {
+ public async getAssetPairsAsync(requestOpts?: AssetPairsRequestOpts & PagedRequestOpts): Promise<PaginatedCollection<AssetPairsItem>> {
if (!_.isUndefined(requestOpts)) {
assert.doesConformToSchema('requestOpts', requestOpts, clientSchemas.AssetPairsRequestOptsSchema);
assert.doesConformToSchema('requestOpts', requestOpts, clientSchemas.pagedRequestOptsSchema);
@@ -78,7 +70,7 @@ export class HttpClient implements Client {
params: _.defaults({}, requestOpts, DEFAULT_PAGED_REQUEST_OPTS),
};
const responseJson = await this._requestAsync('/assetData_pairs', HttpRequestType.Get, httpRequestOpts);
- const assetDataPairs = relayerResponseJsonParsers.parseTokenPairsJson(responseJson);
+ const assetDataPairs = relayerResponseJsonParsers.parseAssetDataPairsJson(responseJson);
return assetDataPairs;
}
/**
@@ -86,7 +78,7 @@ export class HttpClient implements Client {
* @param requestOpts Options specifying orders to retrieve and page information, defaults to { page: 1, perPage: 100 }
* @return The resulting SignedOrders that match the request
*/
- public async getOrdersAsync(requestOpts?: OrdersRequestOpts & PagedRequestOpts): Promise<SignedOrder[]> {
+ public async getOrdersAsync(requestOpts?: OrdersRequestOpts & PagedRequestOpts): Promise<PaginatedCollection<APIOrder>> {
if (!_.isUndefined(requestOpts)) {
assert.doesConformToSchema('requestOpts', requestOpts, clientSchemas.ordersRequestOptsSchema);
assert.doesConformToSchema('requestOpts', requestOpts, clientSchemas.pagedRequestOptsSchema);
@@ -103,7 +95,7 @@ export class HttpClient implements Client {
* @param orderHash An orderHash generated from the desired order
* @return The SignedOrder that matches the supplied orderHash
*/
- public async getOrderAsync(orderHash: string): Promise<SignedOrder> {
+ public async getOrderAsync(orderHash: string): Promise<APIOrder> {
assert.doesConformToSchema('orderHash', orderHash, schemas.orderHashSchema);
const responseJson = await this._requestAsync(`/order/${orderHash}`, HttpRequestType.Get);
const order = relayerResponseJsonParsers.parseOrderJson(responseJson);
@@ -144,6 +136,12 @@ export class HttpClient implements Client {
const fees = relayerResponseJsonParsers.parseOrderConfigResponseJson(responseJson);
return fees;
}
+
+ public async getFeeRecipientsAsync(): Promise<PaginatedCollection<string>> {
+ // TODO
+ return;
+ }
+
/**
* Submit a signed order to the API
* @param signedOrder A SignedOrder instance to submit
diff --git a/packages/connect/src/index.ts b/packages/connect/src/index.ts
index e57967714..e0c4293d9 100644
--- a/packages/connect/src/index.ts
+++ b/packages/connect/src/index.ts
@@ -11,7 +11,7 @@ export {
OrderbookResponse,
OrdersRequestOpts,
PagedRequestOpts,
- TokenPairsItem,
+ AssetPairsItem,
AssetPairsRequestOpts,
Asset,
} from './types';
diff --git a/packages/connect/src/types.ts b/packages/connect/src/types.ts
index 0f2242329..fb51dca98 100644
--- a/packages/connect/src/types.ts
+++ b/packages/connect/src/types.ts
@@ -2,12 +2,12 @@ import { SignedOrder } from '@0xproject/types';
import { BigNumber } from '@0xproject/utils';
export interface Client {
- getAssetPairsAsync: (requestOpts?: AssetPairsRequestOpts & PagedRequestOpts) => Promise<PaginatedCollection<TokenPairsItem>>;
+ getAssetPairsAsync: (requestOpts?: AssetPairsRequestOpts & PagedRequestOpts) => Promise<PaginatedCollection<AssetPairsItem>>;
getOrdersAsync: (requestOpts?: OrdersRequestOpts & PagedRequestOpts) => Promise<PaginatedCollection<APIOrder>>;
getOrderAsync: (orderHash: string) => Promise<APIOrder>;
getOrderbookAsync: (request: OrderbookRequest, requestOpts?: PagedRequestOpts) => Promise<OrderbookResponse>;
getOrderConfigAsync: (request: OrderConfigRequest) => Promise<OrderConfigResponse>;
- getFeeRecipients: () => Promise<PaginatedCollection<string>>;
+ getFeeRecipientsAsync: () => Promise<PaginatedCollection<string>>;
submitOrderAsync: (signedOrder: SignedOrder) => Promise<void>;
}
@@ -81,7 +81,7 @@ export interface AssetPairsRequestOpts {
assetDataB?: string;
}
-export interface TokenPairsItem {
+export interface AssetPairsItem {
assetDataA: Asset;
assetDataB: Asset;
}
diff --git a/packages/connect/src/utils/orderbook_channel_message_parser.ts b/packages/connect/src/utils/orderbook_channel_message_parser.ts
index ca739e587..986209c54 100644
--- a/packages/connect/src/utils/orderbook_channel_message_parser.ts
+++ b/packages/connect/src/utils/orderbook_channel_message_parser.ts
@@ -6,7 +6,7 @@ import { OrdersChannelMessage, OrdersChannelMessageTypes } from '../types';
import { relayerResponseJsonParsers } from './relayer_response_json_parsers';
-export const orderbookChannelMessageParser = {
+export const ordersChannelMessageParser = {
parse(utf8Data: string): OrdersChannelMessage {
// parse the message
const messageObj = JSON.parse(utf8Data);
@@ -19,14 +19,8 @@ export const orderbookChannelMessageParser = {
assert.assert(!_.isUndefined(requestId), `Message is missing a requestId parameter: ${utf8Data}`);
assert.isNumber('requestId', requestId);
switch (type) {
- case OrdersChannelMessageTypes.Snapshot: {
- assert.doesConformToSchema('message', messageObj, schemas.relayerApiOrderbookChannelSnapshotSchema);
- const orderbookJson = messageObj.payload;
- const orderbook = relayerResponseJsonParsers.parseOrderbookResponseJson(orderbookJson);
- return _.assign(messageObj, { payload: orderbook });
- }
case OrdersChannelMessageTypes.Update: {
- assert.doesConformToSchema('message', messageObj, schemas.relayerApiOrderbookChannelUpdateSchema);
+ assert.doesConformToSchema('message', messageObj, schemas.relayerApiOrdersChannelUpdateSchema);
const orderJson = messageObj.payload;
const order = relayerResponseJsonParsers.parseOrderJson(orderJson);
return _.assign(messageObj, { payload: order });
diff --git a/packages/connect/src/utils/relayer_response_json_parsers.ts b/packages/connect/src/utils/relayer_response_json_parsers.ts
index 55f787820..6463aefb0 100644
--- a/packages/connect/src/utils/relayer_response_json_parsers.ts
+++ b/packages/connect/src/utils/relayer_response_json_parsers.ts
@@ -2,13 +2,13 @@ import { assert } from '@0xproject/assert';
import { schemas } from '@0xproject/json-schemas';
import { SignedOrder } from '@0xproject/types';
-import { OrderConfigResponse, OrderbookResponse, TokenPairsItem } from '../types';
+import { AssetPairsItem, OrderbookResponse, OrderConfigResponse } from '../types';
import { typeConverters } from './type_converters';
export const relayerResponseJsonParsers = {
- parseTokenPairsJson(json: any): TokenPairsItem[] {
- assert.doesConformToSchema('assetDataPairs', json, schemas.relayerApiTokenPairsResponseSchema);
+ parseAssetDataPairsJson(json: any): AssetPairsItem[] {
+ assert.doesConformToSchema('assetDataPairs', json, schemas.relayerApiAssetDataPairsResponseSchema);
return json.map((assetDataPair: any) => {
return typeConverters.convertStringsFieldsToBigNumbers(assetDataPair, [
'assetDataA.minAmount',
@@ -27,7 +27,7 @@ export const relayerResponseJsonParsers = {
return typeConverters.convertOrderStringFieldsToBigNumber(json);
},
parseOrderbookResponseJson(json: any): OrderbookResponse {
- assert.doesConformToSchema('orderBook', json, schemas.relayerApiOrderBookResponseSchema);
+ assert.doesConformToSchema('orderBook', json, schemas.relayerApiOrdersResponseSchema);
return typeConverters.convertOrderbookStringFieldsToBigNumber(json);
},
parseOrderConfigResponseJson(json: any): OrderConfigResponse {
diff --git a/packages/connect/src/ws_orderbook_channel.ts b/packages/connect/src/ws_orderbook_channel.ts
index d09fd375d..3d9230792 100644
--- a/packages/connect/src/ws_orderbook_channel.ts
+++ b/packages/connect/src/ws_orderbook_channel.ts
@@ -8,7 +8,7 @@ import {
OrdersChannelMessageTypes,
} from './types';
import { assert } from './utils/assert';
-import { orderbookChannelMessageParser } from './utils/orderbook_channel_message_parser';
+import { ordersChannelMessageParser } from './utils/orderbook_channel_message_parser';
/**
* This class includes all the functionality related to interacting with a websocket endpoint
@@ -72,7 +72,7 @@ export class WebSocketOrderbookChannel implements OrderbookChannel {
}
try {
const data = message.data;
- const parserResult = orderbookChannelMessageParser.parse(data);
+ const parserResult = ordersChannelMessageParser.parse(data);
const subscriptionOpts = this._subscriptionOptsList[parserResult.requestId];
if (_.isUndefined(subscriptionOpts)) {
this._handler.onError(
@@ -82,10 +82,6 @@ export class WebSocketOrderbookChannel implements OrderbookChannel {
return;
}
switch (parserResult.type) {
- case OrdersChannelMessageTypes.Snapshot: {
- this._handler.onSnapshot(this, subscriptionOpts, parserResult.payload);
- break;
- }
case OrdersChannelMessageTypes.Update: {
this._handler.onUpdate(this, subscriptionOpts, parserResult.payload);
break;
diff --git a/packages/connect/test/fixtures/standard_relayer_api/token_pairs.ts b/packages/connect/test/fixtures/standard_relayer_api/token_pairs.ts
index 1225ac4fc..03cce8444 100644
--- a/packages/connect/test/fixtures/standard_relayer_api/token_pairs.ts
+++ b/packages/connect/test/fixtures/standard_relayer_api/token_pairs.ts
@@ -1,8 +1,8 @@
import { BigNumber } from '@0xproject/utils';
-import { TokenPairsItem } from '../../../src/types';
+import { AssetPairsItem } from '../../../src/types';
-export const assetDataPairsResponse: TokenPairsItem[] = [
+export const assetDataPairsResponse: AssetPairsItem[] = [
{
assetDataA: {
address: '0x323b5d4c32345ced77393b3530b1eed0f346429d',
diff --git a/packages/connect/test/http_client_test.ts b/packages/connect/test/http_client_test.ts
index ea5717327..866cf7b2f 100644
--- a/packages/connect/test/http_client_test.ts
+++ b/packages/connect/test/http_client_test.ts
@@ -7,6 +7,8 @@ import 'mocha';
import { HttpClient } from '../src/index';
+import { assetDataPairsResponse } from './fixtures/standard_relayer_api/assetData_pairs';
+import * as assetDataPairsResponseJSON from './fixtures/standard_relayer_api/assetData_pairs.json';
import { feesResponse } from './fixtures/standard_relayer_api/fees';
import * as feesResponseJSON from './fixtures/standard_relayer_api/fees.json';
import { orderResponse } from './fixtures/standard_relayer_api/order/0xabc67323774bdbd24d94f977fa9ac94a50f016026fd13f42990861238897721f';
@@ -15,8 +17,6 @@ import { orderbookResponse } from './fixtures/standard_relayer_api/orderbook';
import * as orderbookJSON from './fixtures/standard_relayer_api/orderbook.json';
import { ordersResponse } from './fixtures/standard_relayer_api/orders';
import * as ordersResponseJSON from './fixtures/standard_relayer_api/orders.json';
-import { assetDataPairsResponse } from './fixtures/standard_relayer_api/assetData_pairs';
-import * as assetDataPairsResponseJSON from './fixtures/standard_relayer_api/assetData_pairs.json';
chai.config.includeStack = true;
chai.use(dirtyChai);
diff --git a/packages/connect/test/orderbook_channel_message_parsers_test.ts b/packages/connect/test/orderbook_channel_message_parsers_test.ts
index ce881e627..9960ad1df 100644
--- a/packages/connect/test/orderbook_channel_message_parsers_test.ts
+++ b/packages/connect/test/orderbook_channel_message_parsers_test.ts
@@ -2,7 +2,7 @@ import * as chai from 'chai';
import * as dirtyChai from 'dirty-chai';
import 'mocha';
-import { orderbookChannelMessageParser } from '../src/utils/orderbook_channel_message_parser';
+import { ordersChannelMessageParser } from '../src/utils/orderbook_channel_message_parser';
import { orderResponse } from './fixtures/standard_relayer_api/order/0xabc67323774bdbd24d94f977fa9ac94a50f016026fd13f42990861238897721f';
import { orderbookResponse } from './fixtures/standard_relayer_api/orderbook';
@@ -20,20 +20,20 @@ chai.config.includeStack = true;
chai.use(dirtyChai);
const expect = chai.expect;
-describe('orderbookChannelMessageParser', () => {
+describe('ordersChannelMessageParser', () => {
describe('#parser', () => {
it('parses snapshot messages', () => {
- const snapshotMessage = orderbookChannelMessageParser.parse(snapshotOrdersChannelMessage);
+ const snapshotMessage = ordersChannelMessageParser.parse(snapshotOrdersChannelMessage);
expect(snapshotMessage.type).to.be.equal('snapshot');
expect(snapshotMessage.payload).to.be.deep.equal(orderbookResponse);
});
it('parses update messages', () => {
- const updateMessage = orderbookChannelMessageParser.parse(updateOrdersChannelMessage);
+ const updateMessage = ordersChannelMessageParser.parse(updateOrdersChannelMessage);
expect(updateMessage.type).to.be.equal('update');
expect(updateMessage.payload).to.be.deep.equal(orderResponse);
});
it('returns unknown message for messages with unsupported types', () => {
- const unknownMessage = orderbookChannelMessageParser.parse(unknownOrdersChannelMessage);
+ const unknownMessage = ordersChannelMessageParser.parse(unknownOrdersChannelMessage);
expect(unknownMessage.type).to.be.equal('unknown');
expect(unknownMessage.payload).to.be.undefined();
});
@@ -43,7 +43,7 @@ describe('orderbookChannelMessageParser', () => {
"requestId": 1,
"payload": {}
}`;
- const badCall = () => orderbookChannelMessageParser.parse(typelessMessage);
+ const badCall = () => ordersChannelMessageParser.parse(typelessMessage);
expect(badCall).throws(`Message is missing a type parameter: ${typelessMessage}`);
});
it('throws when type is not a string', () => {
@@ -53,23 +53,23 @@ describe('orderbookChannelMessageParser', () => {
"requestId": 1,
"payload": {}
}`;
- const badCall = () => orderbookChannelMessageParser.parse(messageWithBadType);
+ const badCall = () => ordersChannelMessageParser.parse(messageWithBadType);
expect(badCall).throws('Expected type to be of type string, encountered: 1');
});
it('throws when snapshot message has malformed payload', () => {
- const badCall = () => orderbookChannelMessageParser.parse(malformedSnapshotOrdersChannelMessage);
+ const badCall = () => ordersChannelMessageParser.parse(malformedSnapshotOrdersChannelMessage);
// tslint:disable-next-line:max-line-length
const errMsg =
'Validation errors: instance.payload requires property "bids", instance.payload requires property "asks"';
expect(badCall).throws(errMsg);
});
it('throws when update message has malformed payload', () => {
- const badCall = () => orderbookChannelMessageParser.parse(malformedUpdateOrdersChannelMessage);
+ const badCall = () => ordersChannelMessageParser.parse(malformedUpdateOrdersChannelMessage);
expect(badCall).throws(/^Expected message to conform to schema/);
});
it('throws when input message is not valid JSON', () => {
const nonJsonString = 'h93b{sdfs9fsd f';
- const badCall = () => orderbookChannelMessageParser.parse(nonJsonString);
+ const badCall = () => ordersChannelMessageParser.parse(nonJsonString);
expect(badCall).throws('Unexpected assetData h in JSON at position 0');
});
});