gensx
Version:
`GenSX command line tools.
85 lines • 2.68 kB
JavaScript
import { getAuth } from "../utils/config.js";
import { USER_AGENT } from "../utils/user-agent.js";
/**
* List projects
*/
export async function listProjects() {
const auth = await getAuth();
if (!auth) {
throw new Error("Not authenticated. Please run 'gensx login' first.");
}
const url = new URL(`/org/${auth.org}/projects`, auth.apiBaseUrl);
const response = await fetch(url.toString(), {
headers: {
Authorization: `Bearer ${auth.token}`,
"User-Agent": USER_AGENT,
},
});
if (!response.ok) {
throw new Error(`Failed to list projects: ${response.status} ${response.statusText}`);
}
const data = (await response.json());
return data.projects;
}
/**
* Create a project
*/
export async function createProject(projectName, environmentName, description) {
const auth = await getAuth();
if (!auth) {
throw new Error("Not authenticated. Please run 'gensx login' first.");
}
// Check if the environment already exists
const exists = await checkProjectExists(projectName);
if (exists) {
throw new Error(`Project ${projectName} already exists`);
}
const url = new URL(`/org/${auth.org}/projects`, auth.apiBaseUrl);
let body = {
name: projectName,
};
if (environmentName) {
body.environmentName = environmentName;
}
if (description) {
body.description = description;
}
const response = await fetch(url.toString(), {
method: "POST",
headers: {
Authorization: `Bearer ${auth.token}`,
"User-Agent": USER_AGENT,
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
if (!response.ok) {
throw new Error(`Failed to create project: ${response.status} ${response.statusText}`);
}
const data = (await response.json());
return data;
}
/**
* Check if a project exists
*/
export async function checkProjectExists(projectName) {
const auth = await getAuth();
if (!auth) {
throw new Error("Not authenticated. Please run 'gensx login' first.");
}
const url = new URL(`/org/${auth.org}/projects/${encodeURIComponent(projectName)}`, auth.apiBaseUrl);
const response = await fetch(url.toString(), {
headers: {
Authorization: `Bearer ${auth.token}`,
"User-Agent": USER_AGENT,
},
});
if (response.status === 404) {
return false;
}
if (!response.ok) {
throw new Error(`Failed to check project: ${response.status} ${response.statusText}`);
}
return true;
}
//# sourceMappingURL=projects.js.map