From 4c797405ad3c96387e6edc33a14c23d65ea2b596 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Tue, 13 Feb 2018 15:36:51 -0800 Subject: Addressed PR feedback --- packages/connect/src/http_client.ts | 38 ++++++++++++++++++++++--------- packages/connect/src/types.ts | 2 +- packages/connect/test/http_client_test.ts | 6 ++--- 3 files changed, 31 insertions(+), 15 deletions(-) (limited to 'packages') 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', }; /** @@ -33,6 +39,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 @@ -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 { @@ -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 { @@ -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 { 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); -- cgit v1.2.3