import { type InferOutput, array, nullable, strictObject, parse, pipe, string, uuid, literal, variant, } from "valibot"; type BaseRequestParams = Partial<{ path: string; body: RequestInit["body"]; method: "GET" | "POST"; }>; const getBaseRequest = ({ path, body, method }: BaseRequestParams): Request => { return new Request(`http://localhost:3040/${path}`, { headers: { userId: "1db09f34-b155-4bf2-b606-dda25365fc89" }, body, method, }); }; const sendImageResponseValidator = strictObject({ ID: pipe(string(), uuid()), ImageID: pipe(string(), uuid()), UserID: pipe(string(), uuid()), }); export const sendImage = async ( imageName: string, base64Image: string, ): Promise> => { const request = getBaseRequest({ 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 dataTypeValidator = variant("type", [ locationDataType, eventDataType, noteDataType, ]); const getUserImagesResponseValidator = array(dataTypeValidator); export type UserImage = InferOutput; export const getUserImages = async (): Promise => { const request = getBaseRequest({ path: "image" }); const res = await fetch(request).then((res) => res.json()); 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); }; export const postCode = async (email: string, code: string): Promise => { const request = getBaseRequest({ path: "code", body: JSON.stringify({ email, code }), method: "POST", }); await fetch(request).then((res) => res.json()); };