Zipline-Android-App/functions/zipline/invites.ts
2025-02-11 18:15:54 +01:00

104 lines
1.9 KiB
TypeScript

import type { APIInvite, APIInvites } from "@/types/zipline";
import axios, { type AxiosError } from "axios";
import * as db from "@/functions/database";
// GET /api/auth/invites
export async function getInvites(): Promise<APIInvites | string> {
const token = db.get("token");
const url = db.get("url");
if (!url || !token) return "Invalid token or URL";
try {
const res = await axios.get(`${url}/api/auth/invites`, {
headers: {
Authorization: token,
},
});
return res.data;
} catch (e) {
const error = e as AxiosError;
const data = error.response?.data as
| {
error: string;
statusCode: number;
}
| undefined;
if (data) return data.error;
return "Something went wrong...";
}
}
export async function createInvite(
expiresAt?: string,
maxUses?: number,
): Promise<APIInvite | string> {
const token = db.get("token");
const url = db.get("url");
if (!url || !token) return "Invalid token or URL";
try {
const res = await axios.post(
`${url}/api/auth/invites`,
{
expiresAt,
maxUses,
},
{
headers: {
Authorization: token,
},
},
);
return res.data;
} catch (e) {
const error = e as AxiosError;
const data = error.response?.data as
| {
error: string;
statusCode: number;
}
| undefined;
if (data) return data.error;
return "Something went wrong...";
}
}
export async function deleteInvite(code: string): Promise<APIInvite | string> {
const token = db.get("token");
const url = db.get("url");
if (!url || !token) return "Invalid token or URL";
try {
const res = await axios.delete(`${url}/api/auth/invites/${code}`, {
headers: {
Authorization: token,
},
});
return res.data;
} catch (e) {
const error = e as AxiosError;
const data = error.response?.data as
| {
error: string;
statusCode: number;
}
| undefined;
if (data) return data.error;
return "Something went wrong...";
}
}