1
1
import { get , patch , post , put , del } from "@/libs/io/rest" ;
2
2
import { IHomebrewClan , IHomebrewDiscipline } from "@/types/data" ;
3
+ import { ICharacter } from "@/types/models" ;
3
4
4
5
export interface VicarNetAccount {
5
6
id : number ;
6
7
email : string ;
7
8
alias : string ;
8
9
}
9
10
10
- export type ClanResponse = { clan : IHomebrewClan , neededHomebrewDisciplines : IHomebrewDiscipline [ ] } ;
11
- type FromInviteResponse = { type : "clan" | "discipline" , content : IHomebrewDiscipline | ClanResponse } ;
11
+ export type ClanResponse = { clan : IHomebrewClan , neededHomebrewDisciplines : IHomebrewDiscipline [ ] } ;
12
+ type FromInviteResponse = { type : "clan" | "discipline" , content : IHomebrewDiscipline | ClanResponse } ;
12
13
13
14
export class VicarNet {
14
15
@@ -27,7 +28,7 @@ export class VicarNet {
27
28
28
29
public static async beginRegister ( email : string , alias : string ) : Promise < boolean > {
29
30
try {
30
- const [ status , data ] = await post < { message : string } > ( "/auth/register" , { email, alias} ) ;
31
+ const [ status , data ] = await post < { message : string } > ( "/auth/register" , { email, alias} ) ;
31
32
return status === 202 && data . message === "await activation" ;
32
33
} catch ( e ) {
33
34
console . error ( e ) ;
@@ -37,7 +38,10 @@ export class VicarNet {
37
38
38
39
public static async finishRegister ( activationCode : string ) : Promise < boolean > {
39
40
try {
40
- const [ status , data ] = await put < { message : string , user : VicarNetAccount & { passkey : string } } > ( "/auth/activate/" + activationCode ) ;
41
+ const [ status , data ] = await put < {
42
+ message : string ,
43
+ user : VicarNetAccount & { passkey : string }
44
+ } > ( "/auth/activate/" + activationCode ) ;
41
45
if ( status === 201 && data . message === "activated" ) {
42
46
const { passkey, ...user } = data . user ;
43
47
localStorage . setItem ( "vicar-net:account" , JSON . stringify ( user ) ) ;
@@ -54,7 +58,7 @@ export class VicarNet {
54
58
55
59
public static async checkLogin ( ) : Promise < boolean > {
56
60
try {
57
- const [ status , data ] = await post < { message : string } > ( "/auth/login" , undefined , this . buildHeaders ( ) ) ;
61
+ const [ status , data ] = await post < { message : string } > ( "/auth/login" , undefined , this . buildHeaders ( ) ) ;
58
62
return status === 200 && data . message === "logged in" ;
59
63
} catch ( e ) {
60
64
console . error ( e ) ;
@@ -64,7 +68,7 @@ export class VicarNet {
64
68
65
69
public static async beginRecover ( email : string ) : Promise < boolean > {
66
70
try {
67
- const [ status , data ] = await patch < { message : string } > ( `/auth/recover/${ email } /begin` ) ;
71
+ const [ status , data ] = await patch < { message : string } > ( `/auth/recover/${ email } /begin` ) ;
68
72
return status === 202 && data . message === "await recover" ;
69
73
} catch ( e ) {
70
74
console . error ( e ) ;
@@ -74,7 +78,10 @@ export class VicarNet {
74
78
75
79
public static async finishRecover ( recoveryCode : string ) : Promise < boolean > {
76
80
try {
77
- const [ status , data ] = await put < { message : string , user : VicarNetAccount & { passkey : string } } > ( `/auth/recover/${ recoveryCode } /finish` ) ;
81
+ const [ status , data ] = await put < {
82
+ message : string ,
83
+ user : VicarNetAccount & { passkey : string }
84
+ } > ( `/auth/recover/${ recoveryCode } /finish` ) ;
78
85
if ( status === 200 && data . message === "recovered" ) {
79
86
const { passkey, ...user } = data . user ;
80
87
localStorage . setItem ( "vicar-net:account" , JSON . stringify ( user ) ) ;
@@ -90,7 +97,7 @@ export class VicarNet {
90
97
91
98
public static async bindVicarShareIdToAlias ( vicarShareId : string ) : Promise < boolean > {
92
99
try {
93
- const [ status , data ] = await post < { message : string } > ( `/share/id` , {
100
+ const [ status , data ] = await post < { message : string } > ( `/share/id` , {
94
101
shareId : vicarShareId
95
102
} , this . buildHeaders ( ) ) ;
96
103
return status === 200 && data . message === "ok" ;
@@ -102,7 +109,7 @@ export class VicarNet {
102
109
103
110
public static async unbindVicarShareIdFromAlias ( ) : Promise < boolean > {
104
111
try {
105
- const [ status , data ] = await del < { message : string } > ( `/share/id` , this . buildHeaders ( ) ) ;
112
+ const [ status , data ] = await del < { message : string } > ( `/share/id` , this . buildHeaders ( ) ) ;
106
113
return status === 200 && data . message === "ok" ;
107
114
} catch ( e ) {
108
115
console . error ( e ) ;
@@ -117,7 +124,7 @@ export class VicarNet {
117
124
}
118
125
119
126
try {
120
- const [ status , data ] = await get < { id : string } > ( `/share/${ idOrAlias } /id` ) ;
127
+ const [ status , data ] = await get < { id : string } > ( `/share/${ idOrAlias } /id` ) ;
121
128
if ( status === 200 ) {
122
129
return data . id ;
123
130
}
@@ -130,15 +137,15 @@ export class VicarNet {
130
137
131
138
private static async ping ( ) : Promise < boolean > {
132
139
try {
133
- const [ status , data ] = await get < { message : string } > ( "/ping" ) ;
140
+ const [ status , data ] = await get < { message : string } > ( "/ping" ) ;
134
141
return status === 200 && data . message === "pong" ;
135
142
} catch ( e ) {
136
143
console . error ( e ) ;
137
144
return false ;
138
145
}
139
146
}
140
147
141
- private static buildHeaders ( ) : { [ key : string ] : string } {
148
+ private static buildHeaders ( ) : { [ key : string ] : string } {
142
149
if ( ! this . isLoggedIn ) {
143
150
return { } ;
144
151
}
@@ -147,9 +154,15 @@ export class VicarNet {
147
154
return { "X-User-ID" : account . id . toString ( ) , "X-User-Passkey" : localStorage . getItem ( "vicar-net:passkey" ) ! } ;
148
155
}
149
156
150
- public static async getClans ( search : string = "" , page : number = 1 , limit : number = 20 ) : Promise < { total : number , items : IHomebrewClan [ ] } > {
157
+ public static async getClans ( search : string = "" , page : number = 1 , limit : number = 20 ) : Promise < {
158
+ total : number ,
159
+ items : IHomebrewClan [ ]
160
+ } > {
151
161
try {
152
- const [ status , data ] = await get < { total : number , items : IHomebrewClan [ ] } > ( `/homebrew/clans?search=${ search } &page=${ page } &limit=${ limit } ` ) ;
162
+ const [ status , data ] = await get < {
163
+ total : number ,
164
+ items : IHomebrewClan [ ]
165
+ } > ( `/homebrew/clans?search=${ search } &page=${ page } &limit=${ limit } ` ) ;
153
166
if ( status === 200 ) {
154
167
return data ;
155
168
}
@@ -160,9 +173,15 @@ export class VicarNet {
160
173
}
161
174
}
162
175
163
- public static async getClan ( id : number ) : Promise < { clan : IHomebrewClan , neededHomebrewDisciplines : IHomebrewDiscipline [ ] } | null > {
176
+ public static async getClan ( id : number ) : Promise < {
177
+ clan : IHomebrewClan ,
178
+ neededHomebrewDisciplines : IHomebrewDiscipline [ ]
179
+ } | null > {
164
180
try {
165
- const [ status , data ] = await get < { clan : IHomebrewClan , neededHomebrewDisciplines : IHomebrewDiscipline [ ] } > ( `/homebrew/clans/${ id } ` , this . buildHeaders ( ) ) ;
181
+ const [ status , data ] = await get < {
182
+ clan : IHomebrewClan ,
183
+ neededHomebrewDisciplines : IHomebrewDiscipline [ ]
184
+ } > ( `/homebrew/clans/${ id } ` , this . buildHeaders ( ) ) ;
166
185
if ( status === 200 ) {
167
186
return data ;
168
187
}
@@ -173,9 +192,15 @@ export class VicarNet {
173
192
}
174
193
}
175
194
176
- public static async getDisciplines ( search : string = "" , page : number = 1 , limit : number = 20 ) : Promise < { total : number , items : IHomebrewDiscipline [ ] } > {
195
+ public static async getDisciplines ( search : string = "" , page : number = 1 , limit : number = 20 ) : Promise < {
196
+ total : number ,
197
+ items : IHomebrewDiscipline [ ]
198
+ } > {
177
199
try {
178
- const [ status , data ] = await get < { total : number , items : IHomebrewDiscipline [ ] } > ( `/homebrew/disciplines?search=${ search } &page=${ page } &limit=${ limit } ` ) ;
200
+ const [ status , data ] = await get < {
201
+ total : number ,
202
+ items : IHomebrewDiscipline [ ]
203
+ } > ( `/homebrew/disciplines?search=${ search } &page=${ page } &limit=${ limit } ` ) ;
179
204
if ( status === 200 ) {
180
205
return data ;
181
206
}
@@ -186,7 +211,7 @@ export class VicarNet {
186
211
}
187
212
}
188
213
189
- public static async getDiscipline ( id : number ) : Promise < IHomebrewDiscipline | null > {
214
+ public static async getDiscipline ( id : number ) : Promise < IHomebrewDiscipline | null > {
190
215
try {
191
216
const [ status , data ] = await get < IHomebrewDiscipline > ( `/homebrew/disciplines/${ id } ` , this . buildHeaders ( ) ) ;
192
217
if ( status === 200 ) {
@@ -199,35 +224,39 @@ export class VicarNet {
199
224
}
200
225
}
201
226
202
- public static async generateAccessCodeForClan ( clan : IHomebrewClan ) : Promise < string | null > {
227
+ public static async generateAccessCodeForClan ( clan : IHomebrewClan ) : Promise < string | null > {
203
228
if ( ! this . isLoggedIn || clan . creator !== this . account . alias ) {
204
229
return null ;
205
230
}
206
231
207
232
try {
208
- const [ _ , data ] = await post < { inviteCode : string } > ( `/homebrew/clans/${ clan . id } /invite` , undefined , this . buildHeaders ( ) ) ;
233
+ const [ _ , data ] = await post < {
234
+ inviteCode : string
235
+ } > ( `/homebrew/clans/${ clan . id } /invite` , undefined , this . buildHeaders ( ) ) ;
209
236
return data . inviteCode ;
210
237
} catch ( e ) {
211
238
console . error ( e ) ;
212
239
return null ;
213
240
}
214
241
}
215
242
216
- public static async generateAccessCodeForDiscipline ( discipline : IHomebrewDiscipline ) : Promise < string | null > {
243
+ public static async generateAccessCodeForDiscipline ( discipline : IHomebrewDiscipline ) : Promise < string | null > {
217
244
if ( ! this . isLoggedIn || discipline . creator !== this . account . alias ) {
218
245
return null ;
219
246
}
220
247
221
248
try {
222
- const [ _ , data ] = await post < { inviteCode : string } > ( `/homebrew/disciplines/${ discipline . id } /invite` , undefined , this . buildHeaders ( ) ) ;
249
+ const [ _ , data ] = await post < {
250
+ inviteCode : string
251
+ } > ( `/homebrew/disciplines/${ discipline . id } /invite` , undefined , this . buildHeaders ( ) ) ;
223
252
return data . inviteCode ;
224
253
} catch ( e ) {
225
254
console . error ( e ) ;
226
255
return null ;
227
256
}
228
257
}
229
258
230
- public static async createClan ( clan : IHomebrewClan ) : Promise < IHomebrewClan | undefined > {
259
+ public static async createClan ( clan : IHomebrewClan ) : Promise < IHomebrewClan | undefined > {
231
260
if ( ! this . isLoggedIn ) {
232
261
return undefined ;
233
262
}
@@ -244,7 +273,7 @@ export class VicarNet {
244
273
}
245
274
}
246
275
247
- public static async updateClan ( clan : IHomebrewClan ) : Promise < IHomebrewClan | undefined > {
276
+ public static async updateClan ( clan : IHomebrewClan ) : Promise < IHomebrewClan | undefined > {
248
277
if ( ! this . isLoggedIn || clan . creator !== this . account . alias ) {
249
278
return undefined ;
250
279
}
@@ -267,15 +296,15 @@ export class VicarNet {
267
296
}
268
297
269
298
try {
270
- const [ status , data ] = await del < { message : string } > ( `/homebrew/clans/${ clan . id } ` , this . buildHeaders ( ) ) ;
299
+ const [ status , data ] = await del < { message : string } > ( `/homebrew/clans/${ clan . id } ` , this . buildHeaders ( ) ) ;
271
300
return status === 200 ;
272
301
} catch ( e ) {
273
302
console . error ( e ) ;
274
303
return false ;
275
304
}
276
305
}
277
306
278
- public static async createDiscipline ( discipline : IHomebrewDiscipline ) : Promise < IHomebrewDiscipline | undefined > {
307
+ public static async createDiscipline ( discipline : IHomebrewDiscipline ) : Promise < IHomebrewDiscipline | undefined > {
279
308
if ( ! this . isLoggedIn ) {
280
309
return undefined ;
281
310
}
@@ -292,7 +321,7 @@ export class VicarNet {
292
321
}
293
322
}
294
323
295
- public static async updateDiscipline ( discipline : IHomebrewDiscipline ) : Promise < IHomebrewDiscipline | undefined > {
324
+ public static async updateDiscipline ( discipline : IHomebrewDiscipline ) : Promise < IHomebrewDiscipline | undefined > {
296
325
if ( ! this . isLoggedIn || discipline . creator !== this . account . alias ) {
297
326
return undefined ;
298
327
}
@@ -315,21 +344,26 @@ export class VicarNet {
315
344
}
316
345
317
346
try {
318
- const [ status , data ] = await del < { message : string } > ( `/homebrew/disciplines/${ discipline . id } ` , this . buildHeaders ( ) ) ;
347
+ const [ status , data ] = await del < {
348
+ message : string
349
+ } > ( `/homebrew/disciplines/${ discipline . id } ` , this . buildHeaders ( ) ) ;
319
350
return status === 200 ;
320
351
} catch ( e ) {
321
352
console . error ( e ) ;
322
353
return false ;
323
354
}
324
355
}
325
356
326
- public static async getMyContent ( ) : Promise < { clans : IHomebrewClan [ ] , disciplines : IHomebrewDiscipline [ ] } > {
357
+ public static async getMyContent ( ) : Promise < { clans : IHomebrewClan [ ] , disciplines : IHomebrewDiscipline [ ] } > {
327
358
if ( ! this . isLoggedIn ) {
328
359
return { clans : [ ] , disciplines : [ ] } ;
329
360
}
330
361
331
362
try {
332
- const [ status , data ] = await get < { clans : IHomebrewClan [ ] , disciplines : IHomebrewDiscipline [ ] } > ( `/homebrew/my-content` , this . buildHeaders ( ) ) ;
363
+ const [ status , data ] = await get < {
364
+ clans : IHomebrewClan [ ] ,
365
+ disciplines : IHomebrewDiscipline [ ]
366
+ } > ( `/homebrew/my-content` , this . buildHeaders ( ) ) ;
333
367
if ( status === 200 ) {
334
368
return data ;
335
369
}
@@ -340,7 +374,7 @@ export class VicarNet {
340
374
}
341
375
}
342
376
343
- public static async getContentFromInvite ( inviteCode : string ) : Promise < FromInviteResponse | undefined > {
377
+ public static async getContentFromInvite ( inviteCode : string ) : Promise < FromInviteResponse | undefined > {
344
378
try {
345
379
const [ _ , data ] = await get < FromInviteResponse > ( `/homebrew/from-invite/${ inviteCode } ` ) ;
346
380
return data ;
@@ -349,4 +383,14 @@ export class VicarNet {
349
383
return undefined ;
350
384
}
351
385
}
386
+
387
+ public static async postCharSync ( roomId : string , charData : string ) : Promise < void > {
388
+ try {
389
+ await post ( `/sync/characters/${ roomId } ` , {
390
+ data : charData
391
+ } , this . buildHeaders ( ) ) ;
392
+ } catch ( e ) {
393
+ console . error ( e ) ;
394
+ }
395
+ }
352
396
}
0 commit comments