From 3edd0587e5e644ce238c0cad98ce37a11c9d72b5 Mon Sep 17 00:00:00 2001 From: Estifo77 <139631617+Estifo77@users.noreply.github.com> Date: Tue, 28 Jul 2026 14:50:53 +0300 Subject: [PATCH] document --- libs/api/src/lib/file-upload/index.ts | 44 ++++++++++++++++----------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/libs/api/src/lib/file-upload/index.ts b/libs/api/src/lib/file-upload/index.ts index cdf8b10ef..d0aa3f60e 100644 --- a/libs/api/src/lib/file-upload/index.ts +++ b/libs/api/src/lib/file-upload/index.ts @@ -1,6 +1,6 @@ -import { useCallback, useState } from 'react'; -import { baseApi } from '../base-api'; -import { resolveTokenFromStorage } from '../session'; +import { useCallback, useState } from "react"; +import { baseApi } from "../base-api"; +import { resolveTokenFromStorage } from "../session"; export interface UploadFileInfo { bucket: string; @@ -17,13 +17,16 @@ export interface UploadKeyResponse { const fileUploadApi = baseApi.injectEndpoints({ endpoints: (builder) => ({ - getFileUploadKey: builder.mutation({ + getFileUploadKey: builder.mutation< + UploadKeyResponse, + { endpoint: string; file: File } + >({ query: ({ endpoint, file }) => ({ url: endpoint, - method: 'POST', + method: "POST", body: { fileName: file.name, - contentType: file.type || 'application/octet-stream', + contentType: file.type || "application/octet-stream", size: file.size, originalname: file.name, }, @@ -35,29 +38,33 @@ const fileUploadApi = baseApi.injectEndpoints({ export const { useGetFileUploadKeyMutation } = fileUploadApi; -export const STORAGE_UPLOAD_ERROR = 'STORAGE_UPLOAD_ERROR'; -export const STORAGE_UPLOAD_TOO_LARGE = 'STORAGE_UPLOAD_TOO_LARGE'; +export const STORAGE_UPLOAD_ERROR = "STORAGE_UPLOAD_ERROR"; +export const STORAGE_UPLOAD_TOO_LARGE = "STORAGE_UPLOAD_TOO_LARGE"; -export function isStorageUploadError( - error: unknown, -): error is Error & { message: typeof STORAGE_UPLOAD_ERROR | typeof STORAGE_UPLOAD_TOO_LARGE } { +export function isStorageUploadError(error: unknown): error is Error & { + message: typeof STORAGE_UPLOAD_ERROR | typeof STORAGE_UPLOAD_TOO_LARGE; +} { return ( error instanceof Error && - (error.message === STORAGE_UPLOAD_ERROR || error.message === STORAGE_UPLOAD_TOO_LARGE) + (error.message === STORAGE_UPLOAD_ERROR || + error.message === STORAGE_UPLOAD_TOO_LARGE) ); } // Bare fetch on purpose: this PUT targets the presigned storage URL directly, not // VITE_BASE_API_URL, so it must not go through baseApi/fetchBaseQuery. -async function uploadToPresigned(file: File, presignedUrl: string): Promise { +async function uploadToPresigned( + file: File, + presignedUrl: string, +): Promise { if (!presignedUrl) throw new Error(STORAGE_UPLOAD_ERROR); const token = resolveTokenFromStorage(); const res = await fetch(presignedUrl, { - method: 'PUT', + method: "PUT", body: file, headers: { - 'Content-Type': file.type || 'application/octet-stream', + "Content-Type": file.type || "application/octet-stream", ...(token ? { Authorization: `Bearer ${token}` } : {}), }, }); @@ -73,10 +80,13 @@ export function useDocumentUpload() { const [isUploading, setIsUploading] = useState(false); const upload = useCallback( - async (file: File, endpoint: string): Promise => { + async (file: File, endpoint = "/documents/get-file-upload-key"): Promise => { setIsUploading(true); try { - const { presigned, fileInfo } = await getFileUploadKey({ endpoint, file }).unwrap(); + const { presigned, fileInfo } = await getFileUploadKey({ + endpoint, + file, + }).unwrap(); await uploadToPresigned(file, presigned); return { presigned, fileInfo }; } finally {