resources/Projects.js

import { constructParams } from "../util.js";
function project(p) {
    return {
        ...p,
        created: new Date(p.created),
        modified: new Date(p.modified),
    };
}
/** Metafold projects endpoint. */
export class Projects {
    client;
    constructor(client) {
        this.client = client;
    }
    /**
     * List projects.
     *
     * @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", "user", "name", "created", or "modified".
     * @param {string} [params.q] - Query string. For details on syntax see the Metafold API docs.
     *   Supported search fields are: "id", "user", and "name".
     * @returns List of project resources.
     */
    async list({ sort, q } = {}) {
        const params = constructParams({ sort, q });
        const r = await this.client.get(`/projects`, { params });
        return r.data.map(project);
    }
    /**
     * Get a project.
     *
     * @param {string} [id] - Override ID of project to get. Defaults to client project ID.
     * @returns Project resource.
     */
    async get(id) {
        const r = await this.client.get(`/projects/${id ?? this.client.projectID}`);
        return project(r.data);
    }
    /**
     * Create a project.
     *
     * @param {string} name - Name of the project.
     * @param {string} [access] - Project access. By default projects are private.
     * @param {Object} [data] - Optional project data. This parameter accepts arbitrary
     *   JSON-serializable data. Helpful for tracking application state.
     * @returns Project resource.
     */
    async create(name, access = "private", data) {
        const payload = constructParams({ name, access, project: data });
        const r = await this.client.post(`/projects`, payload, {
            headers: { "Content-Type": "application/json" },
        });
        return project(r.data);
    }
    /**
     * Duplicate a project.
     *
     * @param {string} id - Project to duplicate.
     * @param {string} name - New project name.
     * @param {string} [access] - New project access. By default projects are private.
     * @returns Project resource.
     */
    async duplicate(id, name, access = "private") {
        const payload = constructParams({ source: id, name, access });
        const r = await this.client.post(`/projects`, payload, {
            headers: { "Content-Type": "application/json" },
        });
        return project(r.data);
    }
    /**
     * Update a project.
     *
     * @param {string} [id] - Override ID of project to update. Defaults to client project ID.
     * @param {Object} [params] - Optional update parameters.
     * @param {string} [params.name] - Optional project name.
     * @param {string} [params.access] - Optional project access.
     * @param {Object} [params.data] - Optional project data. This parameter accepts arbitrary
     *   JSON-serializable data. Helpful for tracking application state.
     * @param {Object} [params.graph] - Optional shape JSON.
     * @returns Updated project resource.
     */
    async update(id, { name, access, data, graph } = {}) {
        const payload = constructParams({ name, access, project: data, graph });
        const r = await this.client.patch(`/projects/${id ?? this.client.projectID}`, payload, {
            headers: { "Content-Type": "application/json" },
        });
        return project(r.data);
    }
    /**
     * Delete a project.
     *
     * @param {string} [id] - Override ID of project to delete. Defaults to client project ID.
     */
    async delete(id) {
        await this.client.delete(`/projects/${id ?? this.client.projectID}`);
    }
}