Skip to content

Commit

Permalink
Re-structure
Browse files Browse the repository at this point in the history
  • Loading branch information
117 committed Mar 27, 2024
1 parent 977012a commit ce3a431
Show file tree
Hide file tree
Showing 3 changed files with 1,129 additions and 137 deletions.
26 changes: 12 additions & 14 deletions src/createClient.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import marketData from "../api/marketData.ts";
import trade from "../api/trading.ts";
import { data, trading } from "./rest.ts";

import {
TokenBucketOptions,
createTokenBucket,
} from "../factory/createTokenBucket.ts";
import { TokenBucketOptions, createTokenBucket } from "./createTokenBucket.ts";

export type RequestOptions<T> = {
method?: string;
Expand All @@ -16,8 +12,8 @@ export type RequestOptions<T> = {
};

export type CreateClientOptions = {
keyId?: string;
secretKey?: string;
key?: string;
secret?: string;
baseURL?: string;
accessToken?: string;
tokenBucket?: TokenBucketOptions;
Expand All @@ -35,8 +31,8 @@ export type ClientWithContext<T extends keyof ClientFactoryMap> =
_context: ClientContext;
};

export type Trade = ReturnType<typeof trade>;
export type MarketData = ReturnType<typeof marketData>;
export type Trade = ReturnType<typeof trading>;
export type MarketData = ReturnType<typeof data>;

// Infer the client type based on the base URL
export type ClientFactoryMap = {
Expand Down Expand Up @@ -80,9 +76,9 @@ export function createClient<T extends keyof ClientFactoryMap>(

// Construct the headers
const headers = new Headers({
"APCA-API-KEY-ID": options.keyId || Deno.env.get("APCA_KEY_ID") || "",
"APCA-API-KEY-ID": options.key || Deno.env.get("APCA_KEY_ID") || "",
"APCA-API-SECRET-KEY":
options.secretKey || Deno.env.get("APCA_KEY_SECRET") || "",
options.secret || Deno.env.get("APCA_KEY_SECRET") || "",
"Content-Type": "application/json",
});

Expand Down Expand Up @@ -127,9 +123,9 @@ export function createClient<T extends keyof ClientFactoryMap>(
let client: ClientFactoryMap[T];

if (options.baseURL === "https://paper-api.alpaca.markets") {
client = trade(context) as ClientFactoryMap[T];
client = trading(context) as ClientFactoryMap[T];
} else if (options.baseURL === "https://data.alpaca.markets") {
client = marketData(context) as ClientFactoryMap[T];
client = data(context) as ClientFactoryMap[T];
} else {
throw new Error("invalid base URL");
}
Expand All @@ -139,3 +135,5 @@ export function createClient<T extends keyof ClientFactoryMap>(

return factory(context);
}

// client.<resource>.<method>(options)
Loading

0 comments on commit ce3a431

Please sign in to comment.