This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathClient.ts
95 lines (82 loc) · 2.05 KB
/
Client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import services from "./services/index.js";
import type { BaseHttpRequest } from "./rest/BaseHttpRequest.js";
import type { ApiRequestOptions } from "./rest/ApiRequestOptions.js";
import { prewrap } from "./rest/prewrap.js";
import { AxiosHttpRequest } from "./rest/AxiosHttpRequest.js";
type Resolver<T> = (options: ApiRequestOptions) => Promise<T>;
type Headers = Record<string, string>;
export type Config = {
BASE: string;
HEADERS?: Headers | Resolver<Headers> | undefined;
};
type HttpRequestConstructor = new (config: Config) => BaseHttpRequest;
interface ClientOptions {
paper: boolean;
credentials: {
key: string;
secret: string;
};
}
export class Client {
private readonly baseHttpRequest: BaseHttpRequest;
constructor(
options?: ClientOptions,
HttpRequest: HttpRequestConstructor = AxiosHttpRequest
) {
const { paper, credentials } = options ?? {};
// base request object for all requests
// changes based on paper/live mode and/or data endpoints
this.baseHttpRequest = new HttpRequest({
BASE:
paper === true || paper === undefined
? "https://paper-api.alpaca.markets"
: "https://api.alpaca.markets",
HEADERS: !!credentials
? {
"APCA-API-KEY-ID": credentials.key,
"APCA-API-SECRET-KEY": credentials.secret,
}
: undefined,
});
}
get v2() {
const {
account,
stocks,
assets,
clock,
calendar,
orders,
positions,
watchlists,
} = services;
return prewrap(
{
...account,
...assets,
...clock,
...calendar,
...orders,
...stocks,
...positions,
...watchlists,
},
this.baseHttpRequest
);
}
get v1beta3() {
const { crypto } = services;
return prewrap(crypto, this.baseHttpRequest);
}
get v1beta1() {
const { news, screener, logos } = services;
return prewrap(
{
...news,
...screener,
...logos,
},
this.baseHttpRequest
);
}
}