this repo has no description
1const API_BASE = '/xrpc'
2
3export class ApiError extends Error {
4 public did?: string
5 public reauthMethods?: string[]
6 constructor(public status: number, public error: string, message: string, did?: string, reauthMethods?: string[]) {
7 super(message)
8 this.name = 'ApiError'
9 this.did = did
10 this.reauthMethods = reauthMethods
11 }
12}
13
14async function xrpc<T>(method: string, options?: {
15 method?: 'GET' | 'POST'
16 params?: Record<string, string>
17 body?: unknown
18 token?: string
19}): Promise<T> {
20 const { method: httpMethod = 'GET', params, body, token } = options ?? {}
21 let url = `${API_BASE}/${method}`
22 if (params) {
23 const searchParams = new URLSearchParams(params)
24 url += `?${searchParams}`
25 }
26 const headers: Record<string, string> = {}
27 if (token) {
28 headers['Authorization'] = `Bearer ${token}`
29 }
30 if (body) {
31 headers['Content-Type'] = 'application/json'
32 }
33 const res = await fetch(url, {
34 method: httpMethod,
35 headers,
36 body: body ? JSON.stringify(body) : undefined,
37 })
38 if (!res.ok) {
39 const err = await res.json().catch(() => ({ error: 'Unknown', message: res.statusText }))
40 throw new ApiError(res.status, err.error, err.message, err.did, err.reauth_methods)
41 }
42 return res.json()
43}
44
45export interface Session {
46 did: string
47 handle: string
48 email?: string
49 emailConfirmed?: boolean
50 preferredChannel?: string
51 preferredChannelVerified?: boolean
52 isAdmin?: boolean
53 active?: boolean
54 status?: 'active' | 'deactivated'
55 accessJwt: string
56 refreshJwt: string
57}
58
59export interface AppPassword {
60 name: string
61 createdAt: string
62}
63
64export interface InviteCode {
65 code: string
66 available: number
67 disabled: boolean
68 forAccount: string
69 createdBy: string
70 createdAt: string
71 uses: { usedBy: string; usedAt: string }[]
72}
73
74export type VerificationChannel = 'email' | 'discord' | 'telegram' | 'signal'
75
76export type DidType = 'plc' | 'web' | 'web-external'
77
78export interface CreateAccountParams {
79 handle: string
80 email: string
81 password: string
82 inviteCode?: string
83 didType?: DidType
84 did?: string
85 verificationChannel?: VerificationChannel
86 discordId?: string
87 telegramUsername?: string
88 signalNumber?: string
89}
90
91export interface CreateAccountResult {
92 handle: string
93 did: string
94 verificationRequired: boolean
95 verificationChannel: string
96}
97
98export interface ConfirmSignupResult {
99 accessJwt: string
100 refreshJwt: string
101 handle: string
102 did: string
103 email?: string
104 emailConfirmed?: boolean
105 preferredChannel?: string
106 preferredChannelVerified?: boolean
107}
108
109export const api = {
110 async createAccount(params: CreateAccountParams): Promise<CreateAccountResult> {
111 return xrpc('com.atproto.server.createAccount', {
112 method: 'POST',
113 body: {
114 handle: params.handle,
115 email: params.email,
116 password: params.password,
117 inviteCode: params.inviteCode,
118 didType: params.didType,
119 did: params.did,
120 verificationChannel: params.verificationChannel,
121 discordId: params.discordId,
122 telegramUsername: params.telegramUsername,
123 signalNumber: params.signalNumber,
124 },
125 })
126 },
127
128 async confirmSignup(did: string, verificationCode: string): Promise<ConfirmSignupResult> {
129 return xrpc('com.atproto.server.confirmSignup', {
130 method: 'POST',
131 body: { did, verificationCode },
132 })
133 },
134
135 async resendVerification(did: string): Promise<{ success: boolean }> {
136 return xrpc('com.atproto.server.resendVerification', {
137 method: 'POST',
138 body: { did },
139 })
140 },
141
142 async createSession(identifier: string, password: string): Promise<Session> {
143 return xrpc('com.atproto.server.createSession', {
144 method: 'POST',
145 body: { identifier, password },
146 })
147 },
148
149 async getSession(token: string): Promise<Session> {
150 return xrpc('com.atproto.server.getSession', { token })
151 },
152
153 async refreshSession(refreshJwt: string): Promise<Session> {
154 return xrpc('com.atproto.server.refreshSession', {
155 method: 'POST',
156 token: refreshJwt,
157 })
158 },
159
160 async deleteSession(token: string): Promise<void> {
161 await xrpc('com.atproto.server.deleteSession', {
162 method: 'POST',
163 token,
164 })
165 },
166
167 async listAppPasswords(token: string): Promise<{ passwords: AppPassword[] }> {
168 return xrpc('com.atproto.server.listAppPasswords', { token })
169 },
170
171 async createAppPassword(token: string, name: string): Promise<{ name: string; password: string; createdAt: string }> {
172 return xrpc('com.atproto.server.createAppPassword', {
173 method: 'POST',
174 token,
175 body: { name },
176 })
177 },
178
179 async revokeAppPassword(token: string, name: string): Promise<void> {
180 await xrpc('com.atproto.server.revokeAppPassword', {
181 method: 'POST',
182 token,
183 body: { name },
184 })
185 },
186
187 async getAccountInviteCodes(token: string): Promise<{ codes: InviteCode[] }> {
188 return xrpc('com.atproto.server.getAccountInviteCodes', { token })
189 },
190
191 async createInviteCode(token: string, useCount: number = 1): Promise<{ code: string }> {
192 return xrpc('com.atproto.server.createInviteCode', {
193 method: 'POST',
194 token,
195 body: { useCount },
196 })
197 },
198
199 async requestPasswordReset(email: string): Promise<void> {
200 await xrpc('com.atproto.server.requestPasswordReset', {
201 method: 'POST',
202 body: { email },
203 })
204 },
205
206 async resetPassword(token: string, password: string): Promise<void> {
207 await xrpc('com.atproto.server.resetPassword', {
208 method: 'POST',
209 body: { token, password },
210 })
211 },
212
213 async requestEmailUpdate(token: string, email: string): Promise<{ tokenRequired: boolean }> {
214 return xrpc('com.atproto.server.requestEmailUpdate', {
215 method: 'POST',
216 token,
217 body: { email },
218 })
219 },
220
221 async updateEmail(token: string, email: string, emailToken?: string): Promise<void> {
222 await xrpc('com.atproto.server.updateEmail', {
223 method: 'POST',
224 token,
225 body: { email, token: emailToken },
226 })
227 },
228
229 async updateHandle(token: string, handle: string): Promise<void> {
230 await xrpc('com.atproto.identity.updateHandle', {
231 method: 'POST',
232 token,
233 body: { handle },
234 })
235 },
236
237 async requestAccountDelete(token: string): Promise<void> {
238 await xrpc('com.atproto.server.requestAccountDelete', {
239 method: 'POST',
240 token,
241 })
242 },
243
244 async deleteAccount(did: string, password: string, deleteToken: string): Promise<void> {
245 await xrpc('com.atproto.server.deleteAccount', {
246 method: 'POST',
247 body: { did, password, token: deleteToken },
248 })
249 },
250
251 async describeServer(): Promise<{
252 availableUserDomains: string[]
253 inviteCodeRequired: boolean
254 links?: { privacyPolicy?: string; termsOfService?: string }
255 }> {
256 return xrpc('com.atproto.server.describeServer')
257 },
258
259 async getNotificationPrefs(token: string): Promise<{
260 preferredChannel: string
261 email: string
262 discordId: string | null
263 discordVerified: boolean
264 telegramUsername: string | null
265 telegramVerified: boolean
266 signalNumber: string | null
267 signalVerified: boolean
268 }> {
269 return xrpc('com.tranquil.account.getNotificationPrefs', { token })
270 },
271
272 async updateNotificationPrefs(token: string, prefs: {
273 preferredChannel?: string
274 discordId?: string
275 telegramUsername?: string
276 signalNumber?: string
277 }): Promise<{ success: boolean }> {
278 return xrpc('com.tranquil.account.updateNotificationPrefs', {
279 method: 'POST',
280 token,
281 body: prefs,
282 })
283 },
284
285 async confirmChannelVerification(token: string, channel: string, code: string): Promise<{ success: boolean }> {
286 return xrpc('com.tranquil.account.confirmChannelVerification', {
287 method: 'POST',
288 token,
289 body: { channel, code },
290 })
291 },
292
293 async getNotificationHistory(token: string): Promise<{
294 notifications: Array<{
295 createdAt: string
296 channel: string
297 notificationType: string
298 status: string
299 subject: string | null
300 body: string
301 }>
302 }> {
303 return xrpc('com.tranquil.account.getNotificationHistory', { token })
304 },
305
306 async getServerStats(token: string): Promise<{
307 userCount: number
308 repoCount: number
309 recordCount: number
310 blobStorageBytes: number
311 }> {
312 return xrpc('com.tranquil.admin.getServerStats', { token })
313 },
314
315 async changePassword(token: string, currentPassword: string, newPassword: string): Promise<void> {
316 await xrpc('com.tranquil.account.changePassword', {
317 method: 'POST',
318 token,
319 body: { currentPassword, newPassword },
320 })
321 },
322
323 async removePassword(token: string): Promise<{ success: boolean }> {
324 return xrpc('com.tranquil.account.removePassword', {
325 method: 'POST',
326 token,
327 })
328 },
329
330 async getPasswordStatus(token: string): Promise<{ hasPassword: boolean }> {
331 return xrpc('com.tranquil.account.getPasswordStatus', { token })
332 },
333
334 async listSessions(token: string): Promise<{
335 sessions: Array<{
336 id: string
337 createdAt: string
338 expiresAt: string
339 isCurrent: boolean
340 }>
341 }> {
342 return xrpc('com.tranquil.account.listSessions', { token })
343 },
344
345 async revokeSession(token: string, sessionId: string): Promise<void> {
346 await xrpc('com.tranquil.account.revokeSession', {
347 method: 'POST',
348 token,
349 body: { sessionId },
350 })
351 },
352
353 async searchAccounts(token: string, options?: {
354 handle?: string
355 cursor?: string
356 limit?: number
357 }): Promise<{
358 cursor?: string
359 accounts: Array<{
360 did: string
361 handle: string
362 email?: string
363 indexedAt: string
364 emailConfirmedAt?: string
365 deactivatedAt?: string
366 }>
367 }> {
368 const params: Record<string, string> = {}
369 if (options?.handle) params.handle = options.handle
370 if (options?.cursor) params.cursor = options.cursor
371 if (options?.limit) params.limit = String(options.limit)
372 return xrpc('com.atproto.admin.searchAccounts', { token, params })
373 },
374
375 async getInviteCodes(token: string, options?: {
376 sort?: 'recent' | 'usage'
377 cursor?: string
378 limit?: number
379 }): Promise<{
380 cursor?: string
381 codes: Array<{
382 code: string
383 available: number
384 disabled: boolean
385 forAccount: string
386 createdBy: string
387 createdAt: string
388 uses: Array<{ usedBy: string; usedAt: string }>
389 }>
390 }> {
391 const params: Record<string, string> = {}
392 if (options?.sort) params.sort = options.sort
393 if (options?.cursor) params.cursor = options.cursor
394 if (options?.limit) params.limit = String(options.limit)
395 return xrpc('com.atproto.admin.getInviteCodes', { token, params })
396 },
397
398 async disableInviteCodes(token: string, codes?: string[], accounts?: string[]): Promise<void> {
399 await xrpc('com.atproto.admin.disableInviteCodes', {
400 method: 'POST',
401 token,
402 body: { codes, accounts },
403 })
404 },
405
406 async getAccountInfo(token: string, did: string): Promise<{
407 did: string
408 handle: string
409 email?: string
410 indexedAt: string
411 emailConfirmedAt?: string
412 invitesDisabled?: boolean
413 deactivatedAt?: string
414 }> {
415 return xrpc('com.atproto.admin.getAccountInfo', { token, params: { did } })
416 },
417
418 async disableAccountInvites(token: string, account: string): Promise<void> {
419 await xrpc('com.atproto.admin.disableAccountInvites', {
420 method: 'POST',
421 token,
422 body: { account },
423 })
424 },
425
426 async enableAccountInvites(token: string, account: string): Promise<void> {
427 await xrpc('com.atproto.admin.enableAccountInvites', {
428 method: 'POST',
429 token,
430 body: { account },
431 })
432 },
433
434 async adminDeleteAccount(token: string, did: string): Promise<void> {
435 await xrpc('com.atproto.admin.deleteAccount', {
436 method: 'POST',
437 token,
438 body: { did },
439 })
440 },
441
442 async describeRepo(token: string, repo: string): Promise<{
443 handle: string
444 did: string
445 didDoc: unknown
446 collections: string[]
447 handleIsCorrect: boolean
448 }> {
449 return xrpc('com.atproto.repo.describeRepo', {
450 token,
451 params: { repo },
452 })
453 },
454
455 async listRecords(token: string, repo: string, collection: string, options?: {
456 limit?: number
457 cursor?: string
458 reverse?: boolean
459 }): Promise<{
460 records: Array<{ uri: string; cid: string; value: unknown }>
461 cursor?: string
462 }> {
463 const params: Record<string, string> = { repo, collection }
464 if (options?.limit) params.limit = String(options.limit)
465 if (options?.cursor) params.cursor = options.cursor
466 if (options?.reverse) params.reverse = 'true'
467 return xrpc('com.atproto.repo.listRecords', { token, params })
468 },
469
470 async getRecord(token: string, repo: string, collection: string, rkey: string): Promise<{
471 uri: string
472 cid: string
473 value: unknown
474 }> {
475 return xrpc('com.atproto.repo.getRecord', {
476 token,
477 params: { repo, collection, rkey },
478 })
479 },
480
481 async createRecord(token: string, repo: string, collection: string, record: unknown, rkey?: string): Promise<{
482 uri: string
483 cid: string
484 }> {
485 return xrpc('com.atproto.repo.createRecord', {
486 method: 'POST',
487 token,
488 body: { repo, collection, record, rkey },
489 })
490 },
491
492 async putRecord(token: string, repo: string, collection: string, rkey: string, record: unknown): Promise<{
493 uri: string
494 cid: string
495 }> {
496 return xrpc('com.atproto.repo.putRecord', {
497 method: 'POST',
498 token,
499 body: { repo, collection, rkey, record },
500 })
501 },
502
503 async deleteRecord(token: string, repo: string, collection: string, rkey: string): Promise<void> {
504 await xrpc('com.atproto.repo.deleteRecord', {
505 method: 'POST',
506 token,
507 body: { repo, collection, rkey },
508 })
509 },
510
511 async getTotpStatus(token: string): Promise<{ enabled: boolean; hasBackupCodes: boolean }> {
512 return xrpc('com.atproto.server.getTotpStatus', { token })
513 },
514
515 async createTotpSecret(token: string): Promise<{ uri: string; qrBase64: string }> {
516 return xrpc('com.atproto.server.createTotpSecret', { method: 'POST', token })
517 },
518
519 async enableTotp(token: string, code: string): Promise<{ success: boolean; backupCodes: string[] }> {
520 return xrpc('com.atproto.server.enableTotp', {
521 method: 'POST',
522 token,
523 body: { code },
524 })
525 },
526
527 async disableTotp(token: string, password: string, code: string): Promise<{ success: boolean }> {
528 return xrpc('com.atproto.server.disableTotp', {
529 method: 'POST',
530 token,
531 body: { password, code },
532 })
533 },
534
535 async regenerateBackupCodes(token: string, password: string, code: string): Promise<{ backupCodes: string[] }> {
536 return xrpc('com.atproto.server.regenerateBackupCodes', {
537 method: 'POST',
538 token,
539 body: { password, code },
540 })
541 },
542
543 async startPasskeyRegistration(token: string, friendlyName?: string): Promise<{ options: unknown }> {
544 return xrpc('com.atproto.server.startPasskeyRegistration', {
545 method: 'POST',
546 token,
547 body: { friendlyName },
548 })
549 },
550
551 async finishPasskeyRegistration(token: string, credential: unknown, friendlyName?: string): Promise<{ id: string; credentialId: string }> {
552 return xrpc('com.atproto.server.finishPasskeyRegistration', {
553 method: 'POST',
554 token,
555 body: { credential, friendlyName },
556 })
557 },
558
559 async listPasskeys(token: string): Promise<{
560 passkeys: Array<{
561 id: string
562 credentialId: string
563 friendlyName: string | null
564 createdAt: string
565 lastUsed: string | null
566 }>
567 }> {
568 return xrpc('com.atproto.server.listPasskeys', { token })
569 },
570
571 async deletePasskey(token: string, id: string): Promise<void> {
572 await xrpc('com.atproto.server.deletePasskey', {
573 method: 'POST',
574 token,
575 body: { id },
576 })
577 },
578
579 async updatePasskey(token: string, id: string, friendlyName: string): Promise<void> {
580 await xrpc('com.atproto.server.updatePasskey', {
581 method: 'POST',
582 token,
583 body: { id, friendlyName },
584 })
585 },
586
587 async listTrustedDevices(token: string): Promise<{
588 devices: Array<{
589 id: string
590 userAgent: string | null
591 friendlyName: string | null
592 trustedAt: string | null
593 trustedUntil: string | null
594 lastSeenAt: string
595 }>
596 }> {
597 return xrpc('com.tranquil.account.listTrustedDevices', { token })
598 },
599
600 async revokeTrustedDevice(token: string, deviceId: string): Promise<{ success: boolean }> {
601 return xrpc('com.tranquil.account.revokeTrustedDevice', {
602 method: 'POST',
603 token,
604 body: { deviceId },
605 })
606 },
607
608 async updateTrustedDevice(token: string, deviceId: string, friendlyName: string): Promise<{ success: boolean }> {
609 return xrpc('com.tranquil.account.updateTrustedDevice', {
610 method: 'POST',
611 token,
612 body: { deviceId, friendlyName },
613 })
614 },
615
616 async getReauthStatus(token: string): Promise<{
617 requiresReauth: boolean
618 lastReauthAt: string | null
619 availableMethods: string[]
620 }> {
621 return xrpc('com.tranquil.account.getReauthStatus', { token })
622 },
623
624 async reauthPassword(token: string, password: string): Promise<{ success: boolean; reauthAt: string }> {
625 return xrpc('com.tranquil.account.reauthPassword', {
626 method: 'POST',
627 token,
628 body: { password },
629 })
630 },
631
632 async reauthTotp(token: string, code: string): Promise<{ success: boolean; reauthAt: string }> {
633 return xrpc('com.tranquil.account.reauthTotp', {
634 method: 'POST',
635 token,
636 body: { code },
637 })
638 },
639
640 async reauthPasskeyStart(token: string): Promise<{ options: unknown }> {
641 return xrpc('com.tranquil.account.reauthPasskeyStart', {
642 method: 'POST',
643 token,
644 })
645 },
646
647 async reauthPasskeyFinish(token: string, credential: unknown): Promise<{ success: boolean; reauthAt: string }> {
648 return xrpc('com.tranquil.account.reauthPasskeyFinish', {
649 method: 'POST',
650 token,
651 body: { credential },
652 })
653 },
654
655 async createPasskeyAccount(params: {
656 handle: string
657 email?: string
658 inviteCode?: string
659 didType?: DidType
660 did?: string
661 signingKey?: string
662 verificationChannel?: VerificationChannel
663 discordId?: string
664 telegramUsername?: string
665 signalNumber?: string
666 }): Promise<{
667 did: string
668 handle: string
669 setupToken: string
670 setupExpiresAt: string
671 }> {
672 return xrpc('com.tranquil.account.createPasskeyAccount', {
673 method: 'POST',
674 body: params,
675 })
676 },
677
678 async startPasskeyRegistrationForSetup(did: string, setupToken: string, friendlyName?: string): Promise<{ options: unknown }> {
679 return xrpc('com.tranquil.account.startPasskeyRegistrationForSetup', {
680 method: 'POST',
681 body: { did, setupToken, friendlyName },
682 })
683 },
684
685 async completePasskeySetup(did: string, setupToken: string, passkeyCredential: unknown, passkeyFriendlyName?: string): Promise<{
686 did: string
687 handle: string
688 appPassword: string
689 appPasswordName: string
690 }> {
691 return xrpc('com.tranquil.account.completePasskeySetup', {
692 method: 'POST',
693 body: { did, setupToken, passkeyCredential, passkeyFriendlyName },
694 })
695 },
696
697 async requestPasskeyRecovery(email: string): Promise<{ success: boolean }> {
698 return xrpc('com.tranquil.account.requestPasskeyRecovery', {
699 method: 'POST',
700 body: { email },
701 })
702 },
703
704 async recoverPasskeyAccount(did: string, recoveryToken: string, newPassword: string): Promise<{ success: boolean }> {
705 return xrpc('com.tranquil.account.recoverPasskeyAccount', {
706 method: 'POST',
707 body: { did, recoveryToken, newPassword },
708 })
709 },
710}