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