import { fetch } from "@tauri-apps/plugin-http"; import { type InferOutput, array, literal, nullable, parse, pipe, strictObject, string, uuid, variant, } from "valibot"; type BaseRequestParams = Partial<{ path: string; body: RequestInit["body"]; method: "GET" | "POST"; }>; export const base = import.meta.env.DEV ? "http://localhost:3040" : "https://haystack.johncosta.tech"; const getBaseRequest = ({ path, body, method }: BaseRequestParams): Request => { return new Request(`${base}/${path}`, { body, method, }); }; const getBaseAuthorizedRequest = ({ path, body, method, }: BaseRequestParams): Request => { return new Request(`${base}/${path}`, { headers: { Authorization: `Bearer ${localStorage.getItem("access")?.toString()}`, }, body, method, }); }; const sendImageResponseValidator = strictObject({ ID: pipe(string(), uuid()), ImageID: pipe(string(), uuid()), UserID: pipe(string(), uuid()), Status: string(), }); export const sendImage = async ( imageName: string, base64Image: string, ): Promise> => { const request = getBaseAuthorizedRequest({ path: `image/${imageName}`, body: base64Image, method: "POST", }); request.headers.set("Content-Type", "application/base64"); const res = await fetch(request).then((res) => res.json()); return parse(sendImageResponseValidator, res); }; const locationValidator = strictObject({ ID: pipe(string(), uuid()), Name: string(), Address: nullable(string()), Description: nullable(string()), }); const contactValidator = strictObject({ ID: pipe(string(), uuid()), Name: string(), Description: nullable(string()), PhoneNumber: nullable(string()), Email: nullable(string()), }); const eventValidator = strictObject({ ID: pipe(string(), uuid()), Name: string(), StartDateTime: nullable(pipe(string())), EndDateTime: nullable(pipe(string())), Description: nullable(string()), LocationID: nullable(pipe(string(), uuid())), // Location: nullable(locationValidator), OrganizerID: nullable(pipe(string(), uuid())), // Organizer: nullable(contactValidator), }); const noteValidator = strictObject({ ID: pipe(string(), uuid()), Name: string(), Description: nullable(string()), Content: string(), }); const locationDataType = strictObject({ type: literal("location"), data: locationValidator, }); const eventDataType = strictObject({ type: literal("event"), data: eventValidator, }); const noteDataType = strictObject({ type: literal("note"), data: noteValidator, }); const contactDataType = strictObject({ type: literal("contact"), data: contactValidator, }); const dataTypeValidator = variant("type", [ locationDataType, eventDataType, noteDataType, contactDataType, ]); const getUserImagesResponseValidator = array(dataTypeValidator); export type UserImage = InferOutput; export const getUserImages = async (): Promise => { const request = getBaseAuthorizedRequest({ path: "image" }); const res = await fetch(request).then((res) => res.json()); console.log("BACKEND RESPONSE: ", res); return parse(getUserImagesResponseValidator, res); }; export const postLogin = async (email: string): Promise => { const request = getBaseRequest({ path: "login", body: JSON.stringify({ email }), method: "POST", }); await fetch(request); }; const codeValidator = strictObject({ access: string(), refresh: string(), }); export const postCode = async ( email: string, code: string, ): Promise> => { const request = getBaseRequest({ path: "code", body: JSON.stringify({ email, code }), method: "POST", }); const res = await fetch(request).then((res) => res.json()); return parse(codeValidator, res); };