-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
229 lines (187 loc) · 6.11 KB
/
index.js
File metadata and controls
229 lines (187 loc) · 6.11 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
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
222
223
224
225
226
227
228
229
var fetch = require('node-fetch-retry');
const SITE = "https://addons-ecs.forgesvc.net/api/";
const VERSION = "v2";
async function get(endpoint, json = true)
{
const response = await fetch(SITE + VERSION + endpoint);
let result;
if(json)
{
result = await response.json();
} else
{
result = await response.text();
}
return result;
}
async function post(endpoint, jsonData, json = true)
{
const response = await fetch(SITE + VERSION + endpoint,
{
method: 'POST',
headers:
{
'Accept': 'application/json',
'Content-Type': 'application/json'
},
retry: 5,
body: JSON.stringify(jsonData)
});
if(json)
{
result = await response.json();
} else
{
result = await response.text();
}
return result;
}
async function getAddonInfo(addonID)
{
if(isNaN(addonID)) throw new Error('addonID was not a number!');
return get('/addon/' + addonID);
}
async function getMultipleAddons(addonIDArray)
{
return post('/addon', addonIDArray);
}
async function addonSearch(categoryID, gameId, gameVersion, index, pageSize, searchFilter, sectionId, sort)
{
if(isNaN(categoryID)) throw new Error('categoryID was not a number!');
if(isNaN(gameId)) throw new Error('gameId was not a number!');
if(isNaN(index)) throw new Error('index was not a number!');
if(isNaN(pageSize)) throw new Error('pageSize was not a number!');
if(isNaN(sectionId)) throw new Error('sectionId was not a number!');
if(isNaN(sort)) throw new Error('sort was not a number!');
return get(`/addon/search?categoryId=${categoryID}&gameId=${gameId}&gameVersion=${encodeURI(gameVersion)}&index=${index}&pageSize=${pageSize}&searchFilter=${encodeURI(searchFilter)}§ionId=${sectionId}&sort=${sort}`);
}
async function getAddonDescription(addonID)
{
if(isNaN(addonID)) throw new Error('addonID was not a number!');
return get(`/addon/${addonID}/description`, false);
}
async function getAddonFileChangelog(addonID, fileID)
{
if(isNaN(addonID)) throw new Error('addonID was not a number!');
if(isNaN(fileID)) throw new Error('fileID was not a number!');
return get(`/addon/${addonID}/file/${fileID}/changelog`, false);
}
async function getAddonFileInformation(addonID, fileID)
{
if(isNaN(addonID)) throw new Error('addonID was not a number!');
if(isNaN(fileID)) throw new Error('fileID was not a number!');
return get(`/addon/${addonID}/file/${fileID}`);
}
async function getAddonFileDownloadUrl(addonID, fileID)
{
if(isNaN(addonID)) throw new Error('addonID was not a number!');
if(isNaN(fileID)) throw new Error('fileID was not a number!');
return get(`/addon/${addonID}/file/${fileID}/download-url`, false);
}
async function getAddonFiles(addonID)
{
if(isNaN(addonID)) throw new Error('addonID was not a number!');
return get(`/addon/${addonID}/files`);
}
async function getFeaturedAddons(gameId, addonIds, featuredCount, popularCount, updatedCount)
{
if(isNaN(gameId)) throw new Error('gameId was not a number!');
if(isNaN(featuredCount)) throw new Error('featuredCount was not a number!');
if(isNaN(popularCount)) throw new Error('popularCount was not a number!');
if(isNaN(updatedCount)) throw new Error('updatedCount was not a number!');
return post('/addon/featured', {GameId: gameId, addonIds: addonIds, featuredCount: featuredCount, popularCount: popularCount, updatedCount: updatedCount});
}
async function getAddonsDatabaseTimestamp()
{
return get(`/addon/timestamp`);
}
async function getAddonByFingerPrint(arr)
{
return post('/fingerprint', arr);
}
async function getMinecraftVersionTimestamp()
{
return get('/minecraft/version/timestamp');
}
async function getMinecraftVersionList()
{
return get('/minecraft/version');
}
async function getMinecraftVersionInfo(versionString)
{
return get(`/minecraft/version/${versionString}`);
}
async function getModloaderVersionTimestamp()
{
return get('/minecraft/modloader/timestamp');
}
async function getModloaderVersionList()
{
return get('/minecraft/modloader');
}
async function getModloaderVersionInfo(versionString)
{
return get(`/minecraft/modloader/${versionString}`);
}
async function getCategoryTimestamp()
{
return get('/category/timestamp');
}
async function getCategoryList()
{
return get('/category');
}
async function getCategoryInfo(categoryID)
{
if(isNaN(categoryID)) throw new Error('categoryID was not a number!');
return get(`/category/${categoryID}`);
}
async function getCategorySectionInfo(sectionId)
{
if(isNaN(sectionId)) throw new Error('sectionId was not a number!');
return get(`/category/section/${sectionId}`);
}
async function getGameTimestamp()
{
return get('/game/timestamp');
}
async function getGamesList(supportsAddons)
{
return get(`/game?${supportsAddons}`);
}
async function getGameInfo(gameId)
{
return get(`/game/${gameId}`);
}
function overwriteFetch(newFetch) // temp function to test https://www.npmjs.com/package/make-fetch-happen
{
fetch = newFetch;
}
module.exports =
{
getAddonInfo: getAddonInfo,
getMultipleAddons: getMultipleAddons,
addonSearch: addonSearch,
getAddonDescription: getAddonDescription,
getAddonFileChangelog: getAddonFileChangelog,
getAddonFileInformation: getAddonFileInformation,
getAddonFileDownloadUrl: getAddonFileDownloadUrl,
getAddonFiles: getAddonFiles,
getFeaturedAddons: getFeaturedAddons,
getAddonsDatabaseTimestamp: getAddonsDatabaseTimestamp,
getAddonByFingerPrint: getAddonByFingerPrint,
getMinecraftVersionTimestamp: getMinecraftVersionTimestamp,
getMinecraftVersionList: getMinecraftVersionList,
getMinecraftVersionInfo: getMinecraftVersionInfo,
getModloaderVersionTimestamp: getModloaderVersionTimestamp,
getModloaderVersionList: getModloaderVersionList,
getModloaderVersionInfo: getModloaderVersionInfo,
getCategoryTimestamp: getCategoryTimestamp,
getCategoryList: getCategoryList,
getCategoryInfo: getCategoryInfo,
getCategorySectionInfo: getCategorySectionInfo,
getGameTimestamp: getGameTimestamp,
getGamesList: getGamesList,
getGameInfo: getGameInfo,
overwriteFetch: overwriteFetch
};