Support single-token Alpaca auth

This commit is contained in:
dirtydishes 2026-05-05 03:14:01 -04:00
parent dd32be7717
commit 5025de78b9
8 changed files with 63 additions and 20 deletions

View file

@ -6,6 +6,7 @@ import WebSocket from "ws";
export type AlpacaEquitiesFeed = "iex" | "sip";
export type AlpacaEquitiesAdapterConfig = {
apiKey: string;
keyId: string;
secretKey: string;
restUrl: string;
@ -63,10 +64,18 @@ const normalizeSymbols = (symbols: string[]): string[] => {
return result;
};
const buildHeaders = (config: AlpacaEquitiesAdapterConfig): Record<string, string> => ({
"APCA-API-KEY-ID": config.keyId,
"APCA-API-SECRET-KEY": config.secretKey
});
const buildHeaders = (config: AlpacaEquitiesAdapterConfig): Record<string, string> => {
if (config.apiKey) {
return {
Authorization: `Bearer ${config.apiKey}`
};
}
return {
"APCA-API-KEY-ID": config.keyId,
"APCA-API-SECRET-KEY": config.secretKey
};
};
const parseTimestamp = (value: string): number => {
const parsed = Date.parse(value);
@ -184,8 +193,10 @@ export const createAlpacaEquitiesAdapter = (
return {
name: "alpaca",
start: async (handlers: EquityIngestHandlers) => {
if (!config.keyId || !config.secretKey) {
throw new Error("Alpaca equities adapter requires ALPACA_KEY_ID and ALPACA_SECRET_KEY.");
if (!config.apiKey && (!config.keyId || !config.secretKey)) {
throw new Error(
"Alpaca equities adapter requires ALPACA_API_KEY or ALPACA_KEY_ID and ALPACA_SECRET_KEY."
);
}
const symbols = normalizeSymbols(config.symbols);
@ -195,7 +206,9 @@ export const createAlpacaEquitiesAdapter = (
const exchangeNameMap = await fetchExchangeMeta(config);
const wsUrl = buildWsUrl(config.wsBaseUrl, config.feed);
const ws = new WebSocket(wsUrl);
const ws = new WebSocket(wsUrl, {
headers: buildHeaders(config)
});
let seq = 0;
let stopped = false;
@ -205,8 +218,8 @@ export const createAlpacaEquitiesAdapter = (
ws.send(
JSON.stringify({
action: "auth",
key: config.keyId,
secret: config.secretKey
key: config.apiKey || config.keyId,
secret: config.apiKey ? "" : config.secretKey
})
);
});