storet-sdk
Version:
A powerful and easy-to-use SDK for integrating storet online storage into your applications. Provides seamless API access to create buckets, upload files, retrieve stored data, and manage cloud storage efficiently. Built for developers using Node.js, Reac
110 lines (97 loc) • 2.76 kB
JavaScript
import axios from "axios";
class StoretSdk {
constructor(apiKey, baseUrl = "http://localhost:5000/api") {
if (!apiKey) throw new Error("API Key is required");
this.apiKey = apiKey;
this.baseUrl = baseUrl;
this.axiosInstance = axios.create({
baseURL: this.baseUrl,
headers: { "x-api-key": this.apiKey },
});
}
// 🪣 Create a new bucket
async createBucket(bucketName) {
try {
const response = await this.axiosInstance.post("/buckets", {
name: bucketName,
});
return response.data;
} catch (error) {
throw error.response ? error.response.data : error;
}
}
// 🪣 Get all buckets
async getAllBuckets() {
try {
const response = await this.axiosInstance.get("/buckets");
return response.data;
} catch (error) {
throw error.response ? error.response.data : error;
}
}
// 🪣 Delete a bucket
async deleteBucket(bucketId) {
try {
const response = await this.axiosInstance.delete(`/buckets/${bucketId}`);
return response.data;
} catch (error) {
throw error.response ? error.response.data : error;
}
}
// 📂 Upload file to a bucket
async uploadFile(bucketId, file) {
try {
const formData = new FormData();
formData.append("file", file);
const response = await this.axiosInstance.post(
`/files/${bucketId}`,
formData,
{
headers: { "Content-Type": "multipart/form-data" },
}
);
return response.data;
} catch (error) {
throw error.response ? error.response.data : error;
}
}
// 📂 Get all files of the user
async getAllFiles() {
try {
const response = await this.axiosInstance.get("/files/user/all");
return response.data;
} catch (error) {
throw error.response ? error.response.data : error;
}
}
// 📂 Get details of a specific file
async getFile(fileId) {
try {
const response = await this.axiosInstance.get(`/files/view/${fileId}`);
return response.data;
} catch (error) {
throw error.response ? error.response.data : error;
}
}
// 📂 Get all files in a specific bucket
async getFilesInBucket(bucketId) {
try {
const response = await this.axiosInstance.get(
`/files/bucket/${bucketId}`
);
return response.data;
} catch (error) {
throw error.response ? error.response.data : error;
}
}
// ❌ Delete a file
async deleteFile(fileId) {
try {
const response = await this.axiosInstance.delete(`/files/${fileId}`);
return response.data;
} catch (error) {
throw error.response ? error.response.data : error;
}
}
}
export default StoretSdk;