UNPKG

svector-sdk

Version:

Official JavaScript and TypeScript SDK for accessing SVECTOR APIs.

45 lines (44 loc) 1.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Files = void 0; const utils_1 = require("../utils"); class Files { constructor(client) { this.client = client; } async create(file, purpose = 'default', filename, options) { const formData = new FormData(); let fileToUpload; if (file instanceof File) { fileToUpload = file; } else { fileToUpload = await (0, utils_1.toFile)(file, filename); } formData.append('file', fileToUpload); formData.append('purpose', purpose); return this.client.request('POST', '/api/v1/files/', formData, { ...options, headers: { 'Accept': 'application/json', ...options?.headers, }, }); } async createFromPath(filePath, purpose = 'default', options) { if (typeof window !== 'undefined') { throw new Error('createFromPath() is only available in Node.js environments'); } try { const fs = require('fs'); const path = require('path'); const fileStream = fs.createReadStream(filePath); const fileName = path.basename(filePath); return this.create(fileStream, purpose, fileName, options); } catch (error) { throw new Error(`Failed to read file: ${error}`); } } } exports.Files = Files;