-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.ts
77 lines (70 loc) · 2.62 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import AccountsClient from './accounts-client';
import AuthClient from './auth-client';
import InstrumentsClient from './instruments-client';
import MarketHoursClient from './market-hours-client';
import MoversClient from './movers-client';
import OptionChainsClient from './option-chains-client';
import OrdersClient from './orders-client';
import PriceHistoryClient from './price-history-client';
import QuotesClient from './quotes-client';
import SavedOrdersClient from './saved-orders-client';
import TransactionHistoryClient from './transaction-history-client';
import UserInfoClient from './user-info-client';
import WatchlistClient from './watchlist-client';
import { AxiosResponse } from 'axios';
export interface ClientOptions {
clientId?: string;
redirectUri?: string; // Made optional due to only being needed for authorization code flow
accessToken?: string;
refreshToken?: string;
}
export default class TdAmeritradeClient {
clientId?: string;
redirectUri?: string;
accessToken?: string;
refreshToken?: string;
accounts: AccountsClient;
auth: AuthClient;
instruments: InstrumentsClient;
marketHours: MarketHoursClient;
movers: MoversClient;
optionChains: OptionChainsClient;
orders: OrdersClient;
priceHistory: PriceHistoryClient;
quotes: QuotesClient;
savedOrders: SavedOrdersClient;
transactionHistory: TransactionHistoryClient;
userInfo: UserInfoClient;
watchlist: WatchlistClient;
constructor(options: ClientOptions) {
const { clientId, redirectUri, accessToken, refreshToken } = options;
this.clientId = clientId;
this.redirectUri = redirectUri ? encodeURI(redirectUri) : undefined;
this.accessToken = accessToken;
this.refreshToken = refreshToken;
this.accounts = new AccountsClient(this);
this.auth = new AuthClient(this);
this.instruments = new InstrumentsClient(this);
this.marketHours = new MarketHoursClient(this);
this.movers = new MoversClient(this);
this.optionChains = new OptionChainsClient(this);
this.orders = new OrdersClient(this);
this.priceHistory = new PriceHistoryClient(this);
this.quotes = new QuotesClient(this);
this.savedOrders = new SavedOrdersClient(this);
this.transactionHistory = new TransactionHistoryClient(this);
this.userInfo = new UserInfoClient(this);
this.watchlist = new WatchlistClient(this);
}
_makeRequest = <T>(
request: (authConfig: {}) => Promise<AxiosResponse<T>>
): Promise<AxiosResponse<T>> => {
const authConfig = this._getAuthConfig();
return request(authConfig);
};
_getAuthConfig = () => ({
headers: {
authorization: `Bearer ${this.accessToken}`,
},
});
}