-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
58 lines (53 loc) · 1.25 KB
/
utils.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
/* eslint-disable @typescript-eslint/ban-types */
import phin from "phin";
const delay = 1000;
/**
* delay stuff
* @param ms
* @returns Promise<T>
*/
export async function rateLimit(ms: number) {
await new Promise(resolve => setTimeout(resolve, ms));
}
/**
* Call api and return the response
* @param {string} url some endpoint
* @param {object} auth authorization
* @param {string} method GET, POST, PUT, DELETE
* @param {object} data data to send
* @returns {Promise<object>}
*/
export async function request(url: string, auth: object, method: string, data?: object) {
await rateLimit(delay);
return await phin({
method: method,
url: url,
headers: auth,
parse: "json",
data: data
});
}
/**
* loop with interval
* @param {Function} callback
* @param {number} delay
* @returns {Promise<void>}
*/
export async function loopInterval(callback: Function, delay: number) {
await callback();
return setInterval(callback, delay);
}
/**
* loop with count
* @param callback
* @param delay
* @param times
* @returns {Promise<void>}
*/
export async function loopCount(callback: Function, delay: number, times: number) {
for (let i = 0; i < times; i++) {
await callback();
await rateLimit(delay);
}
}
export { delay };