UNPKG

@replyke/core

Version:

Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.

64 lines 2.47 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const react_1 = require("react"); const useAxiosPrivate_1 = __importDefault(require("../../config/useAxiosPrivate")); const useProject_1 = __importDefault(require("../projects/useProject")); // Type guard to detect if it's a real browser File function isBrowserFile(file) { return typeof File !== "undefined" && file instanceof File; } function useUploadFile() { const axios = (0, useAxiosPrivate_1.default)(); const { projectId } = (0, useProject_1.default)(); const uploadFile = (0, react_1.useCallback)(async (file, pathParts, options) => { if (!projectId) { throw new Error("No projectId available."); } if (!file || !pathParts || !Array.isArray(pathParts)) { throw new Error("Invalid arguments. File and pathParts are required."); } const formData = new FormData(); // Append file (browser or React Native) if (isBrowserFile(file)) { formData.append("file", file, file.name); } else { formData.append("file", { uri: file.uri, type: file.type || "application/octet-stream", name: file.name, }); } // Append pathParts formData.append("pathParts", JSON.stringify(pathParts)); // Append optional associations if (options?.entityId) { formData.append("entityId", options.entityId); } if (options?.commentId) { formData.append("commentId", options.commentId); } if (options?.spaceId) { formData.append("spaceId", options.spaceId); } if (options?.position !== undefined) { formData.append("position", options.position.toString()); } if (options?.metadata) { formData.append("metadata", JSON.stringify(options.metadata)); } // Make the request const response = await axios.post(`/${projectId}/storage`, formData, { headers: { "Content-Type": "multipart/form-data", }, }); return response.data; }, [projectId, axios]); return uploadFile; } exports.default = useUploadFile; //# sourceMappingURL=useUploadFile.js.map