linkar-uploader
Version:
```ts import { uploadFile } from "linkar-uploader";
41 lines (34 loc) • 1.12 kB
text/typescript
import axios from "axios";
import FormData from "form-data";
import fs from "fs";
import path from "path";
import { UploadedFileInterface } from "./interfaces/upload-file.interface";
/**
* 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
*/
export async function uploadFile(
filePath: string,
apiKey: string,
uploadUrl: string
): Promise<UploadedFileInterface> {
const resolvedPath = path.resolve(filePath);
if (!fs.existsSync(resolvedPath)) {
throw new Error(`File does not exist: ${resolvedPath}`);
}
const form = new FormData();
form.append("file", fs.createReadStream(resolvedPath));
const headers = {
"storage-api-key": apiKey,
...form.getHeaders(),
};
const response = await axios.post(uploadUrl, form, {
maxBodyLength: Infinity,
headers,
});
return response.data as UploadedFileInterface;
}