-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgatewayManager.js
221 lines (209 loc) · 6.92 KB
/
gatewayManager.js
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import axios from "axios";
import { apiErrorHandler } from "./helpers.js";
class GatewayManager {
#DEFAULT_ENDPOINT = "https://api.filebase.io";
#DEFAULT_TIMEOUT = 60000;
#client;
/**
* @summary Creates a new instance of the constructor.
* @param {string} clientKey - The access key ID for authentication.
* @param {string} clientSecret - The secret access key for authentication.
* @tutorial quickstart-gateway
* @example
* import { GatewayManager } from "@filebase/sdk";
* const gatewayManager = new GatewayManager("KEY_FROM_DASHBOARD", "SECRET_FROM_DASHBOARD");
*/
constructor(clientKey, clientSecret) {
const clientEndpoint =
process.env.NODE_ENV === "test"
? process.env.TEST_GW_ENDPOINT || this.#DEFAULT_ENDPOINT
: this.#DEFAULT_ENDPOINT,
encodedToken = Buffer.from(`${clientKey}:${clientSecret}`).toString(
"base64",
),
baseURL = `${clientEndpoint}/v1/gateways`;
this.#client = axios.create({
baseURL: baseURL,
timeout: this.#DEFAULT_TIMEOUT,
headers: { Authorization: `Bearer ${encodedToken}` },
});
}
/**
* @typedef {Object} gateway
* @property {string} name Name for the gateway
* @property {string} domain Custom Domain for the gateway
* @property {boolean} enabled Whether the gateway is enabled or not
* @property {string} private Whether the gateway is scoped to users content
* @property {date} created_at Date the gateway was created
* @property {date} updated_at Date the gateway was last updated
*/
/**
* @typedef {Object} gatewayOptions
* @property {boolean} [domain] Optional Domain to allow for using a Custom Domain
* @property {string} [enabled] Optional Toggle to use for enabling the gateway
* @property {boolean} [private] Optional Boolean determining if gateway is Public or Private
*/
/**
* @summary Creates a gateway with the given name and options
* @param {string} name Unique name across entire platform for the gateway. Must be a valid subdomain name.
* @param {gatewayOptions} [options]
* @returns {Promise<gateway>} - A promise that resolves to the value of a gateway.
* @example
* // Create gateway with name of `create-gateway-example` and a custom domain of `cname.mycustomdomain.com`.
* // The custom domain must already exist and have a CNAME record pointed at `create-gateway-example.myfilebase.com`.
* await gatewayManager.create(`create-gateway-example`, {
* domain: `cname.mycustomdomain.com`
* });
*/
async create(name, options = {}) {
try {
let createOptions = {
name,
};
if (typeof options.domain === "string") {
createOptions.domain = options.domain;
}
if (typeof options.enabled === "boolean") {
createOptions.enabled = options.enabled;
}
if (typeof options.private === "boolean") {
createOptions.private = options.private;
}
const createResponse = await this.#client.request({
method: "POST",
data: createOptions,
});
return createResponse.data;
} catch (err) {
apiErrorHandler(err);
}
}
/**
* @summary Deletes a gateway with the given name.
* @param {string} name - The name of the gateway to delete.
* @returns {Promise<boolean>} - A promise that resolves to true if the gateway was successfully deleted.
* @example
* // Delete gateway with name of `delete-gateway-example`
* await gatewayManager.delete(`delete-name-example`);
*/
async delete(name) {
try {
await this.#client.request({
method: "DELETE",
url: `/${name}`,
validateStatus: (status) => {
return status === 204;
},
});
return true;
} catch (err) {
apiErrorHandler(err);
}
}
/**
* @summary Returns the value of a gateway
* @param {string} name - Parameter representing the name to get.
* @returns {Promise<gateway|false>} - A promise that resolves to the value of a gateway.
* @example
* // Get gateway with name of `gateway-get-example`
* await gatewayManager.get(`gateway-get-example`);
*/
async get(name) {
try {
const getResponse = await this.#client.request({
method: "GET",
url: `/${name}`,
validateStatus: (status) => {
return status === 200 || status === 404;
},
});
return getResponse.status === 200 ? getResponse.data : false;
} catch (err) {
apiErrorHandler(err);
}
}
/**
* @summary Returns a list of gateways
* @returns {Promise<Array.<gateway>>} - A promise that resolves to an array of gateways.
* @example
* // List all gateways
* await gatewayManager.list();
*/
async list() {
try {
const getResponse = await this.#client.request({
method: "GET",
});
return getResponse.data;
} catch (err) {
apiErrorHandler(err);
}
}
/**
* @summary Updates the specified gateway.
* @param {string} name - The name of the gateway to update.
* @param {gatewayOptions} options - The options for the update operation.
*
* @returns {Promise<boolean>} - A Promise that resolves to true if the gateway was updated.
* @example
* // Update gateway with name of `update-gateway-example` and set the gateway to only serve CIDs pinned by user.
* await gatewayManager.update(`update-gateway-example`, {
* private: true
* });
*/
async update(name, options) {
try {
const updateOptions = {
name,
};
if (options?.domain) {
updateOptions.domain = String(options.private);
}
if (options?.enabled) {
updateOptions.enabled = Boolean(options.enabled);
}
if (options?.private) {
updateOptions.private = Boolean(options.private);
}
await this.#client.request({
method: "PUT",
url: `/${name}`,
data: updateOptions,
validateStatus: (status) => {
return status === 200;
},
});
return true;
} catch (err) {
apiErrorHandler(err);
}
}
/**
* @summary Toggles the enabled state of a given gateway.
* @param {string} name - The name of the gateway to toggle.
* @param {boolean} targetState - The new target state.
* @returns {Promise<boolean>} A promise that resolves to true if the gateway was successfully toggled.
* @example
* // Toggle gateway with label of `toggle-gateway-example`
* await gatewayManager.toggle(`toggle-gateway-example`, true); // Enabled
* await gatewayManager.toggle(`toggle-gateway-example`, false); // Disabled
*/
async toggle(name, targetState) {
try {
await this.#client.request({
method: "PUT",
url: `/${name}`,
data: {
enabled: Boolean(targetState),
},
validateStatus: (status) => {
return status === 200;
},
});
return true;
} catch (err) {
apiErrorHandler(err);
}
}
}
export default GatewayManager;