sidechat.js 2.0.3 → 2.0.5
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/dist/classes/SidechatAPIClient.d.ts.map +1 -1
- package/dist/index.cjs +436 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +431 -0
- package/dist/types/SidechatTypes.d.ts +11 -9
- package/dist/types/SidechatTypes.d.ts.map +1 -1
- package/package.json +22 -13
- package/rollup.config.mjs +23 -0
- package/src/types/SidechatTypes.js +1 -3
- package/tsconfig.json +4 -2
- package/dist/sidechat.cjs +0 -2
- package/dist/sidechat.cjs.map +0 -1
- package/dist/sidechat.modern.js +0 -2
- package/dist/sidechat.modern.js.map +0 -1
- package/dist/sidechat.module.js +0 -2
- package/dist/sidechat.module.js.map +0 -1
- package/dist/sidechat.umd.js +0 -2
- package/dist/sidechat.umd.js.map +0 -1
- package/docs/sidechat.js/2.0.3/SidechatAPIClient.html +0 -3
- package/docs/sidechat.js/2.0.3/SidechatAPIError.html +0 -3
- package/docs/sidechat.js/2.0.3/classes_SidechatAPIClient.js.html +0 -425
- package/docs/sidechat.js/2.0.3/classes_SidechatAPIError.js.html +0 -16
- package/docs/sidechat.js/2.0.3/data/search.json +0 -1
- package/docs/sidechat.js/2.0.3/fonts/Inconsolata-Regular.ttf +0 -0
- package/docs/sidechat.js/2.0.3/fonts/OpenSans-Regular.ttf +0 -0
- package/docs/sidechat.js/2.0.3/fonts/WorkSans-Bold.ttf +0 -0
- package/docs/sidechat.js/2.0.3/global.html +0 -3
- package/docs/sidechat.js/2.0.3/index.html +0 -4
- package/docs/sidechat.js/2.0.3/scripts/core.js +0 -702
- package/docs/sidechat.js/2.0.3/scripts/core.min.js +0 -23
- package/docs/sidechat.js/2.0.3/scripts/resize.js +0 -90
- package/docs/sidechat.js/2.0.3/scripts/search.js +0 -265
- package/docs/sidechat.js/2.0.3/scripts/search.min.js +0 -6
- package/docs/sidechat.js/2.0.3/scripts/third-party/Apache-License-2.0.txt +0 -202
- package/docs/sidechat.js/2.0.3/scripts/third-party/fuse.js +0 -9
- package/docs/sidechat.js/2.0.3/scripts/third-party/hljs-line-num-original.js +0 -369
- package/docs/sidechat.js/2.0.3/scripts/third-party/hljs-line-num.js +0 -1
- package/docs/sidechat.js/2.0.3/scripts/third-party/hljs-original.js +0 -5171
- package/docs/sidechat.js/2.0.3/scripts/third-party/hljs.js +0 -1
- package/docs/sidechat.js/2.0.3/scripts/third-party/popper.js +0 -5
- package/docs/sidechat.js/2.0.3/scripts/third-party/tippy.js +0 -1
- package/docs/sidechat.js/2.0.3/scripts/third-party/tocbot.js +0 -672
- package/docs/sidechat.js/2.0.3/scripts/third-party/tocbot.min.js +0 -1
- package/docs/sidechat.js/2.0.3/styles/clean-jsdoc-theme-base.css +0 -975
- package/docs/sidechat.js/2.0.3/styles/clean-jsdoc-theme-dark.css +0 -407
- package/docs/sidechat.js/2.0.3/styles/clean-jsdoc-theme-light.css +0 -388
- package/docs/sidechat.js/2.0.3/styles/clean-jsdoc-theme.min.css +0 -1
- package/docs/sidechat.js/2.0.3/tutorial-Authentication.html +0 -13
- package/docs/sidechat.js/2.0.3/tutorial-Email Registration.html +0 -6
- package/docs/sidechat.js/2.0.3/types_SidechatTypes.js.html +0 -98
- package/src/classes/SidechatAPIClient.d.ts +0 -104
- package/src/classes/SidechatAPIClient.d.ts.map +0 -1
- package/src/classes/SidechatAPIError.d.ts +0 -10
- package/src/classes/SidechatAPIError.d.ts.map +0 -1
- package/src/index.d.ts.map +0 -1
- package/src/types/SidechatTypes.d.ts +0 -224
- package/src/types/SidechatTypes.d.ts.map +0 -1
- /package/{src/index.d.ts → dist/index.d.mts} +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/SidechatAPIClient.html +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/SidechatAPIError.html +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/classes_SidechatAPIClient.js.html +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/classes_SidechatAPIError.js.html +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/data/search.json +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/fonts/Inconsolata-Regular.ttf +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/fonts/OpenSans-Regular.ttf +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/fonts/WorkSans-Bold.ttf +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/global.html +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/index.html +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/core.js +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/core.min.js +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/resize.js +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/search.js +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/search.min.js +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/third-party/Apache-License-2.0.txt +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/third-party/fuse.js +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/third-party/hljs-line-num-original.js +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/third-party/hljs-line-num.js +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/third-party/hljs-original.js +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/third-party/hljs.js +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/third-party/popper.js +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/third-party/tippy.js +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/third-party/tocbot.js +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/third-party/tocbot.min.js +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/styles/clean-jsdoc-theme-base.css +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/styles/clean-jsdoc-theme-dark.css +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/styles/clean-jsdoc-theme-light.css +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/styles/clean-jsdoc-theme.min.css +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/tutorial-Authentication.html +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/tutorial-Email Registration.html +0 -0
- /package/docs/sidechat.js/{2.0.0 → 2.0.4}/types_SidechatTypes.js.html +0 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,431 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Throwable error caused by the Sidechat API
|
|
3
|
+
* @class
|
|
4
|
+
* @extends Error
|
|
5
|
+
*/
|
|
6
|
+
class SidechatAPIError extends Error {
|
|
7
|
+
constructor(message) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = "SidechatAPIError";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* API client class for making requests to Sidechat's private API. You'll need to [authenticate]{@tutorial Authentication} before using most of the methods.
|
|
15
|
+
* @class
|
|
16
|
+
* @since 2.0.0-alpha.0
|
|
17
|
+
*/
|
|
18
|
+
class SidechatAPIClient {
|
|
19
|
+
/**
|
|
20
|
+
* User bearer token
|
|
21
|
+
* @type {SidechatAuthToken}
|
|
22
|
+
* */
|
|
23
|
+
userToken;
|
|
24
|
+
/**
|
|
25
|
+
* Default headers for every API request
|
|
26
|
+
* @type {Object}
|
|
27
|
+
* @static
|
|
28
|
+
*/
|
|
29
|
+
defaultHeaders = {
|
|
30
|
+
Accept: "application/json",
|
|
31
|
+
"Content-Type": "application/json",
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Create a new instance of the API client
|
|
36
|
+
* @param {SidechatAuthToken} [token] - user bearer token
|
|
37
|
+
*/
|
|
38
|
+
constructor(token = "") {
|
|
39
|
+
if (token) {
|
|
40
|
+
this.userToken = token;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Manually set the currently signed in user's token. Generally try to avoid this and instead either pass a token to the constructor or login automatically through the auth functions
|
|
46
|
+
* @method
|
|
47
|
+
* @param {SidechatAuthToken} token - user bearer token
|
|
48
|
+
*/
|
|
49
|
+
setToken = (token) => {
|
|
50
|
+
this.userToken = token;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Initiate the login process with a phone number. Should be followed up with verifySMSCode().
|
|
55
|
+
* @method
|
|
56
|
+
* @param {Number} phoneNumber - US phone number (WITHOUT +1) to send verification code to
|
|
57
|
+
*/
|
|
58
|
+
loginViaSMS = async (phoneNumber) => {
|
|
59
|
+
try {
|
|
60
|
+
const res = await fetch(`https://api.sidechat.lol/v1/login_register`, {
|
|
61
|
+
method: "POST",
|
|
62
|
+
headers: this.defaultHeaders,
|
|
63
|
+
body: JSON.stringify({
|
|
64
|
+
phone_number: `+1${phoneNumber}`,
|
|
65
|
+
version: 3,
|
|
66
|
+
}),
|
|
67
|
+
});
|
|
68
|
+
const json = await res.json();
|
|
69
|
+
return json;
|
|
70
|
+
} catch (err) {
|
|
71
|
+
console.error(err);
|
|
72
|
+
throw new SidechatAPIError("Failed to request SMS verification.");
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Verify the code sent via SMS with loginViaSMS().
|
|
78
|
+
* @method
|
|
79
|
+
* @param {Number} phoneNumber - US phone number (WITHOUT +1) that verification code was sent to
|
|
80
|
+
* @param {String} code - the verification code
|
|
81
|
+
*/
|
|
82
|
+
verifySMSCode = async (phoneNumber, code) => {
|
|
83
|
+
try {
|
|
84
|
+
const res = await fetch(
|
|
85
|
+
`https://api.sidechat.lol/v1/verify_phone_number`,
|
|
86
|
+
{
|
|
87
|
+
method: "POST",
|
|
88
|
+
headers: this.defaultHeaders,
|
|
89
|
+
body: JSON.stringify({
|
|
90
|
+
phone_number: `+1${phoneNumber}`,
|
|
91
|
+
code: code.toUpperCase(),
|
|
92
|
+
}),
|
|
93
|
+
}
|
|
94
|
+
);
|
|
95
|
+
const json = await res.json();
|
|
96
|
+
if (json?.logged_in_user?.token) {
|
|
97
|
+
this.userToken = json.logged_in_user.token;
|
|
98
|
+
}
|
|
99
|
+
return json;
|
|
100
|
+
} catch (err) {
|
|
101
|
+
console.error(err);
|
|
102
|
+
throw new SidechatAPIError("Failed verify this code.");
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Set the user's age
|
|
108
|
+
* @method
|
|
109
|
+
* @param {Number} age - user's age in years
|
|
110
|
+
* @param {String} registrationID - the registration ID generated by verifySMSCode()
|
|
111
|
+
*/
|
|
112
|
+
setAge = async (age, registrationID) => {
|
|
113
|
+
if (age < 13) {
|
|
114
|
+
throw new SidechatAPIError("You're too young to use Offsides.");
|
|
115
|
+
}
|
|
116
|
+
try {
|
|
117
|
+
const res = await fetch(
|
|
118
|
+
`https://api.sidechat.lol/v1/complete_registration`,
|
|
119
|
+
{
|
|
120
|
+
method: "POST",
|
|
121
|
+
headers: this.defaultHeaders,
|
|
122
|
+
body: JSON.stringify({
|
|
123
|
+
age: Number(age),
|
|
124
|
+
registration_id: registrationID,
|
|
125
|
+
}),
|
|
126
|
+
}
|
|
127
|
+
);
|
|
128
|
+
const json = await res.json();
|
|
129
|
+
if (json.token) {
|
|
130
|
+
this.userToken = json.token;
|
|
131
|
+
}
|
|
132
|
+
return json;
|
|
133
|
+
} catch (err) {
|
|
134
|
+
console.error(err);
|
|
135
|
+
throw new SidechatAPIError("Failed verify this code.");
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Initiate the email setup process. Should be followed up with checkEmailVerification().
|
|
141
|
+
* @method
|
|
142
|
+
* @param {String} email - school email address to send verification code to
|
|
143
|
+
* @tutorial Email Registration
|
|
144
|
+
*/
|
|
145
|
+
registerEmail = async (email) => {
|
|
146
|
+
if (!this.userToken) {
|
|
147
|
+
throw new SidechatAPIError("User is not authenticated.");
|
|
148
|
+
}
|
|
149
|
+
try {
|
|
150
|
+
const res = await fetch(
|
|
151
|
+
`https://api.sidechat.lol/v2/users/register_email`,
|
|
152
|
+
{
|
|
153
|
+
method: "POST",
|
|
154
|
+
headers: {
|
|
155
|
+
...this.defaultHeaders,
|
|
156
|
+
Authorization: `Bearer ${this.userToken}`,
|
|
157
|
+
},
|
|
158
|
+
body: JSON.stringify({
|
|
159
|
+
email: email,
|
|
160
|
+
}),
|
|
161
|
+
}
|
|
162
|
+
);
|
|
163
|
+
const json = await res.json();
|
|
164
|
+
if (json.message) {
|
|
165
|
+
throw new SidechatAPIError(json.message);
|
|
166
|
+
}
|
|
167
|
+
return json;
|
|
168
|
+
} catch (err) {
|
|
169
|
+
console.error(error);
|
|
170
|
+
throw new SidechatAPIError("Failed to request email verification.");
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Check is the user's email is verified.
|
|
176
|
+
* @method
|
|
177
|
+
*/
|
|
178
|
+
checkEmailVerification = async () => {
|
|
179
|
+
if (!this.userToken) {
|
|
180
|
+
throw new SidechatAPIError("User is not authenticated.");
|
|
181
|
+
}
|
|
182
|
+
try {
|
|
183
|
+
const res = await fetch(
|
|
184
|
+
`https://api.sidechat.lol/v1/users/check_email_verified`,
|
|
185
|
+
{
|
|
186
|
+
method: "GET",
|
|
187
|
+
headers: {
|
|
188
|
+
...this.defaultHeaders,
|
|
189
|
+
Authorization: `Bearer ${this.userToken}`,
|
|
190
|
+
},
|
|
191
|
+
}
|
|
192
|
+
);
|
|
193
|
+
const json = await res.json();
|
|
194
|
+
if (json.verified_email_updates_response) {
|
|
195
|
+
return json.verified_email_updates_response;
|
|
196
|
+
} else {
|
|
197
|
+
throw new SidechatAPIError(json?.message || "Email is not verified.");
|
|
198
|
+
}
|
|
199
|
+
} catch (err) {
|
|
200
|
+
console.error(error);
|
|
201
|
+
throw new SidechatAPIError("Email is not verified.");
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Set the device ID of the current user
|
|
207
|
+
* @method
|
|
208
|
+
* @param {String} deviceId - the device ID to set
|
|
209
|
+
*/
|
|
210
|
+
setDeviceID = async (deviceID) => {
|
|
211
|
+
if (!this.userToken) {
|
|
212
|
+
throw new SidechatAPIError("User is not authenticated.");
|
|
213
|
+
}
|
|
214
|
+
try {
|
|
215
|
+
const res = await fetch(
|
|
216
|
+
`https://api.sidechat.lol/v1/register_device_token`,
|
|
217
|
+
{
|
|
218
|
+
method: "POST",
|
|
219
|
+
headers: {
|
|
220
|
+
...this.defaultHeaders,
|
|
221
|
+
Authorization: `Bearer ${this.userToken}`,
|
|
222
|
+
},
|
|
223
|
+
body: JSON.stringify({
|
|
224
|
+
build_type: "release",
|
|
225
|
+
bundle_id: "com.flowerave.sidechat",
|
|
226
|
+
device_token: deviceID,
|
|
227
|
+
}),
|
|
228
|
+
}
|
|
229
|
+
);
|
|
230
|
+
const json = await res.json();
|
|
231
|
+
await AsyncStorage.setItem("deviceID", deviceID);
|
|
232
|
+
return json;
|
|
233
|
+
} catch (err) {
|
|
234
|
+
console.error(err);
|
|
235
|
+
throw new SidechatAPIError("Failed verify this code.");
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Get updated status for user and group
|
|
241
|
+
* @method
|
|
242
|
+
* @param {String} [groupID] - ID of a specific group to retrieve info from
|
|
243
|
+
*/
|
|
244
|
+
getUserAndGroup = async (groupID = "") => {
|
|
245
|
+
if (!this.userToken) {
|
|
246
|
+
throw new SidechatAPIError("User is not authenticated.");
|
|
247
|
+
}
|
|
248
|
+
try {
|
|
249
|
+
const res = await fetch(
|
|
250
|
+
`https://api.sidechat.lol/v1/updates?group_id=${groupID}`,
|
|
251
|
+
{
|
|
252
|
+
method: "GET",
|
|
253
|
+
headers: {
|
|
254
|
+
...this.defaultHeaders,
|
|
255
|
+
Authorization: `Bearer ${this.userToken}`,
|
|
256
|
+
},
|
|
257
|
+
}
|
|
258
|
+
);
|
|
259
|
+
const json = await res.json();
|
|
260
|
+
return json;
|
|
261
|
+
} catch (err) {
|
|
262
|
+
console.error(err);
|
|
263
|
+
throw new SidechatAPIError(`Failed to get posts from group.`);
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Fetches posts from the specified category in a group
|
|
269
|
+
* @method
|
|
270
|
+
* @param {String} groupID - group ID
|
|
271
|
+
* @param {"hot"|"recent"|"top"} category - category to filter posts
|
|
272
|
+
* @param {SidechatCursorString} [cursor] - cursor string
|
|
273
|
+
* @returns {SidechatPostsAndCursor} List of posts and cursor
|
|
274
|
+
*/
|
|
275
|
+
getGroupPosts = async (groupID, category = "hot", cursor) => {
|
|
276
|
+
if (!this.userToken) {
|
|
277
|
+
throw new SidechatAPIError("User is not authenticated.");
|
|
278
|
+
}
|
|
279
|
+
try {
|
|
280
|
+
const res = await fetch(
|
|
281
|
+
`https://api.sidechat.lol/v1/posts?group_id=${groupID}&type=${category}${
|
|
282
|
+
cursor ? "&cursor=" + cursor : ""
|
|
283
|
+
}`,
|
|
284
|
+
{
|
|
285
|
+
method: "GET",
|
|
286
|
+
headers: {
|
|
287
|
+
...this.defaultHeaders,
|
|
288
|
+
Authorization: `Bearer ${this.userToken}`,
|
|
289
|
+
},
|
|
290
|
+
}
|
|
291
|
+
);
|
|
292
|
+
const json = await res.json();
|
|
293
|
+
return json;
|
|
294
|
+
} catch (err) {
|
|
295
|
+
console.error(err);
|
|
296
|
+
throw new SidechatAPIError(`Failed to get posts from group.`);
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Upvote or downvote, or unvote a post
|
|
302
|
+
* @method
|
|
303
|
+
* @param {String} postID - post ID to vote on
|
|
304
|
+
* @param {SidechatVoteString} action - whether to upvote, downvote, or reset vote
|
|
305
|
+
*/
|
|
306
|
+
setVote = async (postID, action) => {
|
|
307
|
+
if (!this.userToken) {
|
|
308
|
+
throw new SidechatAPIError("User is not authenticated.");
|
|
309
|
+
}
|
|
310
|
+
try {
|
|
311
|
+
const res = await fetch(`https://api.sidechat.lol/v1/posts/set_vote`, {
|
|
312
|
+
method: "POST",
|
|
313
|
+
headers: {
|
|
314
|
+
...this.defaultHeaders,
|
|
315
|
+
Authorization: `Bearer ${this.userToken}`,
|
|
316
|
+
},
|
|
317
|
+
body: JSON.stringify({
|
|
318
|
+
post_id: postID,
|
|
319
|
+
vote_status: action,
|
|
320
|
+
}),
|
|
321
|
+
});
|
|
322
|
+
const json = await res.json();
|
|
323
|
+
return json;
|
|
324
|
+
} catch (err) {
|
|
325
|
+
console.error(err);
|
|
326
|
+
throw new SidechatAPIError(`Failed to change the vote on post.`);
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Get all the commments on a post
|
|
332
|
+
* @method
|
|
333
|
+
* @param {String} postID - post ID to get comments for
|
|
334
|
+
* @returns {SidechatPostOrComment[]} list of comments
|
|
335
|
+
*/
|
|
336
|
+
getPostComments = async (postID) => {
|
|
337
|
+
if (!this.userToken) {
|
|
338
|
+
throw new SidechatAPIError("User is not authenticated.");
|
|
339
|
+
}
|
|
340
|
+
try {
|
|
341
|
+
const res = await fetch(
|
|
342
|
+
`https://api.sidechat.lol/v1/posts/comments/?post_id=${postID}`,
|
|
343
|
+
{
|
|
344
|
+
method: "GET",
|
|
345
|
+
headers: {
|
|
346
|
+
...this.defaultHeaders,
|
|
347
|
+
Authorization: `Bearer ${this.userToken}`,
|
|
348
|
+
},
|
|
349
|
+
}
|
|
350
|
+
);
|
|
351
|
+
const json = await res.json();
|
|
352
|
+
// Function to preprocess the comments and organize them into a nested structure
|
|
353
|
+
function preprocessComments(apiComments) {
|
|
354
|
+
// Map to store comments by their IDs for efficient lookup
|
|
355
|
+
const commentMap = new Map();
|
|
356
|
+
// List to store top-level comments
|
|
357
|
+
const topLevelComments = [];
|
|
358
|
+
|
|
359
|
+
// Iterate through the API comments
|
|
360
|
+
apiComments.forEach((comment) => {
|
|
361
|
+
// Store the comment in the map with its ID as the key
|
|
362
|
+
commentMap.set(comment.id, comment);
|
|
363
|
+
// Get the parent comment using the reply_post_id
|
|
364
|
+
const parentComment = commentMap.get(comment.reply_post_id);
|
|
365
|
+
// Check if the comment is a top-level comment
|
|
366
|
+
if (
|
|
367
|
+
!parentComment ||
|
|
368
|
+
comment.reply_post_id === comment.parent_post_id
|
|
369
|
+
) {
|
|
370
|
+
// If it's a top-level comment, push it to the topLevelComments array
|
|
371
|
+
topLevelComments.push(comment);
|
|
372
|
+
} else {
|
|
373
|
+
// If it's a reply, add it to the parent comment's replies array
|
|
374
|
+
if (!parentComment.replies) parentComment.replies = [];
|
|
375
|
+
parentComment.replies.push(comment);
|
|
376
|
+
}
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
// Flatten the nested structure and return a single list of comments
|
|
380
|
+
return flattenComments(topLevelComments);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// Function to flatten nested comments into a single list
|
|
384
|
+
function flattenComments(comments) {
|
|
385
|
+
// Use reduce to flatten the nested comments array into a single list
|
|
386
|
+
return comments.reduce((flatComments, comment) => {
|
|
387
|
+
// Push the current comment to the flatComments array
|
|
388
|
+
flatComments.push(comment);
|
|
389
|
+
// If the current comment has replies, recursively flatten them and push to the flatComments array
|
|
390
|
+
if (comment.replies)
|
|
391
|
+
flatComments.push(...flattenComments(comment.replies));
|
|
392
|
+
// Return the flatComments array
|
|
393
|
+
return flatComments;
|
|
394
|
+
}, []);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
const sortedComments = preprocessComments(json.posts);
|
|
398
|
+
return sortedComments;
|
|
399
|
+
} catch (err) {
|
|
400
|
+
console.error(err);
|
|
401
|
+
throw new SidechatAPIError(`Failed to get comments on post.`);
|
|
402
|
+
}
|
|
403
|
+
};
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* Gets groups to be displayed on the "Explore Groups" page
|
|
407
|
+
* @method
|
|
408
|
+
* @returns {SidechatGroup[]}
|
|
409
|
+
*/
|
|
410
|
+
getAvailableGroups = async () => {
|
|
411
|
+
if (!this.userToken) {
|
|
412
|
+
throw new SidechatAPIError("User is not authenticated.");
|
|
413
|
+
}
|
|
414
|
+
try {
|
|
415
|
+
const res = await fetch(`https://api.sidechat.lol/v1/groups/explore`, {
|
|
416
|
+
method: "GET",
|
|
417
|
+
headers: {
|
|
418
|
+
...this.defaultHeaders,
|
|
419
|
+
Authorization: `Bearer ${this.userToken}`,
|
|
420
|
+
},
|
|
421
|
+
});
|
|
422
|
+
const json = await res.json();
|
|
423
|
+
return await json.groups;
|
|
424
|
+
} catch (err) {
|
|
425
|
+
console.error(err);
|
|
426
|
+
throw new SidechatAPIError(`Failed to get groups from explore.`);
|
|
427
|
+
}
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
export { SidechatAPIClient, SidechatAPIError };
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
declare const _default: {};
|
|
2
|
+
export default _default;
|
|
1
3
|
/**
|
|
2
4
|
* A user-created post or comment, depending on the "type" prop
|
|
3
5
|
*/
|
|
4
|
-
type SidechatPostOrComment = {
|
|
6
|
+
export type SidechatPostOrComment = {
|
|
5
7
|
/**
|
|
6
8
|
* - whether this object represents a post or a comment on a post
|
|
7
9
|
*/
|
|
@@ -102,7 +104,7 @@ type SidechatPostOrComment = {
|
|
|
102
104
|
/**
|
|
103
105
|
* A group object, containing metadata about a group as well as its join conditions and states
|
|
104
106
|
*/
|
|
105
|
-
type SidechatGroup = {
|
|
107
|
+
export type SidechatGroup = {
|
|
106
108
|
/**
|
|
107
109
|
* - alphanumeric ID of group
|
|
108
110
|
*/
|
|
@@ -159,7 +161,7 @@ type SidechatGroup = {
|
|
|
159
161
|
/**
|
|
160
162
|
* An asset object, containing information about an image within a post or comment
|
|
161
163
|
*/
|
|
162
|
-
type SidechatAsset = {
|
|
164
|
+
export type SidechatAsset = {
|
|
163
165
|
/**
|
|
164
166
|
* - alphanumeric ID of asset
|
|
165
167
|
*/
|
|
@@ -188,7 +190,7 @@ type SidechatAsset = {
|
|
|
188
190
|
/**
|
|
189
191
|
* A user's identity information
|
|
190
192
|
*/
|
|
191
|
-
type SidechatIdentity = {
|
|
193
|
+
export type SidechatIdentity = {
|
|
192
194
|
/**
|
|
193
195
|
* - representation of user's name (varies across app)
|
|
194
196
|
*/
|
|
@@ -201,23 +203,23 @@ type SidechatIdentity = {
|
|
|
201
203
|
/**
|
|
202
204
|
* An asset URL that can only be accessed by making a request with a user's bearer token attached in the Authorization header
|
|
203
205
|
*/
|
|
204
|
-
type SidechatPrivateAssetURL = string;
|
|
206
|
+
export type SidechatPrivateAssetURL = string;
|
|
205
207
|
/**
|
|
206
208
|
* A cursor for paginating through post lists
|
|
207
209
|
*/
|
|
208
|
-
type SidechatCursorString = string;
|
|
210
|
+
export type SidechatCursorString = string;
|
|
209
211
|
/**
|
|
210
212
|
* A vote on a comment or post
|
|
211
213
|
*/
|
|
212
|
-
type SidechatVoteString = "upvote" | "downvote" | "none";
|
|
214
|
+
export type SidechatVoteString = "upvote" | "downvote" | "none";
|
|
213
215
|
/**
|
|
214
216
|
* A user's bearer authentication token needed for most requests
|
|
215
217
|
*/
|
|
216
|
-
type SidechatAuthToken = string;
|
|
218
|
+
export type SidechatAuthToken = string;
|
|
217
219
|
/**
|
|
218
220
|
* A list of posts and the associated cursor
|
|
219
221
|
*/
|
|
220
|
-
type SidechatPostsAndCursor = {
|
|
222
|
+
export type SidechatPostsAndCursor = {
|
|
221
223
|
posts: SidechatPostOrComment[];
|
|
222
224
|
cursor: SidechatCursorString;
|
|
223
225
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SidechatTypes.d.ts","sourceRoot":"","sources":["../../src/types/SidechatTypes.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"SidechatTypes.d.ts","sourceRoot":"","sources":["../../src/types/SidechatTypes.js"],"names":[],"mappings":";;;;;;;;;UAGU,MAAM,GAAC,SAAS;;;;;;;;;;;;;;;;;;;;WAKhB,aAAa;;;;;;;;;;;;;;;;iBAIb,kBAAkB;;;;YAClB,aAAa,EAAE;;;;;;;;;;;;;;;;cAIf,gBAAgB;;;;;;;;;;;;mBAGhB,WAAW,GAAC,eAAe;;;;kBAC3B,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBAcP,YAAY,GAAC,QAAQ;;;;;;;;qBAErB,MAAM,GAAC,QAAQ,GAAC,cAAc;;;;sBAC9B,SAAS,GAAC,eAAe,GAAC,mBAAmB;;;;8BAC7C,MAAM,GAAC,MAAM;;;;;;;;;;;;eAGb,uBAAuB;;;;;;;;;;;;;;;;;;;;;UASvB,OAAO;;;;kBACP,MAAM,GAAC,KAAK,GAAC,KAAK;;;;;;;;;;;;SAGlB,uBAAuB;;;;;;;;;;;;;;;;;;;;;;;;;;iCAsBpB,QAAQ,GAAC,UAAU,GAAC,MAAM;;;;;;;;;WAW7B,qBAAqB,EAAE;YACvB,oBAAoB"}
|
package/package.json
CHANGED
|
@@ -1,20 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sidechat.js",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.5",
|
|
4
4
|
"description": "A reverse-engineered API wrapper for Sidechat",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"source": "./src/index.js",
|
|
7
|
-
"
|
|
7
|
+
"main": "./dist/index.cjs",
|
|
8
|
+
"module": "./dist/index.mjs",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
8
10
|
"exports": {
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
+
".": {
|
|
12
|
+
"import": {
|
|
13
|
+
"types": "./dist/index.d.mts",
|
|
14
|
+
"default": "./dist/index.mjs"
|
|
15
|
+
},
|
|
16
|
+
"require": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"default": "./dist/index.cjs"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"./src/types": {
|
|
22
|
+
"types": "./dist/types/SidechatTypes.d.ts",
|
|
23
|
+
"default": "./src/types/SidechatTypes.js"
|
|
24
|
+
}
|
|
11
25
|
},
|
|
12
|
-
"unpkg": "./dist/sidechat.umd.js",
|
|
13
|
-
"main": "./dist/sidechat.cjs",
|
|
14
|
-
"module": "./dist/sidechat.module.js",
|
|
15
26
|
"scripts": {
|
|
16
|
-
"build": "
|
|
17
|
-
"dev": "
|
|
27
|
+
"build": "rollup -c",
|
|
28
|
+
"dev": "rollup -c --watch",
|
|
18
29
|
"docs": "jsdoc --configure jsdoc.json --verbose",
|
|
19
30
|
"prepublish": "tsc",
|
|
20
31
|
"typegen": "tsc"
|
|
@@ -34,11 +45,9 @@
|
|
|
34
45
|
},
|
|
35
46
|
"devDependencies": {
|
|
36
47
|
"clean-jsdoc-theme": "^4.2.17",
|
|
37
|
-
"microbundle": "^0.15.1",
|
|
38
48
|
"prompt-sync": "^4.2.0",
|
|
49
|
+
"rollup": "^2.79.1",
|
|
50
|
+
"rollup-plugin-copy": "^3.5.0",
|
|
39
51
|
"typescript": "^5.4.2"
|
|
40
|
-
},
|
|
41
|
-
"dependencies": {
|
|
42
|
-
"tsd-jsdoc": "^2.5.0"
|
|
43
52
|
}
|
|
44
53
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import copy from "rollup-plugin-copy";
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
input: "src/index.js",
|
|
5
|
+
output: [
|
|
6
|
+
{
|
|
7
|
+
file: "dist/index.cjs",
|
|
8
|
+
format: "cjs",
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
file: "dist/index.mjs",
|
|
12
|
+
format: "es",
|
|
13
|
+
},
|
|
14
|
+
],
|
|
15
|
+
plugins: [
|
|
16
|
+
// ...
|
|
17
|
+
copy({
|
|
18
|
+
targets: [
|
|
19
|
+
{ src: "dist/index.d.ts", dest: "dist", rename: "index.d.mts" },
|
|
20
|
+
],
|
|
21
|
+
}),
|
|
22
|
+
],
|
|
23
|
+
};
|
package/tsconfig.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
// Change this to match your project
|
|
3
|
-
"include": ["src/*.js"],
|
|
3
|
+
"include": ["src/*.js", "src/classes/*.js", "src/types/*.js"],
|
|
4
4
|
"exclude": ["src/tests", "src/internal"],
|
|
5
5
|
"compilerOptions": {
|
|
6
6
|
// Tells TypeScript to read JS files, as
|
|
@@ -13,6 +13,8 @@
|
|
|
13
13
|
"emitDeclarationOnly": true,
|
|
14
14
|
// go to js file when using IDE functions like
|
|
15
15
|
// "Go to Definition" in VSCode
|
|
16
|
-
"declarationMap": true
|
|
16
|
+
"declarationMap": true,
|
|
17
|
+
"declarationDir": "dist",
|
|
18
|
+
"outDir": "dist"
|
|
17
19
|
}
|
|
18
20
|
}
|
package/dist/sidechat.cjs
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
function e(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(t){}return(e=function(){return!!t})()}function t(){return t=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var o in r)Object.prototype.hasOwnProperty.call(r,o)&&(e[o]=r[o])}return e},t.apply(this,arguments)}function r(e){return r=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},r(e)}function o(e,t){return o=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},o(e,t)}function n(t){var i="function"==typeof Map?new Map:void 0;return n=function(t){if(null===t||!function(e){try{return-1!==Function.toString.call(e).indexOf("[native code]")}catch(t){return"function"==typeof e}}(t))return t;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==i){if(i.has(t))return i.get(t);i.set(t,n)}function n(){return function(t,r,n){if(e())return Reflect.construct.apply(null,arguments);var i=[null];i.push.apply(i,r);var s=new(t.bind.apply(t,i));return n&&o(s,n.prototype),s}(t,arguments,r(this).constructor)}return n.prototype=Object.create(t.prototype,{constructor:{value:n,enumerable:!1,writable:!0,configurable:!0}}),o(n,t)},n(t)}var i=/*#__PURE__*/function(e){var t,r;function n(t){var r;return(r=e.call(this,t)||this).name="SidechatAPIError",r}return r=e,(t=n).prototype=Object.create(r.prototype),t.prototype.constructor=t,o(t,r),n}(/*#__PURE__*/n(Error));function s(e,t){try{var r=e()}catch(e){return t(e)}return r&&r.then?r.then(void 0,t):r}exports.SidechatAPIClient=function(e){var r=this,o=this,n=this,u=this,c=this,a=this,h=this,l=this,f=this,d=this,p=this,v=this;void 0===e&&(e=""),this.defaultHeaders={Accept:"application/json","Content-Type":"application/json"},this.setToken=function(e){v.userToken=e},this.loginViaSMS=function(e){try{return Promise.resolve(s(function(){return Promise.resolve(fetch("https://api.sidechat.lol/v1/login_register",{method:"POST",headers:r.defaultHeaders,body:JSON.stringify({phone_number:"+1"+e,version:3})})).then(function(e){return Promise.resolve(e.json())})},function(e){throw console.error(e),new i("Failed to request SMS verification.")}))}catch(e){return Promise.reject(e)}},this.verifySMSCode=function(e,t){try{return Promise.resolve(s(function(){return Promise.resolve(fetch("https://api.sidechat.lol/v1/verify_phone_number",{method:"POST",headers:o.defaultHeaders,body:JSON.stringify({phone_number:"+1"+e,code:t.toUpperCase()})})).then(function(e){return Promise.resolve(e.json()).then(function(e){var t;return null!=e&&null!=(t=e.logged_in_user)&&t.token&&(o.userToken=e.logged_in_user.token),e})})},function(e){throw console.error(e),new i("Failed verify this code.")}))}catch(e){return Promise.reject(e)}},this.setAge=function(e,t){try{if(e<13)throw new i("You're too young to use Offsides.");return Promise.resolve(s(function(){return Promise.resolve(fetch("https://api.sidechat.lol/v1/complete_registration",{method:"POST",headers:n.defaultHeaders,body:JSON.stringify({age:Number(e),registration_id:t})})).then(function(e){return Promise.resolve(e.json()).then(function(e){return e.token&&(n.userToken=e.token),e})})},function(e){throw console.error(e),new i("Failed verify this code.")}))}catch(e){return Promise.reject(e)}},this.registerEmail=function(e){try{if(!u.userToken)throw new i("User is not authenticated.");return Promise.resolve(s(function(){return Promise.resolve(fetch("https://api.sidechat.lol/v2/users/register_email",{method:"POST",headers:t({},u.defaultHeaders,{Authorization:"Bearer "+u.userToken}),body:JSON.stringify({email:e})})).then(function(e){return Promise.resolve(e.json()).then(function(e){if(e.message)throw new i(e.message);return e})})},function(){throw console.error(error),new i("Failed to request email verification.")}))}catch(e){return Promise.reject(e)}},this.checkEmailVerification=function(){try{if(!c.userToken)throw new i("User is not authenticated.");return Promise.resolve(s(function(){return Promise.resolve(fetch("https://api.sidechat.lol/v1/users/check_email_verified",{method:"GET",headers:t({},c.defaultHeaders,{Authorization:"Bearer "+c.userToken})})).then(function(e){return Promise.resolve(e.json()).then(function(e){if(e.verified_email_updates_response)return e.verified_email_updates_response;throw new i((null==e?void 0:e.message)||"Email is not verified.")})})},function(){throw console.error(error),new i("Email is not verified.")}))}catch(e){return Promise.reject(e)}},this.setDeviceID=function(e){try{if(!a.userToken)throw new i("User is not authenticated.");return Promise.resolve(s(function(){return Promise.resolve(fetch("https://api.sidechat.lol/v1/register_device_token",{method:"POST",headers:t({},a.defaultHeaders,{Authorization:"Bearer "+a.userToken}),body:JSON.stringify({build_type:"release",bundle_id:"com.flowerave.sidechat",device_token:e})})).then(function(t){return Promise.resolve(t.json()).then(function(t){return Promise.resolve(AsyncStorage.setItem("deviceID",e)).then(function(){return t})})})},function(e){throw console.error(e),new i("Failed verify this code.")}))}catch(e){return Promise.reject(e)}},this.getUserAndGroup=function(e){void 0===e&&(e="");try{if(!h.userToken)throw new i("User is not authenticated.");return Promise.resolve(s(function(){return Promise.resolve(fetch("https://api.sidechat.lol/v1/updates?group_id="+e,{method:"GET",headers:t({},h.defaultHeaders,{Authorization:"Bearer "+h.userToken})})).then(function(e){return Promise.resolve(e.json())})},function(e){throw console.error(e),new i("Failed to get posts from group.")}))}catch(e){return Promise.reject(e)}},this.getGroupPosts=function(e,r,o){void 0===r&&(r="hot");try{if(!l.userToken)throw new i("User is not authenticated.");return Promise.resolve(s(function(){return Promise.resolve(fetch("https://api.sidechat.lol/v1/posts?group_id="+e+"&type="+r+(o?"&cursor="+o:""),{method:"GET",headers:t({},l.defaultHeaders,{Authorization:"Bearer "+l.userToken})})).then(function(e){return Promise.resolve(e.json())})},function(e){throw console.error(e),new i("Failed to get posts from group.")}))}catch(e){return Promise.reject(e)}},this.setVote=function(e,r){try{if(!f.userToken)throw new i("User is not authenticated.");return Promise.resolve(s(function(){return Promise.resolve(fetch("https://api.sidechat.lol/v1/posts/set_vote",{method:"POST",headers:t({},f.defaultHeaders,{Authorization:"Bearer "+f.userToken}),body:JSON.stringify({post_id:e,vote_status:r})})).then(function(e){return Promise.resolve(e.json())})},function(e){throw console.error(e),new i("Failed to change the vote on post.")}))}catch(e){return Promise.reject(e)}},this.getPostComments=function(e){try{if(!d.userToken)throw new i("User is not authenticated.");return Promise.resolve(s(function(){return Promise.resolve(fetch("https://api.sidechat.lol/v1/posts/comments/?post_id="+e,{method:"GET",headers:t({},d.defaultHeaders,{Authorization:"Bearer "+d.userToken})})).then(function(e){function t(e){var t=new Map,o=[];return e.forEach(function(e){t.set(e.id,e);var r=t.get(e.reply_post_id);r&&e.reply_post_id!==e.parent_post_id?(r.replies||(r.replies=[]),r.replies.push(e)):o.push(e)}),r(o)}function r(e){return e.reduce(function(e,t){return e.push(t),t.replies&&e.push.apply(e,r(t.replies)),e},[])}return Promise.resolve(e.json()).then(function(e){return t(e.posts)})})},function(e){throw console.error(e),new i("Failed to get comments on post.")}))}catch(e){return Promise.reject(e)}},this.getAvailableGroups=function(){try{if(!p.userToken)throw new i("User is not authenticated.");return Promise.resolve(s(function(){return Promise.resolve(fetch("https://api.sidechat.lol/v1/groups/explore",{method:"GET",headers:t({},p.defaultHeaders,{Authorization:"Bearer "+p.userToken})})).then(function(e){return Promise.resolve(e.json()).then(function(e){return Promise.resolve(e.groups)})})},function(e){throw console.error(e),new i("Failed to get groups from explore.")}))}catch(e){return Promise.reject(e)}},e&&(this.userToken=e)},exports.SidechatAPIError=i;
|
|
2
|
-
//# sourceMappingURL=sidechat.cjs.map
|