forked from
samuel.fm/statusphere-react
the statusphere demo reworked into a vite/react app in a monorepo
1import * as Lexicon from '@statusphere/lexicon'
2import type {
3 XyzStatusphereGetStatuses,
4 XyzStatusphereGetUser,
5 XyzStatusphereSendStatus,
6} from '@statusphere/lexicon'
7
8class StatusphereAgent extends Lexicon.AtpBaseClient {
9 constructor() {
10 super(StatusphereAgent.fetchHandler)
11 }
12
13 private static fetchHandler: Lexicon.AtpBaseClient['fetchHandler'] = (
14 path,
15 options,
16 ) => {
17 return fetch(path, {
18 ...options,
19 headers: {
20 'Content-Type': 'application/json',
21 },
22 credentials: 'include',
23 })
24 }
25}
26
27const agent = new StatusphereAgent()
28
29// API service
30export const api = {
31 // Login
32 async login(handle: string) {
33 const response = await fetch('/oauth/initiate', {
34 method: 'POST',
35 headers: {
36 'Content-Type': 'application/json',
37 },
38 credentials: 'include',
39 body: JSON.stringify({ handle }),
40 })
41
42 if (!response.ok) {
43 const error = await response.json()
44 throw new Error(error.error || 'Login failed')
45 }
46
47 return response.json()
48 },
49
50 // Logout
51 async logout() {
52 const response = await fetch('/oauth/logout', {
53 method: 'POST',
54 credentials: 'include',
55 })
56
57 if (!response.ok) {
58 throw new Error('Logout failed')
59 }
60
61 return response.json()
62 },
63
64 // Get current user
65 getCurrentUser(params: XyzStatusphereGetUser.QueryParams) {
66 return agent.xyz.statusphere.getUser(params)
67 },
68
69 // Get statuses
70 getStatuses(params: XyzStatusphereGetStatuses.QueryParams) {
71 return agent.xyz.statusphere.getStatuses(params)
72 },
73
74 // Create status
75 createStatus(params: XyzStatusphereSendStatus.InputSchema) {
76 return agent.xyz.statusphere.sendStatus(params)
77 },
78}
79
80export default api