UNPKG

parea-ai

Version:

Client SDK library to connect to Parea AI.

83 lines (82 loc) 2.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.pareaProject = exports.Project = void 0; const PROJECT_ENDPOINT = '/project'; /** * Represents a project and provides methods for managing project-related operations. */ class Project { constructor() { this.client = null; } /** * Sets the HTTP client for making API requests. * @param client The HTTP client instance to be used for API calls. */ setClient(client) { this.client = client; } /** * Sets the name of the project. * @param projectName The name to be assigned to the project. */ setProjectName(projectName) { this.projectName = projectName; } /** * Retrieves the UUID of the project. * @returns A promise that resolves to the project UUID as a string. * @throws Error if the Parea Client is not instantiated. */ async getProjectUUID() { if (!this.client) { console.error('Parea Client not instantiated'); return ''; } if (!this.project) { return await this.getOrCreateProjectIfNecessary(); } return this.project.uuid; } /** * Retrieves an existing project or creates a new one if necessary. * @returns A promise that resolves to the project UUID as a string. * @throws Error if the Parea Client is not instantiated. */ async getOrCreateProjectIfNecessary() { if (!this.client) { console.error('Parea Client not instantiated'); return ''; } const projectName = this.projectName; try { const response = await this.client.request({ method: 'POST', endpoint: PROJECT_ENDPOINT, data: { name: projectName }, }); if (response.data) { const data = response.data; if (data.was_created) { console.log(`Created project ${projectName} with UUID ${data.uuid}`); } this.project = { name: projectName, uuid: data.uuid, createdAt: data.created_at, }; return data.uuid; } return ''; } catch (error) { console.error(`Failed to create project ${projectName} with error ${error}`); return ''; } } } exports.Project = Project; /** * A singleton instance of the Project class. */ exports.pareaProject = new Project();