fix: allowing nullable user lists
This commit is contained in:
@ -1,232 +1,234 @@
|
||||
import { fetch } from "@tauri-apps/plugin-http";
|
||||
|
||||
import {
|
||||
type InferOutput,
|
||||
array,
|
||||
literal,
|
||||
null_,
|
||||
nullable,
|
||||
parse,
|
||||
pipe,
|
||||
strictObject,
|
||||
string,
|
||||
transform,
|
||||
union,
|
||||
uuid,
|
||||
type InferOutput,
|
||||
array,
|
||||
literal,
|
||||
null_,
|
||||
nullable,
|
||||
parse,
|
||||
pipe,
|
||||
strictObject,
|
||||
string,
|
||||
transform,
|
||||
union,
|
||||
uuid,
|
||||
} from "valibot";
|
||||
|
||||
type BaseRequestParams = Partial<{
|
||||
path: string;
|
||||
body: RequestInit["body"];
|
||||
method: "GET" | "POST";
|
||||
path: string;
|
||||
body: RequestInit["body"];
|
||||
method: "GET" | "POST";
|
||||
}>;
|
||||
|
||||
// export const base = "https://haystack.johncosta.tech";
|
||||
export const base = "http://localhost:3040";
|
||||
|
||||
const getBaseRequest = ({ path, body, method }: BaseRequestParams): Request => {
|
||||
return new Request(`${base}/${path}`, {
|
||||
body,
|
||||
method,
|
||||
});
|
||||
return new Request(`${base}/${path}`, {
|
||||
body,
|
||||
method,
|
||||
});
|
||||
};
|
||||
|
||||
const getBaseAuthorizedRequest = ({
|
||||
path,
|
||||
body,
|
||||
method,
|
||||
path,
|
||||
body,
|
||||
method,
|
||||
}: BaseRequestParams): Request => {
|
||||
return new Request(`${base}/${path}`, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${localStorage.getItem("access")?.toString()}`,
|
||||
},
|
||||
body,
|
||||
method,
|
||||
});
|
||||
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(),
|
||||
ID: pipe(string(), uuid()),
|
||||
ImageID: pipe(string(), uuid()),
|
||||
UserID: pipe(string(), uuid()),
|
||||
Status: string(),
|
||||
});
|
||||
|
||||
export const sendImageFile = async (
|
||||
imageName: string,
|
||||
file: File,
|
||||
imageName: string,
|
||||
file: File,
|
||||
): Promise<InferOutput<typeof sendImageResponseValidator>> => {
|
||||
const request = getBaseAuthorizedRequest({
|
||||
path: `images/${imageName}`,
|
||||
body: file,
|
||||
method: "POST",
|
||||
});
|
||||
const request = getBaseAuthorizedRequest({
|
||||
path: `images/${imageName}`,
|
||||
body: file,
|
||||
method: "POST",
|
||||
});
|
||||
|
||||
request.headers.set("Content-Type", "application/oclet-stream");
|
||||
request.headers.set("Content-Type", "application/oclet-stream");
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
export const sendImage = async (
|
||||
imageName: string,
|
||||
base64Image: string,
|
||||
imageName: string,
|
||||
base64Image: string,
|
||||
): Promise<InferOutput<typeof sendImageResponseValidator>> => {
|
||||
const request = getBaseAuthorizedRequest({
|
||||
path: `images/${imageName}`,
|
||||
body: base64Image,
|
||||
method: "POST",
|
||||
});
|
||||
const request = getBaseAuthorizedRequest({
|
||||
path: `images/${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 imageMetaValidator = strictObject({
|
||||
ID: pipe(string(), uuid()),
|
||||
ImageName: string(),
|
||||
Description: string(),
|
||||
Image: null_(),
|
||||
ID: pipe(string(), uuid()),
|
||||
ImageName: string(),
|
||||
Description: string(),
|
||||
Image: null_(),
|
||||
});
|
||||
|
||||
const userImageValidator = strictObject({
|
||||
ID: pipe(string(), uuid()),
|
||||
CreatedAt: pipe(string()),
|
||||
ImageID: pipe(string(), uuid()),
|
||||
UserID: pipe(string(), uuid()),
|
||||
Image: strictObject({
|
||||
...imageMetaValidator.entries,
|
||||
ImageLists: array(
|
||||
strictObject({
|
||||
ID: pipe(string(), uuid()),
|
||||
ImageID: pipe(string(), uuid()),
|
||||
ListID: pipe(string(), uuid()),
|
||||
}),
|
||||
),
|
||||
}),
|
||||
ID: pipe(string(), uuid()),
|
||||
CreatedAt: pipe(string()),
|
||||
ImageID: pipe(string(), uuid()),
|
||||
UserID: pipe(string(), uuid()),
|
||||
Image: strictObject({
|
||||
...imageMetaValidator.entries,
|
||||
ImageLists: pipe(nullable(array(
|
||||
strictObject({
|
||||
ID: pipe(string(), uuid()),
|
||||
ImageID: pipe(string(), uuid()),
|
||||
ListID: pipe(string(), uuid()),
|
||||
}),
|
||||
)), transform(l => l ?? [])),
|
||||
}),
|
||||
});
|
||||
|
||||
const userProcessingImageValidator = strictObject({
|
||||
ID: pipe(string(), uuid()),
|
||||
ImageID: pipe(string(), uuid()),
|
||||
UserID: pipe(string(), uuid()),
|
||||
Image: imageMetaValidator,
|
||||
Status: union([
|
||||
literal("not-started"),
|
||||
literal("in-progress"),
|
||||
literal("complete"),
|
||||
]),
|
||||
ID: pipe(string(), uuid()),
|
||||
ImageID: pipe(string(), uuid()),
|
||||
UserID: pipe(string(), uuid()),
|
||||
Image: imageMetaValidator,
|
||||
Status: union([
|
||||
literal("not-started"),
|
||||
literal("in-progress"),
|
||||
literal("complete"),
|
||||
]),
|
||||
});
|
||||
|
||||
const listValidator = strictObject({
|
||||
ID: pipe(string(), uuid()),
|
||||
UserID: pipe(string(), uuid()),
|
||||
CreatedAt: pipe(string()),
|
||||
Name: string(),
|
||||
Description: nullable(string()),
|
||||
ID: pipe(string(), uuid()),
|
||||
UserID: pipe(string(), uuid()),
|
||||
CreatedAt: pipe(string()),
|
||||
Name: string(),
|
||||
Description: nullable(string()),
|
||||
|
||||
Images: pipe(
|
||||
nullable(
|
||||
array(
|
||||
strictObject({
|
||||
ID: pipe(string(), uuid()),
|
||||
ImageID: pipe(string(), uuid()),
|
||||
ListID: pipe(string(), uuid()),
|
||||
Items: array(
|
||||
strictObject({
|
||||
ID: pipe(string(), uuid()),
|
||||
ImageID: pipe(string(), uuid()),
|
||||
SchemaItemID: pipe(string(), uuid()),
|
||||
Value: string(),
|
||||
}),
|
||||
),
|
||||
}),
|
||||
),
|
||||
),
|
||||
transform((n) => n ?? []),
|
||||
),
|
||||
Images: pipe(
|
||||
nullable(
|
||||
array(
|
||||
strictObject({
|
||||
ID: pipe(string(), uuid()),
|
||||
ImageID: pipe(string(), uuid()),
|
||||
ListID: pipe(string(), uuid()),
|
||||
Items: array(
|
||||
strictObject({
|
||||
ID: pipe(string(), uuid()),
|
||||
ImageID: pipe(string(), uuid()),
|
||||
SchemaItemID: pipe(string(), uuid()),
|
||||
Value: string(),
|
||||
}),
|
||||
),
|
||||
}),
|
||||
),
|
||||
),
|
||||
transform((n) => n ?? []),
|
||||
),
|
||||
|
||||
Schema: strictObject({
|
||||
ID: pipe(string(), uuid()),
|
||||
ListID: pipe(string(), uuid()),
|
||||
SchemaItems: array(
|
||||
strictObject({
|
||||
ID: pipe(string(), uuid()),
|
||||
SchemaID: pipe(string(), uuid()),
|
||||
Item: string(),
|
||||
Value: nullable(string()),
|
||||
Description: string(),
|
||||
}),
|
||||
),
|
||||
}),
|
||||
Schema: strictObject({
|
||||
ID: pipe(string(), uuid()),
|
||||
ListID: pipe(string(), uuid()),
|
||||
SchemaItems: array(
|
||||
strictObject({
|
||||
ID: pipe(string(), uuid()),
|
||||
SchemaID: pipe(string(), uuid()),
|
||||
Item: string(),
|
||||
Value: nullable(string()),
|
||||
Description: string(),
|
||||
}),
|
||||
),
|
||||
}),
|
||||
});
|
||||
|
||||
export type List = InferOutput<typeof listValidator>;
|
||||
|
||||
const imageRequestValidator = strictObject({
|
||||
userImages: array(userImageValidator),
|
||||
processingImages: array(userProcessingImageValidator),
|
||||
lists: array(listValidator),
|
||||
userImages: array(userImageValidator),
|
||||
processingImages: array(userProcessingImageValidator),
|
||||
lists: array(listValidator),
|
||||
});
|
||||
|
||||
export type JustTheImageWhatAreTheseNames = InferOutput<
|
||||
typeof userImageValidator
|
||||
typeof userImageValidator
|
||||
>[];
|
||||
|
||||
export const getUserImages = async (): Promise<
|
||||
InferOutput<typeof imageRequestValidator>
|
||||
InferOutput<typeof imageRequestValidator>
|
||||
> => {
|
||||
const request = getBaseAuthorizedRequest({ path: "images" });
|
||||
const request = getBaseAuthorizedRequest({ path: "images" });
|
||||
|
||||
const res = await fetch(request).then((res) => res.json());
|
||||
const res = await fetch(request).then((res) => res.json());
|
||||
|
||||
return parse(imageRequestValidator, res);
|
||||
console.log("Backend response: ", res);
|
||||
|
||||
return parse(imageRequestValidator, res);
|
||||
};
|
||||
|
||||
export const postLogin = async (email: string): Promise<void> => {
|
||||
const request = getBaseRequest({
|
||||
path: "auth/login",
|
||||
body: JSON.stringify({ email }),
|
||||
method: "POST",
|
||||
});
|
||||
const request = getBaseRequest({
|
||||
path: "auth/login",
|
||||
body: JSON.stringify({ email }),
|
||||
method: "POST",
|
||||
});
|
||||
|
||||
await fetch(request);
|
||||
await fetch(request);
|
||||
};
|
||||
|
||||
const codeValidator = strictObject({
|
||||
access: string(),
|
||||
refresh: string(),
|
||||
access: string(),
|
||||
refresh: string(),
|
||||
});
|
||||
|
||||
export const postCode = async (
|
||||
email: string,
|
||||
code: string,
|
||||
email: string,
|
||||
code: string,
|
||||
): Promise<InferOutput<typeof codeValidator>> => {
|
||||
const request = getBaseRequest({
|
||||
path: "auth/code",
|
||||
body: JSON.stringify({ email, code }),
|
||||
method: "POST",
|
||||
});
|
||||
const request = getBaseRequest({
|
||||
path: "auth/code",
|
||||
body: JSON.stringify({ email, code }),
|
||||
method: "POST",
|
||||
});
|
||||
|
||||
const res = await fetch(request).then((res) => res.json());
|
||||
const res = await fetch(request).then((res) => res.json());
|
||||
|
||||
return parse(codeValidator, res);
|
||||
return parse(codeValidator, res);
|
||||
};
|
||||
|
||||
export const createList = async (
|
||||
title: string,
|
||||
description: string,
|
||||
title: string,
|
||||
description: string,
|
||||
): Promise<void> => {
|
||||
const request = getBaseAuthorizedRequest({
|
||||
path: "stacks",
|
||||
method: "POST",
|
||||
body: JSON.stringify({ title, description }),
|
||||
});
|
||||
const request = getBaseAuthorizedRequest({
|
||||
path: "stacks",
|
||||
method: "POST",
|
||||
body: JSON.stringify({ title, description }),
|
||||
});
|
||||
|
||||
request.headers.set("Content-Type", "application/json");
|
||||
request.headers.set("Content-Type", "application/json");
|
||||
|
||||
await fetch(request);
|
||||
await fetch(request);
|
||||
};
|
||||
|
Reference in New Issue
Block a user