Skip to content

Commit 931eeeb

Browse files
committed
refactor
1 parent 859c47c commit 931eeeb

File tree

6 files changed

+60
-51
lines changed

6 files changed

+60
-51
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ tmp
66
out-tsc
77
# Only exists if Bazel was run
88
bazel-out
9+
.rpt2_cache
910

1011
# dependencies
1112
node_modules/
@@ -47,4 +48,4 @@ Thumbs.db
4748

4849

4950
#tmp
50-
dev.ts
51+
dev.ts

lib/market/tradier-market.client.ts

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
1+
import { AxiosRequestConfig, AxiosResponse, AxiosStatic } from 'axios';
22

33
import { TradierHistoryInterval, TradierSessionFilter, TradierTimeSalesInterval } from './tradier-market.models';
4-
import { TradierConfig } from '../tradier-config';
4+
import { TradierUtil } from '../tradier-util';
55

66
interface TradierMarketEndpoints {
77
quotes: string;
@@ -34,7 +34,8 @@ const endponts: TradierMarketEndpoints = {
3434
export class TradierMarketClient {
3535

3636
public constructor(
37-
private readonly tradierConfig: TradierConfig,
37+
private readonly tradierUtil: TradierUtil,
38+
private readonly axios: AxiosStatic = axios,
3839
) {}
3940

4041
/**
@@ -43,12 +44,12 @@ export class TradierMarketClient {
4344
* @param symbols Comma-delimited list of symbols (equity or option)
4445
*/
4546
public async getQuotes(symbols: string[]) {
46-
const url: string = this.tradierConfig.buildUrl(endponts.quotes);
47-
const config: AxiosRequestConfig = this.tradierConfig.buildConfigWithParams({
47+
const url: string = this.tradierUtil.buildUrl(endponts.quotes);
48+
const config: AxiosRequestConfig = this.tradierUtil.buildConfigWithParams({
4849
symbols: symbols.join(',')
4950
});
5051

51-
const response: AxiosResponse = await axios.get(url, config);
52+
const response: AxiosResponse = await this.axios.get(url, config);
5253
return response.data;
5354
}
5455

@@ -58,13 +59,13 @@ export class TradierMarketClient {
5859
* @param expiration Expiration for the chain (format 2019-05-17)
5960
*/
6061
public async getOptionChains(symbol: string, expiration: string) {
61-
const url: string = this.tradierConfig.buildUrl(endponts.option_chains);
62-
const config: AxiosRequestConfig = this.tradierConfig.buildConfigWithParams({
62+
const url: string = this.tradierUtil.buildUrl(endponts.option_chains);
63+
const config: AxiosRequestConfig = this.tradierUtil.buildConfigWithParams({
6364
symbol,
6465
expiration
6566
});
6667

67-
const response: AxiosResponse = await axios.get(url, config);
68+
const response: AxiosResponse = await this.axios.get(url, config);
6869
return response.data;
6970
}
7071

@@ -74,13 +75,13 @@ export class TradierMarketClient {
7475
* @param expiration Expiration for the chain (format 2019-05-17)
7576
*/
7677
public async getOptionStrikes(symbol: string, expiration: string) {
77-
const url: string = this.tradierConfig.buildUrl(endponts.option_strikes);
78-
const config: AxiosRequestConfig = this.tradierConfig.buildConfigWithParams({
78+
const url: string = this.tradierUtil.buildUrl(endponts.option_strikes);
79+
const config: AxiosRequestConfig = this.tradierUtil.buildConfigWithParams({
7980
symbol,
8081
expiration
8182
});
8283

83-
const response: AxiosResponse = await axios.get(url, config);
84+
const response: AxiosResponse = await this.axios.get(url, config);
8485
return response.data;
8586
}
8687

@@ -95,14 +96,14 @@ export class TradierMarketClient {
9596
* @param strikes Add strike prices to each expiration
9697
*/
9798
public async getOptionExpirations(symbol: string, includeAllRoots: boolean = false, strikes: boolean = false) {
98-
const url: string = this.tradierConfig.buildUrl(endponts.option_expirations);
99-
const config: AxiosRequestConfig = this.tradierConfig.buildConfigWithParams({
99+
const url: string = this.tradierUtil.buildUrl(endponts.option_expirations);
100+
const config: AxiosRequestConfig = this.tradierUtil.buildConfigWithParams({
100101
symbol,
101102
includeAllRoots,
102103
strikes
103104
});
104105

105-
const response: AxiosResponse = await axios.get(url, config);
106+
const response: AxiosResponse = await this.axios.get(url, config);
106107
return response.data;
107108
}
108109

@@ -115,15 +116,15 @@ export class TradierMarketClient {
115116
* @param end End date represented as YYYY-MM-DD
116117
*/
117118
public async getHistoricalPricing(symbol: string, interval?: TradierHistoryInterval, start?: string, end?: string) {
118-
const url: string = this.tradierConfig.buildUrl(endponts.historical_quotes);
119-
const config: AxiosRequestConfig = this.tradierConfig.buildConfigWithParams({
119+
const url: string = this.tradierUtil.buildUrl(endponts.historical_quotes);
120+
const config: AxiosRequestConfig = this.tradierUtil.buildConfigWithParams({
120121
symbol,
121122
interval,
122123
start,
123124
end
124125
});
125126

126-
const response: AxiosResponse = await axios.get(url, config);
127+
const response: AxiosResponse = await this.axios.get(url, config);
127128
return response.data;
128129
}
129130

@@ -141,16 +142,16 @@ export class TradierMarketClient {
141142
public async getTimeAndSales(
142143
symbol: string, interval?: TradierTimeSalesInterval, start?: string, end?: string, session_filter?: TradierSessionFilter,
143144
) {
144-
const url: string = this.tradierConfig.buildUrl(endponts.time_and_sales);
145-
const config: AxiosRequestConfig = this.tradierConfig.buildConfigWithParams({
145+
const url: string = this.tradierUtil.buildUrl(endponts.time_and_sales);
146+
const config: AxiosRequestConfig = this.tradierUtil.buildConfigWithParams({
146147
symbol,
147148
interval,
148149
start,
149150
end,
150151
session_filter
151152
});
152153

153-
const response: AxiosResponse = await axios.get(url, config);
154+
const response: AxiosResponse = await this.axios.get(url, config);
154155
return response.data;
155156
}
156157

@@ -159,10 +160,10 @@ export class TradierMarketClient {
159160
* The list is quite comprehensive and can result in a long download response time.
160161
*/
161162
public async getETBSecurities() {
162-
const url: string = this.tradierConfig.buildUrl(endponts.etb_securities);
163-
const config: AxiosRequestConfig = this.tradierConfig.buildBaseConfig();
163+
const url: string = this.tradierUtil.buildUrl(endponts.etb_securities);
164+
const config: AxiosRequestConfig = this.tradierUtil.buildBaseConfig();
164165

165-
const response: AxiosResponse = await axios.get(url, config);
166+
const response: AxiosResponse = await this.axios.get(url, config);
166167
return response.data;
167168
}
168169

@@ -171,10 +172,10 @@ export class TradierMarketClient {
171172
* If programming logic on whether the market is open/closed – this API call should be used to determine the current state.
172173
*/
173174
public async getClock() {
174-
const url: string = this.tradierConfig.buildUrl(endponts.clock);
175-
const config: AxiosRequestConfig = this.tradierConfig.buildBaseConfig();
175+
const url: string = this.tradierUtil.buildUrl(endponts.clock);
176+
const config: AxiosRequestConfig = this.tradierUtil.buildBaseConfig();
176177

177-
const response: AxiosResponse = await axios.get(url, config);
178+
const response: AxiosResponse = await this.axios.get(url, config);
178179
return response.data;
179180
}
180181

@@ -185,13 +186,13 @@ export class TradierMarketClient {
185186
* @param indexes Whether to include indexes in the results
186187
*/
187188
public async searchForCompanies(q: string, indexes?: boolean) {
188-
const url: string = this.tradierConfig.buildUrl(endponts.search_companies);
189-
const config: AxiosRequestConfig = this.tradierConfig.buildConfigWithParams({
189+
const url: string = this.tradierUtil.buildUrl(endponts.search_companies);
190+
const config: AxiosRequestConfig = this.tradierUtil.buildConfigWithParams({
190191
q,
191192
indexes,
192193
});
193194

194-
const response: AxiosResponse = await axios.get(url, config);
195+
const response: AxiosResponse = await this.axios.get(url, config);
195196
return response.data;
196197
}
197198

@@ -204,14 +205,14 @@ export class TradierMarketClient {
204205
* @param types Which security types to include in lookup
205206
*/
206207
public async searchForSymbols(q: string, exchanges: string[] = [], types?: string) {
207-
const url: string = this.tradierConfig.buildUrl(endponts.lookup_symbol);
208-
const config: AxiosRequestConfig = this.tradierConfig.buildConfigWithParams({
208+
const url: string = this.tradierUtil.buildUrl(endponts.lookup_symbol);
209+
const config: AxiosRequestConfig = this.tradierUtil.buildConfigWithParams({
209210
q,
210211
exchanges: exchanges.join(','),
211212
types
212213
});
213214

214-
const response: AxiosResponse = await axios.get(url, config);
215+
const response: AxiosResponse = await this.axios.get(url, config);
215216
return response.data;
216217
}
217218

lib/tradier-config.ts renamed to lib/tradier-util.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AxiosRequestConfig } from 'axios';
22

3-
import { TradierAccountType } from './tradier.models';
3+
import { TradierAccountType, TradierClientOptions } from './tradier.models';
44

55
interface Headers {
66
Authorization: string;
@@ -13,29 +13,28 @@ enum TradierBaseUrl {
1313
STREAM = 'https://stream.tradier.com',
1414
}
1515

16-
export class TradierConfig {
16+
export class TradierUtil {
1717

1818
private readonly headers: Headers;
1919

2020
public constructor(
21-
private readonly accessToken: string,
22-
private readonly accountType: TradierAccountType,
21+
private readonly options: TradierClientOptions,
2322
) {
2423
this.headers = {
2524
Accept: 'application/json',
26-
Authorization: `Bearer ${this.accessToken}`
25+
Authorization: `Bearer ${this.options.accessToken}`
2726
};
2827
}
2928

3029
public buildUrl(url: string, stream: boolean = false): string {
3130
if (stream) {
32-
if (this.accountType === TradierAccountType.SANDBOX) {
31+
if (this.options.accountType === TradierAccountType.SANDBOX) {
3332
throw new Error('Stream cannot be used with Sandbox Account');
3433
}
3534
return `${TradierBaseUrl.SANDBOX}${url}`;
3635
}
3736

38-
if (this.accountType === TradierAccountType.API) {
37+
if (this.options.accountType === TradierAccountType.API) {
3938
return `${TradierBaseUrl.API}${url}`;
4039
}
4140

lib/tradier.client.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
import { TradierAccountType } from './tradier.models';
1+
import axios from 'axios';
2+
3+
import { TradierClientOptions } from './tradier.models';
24
import { TradierMarketClient } from './market';
3-
import { TradierConfig } from './tradier-config';
5+
import { TradierUtil } from './tradier-util';
46

57
export class TradierClient {
68

7-
private readonly tradierConfig: TradierConfig;
9+
private readonly tradierUtil: TradierUtil;
810

911
public readonly market: TradierMarketClient;
1012

1113
public constructor(
12-
private readonly accessToken: string,
13-
private readonly accountType: TradierAccountType
14+
private readonly options: TradierClientOptions,
1415
) {
15-
this.tradierConfig = new TradierConfig(this.accessToken, this.accountType);
16+
this.tradierUtil = new TradierUtil(this.options);
1617

17-
this.market = new TradierMarketClient(this.tradierConfig);
18+
this.market = new TradierMarketClient(this.tradierUtil, axios);
1819
}
1920
}

lib/tradier.models.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,8 @@ export enum TradierAccountType {
7878
SANDBOX = 'sandbox',
7979
API = 'api',
8080
}
81+
82+
export interface TradierClientOptions {
83+
accessToken: string;
84+
accountType: TradierAccountType;
85+
}

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "tradier-client",
3-
"version": "0.0.1",
2+
"name": "@reycodev/tradier-client",
3+
"version": "0.1.0",
44
"description": "Node Tradier Brokerage API Client",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -10,7 +10,9 @@
1010
"scripts": {
1111
"test": "echo \"Error: no test specified\" && exit 1",
1212
"dev": "tsc-watch -p tsconfig.json --onSuccess \"node dist/dev.js\"",
13-
"build": "rm -rf dist && tsc -p tsconfig.json"
13+
"build": "rm -rf dist && tsc -p tsconfig.json",
14+
"prepublish:npm": "npm run build",
15+
"publish:npm": "npm publish --access public"
1416
},
1517
"repository": {
1618
"type": "git",

0 commit comments

Comments
 (0)