Zipline-Android-App/functions/zipline/user.ts
2025-01-27 16:02:42 +01:00

102 lines
2 KiB
TypeScript

import type { APIRecentFiles, APISelfUser } from "@/types/zipline";
import * as db from "@/functions/database";
import axios, { type AxiosError } from "axios";
// GET /api/user
export async function getCurrentUser(): Promise<APISelfUser | 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/user`, {
headers: {
Authorization: token,
},
});
return res.data.user;
} 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..."
}
}
// GET /api/user/recent
export async function getRecentFiles(): Promise<APIRecentFiles | 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/user/recent`, {
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..."
}
}
// GET /api/user/avatar
export async function getCurrentUserAvatar(): Promise<string | null> {
const token = db.get("token");
const url = db.get("url");
if (!url || !token) return null;
try {
const res = await axios.get(`${url}/api/user/avatar`, {
headers: {
Authorization: token,
},
});
return res.data;
} catch (e) {
return null;
}
}
type EditCurrentUserOptions = Partial<
Omit<
APISelfUser,
| "role"
| "id"
| "createdAt"
| "updatedAt"
| "oauthProviders"
| "totpSecret"
| "passkeys"
| "quota"
| "sessions"
| "view"
> & {
view?: Partial<APISelfUser["view"]>;
avatar?: string;
password?: string;
}
>;
export async function editCurrentUser(options: EditCurrentUserOptions = {}) {}