@basictech/cli
Version:
Basic CLI for creating & managing your projects
133 lines • 4.28 kB
JavaScript
import { CONSTANTS } from './constants.js';
import { AuthService } from './auth.js';
import { ApiError, handleError } from './errors.js';
export class ApiClient {
static instance;
authService;
constructor() {
this.authService = AuthService.getInstance();
}
static getInstance() {
if (!ApiClient.instance) {
ApiClient.instance = new ApiClient();
}
return ApiClient.instance;
}
async request(endpoint, options = {}) {
try {
const token = await this.authService.getToken();
const response = await fetch(`${CONSTANTS.API_BASE}${endpoint}`, {
...options,
headers: {
'Content-Type': 'application/json',
...(token && { Authorization: `Bearer ${token.access_token}` }),
...options.headers,
},
});
if (!response.ok) {
const errorText = await response.text();
throw new ApiError(`API Error: ${response.status} - ${errorText}`, response.status);
}
return response.json();
}
catch (error) {
throw handleError(error);
}
}
async getProjects() {
const response = await this.request('/project');
return response.data;
}
async createProject(data) {
const response = await this.request('/project', {
method: 'POST',
body: JSON.stringify(data),
});
return response.data;
}
async createProjectWithTeam(name, slug, teamId) {
const response = await this.request('/project', {
method: 'POST',
body: JSON.stringify({
name,
slug,
team_id: teamId
}),
});
return response.data;
}
async getProject(projectId) {
const response = await this.request(`/project/${projectId}`);
return response.data;
}
async getTeams() {
const response = await this.request('/team');
return response.data;
}
async createTeam(name, slug) {
const response = await this.request('/team', {
method: 'POST',
body: JSON.stringify({ name, slug }),
});
return response.data;
}
async checkTeamSlugAvailability(slug) {
try {
const response = await this.request(`/team/slug?slug=${encodeURIComponent(slug)}`);
return response.available;
}
catch (error) {
// If there's an error checking availability, assume it's not available
return false;
}
}
async getProjectSchema(projectId) {
try {
const response = await this.request(`/project/${projectId}/schema`);
if (response.data.length === 0) {
return null;
}
return response.data[0].schema;
}
catch (error) {
if (error instanceof ApiError && error.statusCode === 404) {
return null;
}
throw error;
}
}
async pushProjectSchema(projectId, schema) {
await this.request(`/project/${projectId}/schema`, {
method: 'POST',
body: JSON.stringify({ schema }),
});
}
async validateSchema(schema) {
const response = await this.request('/utils/schema/verifyUpdateSchema', {
method: 'POST',
body: JSON.stringify({ schema }),
});
return response;
}
async compareSchema(schema) {
const response = await this.request('/utils/schema/compareSchema', {
method: 'POST',
body: JSON.stringify({ schema }),
});
return response;
}
async checkLatestRelease() {
try {
const response = await fetch(`https://registry.npmjs.org/@basictech/cli/latest`);
if (!response.ok) {
throw new Error('Failed to check for updates');
}
const data = await response.json();
return data.version;
}
catch (error) {
throw handleError(error);
}
}
}
//# sourceMappingURL=api.js.map