Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

v7 fixes #117

Draft
wants to merge 2 commits into
base: v7
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,20 @@ export const OpenAPI: OpenAPIConfig = {

type HttpRequestConstructor = new (config: OpenAPIConfig) => BaseHttpRequest;

export enum BaseURL {
BROKER_SANDBOX = "https://broker-api.sandbox.alpaca.markets",
BROKER_PRODUCTION = "https://broker-api.alpaca.markets",
TRADING_PAPER = "https://paper-api.alpaca.markets",
TRADING_LIVE = "https://api.alpaca.markets",
DATA = "https://data.alpaca.markets",
MARKET_DATA_STREAM = "wss://stream.data.alpaca.markets",
TRADING_STREAM_PAPER = "wss://paper-api.alpaca.markets/stream",
TRADING_STREAM_LIVE = "wss://api.alpaca.markets/stream",
}

interface ClientOptions {
paper: boolean;
baseURL?: BaseURL;
credentials: {
key: string;
secret: string;
Expand All @@ -36,15 +48,17 @@ export class Client {
options?: ClientOptions,
HttpRequest: HttpRequestConstructor = AxiosHttpRequest
) {
const { paper, credentials } = options ?? {};
const { paper, baseURL, credentials } = options ?? {};

// base request object for all requests
// changes based on paper/live mode and/or data endpoints
this.baseHttpRequest = new HttpRequest({
BASE:
baseURL ? baseURL :
paper === true || paper === undefined
? "https://paper-api.alpaca.markets"
: "https://api.alpaca.markets",
? BaseURL.TRADING_PAPER
: BaseURL.TRADING_LIVE,
// TODO: need to implement credentials accordingly
HEADERS: !!credentials
? {
"APCA-API-KEY-ID": credentials.key,
Expand All @@ -55,19 +69,19 @@ export class Client {
}

get account() {
return prewrap(account, this.baseHttpRequest.config);
return prewrap(account, this.baseHttpRequest);
}

get assets() {
return prewrap(assets, this.baseHttpRequest.config);
return prewrap(assets, this.baseHttpRequest);
}

get clock() {
return prewrap(clock, this.baseHttpRequest.config);
return prewrap(clock, this.baseHttpRequest);
}

get calendar() {
return prewrap(calendar, this.baseHttpRequest.config);
return prewrap(calendar, this.baseHttpRequest);
}
}

Expand Down
23 changes: 3 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ export { Client } from "./Client.js";
export { ApiError } from "./rest/ApiError";
export { BaseHttpRequest } from "./rest/BaseHttpRequest";
export { CancelablePromise, CancelError } from "./rest/CancelablePromise";
export { OpenAPI } from "./OpenAPI";
export type { OpenAPIConfig } from "./OpenAPI";
export { OpenAPI } from "./Client";
export type { OpenAPIConfig } from "./Client";

export type { Account } from "./entities/Account.js";
export { AccountConfigurations } from "./entities/AccountConfigurations.js";
Expand Down Expand Up @@ -32,17 +32,6 @@ export { TradingActivities } from "./entities/TradingActivities.js";
export type { UpdateWatchlistRequest } from "./entities/UpdateWatchlistRequest.js";
export type { Watchlist } from "./entities/Watchlist.js";

export { Account } from "./api/account";
export { AccountConfigurations } from "./api/AccountConfigurations";
export { AccountService } from "./api/AccountService.js";
export { Calendar } from "./api/calendar";
export { Clock } from "./api/clock";
export { Assets } from "./api/assets";
export { Orders } from "./api/orders";
export { PortfolioHistoryService } from "./api/PortfolioHistoryService.js";
export { Positions } from "./api/positions";
export { Watchlists } from "./api/watchlists";

export { adjustment } from "./entities/adjustment.js";
export { Bar } from "./entities/Bar.js";
export type { BarsResponse } from "./entities/BarsResponse.js";
Expand Down Expand Up @@ -78,10 +67,4 @@ export type { stock_symbols } from "./entities/stock_symbols.js";
export type { timeframe } from "./entities/timeframe.js";
export type { Trade } from "./entities/Trade.js";
export type { TradesResponse } from "./entities/TradesResponse.js";
export type { XBBO } from "./entities/XBBO.js";

export { Crypto } from "./api/crypto";
export { Logos } from "./api/logos";
export { News } from "./api/news";
export { Screener } from "./api/screener";
export { Stocks } from "./api/stocks";
export type { XBBO } from "./entities/XBBO.js";
2 changes: 1 addition & 1 deletion src/rest/AxiosHttpRequest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ApiRequestOptions } from "./ApiRequestOptions";
import { BaseHttpRequest } from "./BaseHttpRequest";
import type { CancelablePromise } from "./CancelablePromise";
import type { OpenAPIConfig } from "../OpenAPI";
import type { OpenAPIConfig } from "../Client";
import { request as __request } from "./request";

export class AxiosHttpRequest extends BaseHttpRequest {
Expand Down
6 changes: 4 additions & 2 deletions src/rest/BaseHttpRequest.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { ApiRequestOptions } from "./ApiRequestOptions";
import type { CancelablePromise } from "./CancelablePromise";
import type { OpenAPIConfig } from "../OpenAPI";
import type { OpenAPIConfig } from "../Client";

export abstract class BaseHttpRequest {
constructor(public readonly config: OpenAPIConfig) {}
constructor(public readonly config: OpenAPIConfig) {
this.config = config;
}

public abstract request<T>(options: ApiRequestOptions): CancelablePromise<T>;
}
2 changes: 1 addition & 1 deletion src/rest/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { ApiRequestOptions } from "./ApiRequestOptions";
import type { ApiResult } from "./ApiResult";
import { CancelablePromise } from "./CancelablePromise";
import type { OnCancel } from "./CancelablePromise";
import type { OpenAPIConfig } from "../OpenAPI";
import type { OpenAPIConfig } from "../Client";

export const isDefined = <T>(
value: T | null | undefined
Expand Down