The Nafri class is a utility for handling file uploads, deletions, and listing files through an API. It requires a configuration object during initialization containing a baseUrl and an apiKey.
To initialize the Nafri class, provide a configuration object with baseUrl and apiKey. If these are missing, the class will throw an error.
const nafri = new Nafri({
baseUrl: "https://example.com/api",
apiKey: "your-api-key",
});Uploads a file to the server. Throws an error if the file is not provided or if the upload fails.
const response = await nafri.uploadFile(file);
console.log(response);Returns: An object containing the response from the server.
Deletes a file from the server using its file ID. Throws an error if the operation fails.
const response = await nafri.deleteFile("file-id");
console.log(response);Returns: An object containing the response from the server.
Retrieves a list of uploaded files from the server. Throws an error if the operation fails.
const files = await nafri.listFiles();
console.log(files);Returns: An array of file objects.
const nafri = new Nafri({
baseUrl: "https://example.com/api",
apiKey: "your-api-key",
});
(async () => {
try {
// Upload a file
const uploadResponse = await nafri.uploadFile(file);
console.log("File uploaded:", uploadResponse);
// List files
const files = await nafri.listFiles();
console.log("Files:", files);
// Delete a file
const deleteResponse = await nafri.deleteFile("file-id");
console.log("File deleted:", deleteResponse);
} catch (error) {
console.error("Error:", error);
}
})();Error if required configuration is missing during initialization.Error if an API request fails or if required parameters are not provided.