Skip to content

Commit

Permalink
Update API endpoints and types
Browse files Browse the repository at this point in the history
  • Loading branch information
117 committed Mar 24, 2024
1 parent c7daa5d commit fc4124b
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 63 deletions.
4 changes: 2 additions & 2 deletions api/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { default as marketData } from "./marketData/index.ts";
export { default as trade } from "./trade/consumer.ts";
export { default as marketData } from "./marketData/endpoints.ts";
export { default as trade } from "./trade/endpoints.ts";
6 changes: 1 addition & 5 deletions api/marketData/index.ts → api/marketData/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,4 @@ const methods = ({ request }: ClientContext) => ({
},
});

export default <T extends ReturnType<typeof methods>>(
context: ClientContext
): T => {
return methods(context) as T;
};
export default (context: ClientContext) => methods(context);
8 changes: 2 additions & 6 deletions api/trade/consumer.ts → api/trade/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import { OptionContract, OptionContractsQueryParams } from "./types/options.ts";
import { CreateOrderOptions, Order, PatchOrderOptions } from "./types/order.ts";
import { ClosePositionOptions, Position } from "./types/position.ts";

const methods = ({ request }: ClientContext) => ({
export const methods = ({ request }: ClientContext) => ({
v2: {
account: {
get: () =>
Expand Down Expand Up @@ -383,8 +383,4 @@ const methods = ({ request }: ClientContext) => ({
},
});

export default <T extends ReturnType<typeof methods>>(
context: ClientContext
): T => {
return methods(context) as T;
};
export default (context: ClientContext) => methods(context);
44 changes: 11 additions & 33 deletions api/trade/types/websocket_2.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { EventMap, WebSocketWithEvents } from "../../types/websocket.ts";

type Trade = {
T: "t";
i: number;
Expand Down Expand Up @@ -120,38 +122,14 @@ type StockDataMessage =
| ErrorMessage
| SubscriptionMessage;

interface StockChannelEventMap {
trades: Trade;
quotes: Quote;
bars: Bar;
dailyBars: Bar;
updatedBars: Bar;
statuses: TradingStatus;
lulds: LULD;
interface StockChannelEventMap extends EventMap {
trades: { T: "trades"; data: Trade };
quotes: { T: "quotes"; data: Quote };
bars: { T: "bars"; data: Bar };
dailyBars: { T: "dailyBars"; data: Bar };
updatedBars: { T: "updatedBars"; data: Bar };
statuses: { T: "statuses"; data: TradingStatus };
lulds: { T: "lulds"; data: LULD };
}

type SubscriptionRequest = {
channel: keyof StockChannelEventMap;
symbols: string[];
};

export type StockDataWebSocket = {
on: <E extends keyof StockChannelEventMap>(
channel: E,
event: E extends keyof StockChannelEventMap
? StockChannelEventMap[E]["T"]
: never,
handler: (
data: Extract<
StockChannelEventMap[E],
{ T: StockChannelEventMap[E]["T"] }
>
) => void
) => void;
subscribe: (
requests: SubscriptionRequest[]
) => Promise<SubscriptionRequest[]>;
unsubscribe: (
requests: SubscriptionRequest[]
) => Promise<SubscriptionRequest[]>;
};
export type StockDataWebSocket = WebSocketWithEvents<StockChannelEventMap>;
3 changes: 0 additions & 3 deletions api/types/shared.ts

This file was deleted.

27 changes: 27 additions & 0 deletions api/types/websocket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// The expected type for an event map for a given channel
export type EventMap = {
// deno-lint-ignore no-explicit-any
[EventKey: string]: { T: string; data: any };
};

// The expected type of a subscription request for a channel event map
export type SubscriptionRequest<ChannelEventMap extends EventMap> = {
channel: keyof ChannelEventMap;
symbols?: string[];
};

export type WebSocketWithEvents<ChannelEventMap extends EventMap> = {
on: <E extends keyof ChannelEventMap>(
channel: E,
event: ChannelEventMap[E]["T"],
handler: (
data: Extract<ChannelEventMap[E], { T: ChannelEventMap[E]["T"] }>
) => void
) => void;
subscribe: (
requests: SubscriptionRequest<ChannelEventMap>[]
) => Promise<SubscriptionRequest<ChannelEventMap>[]>;
unsubscribe: (
requests: SubscriptionRequest<ChannelEventMap>[]
) => Promise<SubscriptionRequest<ChannelEventMap>[]>;
};
44 changes: 30 additions & 14 deletions factory/createClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,52 @@ import { StockDataWebSocket } from "../api/trade/types/websocket_2.ts";
import { CryptoWebSocket } from "../api/trade/types/websocket_3.ts";
import { NewsWebSocket } from "../api/trade/types/websocket_4.ts";
import { OptionsWebSocket } from "../api/trade/types/websocket_5.ts";
import { ClientContextConsumer } from "../api/types/shared.ts";
import { TokenBucketOptions, createTokenBucket } from "./createTokenBucket.ts";

// The options required to make a request
export type RequestOptions = {
path: string;
data?: object;
method?: string;
// deno-lint-ignore no-explicit-any
params?: Record<string, any>;
};

// Used to share the client options and request function between the different API methods
export type ClientContext = {
options: CreateClientOptions;
request: <T>(options: RequestOptions) => Promise<T>;
};

// The options required to create a client
type CreateClientOptions = {
export type CreateClientOptions = {
keyId: string;
secretKey: string;
baseURL: string;
tokenBucket?: TokenBucketOptions;
};

// The options required to make a request
type RequestOptions = {
path: string;
data?: object;
method?: string;
// Infer the return type of an API method by looking at the return type of the function
export type ExtractedClientMethodReturn<T> = T extends (
// deno-lint-ignore no-explicit-any
params?: Record<string, any>;
};
...args: any[]
) => infer R
? R
: T;

// The expected type of a client method
export type ClientMethod<T> = (context: ClientContext) => T;

// The return type of a client method
export type ClientMethodReturnType<T> = ExtractedClientMethodReturn<
ReturnType<ClientMethod<T>>
>;

// The client object that is returned by createClient
// The object returned by createClient
export type Client = {
rest: {
trade: ReturnType<ClientContextConsumer<ReturnType<typeof trade>>>;
marketData: ReturnType<
ClientContextConsumer<ReturnType<typeof marketData>>
>;
trade: ClientMethodReturnType<typeof trade>;
marketData: ClientMethodReturnType<typeof marketData>;
};
websocket: {
trade: TradeWebSocket;
Expand Down Expand Up @@ -128,11 +141,14 @@ export function createClient({
);
};

// Build the context object
const context: ClientContext = { options, request };

return {
rest: {
// Instantiate the trade methods with the context
trade: trade(context),
// Instantiate the market data methods with the context
marketData: marketData(context),
},
websocket: {} as any,
Expand Down

0 comments on commit fc4124b

Please sign in to comment.