diff --git a/frontend/src/network/index.ts b/frontend/src/network/index.ts index 8b22a4b..ac35c4b 100644 --- a/frontend/src/network/index.ts +++ b/frontend/src/network/index.ts @@ -1,96 +1,102 @@ import { - array, - InferOutput, - null as Null, - nullable, - object, - parse, - pipe, - string, - uuid, + array, + InferOutput, + null as Null, + nullable, + object, + parse, + pipe, + string, + uuid, } from "valibot"; type BaseRequestParams = Partial<{ - path: string; - body: RequestInit["body"]; - method: "GET" | "POST"; + 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, - }); + 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()), + ID: pipe(string(), uuid()), + ImageID: pipe(string(), uuid()), + UserID: pipe(string(), uuid()), }); export const sendImage = async ( - imageName: string, - base64Image: string, + imageName: string, + base64Image: string, ): Promise> => { - const request = getBaseRequest({ - path: `image/${imageName}`, - body: base64Image, - method: "POST", - }); + const request = getBaseRequest({ + path: `image/${imageName}`, + body: base64Image, + method: "POST", + }); - request.headers.set("Content-Type", "application/base64"); + request.headers.set("Content-Type", "application/base64"); - const res = await fetch(request).then((res) => res.json()); + const res = await fetch(request).then((res) => res.json()); - return parse(sendImageResponseValidator, res); + return parse(sendImageResponseValidator, res); }; const getUserImagesResponseValidator = array( - object({ - ID: pipe(string(), uuid()), - Image: object({ - ID: pipe(string(), uuid()), - ImageName: string(), - Image: Null(), - }), - Tags: nullable( - array( - object({ - ID: pipe(string(), uuid()), - Tag: string(), - ImageID: pipe(string(), uuid()), - }), - ), - ), - Links: nullable( - array( - object({ - ID: pipe(string(), uuid()), - Links: string(), - ImageID: pipe(string(), uuid()), - }), - ), - ), - Text: nullable( - array( - object({ - ID: pipe(string(), uuid()), - ImageText: string(), - ImageID: pipe(string(), uuid()), - }), - ), - ), - }), + object({ + ID: pipe(string(), uuid()), + Image: object({ + ID: pipe(string(), uuid()), + ImageName: string(), + Image: Null(), + }), + Tags: nullable( + array( + object({ + ID: pipe(string(), uuid()), + TagID: pipe(string(), uuid()), + ImageID: pipe(string(), uuid()), + + Tag: object({ + ID: pipe(string(), uuid()), + Tag: string(), + UserID: pipe(string(), uuid()), + }), + }), + ), + ), + Links: nullable( + array( + object({ + ID: pipe(string(), uuid()), + Links: string(), + ImageID: pipe(string(), uuid()), + }), + ), + ), + Text: nullable( + array( + object({ + ID: pipe(string(), uuid()), + ImageText: string(), + ImageID: pipe(string(), uuid()), + }), + ), + ), + }), ); export const getUserImages = async (): Promise< - InferOutput + InferOutput > => { - const request = getBaseRequest({ path: "image" }); + const request = getBaseRequest({ path: "image" }); - const res = await fetch(request).then((res) => res.json()); + const res = await fetch(request).then((res) => res.json()); - return parse(getUserImagesResponseValidator, res); + return parse(getUserImagesResponseValidator, res); };