@master-chief/alpaca
Advanced tools
Comparing version 3.4.1 to 3.5.0
{ | ||
"name": "@master-chief/alpaca", | ||
"version": "3.4.1", | ||
"version": "3.5.0", | ||
"description": "a TypeScript Node.js library for the https://alpaca.markets REST API and WebSocket streams", | ||
@@ -9,3 +9,4 @@ "author": "master-chief", | ||
"AqilCont", | ||
"KalebMills" | ||
"KalebMills", | ||
"anonrose" | ||
], | ||
@@ -33,3 +34,3 @@ "license": "ISC", | ||
"scripts": { | ||
"build": "npm run clean && npm run build:mjs && npm run build:cjs && ./remap.sh", | ||
"build": "npm run clean && npm i && npm run build:mjs && npm run build:cjs && ./remap.sh", | ||
"build:mjs": "tsc --outDir dist/mjs --moduleResolution node --allowSyntheticDefaultImports -d --declarationDir types --module es2020 --target es2020 src/*.ts", | ||
@@ -36,0 +37,0 @@ "build:cjs": "tsc --outDir dist/cjs --moduleResolution node --esModuleInterop src/*.ts", |
@@ -29,2 +29,3 @@ # alpaca | ||
- [x] Hybrid CommonJS and ESM support. | ||
- [x] OAuth integration support. | ||
@@ -57,4 +58,3 @@ ## Install | ||
If you wish to use env vars, populate these fields with `process.env` on your | ||
own. Paper account key detection is automatic. | ||
If you wish to use env vars, populate these fields with `process.env` on your own. Paper account key detection is automatic. Using OAuth? Simply pass an `access_token` in the credentials object. | ||
@@ -66,2 +66,3 @@ ```typescript | ||
secret: 'xxxxxxxxxxxx', | ||
// access_token: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' | ||
}, | ||
@@ -68,0 +69,0 @@ rate_limit: true, |
@@ -22,3 +22,2 @@ import qs from 'qs' | ||
LastTrade, | ||
Credentials, | ||
RawOrder, | ||
@@ -28,2 +27,4 @@ RawPosition, | ||
Activity, | ||
DefaultCredentials, | ||
OAuthCredentials, | ||
} from './entities.js' | ||
@@ -61,7 +62,16 @@ | ||
constructor( | ||
protected options: { | ||
credentials: Credentials | ||
public params: { | ||
credentials?: DefaultCredentials | OAuthCredentials | ||
rate_limit?: boolean | ||
}, | ||
) {} | ||
) { | ||
if ( | ||
'access_token' in params.credentials && | ||
('key' in params.credentials || 'secret' in params.credentials) | ||
) { | ||
throw new Error( | ||
"can't create client with both default and oauth credentials", | ||
) | ||
} | ||
} | ||
@@ -323,10 +333,21 @@ async isAuthenticated(): Promise<boolean> { | ||
): Promise<T> { | ||
// modify the base url if paper key | ||
if ( | ||
this.options.credentials.key.startsWith('PK') && | ||
let headers = {} | ||
if ('access_token' in this.params.credentials) { | ||
headers[ | ||
'Authorization' | ||
] = `Bearer ${this.params.credentials.access_token}` | ||
url == urls.rest.account | ||
) { | ||
url = urls.rest.account.replace('api.', 'paper-api.') | ||
} else { | ||
headers['APCA-API-KEY-ID'] = this.params.credentials.key | ||
headers['APCA-API-SECRET-KEY'] = this.params.credentials.secret | ||
if ( | ||
this.params.credentials.key.startsWith('PK') && | ||
url == urls.rest.account | ||
) { | ||
url = urls.rest.account.replace('api.', 'paper-api.') | ||
} | ||
} | ||
// modify the base url if paper key | ||
// convert any dates to ISO 8601 for Alpaca | ||
@@ -342,3 +363,3 @@ if (data) { | ||
return new Promise<T>(async (resolve, reject) => { | ||
if (this.options.rate_limit) { | ||
if (this.params.rate_limit) { | ||
await new Promise((resolve) => this.limiter.removeTokens(1, resolve)) | ||
@@ -349,6 +370,3 @@ } | ||
method: method, | ||
headers: { | ||
'APCA-API-KEY-ID': this.options.credentials.key, | ||
'APCA-API-SECRET-KEY': this.options.credentials.secret, | ||
}, | ||
headers, | ||
body: JSON.stringify(data), | ||
@@ -355,0 +373,0 @@ }) |
@@ -5,3 +5,3 @@ /** | ||
*/ | ||
export interface Credentials { | ||
export interface DefaultCredentials { | ||
key: string | ||
@@ -12,2 +12,10 @@ secret: string | ||
/** | ||
* Client ID for Oauth requests on behalf of users. | ||
* Can be passed to the AlpacaClient. | ||
*/ | ||
export interface OAuthCredentials { | ||
access_token: String | ||
} | ||
/** | ||
* The account information with unparsed types, exactly as Alpaca provides it. | ||
@@ -14,0 +22,0 @@ * We encourage you to use the Account interface, which has many of these fields parsed. |
@@ -20,2 +20,4 @@ import { AlpacaClient } from './client.js' | ||
Clock, | ||
DefaultCredentials, | ||
OAuthCredentials, | ||
AccountConfigurations, | ||
@@ -22,0 +24,0 @@ NonTradeActivity, |
@@ -8,3 +8,3 @@ import WebSocket from 'ws' | ||
AggregateMinute, | ||
Credentials, | ||
DefaultCredentials, | ||
Quote, | ||
@@ -47,3 +47,3 @@ Trade, | ||
protected params: { | ||
credentials: Credentials | ||
credentials: DefaultCredentials | ||
stream: 'account' | 'market_data' | ||
@@ -50,0 +50,0 @@ }, |
@@ -1,6 +0,6 @@ | ||
import { Account, Order, Position, Asset, Watchlist, Calendar, Clock, AccountConfigurations, PortfolioHistory, Bar, LastQuote, LastTrade, Credentials, Activity } from './entities.js'; | ||
import { Account, Order, Position, Asset, Watchlist, Calendar, Clock, AccountConfigurations, PortfolioHistory, Bar, LastQuote, LastTrade, Activity, DefaultCredentials, OAuthCredentials } from './entities.js'; | ||
import { GetOrder, GetOrders, PlaceOrder, ReplaceOrder, CancelOrder, GetPosition, ClosePosition, GetAsset, GetAssets, GetWatchList, CreateWatchList, UpdateWatchList, AddToWatchList, RemoveFromWatchList, DeleteWatchList, GetCalendar, UpdateAccountConfigurations, GetAccountActivities, GetPortfolioHistory, GetBars, GetLastTrade, GetLastQuote } from './params.js'; | ||
export declare class AlpacaClient { | ||
protected options: { | ||
credentials: Credentials; | ||
params: { | ||
credentials?: DefaultCredentials | OAuthCredentials; | ||
rate_limit?: boolean; | ||
@@ -10,4 +10,4 @@ }; | ||
private parser; | ||
constructor(options: { | ||
credentials: Credentials; | ||
constructor(params: { | ||
credentials?: DefaultCredentials | OAuthCredentials; | ||
rate_limit?: boolean; | ||
@@ -14,0 +14,0 @@ }); |
@@ -5,3 +5,3 @@ /** | ||
*/ | ||
export interface Credentials { | ||
export interface DefaultCredentials { | ||
key: string; | ||
@@ -11,2 +11,9 @@ secret: string; | ||
/** | ||
* Client ID for Oauth requests on behalf of users. | ||
* Can be passed to the AlpacaClient. | ||
*/ | ||
export interface OAuthCredentials { | ||
access_token: String; | ||
} | ||
/** | ||
* The account information with unparsed types, exactly as Alpaca provides it. | ||
@@ -13,0 +20,0 @@ * We encourage you to use the Account interface, which has many of these fields parsed. |
@@ -10,3 +10,3 @@ import { AlpacaClient } from './client.js'; | ||
export default _default; | ||
export { Account, Order, Position, Asset, Watchlist, Calendar, Clock, AccountConfigurations, NonTradeActivity, TradeActivity, PortfolioHistory, Bar, LastQuote, LastTrade, } from './entities.js'; | ||
export { Account, Order, Position, Asset, Watchlist, Calendar, Clock, DefaultCredentials, OAuthCredentials, AccountConfigurations, NonTradeActivity, TradeActivity, PortfolioHistory, Bar, LastQuote, LastTrade, } from './entities.js'; | ||
export { GetOrder, GetOrders, PlaceOrder, ReplaceOrder, CancelOrder, GetPosition, ClosePosition, GetAsset, GetAssets, GetWatchList, CreateWatchList, UpdateWatchList, AddToWatchList, RemoveFromWatchList, DeleteWatchList, GetCalendar, UpdateAccountConfigurations, GetAccountActivities, GetPortfolioHistory, GetBars, GetLastTrade, GetLastQuote, } from './params.js'; |
/// <reference types="node" /> | ||
import { EventEmitter } from 'events'; | ||
import { AccountUpdate, AggregateMinute, Credentials, Quote, Trade, TradeUpdate } from './entities.js'; | ||
import { AccountUpdate, AggregateMinute, DefaultCredentials, Quote, Trade, TradeUpdate } from './entities.js'; | ||
export declare interface AlpacaStream { | ||
@@ -22,3 +22,3 @@ on<U extends keyof AlpacaStreamEvents>(event: U, listener: AlpacaStreamEvents[U]): this; | ||
protected params: { | ||
credentials: Credentials; | ||
credentials: DefaultCredentials; | ||
stream: 'account' | 'market_data'; | ||
@@ -31,3 +31,3 @@ }; | ||
constructor(params: { | ||
credentials: Credentials; | ||
credentials: DefaultCredentials; | ||
stream: 'account' | 'market_data'; | ||
@@ -34,0 +34,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
160483
4862
391