sidechat.js 2.0.2 → 2.0.4

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.
Files changed (54) hide show
  1. package/dist/index.cjs +436 -0
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.mjs +431 -0
  4. package/package.json +18 -13
  5. package/rollup.config.mjs +23 -0
  6. package/tsconfig.json +4 -2
  7. package/dist/sidechat.cjs +0 -2
  8. package/dist/sidechat.cjs.map +0 -1
  9. package/dist/sidechat.modern.js +0 -2
  10. package/dist/sidechat.modern.js.map +0 -1
  11. package/dist/sidechat.module.js +0 -2
  12. package/dist/sidechat.module.js.map +0 -1
  13. package/dist/sidechat.umd.js +0 -2
  14. package/dist/sidechat.umd.js.map +0 -1
  15. package/src/classes/SidechatAPIClient.d.ts +0 -104
  16. package/src/classes/SidechatAPIClient.d.ts.map +0 -1
  17. package/src/classes/SidechatAPIError.d.ts +0 -10
  18. package/src/classes/SidechatAPIError.d.ts.map +0 -1
  19. package/src/index.d.ts.map +0 -1
  20. package/src/types/SidechatTypes.d.ts +0 -224
  21. package/src/types/SidechatTypes.d.ts.map +0 -1
  22. /package/{src/index.d.ts → dist/index.d.mts} +0 -0
  23. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/SidechatAPIClient.html +0 -0
  24. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/SidechatAPIError.html +0 -0
  25. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/classes_SidechatAPIClient.js.html +0 -0
  26. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/classes_SidechatAPIError.js.html +0 -0
  27. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/data/search.json +0 -0
  28. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/fonts/Inconsolata-Regular.ttf +0 -0
  29. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/fonts/OpenSans-Regular.ttf +0 -0
  30. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/fonts/WorkSans-Bold.ttf +0 -0
  31. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/global.html +0 -0
  32. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/index.html +0 -0
  33. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/core.js +0 -0
  34. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/core.min.js +0 -0
  35. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/resize.js +0 -0
  36. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/search.js +0 -0
  37. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/search.min.js +0 -0
  38. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/third-party/Apache-License-2.0.txt +0 -0
  39. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/third-party/fuse.js +0 -0
  40. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/third-party/hljs-line-num-original.js +0 -0
  41. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/third-party/hljs-line-num.js +0 -0
  42. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/third-party/hljs-original.js +0 -0
  43. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/third-party/hljs.js +0 -0
  44. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/third-party/popper.js +0 -0
  45. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/third-party/tippy.js +0 -0
  46. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/third-party/tocbot.js +0 -0
  47. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/scripts/third-party/tocbot.min.js +0 -0
  48. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/styles/clean-jsdoc-theme-base.css +0 -0
  49. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/styles/clean-jsdoc-theme-dark.css +0 -0
  50. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/styles/clean-jsdoc-theme-light.css +0 -0
  51. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/styles/clean-jsdoc-theme.min.css +0 -0
  52. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/tutorial-Authentication.html +0 -0
  53. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/tutorial-Email Registration.html +0 -0
  54. /package/docs/sidechat.js/{2.0.0 → 2.0.4}/types_SidechatTypes.js.html +0 -0
package/dist/index.cjs ADDED
@@ -0,0 +1,436 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ /**
6
+ * Throwable error caused by the Sidechat API
7
+ * @class
8
+ * @extends Error
9
+ */
10
+ class SidechatAPIError extends Error {
11
+ constructor(message) {
12
+ super(message);
13
+ this.name = "SidechatAPIError";
14
+ }
15
+ }
16
+
17
+ /**
18
+ * API client class for making requests to Sidechat's private API. You'll need to [authenticate]{@tutorial Authentication} before using most of the methods.
19
+ * @class
20
+ * @since 2.0.0-alpha.0
21
+ */
22
+ class SidechatAPIClient {
23
+ /**
24
+ * User bearer token
25
+ * @type {SidechatAuthToken}
26
+ * */
27
+ userToken;
28
+ /**
29
+ * Default headers for every API request
30
+ * @type {Object}
31
+ * @static
32
+ */
33
+ defaultHeaders = {
34
+ Accept: "application/json",
35
+ "Content-Type": "application/json",
36
+ };
37
+
38
+ /**
39
+ * Create a new instance of the API client
40
+ * @param {SidechatAuthToken} [token] - user bearer token
41
+ */
42
+ constructor(token = "") {
43
+ if (token) {
44
+ this.userToken = token;
45
+ }
46
+ }
47
+
48
+ /**
49
+ * 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
50
+ * @method
51
+ * @param {SidechatAuthToken} token - user bearer token
52
+ */
53
+ setToken = (token) => {
54
+ this.userToken = token;
55
+ };
56
+
57
+ /**
58
+ * Initiate the login process with a phone number. Should be followed up with verifySMSCode().
59
+ * @method
60
+ * @param {Number} phoneNumber - US phone number (WITHOUT +1) to send verification code to
61
+ */
62
+ loginViaSMS = async (phoneNumber) => {
63
+ try {
64
+ const res = await fetch(`https://api.sidechat.lol/v1/login_register`, {
65
+ method: "POST",
66
+ headers: this.defaultHeaders,
67
+ body: JSON.stringify({
68
+ phone_number: `+1${phoneNumber}`,
69
+ version: 3,
70
+ }),
71
+ });
72
+ const json = await res.json();
73
+ return json;
74
+ } catch (err) {
75
+ console.error(err);
76
+ throw new SidechatAPIError("Failed to request SMS verification.");
77
+ }
78
+ };
79
+
80
+ /**
81
+ * Verify the code sent via SMS with loginViaSMS().
82
+ * @method
83
+ * @param {Number} phoneNumber - US phone number (WITHOUT +1) that verification code was sent to
84
+ * @param {String} code - the verification code
85
+ */
86
+ verifySMSCode = async (phoneNumber, code) => {
87
+ try {
88
+ const res = await fetch(
89
+ `https://api.sidechat.lol/v1/verify_phone_number`,
90
+ {
91
+ method: "POST",
92
+ headers: this.defaultHeaders,
93
+ body: JSON.stringify({
94
+ phone_number: `+1${phoneNumber}`,
95
+ code: code.toUpperCase(),
96
+ }),
97
+ }
98
+ );
99
+ const json = await res.json();
100
+ if (json?.logged_in_user?.token) {
101
+ this.userToken = json.logged_in_user.token;
102
+ }
103
+ return json;
104
+ } catch (err) {
105
+ console.error(err);
106
+ throw new SidechatAPIError("Failed verify this code.");
107
+ }
108
+ };
109
+
110
+ /**
111
+ * Set the user's age
112
+ * @method
113
+ * @param {Number} age - user's age in years
114
+ * @param {String} registrationID - the registration ID generated by verifySMSCode()
115
+ */
116
+ setAge = async (age, registrationID) => {
117
+ if (age < 13) {
118
+ throw new SidechatAPIError("You're too young to use Offsides.");
119
+ }
120
+ try {
121
+ const res = await fetch(
122
+ `https://api.sidechat.lol/v1/complete_registration`,
123
+ {
124
+ method: "POST",
125
+ headers: this.defaultHeaders,
126
+ body: JSON.stringify({
127
+ age: Number(age),
128
+ registration_id: registrationID,
129
+ }),
130
+ }
131
+ );
132
+ const json = await res.json();
133
+ if (json.token) {
134
+ this.userToken = json.token;
135
+ }
136
+ return json;
137
+ } catch (err) {
138
+ console.error(err);
139
+ throw new SidechatAPIError("Failed verify this code.");
140
+ }
141
+ };
142
+
143
+ /**
144
+ * Initiate the email setup process. Should be followed up with checkEmailVerification().
145
+ * @method
146
+ * @param {String} email - school email address to send verification code to
147
+ * @tutorial Email Registration
148
+ */
149
+ registerEmail = async (email) => {
150
+ if (!this.userToken) {
151
+ throw new SidechatAPIError("User is not authenticated.");
152
+ }
153
+ try {
154
+ const res = await fetch(
155
+ `https://api.sidechat.lol/v2/users/register_email`,
156
+ {
157
+ method: "POST",
158
+ headers: {
159
+ ...this.defaultHeaders,
160
+ Authorization: `Bearer ${this.userToken}`,
161
+ },
162
+ body: JSON.stringify({
163
+ email: email,
164
+ }),
165
+ }
166
+ );
167
+ const json = await res.json();
168
+ if (json.message) {
169
+ throw new SidechatAPIError(json.message);
170
+ }
171
+ return json;
172
+ } catch (err) {
173
+ console.error(error);
174
+ throw new SidechatAPIError("Failed to request email verification.");
175
+ }
176
+ };
177
+
178
+ /**
179
+ * Check is the user's email is verified.
180
+ * @method
181
+ */
182
+ checkEmailVerification = async () => {
183
+ if (!this.userToken) {
184
+ throw new SidechatAPIError("User is not authenticated.");
185
+ }
186
+ try {
187
+ const res = await fetch(
188
+ `https://api.sidechat.lol/v1/users/check_email_verified`,
189
+ {
190
+ method: "GET",
191
+ headers: {
192
+ ...this.defaultHeaders,
193
+ Authorization: `Bearer ${this.userToken}`,
194
+ },
195
+ }
196
+ );
197
+ const json = await res.json();
198
+ if (json.verified_email_updates_response) {
199
+ return json.verified_email_updates_response;
200
+ } else {
201
+ throw new SidechatAPIError(json?.message || "Email is not verified.");
202
+ }
203
+ } catch (err) {
204
+ console.error(error);
205
+ throw new SidechatAPIError("Email is not verified.");
206
+ }
207
+ };
208
+
209
+ /**
210
+ * Set the device ID of the current user
211
+ * @method
212
+ * @param {String} deviceId - the device ID to set
213
+ */
214
+ setDeviceID = async (deviceID) => {
215
+ if (!this.userToken) {
216
+ throw new SidechatAPIError("User is not authenticated.");
217
+ }
218
+ try {
219
+ const res = await fetch(
220
+ `https://api.sidechat.lol/v1/register_device_token`,
221
+ {
222
+ method: "POST",
223
+ headers: {
224
+ ...this.defaultHeaders,
225
+ Authorization: `Bearer ${this.userToken}`,
226
+ },
227
+ body: JSON.stringify({
228
+ build_type: "release",
229
+ bundle_id: "com.flowerave.sidechat",
230
+ device_token: deviceID,
231
+ }),
232
+ }
233
+ );
234
+ const json = await res.json();
235
+ await AsyncStorage.setItem("deviceID", deviceID);
236
+ return json;
237
+ } catch (err) {
238
+ console.error(err);
239
+ throw new SidechatAPIError("Failed verify this code.");
240
+ }
241
+ };
242
+
243
+ /**
244
+ * Get updated status for user and group
245
+ * @method
246
+ * @param {String} [groupID] - ID of a specific group to retrieve info from
247
+ */
248
+ getUserAndGroup = async (groupID = "") => {
249
+ if (!this.userToken) {
250
+ throw new SidechatAPIError("User is not authenticated.");
251
+ }
252
+ try {
253
+ const res = await fetch(
254
+ `https://api.sidechat.lol/v1/updates?group_id=${groupID}`,
255
+ {
256
+ method: "GET",
257
+ headers: {
258
+ ...this.defaultHeaders,
259
+ Authorization: `Bearer ${this.userToken}`,
260
+ },
261
+ }
262
+ );
263
+ const json = await res.json();
264
+ return json;
265
+ } catch (err) {
266
+ console.error(err);
267
+ throw new SidechatAPIError(`Failed to get posts from group.`);
268
+ }
269
+ };
270
+
271
+ /**
272
+ * Fetches posts from the specified category in a group
273
+ * @method
274
+ * @param {String} groupID - group ID
275
+ * @param {"hot"|"recent"|"top"} category - category to filter posts
276
+ * @param {SidechatCursorString} [cursor] - cursor string
277
+ * @returns {SidechatPostsAndCursor} List of posts and cursor
278
+ */
279
+ getGroupPosts = async (groupID, category = "hot", cursor) => {
280
+ if (!this.userToken) {
281
+ throw new SidechatAPIError("User is not authenticated.");
282
+ }
283
+ try {
284
+ const res = await fetch(
285
+ `https://api.sidechat.lol/v1/posts?group_id=${groupID}&type=${category}${
286
+ cursor ? "&cursor=" + cursor : ""
287
+ }`,
288
+ {
289
+ method: "GET",
290
+ headers: {
291
+ ...this.defaultHeaders,
292
+ Authorization: `Bearer ${this.userToken}`,
293
+ },
294
+ }
295
+ );
296
+ const json = await res.json();
297
+ return json;
298
+ } catch (err) {
299
+ console.error(err);
300
+ throw new SidechatAPIError(`Failed to get posts from group.`);
301
+ }
302
+ };
303
+
304
+ /**
305
+ * Upvote or downvote, or unvote a post
306
+ * @method
307
+ * @param {String} postID - post ID to vote on
308
+ * @param {SidechatVoteString} action - whether to upvote, downvote, or reset vote
309
+ */
310
+ setVote = async (postID, action) => {
311
+ if (!this.userToken) {
312
+ throw new SidechatAPIError("User is not authenticated.");
313
+ }
314
+ try {
315
+ const res = await fetch(`https://api.sidechat.lol/v1/posts/set_vote`, {
316
+ method: "POST",
317
+ headers: {
318
+ ...this.defaultHeaders,
319
+ Authorization: `Bearer ${this.userToken}`,
320
+ },
321
+ body: JSON.stringify({
322
+ post_id: postID,
323
+ vote_status: action,
324
+ }),
325
+ });
326
+ const json = await res.json();
327
+ return json;
328
+ } catch (err) {
329
+ console.error(err);
330
+ throw new SidechatAPIError(`Failed to change the vote on post.`);
331
+ }
332
+ };
333
+
334
+ /**
335
+ * Get all the commments on a post
336
+ * @method
337
+ * @param {String} postID - post ID to get comments for
338
+ * @returns {SidechatPostOrComment[]} list of comments
339
+ */
340
+ getPostComments = async (postID) => {
341
+ if (!this.userToken) {
342
+ throw new SidechatAPIError("User is not authenticated.");
343
+ }
344
+ try {
345
+ const res = await fetch(
346
+ `https://api.sidechat.lol/v1/posts/comments/?post_id=${postID}`,
347
+ {
348
+ method: "GET",
349
+ headers: {
350
+ ...this.defaultHeaders,
351
+ Authorization: `Bearer ${this.userToken}`,
352
+ },
353
+ }
354
+ );
355
+ const json = await res.json();
356
+ // Function to preprocess the comments and organize them into a nested structure
357
+ function preprocessComments(apiComments) {
358
+ // Map to store comments by their IDs for efficient lookup
359
+ const commentMap = new Map();
360
+ // List to store top-level comments
361
+ const topLevelComments = [];
362
+
363
+ // Iterate through the API comments
364
+ apiComments.forEach((comment) => {
365
+ // Store the comment in the map with its ID as the key
366
+ commentMap.set(comment.id, comment);
367
+ // Get the parent comment using the reply_post_id
368
+ const parentComment = commentMap.get(comment.reply_post_id);
369
+ // Check if the comment is a top-level comment
370
+ if (
371
+ !parentComment ||
372
+ comment.reply_post_id === comment.parent_post_id
373
+ ) {
374
+ // If it's a top-level comment, push it to the topLevelComments array
375
+ topLevelComments.push(comment);
376
+ } else {
377
+ // If it's a reply, add it to the parent comment's replies array
378
+ if (!parentComment.replies) parentComment.replies = [];
379
+ parentComment.replies.push(comment);
380
+ }
381
+ });
382
+
383
+ // Flatten the nested structure and return a single list of comments
384
+ return flattenComments(topLevelComments);
385
+ }
386
+
387
+ // Function to flatten nested comments into a single list
388
+ function flattenComments(comments) {
389
+ // Use reduce to flatten the nested comments array into a single list
390
+ return comments.reduce((flatComments, comment) => {
391
+ // Push the current comment to the flatComments array
392
+ flatComments.push(comment);
393
+ // If the current comment has replies, recursively flatten them and push to the flatComments array
394
+ if (comment.replies)
395
+ flatComments.push(...flattenComments(comment.replies));
396
+ // Return the flatComments array
397
+ return flatComments;
398
+ }, []);
399
+ }
400
+
401
+ const sortedComments = preprocessComments(json.posts);
402
+ return sortedComments;
403
+ } catch (err) {
404
+ console.error(err);
405
+ throw new SidechatAPIError(`Failed to get comments on post.`);
406
+ }
407
+ };
408
+
409
+ /**
410
+ * Gets groups to be displayed on the "Explore Groups" page
411
+ * @method
412
+ * @returns {SidechatGroup[]}
413
+ */
414
+ getAvailableGroups = async () => {
415
+ if (!this.userToken) {
416
+ throw new SidechatAPIError("User is not authenticated.");
417
+ }
418
+ try {
419
+ const res = await fetch(`https://api.sidechat.lol/v1/groups/explore`, {
420
+ method: "GET",
421
+ headers: {
422
+ ...this.defaultHeaders,
423
+ Authorization: `Bearer ${this.userToken}`,
424
+ },
425
+ });
426
+ const json = await res.json();
427
+ return await json.groups;
428
+ } catch (err) {
429
+ console.error(err);
430
+ throw new SidechatAPIError(`Failed to get groups from explore.`);
431
+ }
432
+ };
433
+ }
434
+
435
+ exports.SidechatAPIClient = SidechatAPIClient;
436
+ exports.SidechatAPIError = SidechatAPIError;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":"8BAA8B,gCAAgC;6BACjC,+BAA+B"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":"8BAC8B,gCAAgC;6BACjC,+BAA+B"}