diff --git a/src/projects/project.ts b/src/projects/project.ts index 995be1e..0f4ed2d 100644 --- a/src/projects/project.ts +++ b/src/projects/project.ts @@ -1,13 +1,13 @@ import ProjectsApiClient from './projectsApiClient'; /** - * Represents a MindsDB project and all supported operations. + * Represents a MindsDB project and all supported operation related to project. */ export default class Project { - /** API client to use for executing mlEngine operations. */ + /** API client to use for executing project operations. */ projectApiClient: ProjectsApiClient; - /** Name of the mlEngine. */ + /** Name of the project. */ name: string; /** diff --git a/src/projects/projectsApiClient.ts b/src/projects/projectsApiClient.ts index 5c84031..ecef4a4 100644 --- a/src/projects/projectsApiClient.ts +++ b/src/projects/projectsApiClient.ts @@ -3,19 +3,29 @@ import Project from './project'; /** Abstract class outlining Project operations supported by the SDK. */ export default abstract class ProjectsApiClient { /** - * Gets all MindsDB projects for the current user. + * Gets all MindsDB projects. + * * @returns {Promise>} - All projects. */ abstract getAllProjects(): Promise>; + /** + * Gets a MindsDB project by name. + * + * @param {string} name - Name of the project to get. + */ + abstract getProject(name: string): Promise; + /** * Creates a new MindsDB project. - * @param {string} name - Name of the project. + * + * @param {string} name - Name of the project to create. * @returns {Promise} - The created project. */ abstract createProject(name: string): Promise; /** + * Delete a MindsDB project by name. * * @param {string} name - Name of the project to delete. */ diff --git a/src/projects/projectsRestApiClient.ts b/src/projects/projectsRestApiClient.ts index b488bfe..f25edb7 100644 --- a/src/projects/projectsRestApiClient.ts +++ b/src/projects/projectsRestApiClient.ts @@ -20,7 +20,9 @@ export default class ProjectsRestApiClient extends ProjectsApiClient { /** * Constructor for Projects API client. + * @param {SqlApiClient} sqlClient - SQL API client to send all SQL query requests. * @param {Axios} client - Axios instance to send all HTTP requests. + * @param {HttpAuthenticator} authenticator - Authenticator to use for reauthenticate if needed. */ constructor( sqlClient: SqlApiClient, @@ -42,6 +44,7 @@ export default class ProjectsRestApiClient extends ProjectsApiClient { /** * Gets all MindsDB projects for the current user. + * * @returns {Promise>} - All projects. * @throws {MindsDbError} - Something went wrong fetching projects. */ @@ -56,15 +59,42 @@ export default class ProjectsRestApiClient extends ProjectsApiClient { if (!projectsResponse.data) { return []; } - return projectsResponse.data; + + const projects = projectsResponse.data.map((project: any) => { + return new Project(this, project.name); + }); + + return projects; } catch (error) { throw MindsDbError.fromHttpError(error, projectsUrl); } } /** + * Gets a MindsDB project by name. + * + * @param {string} name - Name of the project to get. + * @returns {Promise} - The project. + */ + override async getProject(name: string): Promise { + const projectsUrl = this.getProjectsUrl() + `/${name}`; + const { authenticator, client } = this; + + const projectResponse = await client.get( + projectsUrl, + getBaseRequestConfig(authenticator) + ); + if (!projectResponse.data) { + throw new MindsDbError('Project not found'); + } + + return new Project(this, projectResponse.data.name); + } + + /** + * Creates a new MindsDB project. * - * @param name - Name of the project to create. + * @param {string} name - Name of the project to create. * @returns {Promise} - The created project. */ override async createProject(name: string): Promise { @@ -79,6 +109,7 @@ export default class ProjectsRestApiClient extends ProjectsApiClient { } /** + * Delete a MindsDB project by name. * * @param {string} name - Name of the project to delete. */