@nestbox-ai/cli
Version:
The cli tools that helps developers to build agents
67 lines • 2.61 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.setupAuthAndConfig = setupAuthAndConfig;
exports.withAuth = withAuth;
const chalk_1 = __importDefault(require("chalk"));
const admin_1 = require("@nestbox-ai/admin");
const auth_1 = require("./auth");
/**
* Common authentication and configuration setup
* Returns authentication token and configured API client
* Exits the process if authentication fails
*/
function setupAuthAndConfig() {
const authToken = (0, auth_1.getAuthToken)();
if (!authToken) {
console.error(chalk_1.default.red('No authentication token found. Please login first.'));
return null;
}
const configuration = new admin_1.Configuration({
basePath: authToken.serverUrl,
baseOptions: {
headers: {
"Authorization": authToken.token,
}
}
});
return {
authToken,
configuration
};
}
/**
* Wrapper function that ensures authentication is set up before executing a callback
* Automatically handles authentication errors and provides configured API instances
*/
function withAuth(callback) {
return __awaiter(this, void 0, void 0, function* () {
const authResult = setupAuthAndConfig();
if (!authResult) {
return;
}
try {
return yield callback(authResult);
}
catch (error) {
if (error.response && error.response.status === 401) {
console.error(chalk_1.default.red('Authentication token has expired. Please login again using "nestbox login <domain>".'));
}
else {
throw error; // Re-throw non-auth errors
}
}
});
}
//# sourceMappingURL=api.js.map