-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
66 lines (56 loc) · 1.5 KB
/
index.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
import PnwKitAPI from './api';
import memoize from 'memoizee';
import {RateLimitI} from './interfaces/GraphQLService';
/**
* The main application class
*/
export class Kit extends PnwKitAPI {
[key: string]: any;
apiKey = '';
private rateLimitData = {
resetAfterSeconds: 0,
limit: 0,
remaining: 0,
reset: 0,
};
/**
* Set the pnwkit instance's key.
* @param key
*/
setKey(key: string): void {
this.apiKey = key;
}
get rateLimit(): RateLimitI {
return new Proxy(this.rateLimitData, {
set: () => {
return false;
},
});
}
/**
* Do not call this function, this is internal.
* @param value
*/
setRateLimit(value: RateLimitI): void {
this.rateLimitData = value;
}
/**
* Lets you cache results of a query call
*
* It returns a cached version of the function that accepts the same arguments but caches it for a certain period of time
* @param {(...args: any[]) => any} queryFunc The query function you want to cache
* @param {number} maxAgeMinutes The max number of minutes to cache it for
*
* @return {Function} returns a cached version of the function
*/
cached(queryFunc: (...args: any[]) => any, maxAgeMinutes: number) {
return memoize(queryFunc.bind(this), {maxAge: maxAgeMinutes * 60 * 1000, promise: true, primitive: true});
}
}
const kit = new Kit();
for (const [key] of Object.entries(kit)) {
exports[key] = kit[key];
}
exports.setKey = kit.setKey;
exports.cached = kit.cached;
export default kit;