94 lines
2.2 KiB
TypeScript
94 lines
2.2 KiB
TypeScript
import {
|
|
type InferOutput,
|
|
array,
|
|
nullable,
|
|
object,
|
|
parse,
|
|
pipe,
|
|
string,
|
|
null_,
|
|
uuid,
|
|
literal,
|
|
variant,
|
|
date,
|
|
isoDate,
|
|
isoDateTime,
|
|
} 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: "fcc22dbb-7792-4595-be8e-d0439e13990a" },
|
|
body,
|
|
method,
|
|
});
|
|
};
|
|
|
|
const sendImageResponseValidator = object({
|
|
ID: pipe(string(), uuid()),
|
|
ImageID: pipe(string(), uuid()),
|
|
UserID: pipe(string(), uuid()),
|
|
});
|
|
|
|
export const sendImage = async (
|
|
imageName: string,
|
|
base64Image: string,
|
|
): Promise<InferOutput<typeof sendImageResponseValidator>> => {
|
|
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 = object({
|
|
ID: pipe(string(), uuid()),
|
|
Name: string(),
|
|
Address: nullable(string()),
|
|
Description: nullable(string()),
|
|
});
|
|
|
|
const eventValidator = object({
|
|
ID: pipe(string(), uuid()),
|
|
Name: string(),
|
|
StartDateTime: nullable(pipe(string())),
|
|
EndDateTime: nullable(pipe(string())),
|
|
Description: nullable(string()),
|
|
LocationID: nullable(pipe(string(), uuid())),
|
|
Location: null_(),
|
|
});
|
|
|
|
const locationDataType = object({
|
|
type: literal("location"),
|
|
data: locationValidator,
|
|
});
|
|
|
|
const eventDataType = object({
|
|
type: literal("event"),
|
|
data: eventValidator,
|
|
});
|
|
|
|
const dataTypeValidator = variant("type", [locationDataType, eventDataType]);
|
|
const getUserImagesResponseValidator = array(dataTypeValidator);
|
|
|
|
export type UserImage = InferOutput<typeof dataTypeValidator>;
|
|
|
|
export const getUserImages = async (): Promise<UserImage[]> => {
|
|
const request = getBaseRequest({ path: "image" });
|
|
|
|
const res = await fetch(request).then((res) => res.json());
|
|
|
|
return parse(getUserImagesResponseValidator, res);
|
|
};
|