fix: realign sqlite path and stabilize client data
This commit is contained in:
+40
-15
@@ -1,27 +1,50 @@
|
||||
import axios from 'axios';
|
||||
import type { Drawing, Collection } from '../types';
|
||||
import axios from "axios";
|
||||
import type { Drawing, Collection } from "../types";
|
||||
|
||||
export const API_URL = import.meta.env.VITE_API_URL || '/api';
|
||||
export const API_URL = import.meta.env.VITE_API_URL || "/api";
|
||||
|
||||
export const api = axios.create({
|
||||
baseURL: API_URL,
|
||||
});
|
||||
|
||||
export const getDrawings = async (search?: string, collectionId?: string | null) => {
|
||||
const coerceTimestamp = (value: string | number | Date): number => {
|
||||
if (typeof value === "number") return value;
|
||||
if (value instanceof Date) return value.getTime();
|
||||
const parsed = Date.parse(value);
|
||||
return Number.isNaN(parsed) ? Date.now() : parsed;
|
||||
};
|
||||
|
||||
const deserializeDrawing = (drawing: any): Drawing => ({
|
||||
...drawing,
|
||||
createdAt: coerceTimestamp(drawing.createdAt),
|
||||
updatedAt: coerceTimestamp(drawing.updatedAt),
|
||||
});
|
||||
|
||||
export const getDrawings = async (
|
||||
search?: string,
|
||||
collectionId?: string | null
|
||||
) => {
|
||||
const params: any = {};
|
||||
if (search) params.search = search;
|
||||
if (collectionId !== undefined) params.collectionId = collectionId === null ? 'null' : collectionId;
|
||||
const response = await api.get<Drawing[]>('/drawings', { params });
|
||||
return response.data;
|
||||
if (collectionId !== undefined)
|
||||
params.collectionId = collectionId === null ? "null" : collectionId;
|
||||
const response = await api.get<Drawing[]>("/drawings", { params });
|
||||
return response.data.map(deserializeDrawing);
|
||||
};
|
||||
|
||||
export const getDrawing = async (id: string) => {
|
||||
const response = await api.get<Drawing>(`/drawings/${id}`);
|
||||
return response.data;
|
||||
return deserializeDrawing(response.data);
|
||||
};
|
||||
|
||||
export const createDrawing = async (name?: string, collectionId?: string | null) => {
|
||||
const response = await api.post<{ id: string }>('/drawings', { name, collectionId });
|
||||
export const createDrawing = async (
|
||||
name?: string,
|
||||
collectionId?: string | null
|
||||
) => {
|
||||
const response = await api.post<{ id: string }>("/drawings", {
|
||||
name,
|
||||
collectionId,
|
||||
});
|
||||
return response.data;
|
||||
};
|
||||
|
||||
@@ -36,22 +59,24 @@ export const deleteDrawing = async (id: string) => {
|
||||
};
|
||||
|
||||
export const duplicateDrawing = async (id: string) => {
|
||||
const response = await api.post<{ id: string }>(`/drawings/${id}/duplicate`);
|
||||
return response.data;
|
||||
const response = await api.post<Drawing>(`/drawings/${id}/duplicate`);
|
||||
return deserializeDrawing(response.data);
|
||||
};
|
||||
|
||||
export const getCollections = async () => {
|
||||
const response = await api.get<Collection[]>('/collections');
|
||||
const response = await api.get<Collection[]>("/collections");
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export const createCollection = async (name: string) => {
|
||||
const response = await api.post<Collection>('/collections', { name });
|
||||
const response = await api.post<Collection>("/collections", { name });
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export const updateCollection = async (id: string, name: string) => {
|
||||
const response = await api.put<{ success: true }>(`/collections/${id}`, { name });
|
||||
const response = await api.put<{ success: true }>(`/collections/${id}`, {
|
||||
name,
|
||||
});
|
||||
return response.data;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user