utopia-ui/app/src/api/inviteApi.ts
Max 1e7320b895
feat(app): qR invites (#267)
* Add component to show invite link (WIP)

* Show invite link with copy functionality and QR-Code, add tests

* Query secrets

* Update directus collections

* Add config and invite api

* Let vite resolve paths using tsconfig

* Redeem invite link when logged in or after logging in

* Redirect to inviting profile when redeeming

* Fix some logic with login and redeeming

* Use correct redeem flow

* Hide missing form error

* Add basic relations view

* Pass profile to redeem Api and adapt to changed redeem flow

* Remove unnecessary aliases in vite config

* Remove dead import

* gitignore mac specific file

* Remove lazy loading

* Fix linting

* add InviteApi import

* Change case of file name (tbd)

* Don't toast error if user profile was not loaded yet

* Fix casing

* avoid app crash when profile of a new item is opened

---------

Co-authored-by: Anton Tranelis <mail@antontranelis.de>
2025-07-11 13:37:05 +02:00

80 lines
2.1 KiB
TypeScript

/* @eslint-disable-next-line import/no-relative-parent-imports */
import { config } from '@/config'
import type { UserApi } from 'utopia-ui'
type InvitingProfileResponse = [
{
item: string
},
]
export class InviteApi {
userApi: UserApi
constructor(userApi: UserApi) {
this.userApi = userApi
}
async validateInvite(inviteId: string): Promise<string | null> {
try {
const response = await fetch(
`${config.apiUrl}/flows/trigger/${config.validateInviteFlowId}?secret=${inviteId}`,
{
method: 'GET',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
},
},
)
if (!response.ok) return null
const data = (await response.json()) as InvitingProfileResponse
return data[0].item
} 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 redeemInvite(inviteId: string, itemId: string): Promise<string | null> {
try {
const token = await this.userApi.getToken()
if (!token) {
throw new Error('User is not authenticated. Cannot redeem invite.')
}
const response = await fetch(`${config.apiUrl}/flows/trigger/${config.redeemInviteFlowId}`, {
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({ secret: inviteId, item: itemId }),
})
if (!response.ok) return null
return (await response.json()) as string
} 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.')
}
}
}
}