rotur-sdk 1.0.1 → 1.0.2

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/index.d.mts CHANGED
@@ -496,6 +496,7 @@ type WSMessage = {
496
496
  };
497
497
 
498
498
  type Handler = (msg: any) => void;
499
+ type KeyChangeCallback = (key: string, value: any, oldValue: any) => void;
499
500
  declare class RoturSocket extends EventTarget {
500
501
  private url;
501
502
  private ws;
@@ -507,11 +508,18 @@ declare class RoturSocket extends EventTarget {
507
508
  private _username;
508
509
  private handlers;
509
510
  private rooms;
511
+ private keyCache;
512
+ private keyChangeCallbacks;
510
513
  constructor(url?: string);
511
514
  get connected(): boolean;
512
515
  get userId(): string | null;
513
516
  get username(): string | null;
514
517
  get joinedRooms(): string[];
518
+ getKey(key: string): any;
519
+ getAllKeys(): Record<string, any>;
520
+ onKeyChange(callback: KeyChangeCallback): () => void;
521
+ offKeyChange(callback: KeyChangeCallback): void;
522
+ private notifyKeyChange;
515
523
  connect(token: string): Promise<{
516
524
  user_id: UserId;
517
525
  username: Username;
@@ -529,8 +537,7 @@ declare class RoturSocket extends EventTarget {
529
537
  leave(rooms: string | string[]): void;
530
538
  listRooms(): Promise<string[]>;
531
539
  roomState(room: string): void;
532
- setPresence(presence: Presence): void;
533
- setStatus(status: string): void;
540
+ setStatus(status: string, presence?: Presence): void;
534
541
  addActivity(activity: Activity & {
535
542
  id: string;
536
543
  }): void;
@@ -579,6 +586,7 @@ declare class Rotur {
579
586
  get token(): string | null;
580
587
  get loggedIn(): boolean;
581
588
  setToken(token: string): void;
589
+ private _socketReady;
582
590
  login(options?: {
583
591
  system?: string;
584
592
  timeout?: number;
@@ -616,6 +624,9 @@ declare class MeNamespace extends Namespace {
616
624
  refreshToken(): Promise<{
617
625
  token: string;
618
626
  }>;
627
+ getKey(key: string): any;
628
+ getAllKeys(): Record<string, any>;
629
+ onKeyChange(callback: (key: string, value: any, oldValue: any) => void): () => void;
619
630
  transfer(to: Username, amount: number, note?: string): Promise<any>;
620
631
  claimDaily(): Promise<{
621
632
  message: string;
@@ -875,27 +886,6 @@ declare class CosmeticsNamespace extends Namespace {
875
886
  message: string;
876
887
  }>;
877
888
  overlays(filepath: string): Promise<Response>;
878
- /** Admin: List all cosmetics in catalog */
879
- adminList(options?: {
880
- type?: string;
881
- }): Promise<{
882
- cosmetics: CosmeticCatalogEntryPublic[];
883
- total: number;
884
- }>;
885
- /** Admin: Create a cosmetic */
886
- adminCreate(data: AdminCosmeticCreate): Promise<{
887
- message: string;
888
- cosmetic: CosmeticCatalogEntryPublic;
889
- }>;
890
- /** Admin: Update a cosmetic */
891
- adminUpdate(id: string, data: AdminCosmeticUpdate): Promise<{
892
- message: string;
893
- cosmetic: CosmeticCatalogEntryPublic;
894
- }>;
895
- /** Admin: Delete a cosmetic */
896
- adminDelete(id: string): Promise<{
897
- message: string;
898
- }>;
899
889
  }
900
890
  declare class PushNamespace extends Namespace {
901
891
  vapidKeys(): Promise<{
package/dist/index.d.ts CHANGED
@@ -496,6 +496,7 @@ type WSMessage = {
496
496
  };
497
497
 
498
498
  type Handler = (msg: any) => void;
499
+ type KeyChangeCallback = (key: string, value: any, oldValue: any) => void;
499
500
  declare class RoturSocket extends EventTarget {
500
501
  private url;
501
502
  private ws;
@@ -507,11 +508,18 @@ declare class RoturSocket extends EventTarget {
507
508
  private _username;
508
509
  private handlers;
509
510
  private rooms;
511
+ private keyCache;
512
+ private keyChangeCallbacks;
510
513
  constructor(url?: string);
511
514
  get connected(): boolean;
512
515
  get userId(): string | null;
513
516
  get username(): string | null;
514
517
  get joinedRooms(): string[];
518
+ getKey(key: string): any;
519
+ getAllKeys(): Record<string, any>;
520
+ onKeyChange(callback: KeyChangeCallback): () => void;
521
+ offKeyChange(callback: KeyChangeCallback): void;
522
+ private notifyKeyChange;
515
523
  connect(token: string): Promise<{
516
524
  user_id: UserId;
517
525
  username: Username;
@@ -529,8 +537,7 @@ declare class RoturSocket extends EventTarget {
529
537
  leave(rooms: string | string[]): void;
530
538
  listRooms(): Promise<string[]>;
531
539
  roomState(room: string): void;
532
- setPresence(presence: Presence): void;
533
- setStatus(status: string): void;
540
+ setStatus(status: string, presence?: Presence): void;
534
541
  addActivity(activity: Activity & {
535
542
  id: string;
536
543
  }): void;
@@ -579,6 +586,7 @@ declare class Rotur {
579
586
  get token(): string | null;
580
587
  get loggedIn(): boolean;
581
588
  setToken(token: string): void;
589
+ private _socketReady;
582
590
  login(options?: {
583
591
  system?: string;
584
592
  timeout?: number;
@@ -616,6 +624,9 @@ declare class MeNamespace extends Namespace {
616
624
  refreshToken(): Promise<{
617
625
  token: string;
618
626
  }>;
627
+ getKey(key: string): any;
628
+ getAllKeys(): Record<string, any>;
629
+ onKeyChange(callback: (key: string, value: any, oldValue: any) => void): () => void;
619
630
  transfer(to: Username, amount: number, note?: string): Promise<any>;
620
631
  claimDaily(): Promise<{
621
632
  message: string;
@@ -875,27 +886,6 @@ declare class CosmeticsNamespace extends Namespace {
875
886
  message: string;
876
887
  }>;
877
888
  overlays(filepath: string): Promise<Response>;
878
- /** Admin: List all cosmetics in catalog */
879
- adminList(options?: {
880
- type?: string;
881
- }): Promise<{
882
- cosmetics: CosmeticCatalogEntryPublic[];
883
- total: number;
884
- }>;
885
- /** Admin: Create a cosmetic */
886
- adminCreate(data: AdminCosmeticCreate): Promise<{
887
- message: string;
888
- cosmetic: CosmeticCatalogEntryPublic;
889
- }>;
890
- /** Admin: Update a cosmetic */
891
- adminUpdate(id: string, data: AdminCosmeticUpdate): Promise<{
892
- message: string;
893
- cosmetic: CosmeticCatalogEntryPublic;
894
- }>;
895
- /** Admin: Delete a cosmetic */
896
- adminDelete(id: string): Promise<{
897
- message: string;
898
- }>;
899
889
  }
900
890
  declare class PushNamespace extends Namespace {
901
891
  vapidKeys(): Promise<{
package/dist/index.js CHANGED
@@ -139,6 +139,8 @@ var RoturSocket = class extends EventTarget {
139
139
  _username = null;
140
140
  handlers = /* @__PURE__ */ new Map();
141
141
  rooms = /* @__PURE__ */ new Set();
142
+ keyCache = {};
143
+ keyChangeCallbacks = /* @__PURE__ */ new Set();
142
144
  get connected() {
143
145
  return this._connected;
144
146
  }
@@ -151,6 +153,27 @@ var RoturSocket = class extends EventTarget {
151
153
  get joinedRooms() {
152
154
  return [...this.rooms];
153
155
  }
156
+ getKey(key) {
157
+ return this.keyCache[key];
158
+ }
159
+ getAllKeys() {
160
+ return { ...this.keyCache };
161
+ }
162
+ onKeyChange(callback) {
163
+ this.keyChangeCallbacks.add(callback);
164
+ return () => this.keyChangeCallbacks.delete(callback);
165
+ }
166
+ offKeyChange(callback) {
167
+ this.keyChangeCallbacks.delete(callback);
168
+ }
169
+ notifyKeyChange(key, value, oldValue) {
170
+ for (const cb of this.keyChangeCallbacks) {
171
+ try {
172
+ cb(key, value, oldValue);
173
+ } catch {
174
+ }
175
+ }
176
+ }
154
177
  connect(token) {
155
178
  this.token = token;
156
179
  return new Promise((resolve, reject) => {
@@ -168,11 +191,19 @@ var RoturSocket = class extends EventTarget {
168
191
  this._connected = true;
169
192
  this._userId = msg.user_id;
170
193
  this._username = msg.username;
194
+ if (msg.user && typeof msg.user === "object") {
195
+ this.keyCache = { ...msg.user };
196
+ }
171
197
  this.startHeartbeat();
172
198
  resolve(msg);
173
199
  }
174
200
  if (cmd === "join_ok") this.rooms.add(msg.room);
175
201
  if (cmd === "leave_ok") this.rooms.delete(msg.room);
202
+ if (cmd === "key_update" && typeof msg.key === "string") {
203
+ const old = this.keyCache[msg.key];
204
+ this.keyCache[msg.key] = msg.value;
205
+ this.notifyKeyChange(msg.key, msg.value, old);
206
+ }
176
207
  this.emit(cmd, msg);
177
208
  };
178
209
  this.ws.onerror = (e) => {
@@ -248,11 +279,8 @@ var RoturSocket = class extends EventTarget {
248
279
  roomState(room) {
249
280
  this.send({ cmd: "room_state", room });
250
281
  }
251
- setPresence(presence) {
252
- this.send({ cmd: "set_status", presence });
253
- }
254
- setStatus(status) {
255
- this.send({ cmd: "set_status", status });
282
+ setStatus(status, presence) {
283
+ this.send({ cmd: "set_status", status, presence });
256
284
  }
257
285
  addActivity(activity) {
258
286
  this.send({ cmd: "add_activity", ...activity });
@@ -286,6 +314,7 @@ var RoturSocket = class extends EventTarget {
286
314
  this.token = null;
287
315
  this.cleanup();
288
316
  this.rooms.clear();
317
+ this.keyCache = {};
289
318
  if (this.ws) {
290
319
  this.ws.onclose = null;
291
320
  this.ws.close();
@@ -383,6 +412,9 @@ var Rotur = class {
383
412
  this._token = options?.token ?? null;
384
413
  this._http = new Http(() => this._token);
385
414
  this.socket = new RoturSocket(options?.wsUrl);
415
+ if (this._token) {
416
+ this._socketReady = this.socket.connect(this._token).catch(() => null);
417
+ }
386
418
  }
387
419
  get token() {
388
420
  return this._token;
@@ -392,18 +424,26 @@ var Rotur = class {
392
424
  }
393
425
  setToken(token) {
394
426
  this._token = token;
427
+ if (!this.socket.connected) {
428
+ this._socketReady = this.socket.connect(this._token).catch(() => null);
429
+ }
395
430
  }
431
+ _socketReady = null;
396
432
  async login(options) {
397
433
  const { token } = await performAuth(options);
398
434
  this._token = token;
435
+ this._socketReady = this.socket.connect(this._token).catch(() => null);
399
436
  return this;
400
437
  }
401
438
  async connectSocket() {
402
439
  if (!this._token) throw new ApiError(401, { error: "Login first" });
403
- return this.socket.connect(this._token);
440
+ const result = await this.socket.connect(this._token);
441
+ this._socketReady = Promise.resolve(result);
442
+ return result;
404
443
  }
405
444
  logout() {
406
445
  this._token = null;
446
+ this._socketReady = null;
407
447
  this.socket.disconnect();
408
448
  }
409
449
  };
@@ -459,6 +499,15 @@ var MeNamespace = class extends Namespace {
459
499
  async refreshToken() {
460
500
  return this.$post("/me/refresh_token");
461
501
  }
502
+ getKey(key) {
503
+ return this.r.socket.getKey(key);
504
+ }
505
+ getAllKeys() {
506
+ return this.r.socket.getAllKeys();
507
+ }
508
+ onKeyChange(callback) {
509
+ return this.r.socket.onKeyChange(callback);
510
+ }
462
511
  async transfer(to, amount, note) {
463
512
  return this.$post("/me/transfer", { to, amount, note });
464
513
  }
@@ -932,22 +981,6 @@ var CosmeticsNamespace = class extends Namespace {
932
981
  async overlays(filepath) {
933
982
  return this.$get(`/cosmetics/overlays/${filepath}`, void 0, false);
934
983
  }
935
- /** Admin: List all cosmetics in catalog */
936
- async adminList(options) {
937
- return this.$get("/cosmetics/admin/list", options, true);
938
- }
939
- /** Admin: Create a cosmetic */
940
- async adminCreate(data) {
941
- return this.$post("/cosmetics/admin/create", data);
942
- }
943
- /** Admin: Update a cosmetic */
944
- async adminUpdate(id, data) {
945
- return this.$patch(`/cosmetics/admin/update/${id}`, data);
946
- }
947
- /** Admin: Delete a cosmetic */
948
- async adminDelete(id) {
949
- return this.$del(`/cosmetics/admin/delete/${id}`);
950
- }
951
984
  };
952
985
  var PushNamespace = class extends Namespace {
953
986
  async vapidKeys() {
package/dist/index.mjs CHANGED
@@ -109,6 +109,8 @@ var RoturSocket = class extends EventTarget {
109
109
  _username = null;
110
110
  handlers = /* @__PURE__ */ new Map();
111
111
  rooms = /* @__PURE__ */ new Set();
112
+ keyCache = {};
113
+ keyChangeCallbacks = /* @__PURE__ */ new Set();
112
114
  get connected() {
113
115
  return this._connected;
114
116
  }
@@ -121,6 +123,27 @@ var RoturSocket = class extends EventTarget {
121
123
  get joinedRooms() {
122
124
  return [...this.rooms];
123
125
  }
126
+ getKey(key) {
127
+ return this.keyCache[key];
128
+ }
129
+ getAllKeys() {
130
+ return { ...this.keyCache };
131
+ }
132
+ onKeyChange(callback) {
133
+ this.keyChangeCallbacks.add(callback);
134
+ return () => this.keyChangeCallbacks.delete(callback);
135
+ }
136
+ offKeyChange(callback) {
137
+ this.keyChangeCallbacks.delete(callback);
138
+ }
139
+ notifyKeyChange(key, value, oldValue) {
140
+ for (const cb of this.keyChangeCallbacks) {
141
+ try {
142
+ cb(key, value, oldValue);
143
+ } catch {
144
+ }
145
+ }
146
+ }
124
147
  connect(token) {
125
148
  this.token = token;
126
149
  return new Promise((resolve, reject) => {
@@ -138,11 +161,19 @@ var RoturSocket = class extends EventTarget {
138
161
  this._connected = true;
139
162
  this._userId = msg.user_id;
140
163
  this._username = msg.username;
164
+ if (msg.user && typeof msg.user === "object") {
165
+ this.keyCache = { ...msg.user };
166
+ }
141
167
  this.startHeartbeat();
142
168
  resolve(msg);
143
169
  }
144
170
  if (cmd === "join_ok") this.rooms.add(msg.room);
145
171
  if (cmd === "leave_ok") this.rooms.delete(msg.room);
172
+ if (cmd === "key_update" && typeof msg.key === "string") {
173
+ const old = this.keyCache[msg.key];
174
+ this.keyCache[msg.key] = msg.value;
175
+ this.notifyKeyChange(msg.key, msg.value, old);
176
+ }
146
177
  this.emit(cmd, msg);
147
178
  };
148
179
  this.ws.onerror = (e) => {
@@ -218,11 +249,8 @@ var RoturSocket = class extends EventTarget {
218
249
  roomState(room) {
219
250
  this.send({ cmd: "room_state", room });
220
251
  }
221
- setPresence(presence) {
222
- this.send({ cmd: "set_status", presence });
223
- }
224
- setStatus(status) {
225
- this.send({ cmd: "set_status", status });
252
+ setStatus(status, presence) {
253
+ this.send({ cmd: "set_status", status, presence });
226
254
  }
227
255
  addActivity(activity) {
228
256
  this.send({ cmd: "add_activity", ...activity });
@@ -256,6 +284,7 @@ var RoturSocket = class extends EventTarget {
256
284
  this.token = null;
257
285
  this.cleanup();
258
286
  this.rooms.clear();
287
+ this.keyCache = {};
259
288
  if (this.ws) {
260
289
  this.ws.onclose = null;
261
290
  this.ws.close();
@@ -353,6 +382,9 @@ var Rotur = class {
353
382
  this._token = options?.token ?? null;
354
383
  this._http = new Http(() => this._token);
355
384
  this.socket = new RoturSocket(options?.wsUrl);
385
+ if (this._token) {
386
+ this._socketReady = this.socket.connect(this._token).catch(() => null);
387
+ }
356
388
  }
357
389
  get token() {
358
390
  return this._token;
@@ -362,18 +394,26 @@ var Rotur = class {
362
394
  }
363
395
  setToken(token) {
364
396
  this._token = token;
397
+ if (!this.socket.connected) {
398
+ this._socketReady = this.socket.connect(this._token).catch(() => null);
399
+ }
365
400
  }
401
+ _socketReady = null;
366
402
  async login(options) {
367
403
  const { token } = await performAuth(options);
368
404
  this._token = token;
405
+ this._socketReady = this.socket.connect(this._token).catch(() => null);
369
406
  return this;
370
407
  }
371
408
  async connectSocket() {
372
409
  if (!this._token) throw new ApiError(401, { error: "Login first" });
373
- return this.socket.connect(this._token);
410
+ const result = await this.socket.connect(this._token);
411
+ this._socketReady = Promise.resolve(result);
412
+ return result;
374
413
  }
375
414
  logout() {
376
415
  this._token = null;
416
+ this._socketReady = null;
377
417
  this.socket.disconnect();
378
418
  }
379
419
  };
@@ -429,6 +469,15 @@ var MeNamespace = class extends Namespace {
429
469
  async refreshToken() {
430
470
  return this.$post("/me/refresh_token");
431
471
  }
472
+ getKey(key) {
473
+ return this.r.socket.getKey(key);
474
+ }
475
+ getAllKeys() {
476
+ return this.r.socket.getAllKeys();
477
+ }
478
+ onKeyChange(callback) {
479
+ return this.r.socket.onKeyChange(callback);
480
+ }
432
481
  async transfer(to, amount, note) {
433
482
  return this.$post("/me/transfer", { to, amount, note });
434
483
  }
@@ -902,22 +951,6 @@ var CosmeticsNamespace = class extends Namespace {
902
951
  async overlays(filepath) {
903
952
  return this.$get(`/cosmetics/overlays/${filepath}`, void 0, false);
904
953
  }
905
- /** Admin: List all cosmetics in catalog */
906
- async adminList(options) {
907
- return this.$get("/cosmetics/admin/list", options, true);
908
- }
909
- /** Admin: Create a cosmetic */
910
- async adminCreate(data) {
911
- return this.$post("/cosmetics/admin/create", data);
912
- }
913
- /** Admin: Update a cosmetic */
914
- async adminUpdate(id, data) {
915
- return this.$patch(`/cosmetics/admin/update/${id}`, data);
916
- }
917
- /** Admin: Delete a cosmetic */
918
- async adminDelete(id) {
919
- return this.$del(`/cosmetics/admin/delete/${id}`);
920
- }
921
954
  };
922
955
  var PushNamespace = class extends Namespace {
923
956
  async vapidKeys() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rotur-sdk",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "The official SDK for the Rotur platform - auth, profiles, posts, credits, keys, groups, items, gifts, tokens, validators, status, files, cosmetics, push notifications, and more",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",