Skip to content

Commit

Permalink
perf: add static fetch method
Browse files Browse the repository at this point in the history
  • Loading branch information
vansergen committed Sep 6, 2020
1 parent 6383739 commit e4dffd5
Showing 1 changed file with 53 additions and 11 deletions.
64 changes: 53 additions & 11 deletions src/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,25 @@ export const ExanteLiveURL = "https://api-live.exante.eu/";
export const DefaultAPIVersion = "2.0";

export interface IExanteOptions {
/**
* Client id
*/
client_id: string;
/**
* Application id
*/
app_id: string;
/**
* Shared secret
*/
shared_key: string;
/**
* Use demo endpoint
*/
demo?: boolean;
/**
* Base url - e.g., https://api-live.exante.eu/
*/
url?: string | URL;
}

Expand Down Expand Up @@ -45,6 +60,9 @@ export interface ILastQuoteOptions extends IVersion {
* Symbol id or symbol ids
*/
symbolIds: string | string[];
/**
* Quote level
*/
level?: "best_price" | "market_depth";
}

Expand Down Expand Up @@ -78,9 +96,21 @@ export interface ICandlesOptions extends ISymbolId {
}

export interface ITicksOptions extends ISymbolId {
/**
* Starting timestamp in ms
*/
from?: string | number;
/**
* Ending timestamp in ms
*/
to?: string | number;
/**
* Maximum amount of candles to retrieve
*/
size?: string | number;
/**
* Tick types
*/
type?: "quotes" | "trades";
}

Expand Down Expand Up @@ -497,18 +527,9 @@ export class RestClient {
url: string | URL,
{ headers = this.headers, ...options }: fetch.RequestInit = {}
): Promise<unknown> {
const response = await fetch(url.toString(), { headers, ...options });
const response = await RestClient.fetch(url, { headers, ...options });

if (!response.ok) {
throw new FetchError(response.statusText, response);
}

try {
const data = await response.json();
return data;
} catch (error) {
throw new FetchError(error.message, response);
}
return response;
}

/**
Expand Down Expand Up @@ -753,6 +774,27 @@ export class RestClient {
const jwt = `${encodedHeader}.${encodedPayload}.${encodedSignature}`;
return jwt;
}

/**
* Make a request and parse the body as JSON
*/
public static async fetch(
url: string | URL,
options: fetch.RequestInit = {}
): Promise<unknown> {
const response = await fetch(url.toString(), { ...options });

if (!response.ok) {
throw new FetchError(response.statusText, response);
}

try {
const data = await response.json();
return data;
} catch (error) {
throw new FetchError(error.message, response);
}
}
}

export default RestClient;

0 comments on commit e4dffd5

Please sign in to comment.