UNPKG

linkar-uploader

Version:

```ts import { uploadFile } from "linkar-uploader";

36 lines (35 loc) 1.38 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.uploadFile = uploadFile; const axios_1 = __importDefault(require("axios")); const form_data_1 = __importDefault(require("form-data")); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); /** * Uploads a file to a specified URL using a Linkar-style API key. * * @param filePath - Absolute or relative path to the file * @param apiKey - API key to authenticate the upload * @param uploadUrl - Full upload URL (e.g. https://storage.linkar.cc/api/v1/storage/single) * @returns Response data from the server */ async function uploadFile(filePath, apiKey, uploadUrl) { const resolvedPath = path_1.default.resolve(filePath); if (!fs_1.default.existsSync(resolvedPath)) { throw new Error(`File does not exist: ${resolvedPath}`); } const form = new form_data_1.default(); form.append("file", fs_1.default.createReadStream(resolvedPath)); const headers = { "storage-api-key": apiKey, ...form.getHeaders(), }; const response = await axios_1.default.post(uploadUrl, form, { maxBodyLength: Infinity, headers, }); return response.data; }