Add config and invite api

This commit is contained in:
Maximilian Harz 2025-06-25 20:50:34 +02:00
parent d5e730134e
commit b3c8b7fc67
5 changed files with 90 additions and 3 deletions

View File

@ -1 +1,5 @@
VITE_OPEN_COLLECTIVE_API_KEY=your_key
VITE_OPEN_COLLECTIVE_API_KEY=your_key
VITE_API_URL=https://api.utopia-lab.org
VITE_VALIDATE_INVITE_FLOW_ID=01d61db0-25aa-4bfa-bc24-c6a8f208a455
VITE_REDEEM_INVITE_FLOW_ID=todo

View File

@ -175,6 +175,7 @@ function App() {
<Quests />
<Routes>
<Route path='/*' element={<MapContainer map={map} layers={layers} />}>
<Route path='invite' element={<InvitePage />} />
<Route path='login' element={<LoginPage />} />
<Route path='signup' element={<SignupPage />} />
<Route

View File

@ -0,0 +1,69 @@
import { config } from '@/config'
interface InvitingProfileResponse {
id: string
}
export class inviteApi {
async getInvitingProfileId(inviteId: string): Promise<string | null> {
try {
const response = await fetch(
`${config.apiUrl}/flows/trigger/${config.validateInviteFlowId}/${inviteId}`,
{
method: 'GET',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
},
},
)
if (!response.ok) {
return null
}
const data = (await response.json()) as InvitingProfileResponse
return data.id
} catch (error: unknown) {
// eslint-disable-next-line no-console
console.error('Error fetching inviting profile:', error)
if (error instanceof Error && error.message) {
throw new Error(error.message)
} else {
throw new Error('An unknown error occurred while fetching the inviting profile.')
}
}
}
async validateInvite(inviteId: string): Promise<boolean> {
const invitingProfileId = await this.getInvitingProfileId(inviteId)
return invitingProfileId !== null
}
async redeemInvite(inviteId: string): Promise<boolean> {
try {
const response = await fetch(
`${config.apiUrl}/flows/trigger/${config.redeemInviteFlowId}/${inviteId}`,
{
method: 'GET',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
},
},
)
return response.ok
} catch (error: unknown) {
// eslint-disable-next-line no-console
console.error('Error fetching inviting profile:', error)
if (error instanceof Error && error.message) {
throw new Error(error.message)
} else {
throw new Error('An unknown error occurred while fetching the inviting profile.')
}
}
}
}

View File

@ -0,0 +1,7 @@
export const config = {
apiUrl: String(import.meta.env.VITE_API_URL ?? 'https://api.utopia-lab.org'),
validateInviteFlowId: String(
import.meta.env.VITE_VALIDATE_INVITE_FLOW_ID ?? '01d61db0-25aa-4bfa-bc24-c6a8f208a455',
),
redeemInviteFlowId: String(import.meta.env.VITE_REDEEM_INVITE_FLOW_ID ?? 'todo'),
}

View File

@ -17,8 +17,14 @@
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
"noFallthroughCasesInSwitch": true,
/* Paths */
"baseUrl": ".",
"paths": {
"@/*": ["src/*"],
}
},
"include": ["src"],
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
}