startgg-helper-browser 2.0.2 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/main.js +78 -27
- package/package.json +2 -2
package/main.js
CHANGED
|
@@ -41,31 +41,6 @@ class GraphQLError extends Error {
|
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
async function requestStartGG(schema, variables, token){
|
|
45
|
-
const response = await fetch('https://api.start.gg/gql/alpha', {
|
|
46
|
-
method: 'POST',
|
|
47
|
-
headers: {
|
|
48
|
-
'Content-Type': 'application/json',
|
|
49
|
-
'accept': 'application/json',
|
|
50
|
-
'Authorization': token
|
|
51
|
-
},
|
|
52
|
-
body: JSON.stringify({
|
|
53
|
-
'query': schema,
|
|
54
|
-
'variables': variables
|
|
55
|
-
}),
|
|
56
|
-
});
|
|
57
|
-
const json = await response.json();
|
|
58
|
-
if (!json){
|
|
59
|
-
throw "Empty response"
|
|
60
|
-
}
|
|
61
|
-
if (!json.data){
|
|
62
|
-
throw new GraphQLError(response, {schema, variables}, json);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
return json.data;
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
|
|
69
44
|
export class SGGHelperClient {
|
|
70
45
|
#token;
|
|
71
46
|
|
|
@@ -77,7 +52,83 @@ export class SGGHelperClient {
|
|
|
77
52
|
this.#token = token;
|
|
78
53
|
}
|
|
79
54
|
|
|
80
|
-
request(schema, variables){
|
|
81
|
-
|
|
55
|
+
async request(schema, variables){
|
|
56
|
+
const response = await fetch('https://api.start.gg/gql/alpha', {
|
|
57
|
+
method: 'POST',
|
|
58
|
+
headers: {
|
|
59
|
+
'Content-Type': 'application/json',
|
|
60
|
+
'accept': 'application/json',
|
|
61
|
+
'Authorization': this.#token
|
|
62
|
+
},
|
|
63
|
+
body: JSON.stringify({
|
|
64
|
+
'query': schema,
|
|
65
|
+
'variables': variables
|
|
66
|
+
}),
|
|
67
|
+
});
|
|
68
|
+
const json = await response.json();
|
|
69
|
+
if (!json){
|
|
70
|
+
throw "Empty response"
|
|
71
|
+
}
|
|
72
|
+
if (!json.data){
|
|
73
|
+
throw new GraphQLError(response, {schema, variables}, json);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return json.data;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export class RateLimitingSGGHelperClient extends SGGHelperClient {
|
|
81
|
+
#pauseTimer;
|
|
82
|
+
#pausePromise;
|
|
83
|
+
#pauseDelay = 60000;
|
|
84
|
+
//bool paused = !!pauseTimer;
|
|
85
|
+
|
|
86
|
+
isPaused(){
|
|
87
|
+
return !!this.#pausePromise;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
pause(){
|
|
91
|
+
if (this.isPaused()){
|
|
92
|
+
clearTimeout(this.#pauseTimer);
|
|
93
|
+
} else {
|
|
94
|
+
let resolve, reject;
|
|
95
|
+
let promise = new Promise((resolve_, reject_) => {
|
|
96
|
+
resolve = resolve_;
|
|
97
|
+
reject = reject_;
|
|
98
|
+
})
|
|
99
|
+
this.#pausePromise = {promise, resolve, reject};
|
|
100
|
+
}
|
|
101
|
+
this.#pauseTimer = setTimeout(() => {
|
|
102
|
+
this.#unpause();
|
|
103
|
+
}, this.#pauseDelay);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
#unpause(){
|
|
107
|
+
if (this.isPaused()){
|
|
108
|
+
this.#pausePromise.resolve();
|
|
109
|
+
this.#pauseTimer = undefined;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async request(schema, variables){
|
|
114
|
+
console.log(this.#pausePromise);
|
|
115
|
+
if (this.isPaused()) await this.#pausePromise;
|
|
116
|
+
|
|
117
|
+
try {
|
|
118
|
+
return await super.request(schema, variables);
|
|
119
|
+
} catch (err){
|
|
120
|
+
if (err instanceof GraphQLError){
|
|
121
|
+
if (err.response.status == 429){
|
|
122
|
+
//IT WAS A RATE LIMIT ERROR
|
|
123
|
+
console.warn("Rate limit exceeded. Pausing this client for 60 seconds. The caller should initiate a retry.")
|
|
124
|
+
this.pause();
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
throw err;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
stop(){
|
|
132
|
+
clearTimeout(this.#pauseTimer);
|
|
82
133
|
}
|
|
83
134
|
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "startgg-helper-browser",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "A set of functions and classes useful to communicate with the start.gg API from a web app",
|
|
5
5
|
"main": "main.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"
|
|
7
|
+
"build": "node test/webpack.js"
|
|
8
8
|
},
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|