resources/Assets.js

import axios from "axios";
import { constructParams } from "../util.js";
function asset(a) {
    return {
        ...a,
        created: new Date(a.created),
        modified: new Date(a.modified),
    };
}
/** Metafold assets endpoint. */
export class Assets {
    client;
    constructor(client) {
        this.client = client;
    }
    /**
     * List assets.
     *
     * @param {Object} [params] - Optional list parameters.
     * @param {string} [params.sort] - Sort string. For details on syntax see the Metafold API docs.
     *   Supported sorting fields are: "id", "filename", "size", "created", or "modified".
     * @param {string} [params.q] - Query string. For details on syntax see the Metafold API docs.
     *   Supported search fields are: "id" and "filename".
     * @returns List of asset resources.
     */
    async list({ sort, q } = {}) {
        const params = constructParams({ sort, q });
        const r = await this.client.get(`/projects/${this.client.projectID}/assets`, { params });
        return r.data.map(asset);
    }
    /**
     * Get an asset.
     *
     * @param {string} id - ID of asset to get.
     * @returns Asset resource.
     */
    async get(id) {
        const r = await this.client.get(`/projects/${this.client.projectID}/assets/${id}`);
        return asset(r.data);
    }
    /**
     * Download an asset.
     *
     * @param {string} id - ID of asset to download.
     * @param {string} [responseType="stream"] - Type of response data.
     *   Options include: "arraybuffer", "document", "json", "text", or "stream".
     *   See {@link https://axios-http.com/docs/req_config|Axios docs} for details.
     * @returns Response object with asset data.
     */
    // TODO: Add support for axios onDownloadProgress callback
    async download(id, responseType = "stream") {
        const params = { download: true };
        const r = await this.client.get(`/projects/${this.client.projectID}/assets/${id}`, { params });
        return await axios.get(r.data.link, { responseType });
    }
    /**
     * Retrieve public asset download URL.
     *
     * @param {string} id - ID of asset to download.
     * @param {Object} [params] - Optional download parameters.
     * @param {string} [params.filename] - Filename of downloaded file.
     * @returns {string} Asset download URL. Note the URL is only valid from one hour of generation.
     */
    async downloadURL(id, { filename } = {}) {
        const params = constructParams({ download: true, filename });
        const r = await this.client.get(`/projects/${this.client.projectID}/assets/${id}`, { params });
        return r.data.link;
    }
    /**
     * Upload an asset.
     *
     * @param {any} data - Asset data to upload, typically a File or Blob.
     * @param {string} [filename] - Name of the file.
     * @returns Asset resource.
     */
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    async create(data, filename) {
        const form = new FormData();
        if (filename) {
            form.append("file", data, filename);
        }
        else {
            form.append("file", data);
        }
        const r = await this.client.post(`/projects/${this.client.projectID}/assets`, form);
        return asset(r.data);
    }
    /**
     * Update an asset.
     *
     * @param {string} id - ID of asset to update.
     * @param {any} data - Asset data to upload, typically a File or Blob.
     * @param {string} [filename] - Name of the file.
     * @returns Asset resource.
     */
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    async update(id, data, filename) {
        const form = new FormData();
        if (filename) {
            form.append("file", data, filename);
        }
        else {
            form.append("file", data);
        }
        const r = await this.client.patch(`/projects/${this.client.projectID}/assets/${id}`, form);
        return asset(r.data);
    }
    /**
     * Delete an asset.
     *
     * @param {string} id - ID of asset to delete.
     */
    async delete(id) {
        await this.client.delete(`/projects/${this.client.projectID}/assets/${id}`);
    }
}