superjolt
Version:
AI-powered deployment platform with MCP support - Deploy JavaScript apps using natural language with Claude Desktop
199 lines ⢠8.35 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiService = void 0;
const common_1 = require("@nestjs/common");
const axios_1 = require("@nestjs/axios");
const rxjs_1 = require("rxjs");
const config_service_1 = require("./config.service");
const auth_service_1 = require("./auth.service");
const logger_service_1 = require("./logger.service");
let ApiService = class ApiService {
httpService;
configService;
authService;
logger;
constructor(httpService, configService, authService, logger) {
this.httpService = httpService;
this.configService = configService;
this.authService = authService;
this.logger = logger;
}
async createMachine() {
return this.request('POST', '/machine', {});
}
async listMachines() {
return this.request('GET', '/machines');
}
async deleteMachine(machineId) {
await this.request('DELETE', `/machine/${machineId}`);
}
async renameMachine(machineId, newName) {
return this.request('PUT', `/machine/${encodeURIComponent(machineId)}/rename`, { name: newName });
}
async listServices(machineId) {
const params = new URLSearchParams();
if (machineId) {
params.append('machineId', machineId);
}
const query = params.toString();
return this.request('GET', `/services${query ? `?${query}` : ''}`);
}
async getServiceLogs(serviceId, options = {}) {
const params = new URLSearchParams();
if (options.tail) {
params.append('tail', options.tail.toString());
}
const queryString = params.toString();
const path = `/service/${encodeURIComponent(serviceId)}/logs${queryString ? `?${queryString}` : ''}`;
return this.request('GET', path);
}
async startService(serviceId) {
return this.request('POST', `/service/${encodeURIComponent(serviceId)}/start`);
}
async stopService(serviceId) {
return this.request('POST', `/service/${encodeURIComponent(serviceId)}/stop`);
}
async restartService(serviceId) {
return this.request('POST', `/service/${encodeURIComponent(serviceId)}/restart`);
}
async renameService(serviceId, newName) {
return this.request('PUT', `/service/${encodeURIComponent(serviceId)}/rename`, { name: newName });
}
async deleteService(serviceId) {
return this.request('DELETE', `/service/${encodeURIComponent(serviceId)}`);
}
async resetAllResources() {
return this.request('DELETE', '/reset');
}
async getCurrentUser() {
return this.request('GET', '/me');
}
async setDefaultMachine(machineId) {
return this.request('PUT', '/machine/use', {
machineId,
});
}
async setEnvVars(serviceId, envVars) {
return this.request('POST', `/service/${encodeURIComponent(serviceId)}/env`, envVars);
}
async getEnvVar(serviceId, key) {
return this.request('GET', `/service/${encodeURIComponent(serviceId)}/env/${encodeURIComponent(key)}`);
}
async listEnvVars(serviceId) {
return this.request('GET', `/service/${encodeURIComponent(serviceId)}/env`);
}
async deleteEnvVar(serviceId, key) {
return this.request('DELETE', `/service/${encodeURIComponent(serviceId)}/env/${encodeURIComponent(key)}`);
}
async createCustomDomain(data) {
return this.request('POST', '/custom-domains', data);
}
async listCustomDomains(serviceId) {
const params = new URLSearchParams();
if (serviceId) {
params.append('serviceId', serviceId);
}
const query = params.toString();
return this.request('GET', `/custom-domains${query ? `?${query}` : ''}`);
}
async getCustomDomainStatus(domain) {
return this.request('GET', `/custom-domains/${encodeURIComponent(domain)}/status`);
}
async deleteCustomDomain(domain) {
return this.request('DELETE', `/custom-domains/${encodeURIComponent(domain)}`);
}
async request(method, path, data, retry = true) {
try {
const apiUrl = this.configService.getApiUrl();
const url = `${apiUrl}${path}`;
const headers = await this.getHeaders();
const config = { headers };
let response;
switch (method) {
case 'GET':
response = await (0, rxjs_1.firstValueFrom)(this.httpService.get(url, config));
break;
case 'POST':
response = await (0, rxjs_1.firstValueFrom)(this.httpService.post(url, data, config));
break;
case 'PUT':
response = await (0, rxjs_1.firstValueFrom)(this.httpService.put(url, data, config));
break;
case 'DELETE':
response = await (0, rxjs_1.firstValueFrom)(this.httpService.delete(url, config));
break;
}
return response.data;
}
catch (error) {
const loggerOptions = this.logger.silent;
if (error.response?.status === 401 && retry && !loggerOptions) {
await this.handleUnauthorized();
return this.request(method, path, data, false);
}
return this.handleError(error);
}
}
async getHeaders() {
const headers = {
'Content-Type': 'application/json',
};
const token = await this.authService.getToken();
if (token) {
headers['Authorization'] = `Bearer ${token}`;
}
return headers;
}
async handleUnauthorized() {
this.logger.log('\nš Authentication required. Please log in to continue.\n');
await this.authService.deleteToken();
await this.authService.performOAuthFlow();
}
async handleError(error) {
if (error.response) {
const errorData = {
message: error.response.data?.message || 'Unknown error',
statusCode: error.response.status,
error: error.response.data?.error,
};
switch (error.response.status) {
case 400:
throw new Error(`Bad Request: ${errorData.message}`);
case 401:
throw new Error('Authentication failed. Please run "superjolt login" first.');
case 403:
throw new Error('Forbidden: You do not have permission to perform this action');
case 404:
throw new Error(`Not Found: ${errorData.message}`);
case 503:
throw new Error('Service Unavailable: The API service is temporarily unavailable');
default:
throw new Error(`API Error (${errorData.statusCode}): ${errorData.message}`);
}
}
else if (error.request) {
throw new Error('Network Error: Unable to connect to the API. Please check your connection and API URL.');
}
else {
throw new Error(`Error: ${error.message}`);
}
}
};
exports.ApiService = ApiService;
exports.ApiService = ApiService = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [axios_1.HttpService,
config_service_1.ConfigService,
auth_service_1.AuthService,
logger_service_1.LoggerService])
], ApiService);
//# sourceMappingURL=api.service.js.map