Documentation

    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.

    Initialization

    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",
    });

    Methods

    uploadFile(file)

    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);
    • file: The file object to upload.

    Returns: An object containing the response from the server.

    deleteFile(fileId)

    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);
    • fileId: The ID of the file to delete.

    Returns: An object containing the response from the server.

    listFiles()

    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.

    Example Usage

    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);
      }
    })();

    Errors

    • Throws Error if required configuration is missing during initialization.
    • Throws Error if an API request fails or if required parameters are not provided.