-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWrapper.js
More file actions
115 lines (99 loc) · 3.97 KB
/
Wrapper.js
File metadata and controls
115 lines (99 loc) · 3.97 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (c) 2021-2022 BuiltByBit (Mick Capital Pty. Ltd.)
// MIT License (https://github.com/BuiltByBit/js-api-wrapper/blob/main/LICENSE)
const axios = require("axios");
const { Token } = require("./Token");
const { Http } = require("./Http.js");
const { APIError } = require("./APIError.js");
const { Throttler } = require("./Throttler.js");
const { AlertsHelper } = require("./helpers/AlertsHelper.js");
const { ConversationsHelper } = require("./helpers/ConversationsHelper.js");
const { ThreadsHelper } = require("./helpers/ThreadsHelper.js");
const { MembersHelper } = require("./helpers/members/MembersHelper.js");
const { ResourcesHelper } = require("./helpers/resources/ResourcesHelper.js");
/** The primary wrapping type for interactions with BuiltByBit's API. */
class Wrapper {
/** The base API URL and version which will be prepended to non-absolute paths by axios. */
static #BASE_URL = "https://api.builtbybit.com/v1";
#http;
/** Initialise this wrapper with a provided API token.
*
* <br><br>
*
* By default, initialisation of this wrapper will execute a health check which we expect to always succeed under
* nominal conditions. If the request does fail, we expect subsequent requests to other endpoints to also fail. In
* this situation, we conclude that an initialisation failure has occured. We reject the returned Promise and pass
* back the error to the caller.
*
* @param {Token} token The constructed API token.
* @param {boolean} health Whether or not to execute a health check during initialisation.
*/
async init(token, health = true) {
let client = axios.create({baseURL: Wrapper.#BASE_URL, headers: token.asHeader()});
this.#http = new Http(client, new Throttler());
if (health) await this.health();
}
/** Schedule an empty request which we expect to always succeed under nominal conditions. */
async health() {
if (await this.#http.get("/health") !== "ok") {
throw APIError.internal("The health response contained unexpected data.");
}
}
/** Schedule an empty request and measure how long the API took to respond.
*
* <br><br>
*
* This duration may not be representative of the raw request latency due to the fact that requests may be stalled
* locally within this wrapper to ensure compliance with rate limiting rules. Whilst this is a trade-off, it can
* be argued that the returned duration will be more representative of the true latencies experienced.
*
* @return {number} The response time in milliseconds.
*/
async ping() {
let start = Date.now();
await this.health();
return Date.now() - start;
}
/** Access alert-related helper functions.
*
* @return {AlertsHelper} A newly-constructed alert helper instance.
*/
alerts() {
return new AlertsHelper(this);
}
/** Access conversation-related helper functions.
*
* @return {ConversationsHelper} A newly-constructed conversation helper instance.
*/
conversations() {
return new ConversationsHelper(this);
}
/** Access thread-related helper functions.
*
* @return {ThreadsHelper} A newly-constructed thread helper instance.
*/
threads() {
return new ThreadsHelper(this);
}
/** Access member-related helper functions.
*
* @return {MembersHelper} A newly-constructed member helper instance.
*/
members() {
return new MembersHelper(this);
}
/** Access resource-related helper functions.
*
* @return {ResourcesHelper} A newly-constructed resource helper instance.
*/
resources() {
return new ResourcesHelper(this);
}
/** Access the inner Http instance which can be used to make raw requests.
*
* @returns {Http} The current Http instance in use by this wrapper.
*/
http() {
return this.#http;
}
}
exports.Wrapper = Wrapper;