aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrandon Millman <brandon.millman@gmail.com>2018-02-14 07:36:51 +0800
committerBrandon Millman <brandon.millman@gmail.com>2018-02-14 07:50:28 +0800
commit4c797405ad3c96387e6edc33a14c23d65ea2b596 (patch)
treeff1493fcb371e5c3c20f95efc15a78cead5cc1bf
parent713c922e351b284a4a17cde4899b133bfe939c4d (diff)
downloaddexon-sol-tools-4c797405ad3c96387e6edc33a14c23d65ea2b596.tar
dexon-sol-tools-4c797405ad3c96387e6edc33a14c23d65ea2b596.tar.gz
dexon-sol-tools-4c797405ad3c96387e6edc33a14c23d65ea2b596.tar.bz2
dexon-sol-tools-4c797405ad3c96387e6edc33a14c23d65ea2b596.tar.lz
dexon-sol-tools-4c797405ad3c96387e6edc33a14c23d65ea2b596.tar.xz
dexon-sol-tools-4c797405ad3c96387e6edc33a14c23d65ea2b596.tar.zst
dexon-sol-tools-4c797405ad3c96387e6edc33a14c23d65ea2b596.zip
Addressed PR feedback
-rw-r--r--packages/connect/src/http_client.ts38
-rw-r--r--packages/connect/src/types.ts2
-rw-r--r--packages/connect/test/http_client_test.ts6
3 files changed, 31 insertions, 15 deletions
diff --git a/packages/connect/src/http_client.ts b/packages/connect/src/http_client.ts
index 2cabaae76..815d42e67 100644
--- a/packages/connect/src/http_client.ts
+++ b/packages/connect/src/http_client.ts
@@ -24,7 +24,13 @@ import { relayerResponseJsonParsers } from './utils/relayer_response_json_parser
const TRAILING_SLASHES_REGEX = /\/+$/;
const DEFAULT_PAGED_REQUEST_OPTS: PagedRequestOpts = {
page: 0,
- per_page: 100,
+ 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',
};
/**
@@ -34,6 +40,22 @@ const DEFAULT_PAGED_REQUEST_OPTS: PagedRequestOpts = {
export class HttpClient implements Client {
private _apiEndpointUrl: string;
/**
+ * Format parameters to be appended to http requests into query string form
+ */
+ private static _buildQueryStringFromHttpParams(params?: object): string {
+ // if params are undefined or empty, return an empty string
+ 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);
+ return `?${stringifiedParams}`;
+ }
+ /**
* Instantiates a new HttpClient instance
* @param url The relayer API base HTTP url you would like to interact with
* @return An instance of HttpClient
@@ -44,8 +66,7 @@ export class HttpClient implements Client {
}
/**
* Retrieve token pair info from the API
- * @param requestOpts An optional (TokenPairsRequestOpts & PagedRequestOpts) instance describing token information
- * to retrieve with page information, defaults to { page: 0, per_page: 100 }
+ * @param requestOpts Options specifying token information to retrieve and page information, defaults to { page: 0, perPage: 100 }
* @return The resulting TokenPairsItems that match the request
*/
public async getTokenPairsAsync(requestOpts?: TokenPairsRequestOpts & PagedRequestOpts): Promise<TokenPairsItem[]> {
@@ -62,8 +83,7 @@ export class HttpClient implements Client {
}
/**
* Retrieve orders from the API
- * @param requestOpts An optional (OrdersRequestOpts & PagedRequestOpts) instance describing specific orders to retrieve
- * with page information, defaults to { page: 0, per_page: 100 }
+ * @param requestOpts Options specifying orders to retrieve and page information, defaults to { page: 0, perPage: 100 }
* @return The resulting SignedOrders that match the request
*/
public async getOrdersAsync(requestOpts?: OrdersRequestOpts & PagedRequestOpts): Promise<SignedOrder[]> {
@@ -92,7 +112,7 @@ export class HttpClient implements Client {
/**
* Retrieve an orderbook from the API
* @param request An OrderbookRequest instance describing the specific orderbook to retrieve
- * @param requestOpts An optional PagedRequestOpts instance describing page information, defaults to { page: 0, per_page: 100 }
+ * @param requestOpts Options specifying page information, defaults to { page: 0, perPage: 100 }
* @return The resulting OrderbookResponse that matches the request
*/
public async getOrderbookAsync(
@@ -142,11 +162,7 @@ export class HttpClient implements Client {
): Promise<any> {
const params = _.get(requestOptions, 'params');
const payload = _.get(requestOptions, 'payload');
- let query = '';
- if (!_.isUndefined(params) && !_.isEmpty(params)) {
- const stringifiedParams = queryString.stringify(params);
- query = `?${stringifiedParams}`;
- }
+ const query = HttpClient._buildQueryStringFromHttpParams(params);
const url = `${this._apiEndpointUrl}${path}${query}`;
const headers = new Headers({
'content-type': 'application/json',
diff --git a/packages/connect/src/types.ts b/packages/connect/src/types.ts
index 38f213dca..970eff498 100644
--- a/packages/connect/src/types.ts
+++ b/packages/connect/src/types.ts
@@ -169,7 +169,7 @@ export interface FeesResponse {
export interface PagedRequestOpts {
page?: number;
- per_page?: number;
+ perPage?: number;
}
export interface HttpRequestOptions {
diff --git a/packages/connect/test/http_client_test.ts b/packages/connect/test/http_client_test.ts
index 62a159628..93b252ace 100644
--- a/packages/connect/test/http_client_test.ts
+++ b/packages/connect/test/http_client_test.ts
@@ -51,7 +51,7 @@ describe('HttpClient', () => {
const tokenPairsRequestOpts = {
tokenA: tokenAddress,
page: 3,
- per_page: 50,
+ perPage: 50,
};
const urlWithQuery = `${url}?page=3&per_page=50&tokenA=${tokenAddress}`;
fetchMock.get(urlWithQuery, tokenPairsResponseJSON);
@@ -76,7 +76,7 @@ describe('HttpClient', () => {
const ordersRequest = {
tokenAddress,
page: 3,
- per_page: 50,
+ perPage: 50,
};
const urlWithQuery = `${url}?page=3&per_page=50&tokenAddress=${tokenAddress}`;
fetchMock.get(urlWithQuery, ordersResponseJSON);
@@ -122,7 +122,7 @@ describe('HttpClient', () => {
fetchMock.get(urlWithQuery, orderbookJSON);
const pagedRequestOptions = {
page: 3,
- per_page: 50,
+ perPage: 50,
};
const orderbook = await relayerClient.getOrderbookAsync(request, pagedRequestOptions);
expect(orderbook).to.be.deep.equal(orderbookResponse);