@rimori/client 2.3.0-next.1 → 2.3.0-next.3

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.
@@ -1,4 +1,5 @@
1
1
  import { SupabaseClient } from '@supabase/supabase-js';
2
+ import { RimoriClient } from '../plugin/RimoriClient';
2
3
  export type TriggerAction = {
3
4
  action_key: string;
4
5
  } & Record<string, string | number | boolean>;
@@ -25,7 +26,8 @@ export interface Exercise {
25
26
  }
26
27
  export declare class ExerciseController {
27
28
  private supabase;
28
- constructor(supabase: SupabaseClient);
29
+ private rimoriClient;
30
+ constructor(supabase: SupabaseClient, rimoriClient: RimoriClient);
29
31
  /**
30
32
  * Fetches weekly exercises from the weekly_exercises view.
31
33
  * Shows exercises for the current week that haven't expired.
@@ -33,13 +35,14 @@ export declare class ExerciseController {
33
35
  */
34
36
  viewWeeklyExercises(): Promise<Exercise[]>;
35
37
  /**
36
- * Creates a new exercise via the backend API.
38
+ * Creates multiple exercises via the backend API.
39
+ * All requests are made in parallel but only one event is emitted.
37
40
  * @param token The token to use for authentication.
38
41
  * @param backendUrl The URL of the backend API.
39
- * @param params Exercise creation parameters.
40
- * @returns Created exercise object.
42
+ * @param exercises Exercise creation parameters.
43
+ * @returns Created exercise objects.
41
44
  */
42
- addExercise(token: string, backendUrl: string, params: CreateExerciseParams): Promise<Exercise>;
45
+ addExercise(token: string, backendUrl: string, exercises: CreateExerciseParams[]): Promise<Exercise[]>;
43
46
  /**
44
47
  * Deletes an exercise via the backend API.
45
48
  * @param token The token to use for authentication.
@@ -8,8 +8,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  export class ExerciseController {
11
- constructor(supabase) {
11
+ constructor(supabase, rimoriClient) {
12
12
  this.supabase = supabase;
13
+ this.rimoriClient = rimoriClient;
13
14
  }
14
15
  /**
15
16
  * Fetches weekly exercises from the weekly_exercises view.
@@ -26,27 +27,32 @@ export class ExerciseController {
26
27
  });
27
28
  }
28
29
  /**
29
- * Creates a new exercise via the backend API.
30
+ * Creates multiple exercises via the backend API.
31
+ * All requests are made in parallel but only one event is emitted.
30
32
  * @param token The token to use for authentication.
31
33
  * @param backendUrl The URL of the backend API.
32
- * @param params Exercise creation parameters.
33
- * @returns Created exercise object.
34
+ * @param exercises Exercise creation parameters.
35
+ * @returns Created exercise objects.
34
36
  */
35
- addExercise(token, backendUrl, params) {
37
+ addExercise(token, backendUrl, exercises) {
36
38
  return __awaiter(this, void 0, void 0, function* () {
37
- const response = yield fetch(`${backendUrl}/exercises`, {
38
- method: 'POST',
39
- headers: {
40
- 'Content-Type': 'application/json',
41
- Authorization: `Bearer ${token}`,
42
- },
43
- body: JSON.stringify(params),
44
- });
45
- if (!response.ok) {
46
- const errorText = yield response.text();
47
- throw new Error(`Failed to create exercise: ${errorText}`);
48
- }
49
- return yield response.json();
39
+ const responses = yield Promise.all(exercises.map((exercise) => __awaiter(this, void 0, void 0, function* () {
40
+ const response = yield fetch(`${backendUrl}/exercises`, {
41
+ method: 'POST',
42
+ headers: {
43
+ 'Content-Type': 'application/json',
44
+ Authorization: `Bearer ${token}`,
45
+ },
46
+ body: JSON.stringify(exercise),
47
+ });
48
+ if (!response.ok) {
49
+ const errorText = yield response.text();
50
+ throw new Error(`Failed to create exercise: ${errorText}`);
51
+ }
52
+ return yield response.json();
53
+ })));
54
+ this.rimoriClient.event.emit('global.exercises.triggerChange');
55
+ return responses;
50
56
  });
51
57
  }
52
58
  /**
@@ -68,6 +74,7 @@ export class ExerciseController {
68
74
  const errorText = yield response.text();
69
75
  throw new Error(`Failed to delete exercise: ${errorText}`);
70
76
  }
77
+ this.rimoriClient.event.emit('global.exercises.triggerChange');
71
78
  return yield response.json();
72
79
  });
73
80
  }
@@ -253,11 +253,12 @@ export declare class RimoriClient {
253
253
  */
254
254
  view: () => Promise<import("../controller/ExerciseController").Exercise[]>;
255
255
  /**
256
- * Creates a new exercise via the backend API.
257
- * @param params Exercise creation parameters.
258
- * @returns Created exercise object.
256
+ * Creates a new exercise or multiple exercises via the backend API.
257
+ * When creating multiple exercises, all requests are made in parallel but only one event is emitted.
258
+ * @param params Exercise creation parameters (single or array).
259
+ * @returns Created exercise objects.
259
260
  */
260
- add: (params: CreateExerciseParams) => Promise<import("../controller/ExerciseController").Exercise>;
261
+ add: (params: CreateExerciseParams | CreateExerciseParams[]) => Promise<import("../controller/ExerciseController").Exercise[]>;
261
262
  /**
262
263
  * Deletes an exercise via the backend API.
263
264
  * @param id The exercise ID to delete.
@@ -250,14 +250,16 @@ export class RimoriClient {
250
250
  return this.exerciseController.viewWeeklyExercises();
251
251
  }),
252
252
  /**
253
- * Creates a new exercise via the backend API.
254
- * @param params Exercise creation parameters.
255
- * @returns Created exercise object.
253
+ * Creates a new exercise or multiple exercises via the backend API.
254
+ * When creating multiple exercises, all requests are made in parallel but only one event is emitted.
255
+ * @param params Exercise creation parameters (single or array).
256
+ * @returns Created exercise objects.
256
257
  */
257
258
  add: (params) => __awaiter(this, void 0, void 0, function* () {
258
259
  const token = yield this.pluginController.getToken();
259
260
  const backendUrl = this.pluginController.getBackendUrl();
260
- return this.exerciseController.addExercise(token, backendUrl, params);
261
+ const exercises = Array.isArray(params) ? params : [params];
262
+ return this.exerciseController.addExercise(token, backendUrl, exercises);
261
263
  }),
262
264
  /**
263
265
  * Deletes an exercise via the backend API.
@@ -273,7 +275,7 @@ export class RimoriClient {
273
275
  this.rimoriInfo = info;
274
276
  this.superbase = supabase;
275
277
  this.pluginController = controller;
276
- this.exerciseController = new ExerciseController(supabase);
278
+ this.exerciseController = new ExerciseController(supabase, this);
277
279
  this.accomplishmentHandler = new AccomplishmentController(info.pluginId);
278
280
  this.settingsController = new SettingsController(supabase, info.pluginId, info.guild);
279
281
  this.sharedContentController = new SharedContentController(supabase, this);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rimori/client",
3
- "version": "2.3.0-next.1",
3
+ "version": "2.3.0-next.3",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "repository": {
@@ -1,4 +1,5 @@
1
1
  import { SupabaseClient } from '@supabase/supabase-js';
2
+ import { RimoriClient } from '../plugin/RimoriClient';
2
3
 
3
4
  export type TriggerAction = { action_key: string } & Record<string, string | number | boolean>;
4
5
 
@@ -27,9 +28,11 @@ export interface Exercise {
27
28
 
28
29
  export class ExerciseController {
29
30
  private supabase: SupabaseClient;
31
+ private rimoriClient: RimoriClient;
30
32
 
31
- constructor(supabase: SupabaseClient) {
33
+ constructor(supabase: SupabaseClient, rimoriClient: RimoriClient) {
32
34
  this.supabase = supabase;
35
+ this.rimoriClient = rimoriClient;
33
36
  }
34
37
 
35
38
  /**
@@ -48,28 +51,37 @@ export class ExerciseController {
48
51
  }
49
52
 
50
53
  /**
51
- * Creates a new exercise via the backend API.
54
+ * Creates multiple exercises via the backend API.
55
+ * All requests are made in parallel but only one event is emitted.
52
56
  * @param token The token to use for authentication.
53
57
  * @param backendUrl The URL of the backend API.
54
- * @param params Exercise creation parameters.
55
- * @returns Created exercise object.
58
+ * @param exercises Exercise creation parameters.
59
+ * @returns Created exercise objects.
56
60
  */
57
- public async addExercise(token: string, backendUrl: string, params: CreateExerciseParams): Promise<Exercise> {
58
- const response = await fetch(`${backendUrl}/exercises`, {
59
- method: 'POST',
60
- headers: {
61
- 'Content-Type': 'application/json',
62
- Authorization: `Bearer ${token}`,
63
- },
64
- body: JSON.stringify(params),
65
- });
61
+ public async addExercise(token: string, backendUrl: string, exercises: CreateExerciseParams[]): Promise<Exercise[]> {
62
+ const responses = await Promise.all(
63
+ exercises.map(async (exercise) => {
64
+ const response = await fetch(`${backendUrl}/exercises`, {
65
+ method: 'POST',
66
+ headers: {
67
+ 'Content-Type': 'application/json',
68
+ Authorization: `Bearer ${token}`,
69
+ },
70
+ body: JSON.stringify(exercise),
71
+ });
66
72
 
67
- if (!response.ok) {
68
- const errorText = await response.text();
69
- throw new Error(`Failed to create exercise: ${errorText}`);
70
- }
73
+ if (!response.ok) {
74
+ const errorText = await response.text();
75
+ throw new Error(`Failed to create exercise: ${errorText}`);
76
+ }
71
77
 
72
- return await response.json();
78
+ return await response.json();
79
+ }),
80
+ );
81
+
82
+ this.rimoriClient.event.emit('global.exercises.triggerChange');
83
+
84
+ return responses;
73
85
  }
74
86
 
75
87
  /**
@@ -95,6 +107,7 @@ export class ExerciseController {
95
107
  const errorText = await response.text();
96
108
  throw new Error(`Failed to delete exercise: ${errorText}`);
97
109
  }
110
+ this.rimoriClient.event.emit('global.exercises.triggerChange');
98
111
 
99
112
  return await response.json();
100
113
  }
@@ -39,7 +39,7 @@ export class RimoriClient {
39
39
  this.rimoriInfo = info;
40
40
  this.superbase = supabase;
41
41
  this.pluginController = controller;
42
- this.exerciseController = new ExerciseController(supabase);
42
+ this.exerciseController = new ExerciseController(supabase, this);
43
43
  this.accomplishmentHandler = new AccomplishmentController(info.pluginId);
44
44
  this.settingsController = new SettingsController(supabase, info.pluginId, info.guild);
45
45
  this.sharedContentController = new SharedContentController(supabase, this);
@@ -475,14 +475,16 @@ export class RimoriClient {
475
475
  },
476
476
 
477
477
  /**
478
- * Creates a new exercise via the backend API.
479
- * @param params Exercise creation parameters.
480
- * @returns Created exercise object.
478
+ * Creates a new exercise or multiple exercises via the backend API.
479
+ * When creating multiple exercises, all requests are made in parallel but only one event is emitted.
480
+ * @param params Exercise creation parameters (single or array).
481
+ * @returns Created exercise objects.
481
482
  */
482
- add: async (params: CreateExerciseParams) => {
483
+ add: async (params: CreateExerciseParams | CreateExerciseParams[]) => {
483
484
  const token = await this.pluginController.getToken();
484
485
  const backendUrl = this.pluginController.getBackendUrl();
485
- return this.exerciseController.addExercise(token, backendUrl, params);
486
+ const exercises = Array.isArray(params) ? params : [params];
487
+ return this.exerciseController.addExercise(token, backendUrl, exercises);
486
488
  },
487
489
 
488
490
  /**