@archon-inc/sdk
Version:
Integrate easily to our government platform using this SDK. More info on https://archon.inc/sdk
138 lines (137 loc) • 4.73 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Archon = void 0;
const axios_1 = __importStar(require("axios"));
class Archon {
constructor() {
this.rootUrl = "http://engine:3000";
this.publicUrl = "http://localhost:8333";
this.componentName = "UNSET_NAME";
if (Archon.instance) {
return Archon.instance;
}
Archon.instance = this;
}
setServiceToken(token) {
this.serviceToken = token;
}
setUserUuid(uuid) {
this.userId = uuid;
}
setRoleName(role) {
this.roleName = role;
}
setComponentName(name) {
this.componentName = name;
}
getComponentName() {
return this.componentName;
}
getPublicUrl() {
return this.publicUrl;
}
async authenticate() {
if (!this.serviceToken) {
// try to load the service token from the environment
if (process?.env?.SERVICE_TOKEN) {
this.serviceToken = process.env.SERVICE_TOKEN;
}
else {
throw new Error("Service token not provided");
}
}
const payload = {
serviceToken: this.serviceToken,
};
if (process.env.ARCHON_SERVICE_USERNAME && process.env.ARCHON_SERVICE_ROLENAME) {
payload["username"] = process.env.ARCHON_SERVICE_USERNAME;
payload["roleName"] = process.env.ARCHON_SERVICE_ROLENAME;
}
else if (this.userId && this.roleName) {
payload["userId"] = this.userId;
payload["roleName"] = this.roleName;
}
else {
throw new Error("Credentials not provided");
}
try {
const response = await (0, axios_1.default)({
method: "POST",
url: `${this.rootUrl}/local/serviceAuth`,
headers: {
'Content-Type': 'application/json',
},
data: payload
});
this.sessionId = response.data.session_id;
}
catch (error) {
if ((0, axios_1.isAxiosError)(error)) {
console.error("Error: " + error.response?.data);
console.error("Status " + error.response?.status);
}
else {
console.error(error);
}
}
}
async request(route, method, body) {
if (!this.sessionId) {
await this.authenticate();
}
try {
const response = await (0, axios_1.default)({
method: method,
url: `${this.rootUrl}${route}`,
headers: {
// if its not a get request, make the content type application/json
...(method ? { 'Content-Type': 'application/json' } : {}),
"x-session-id": this.sessionId,
},
data: body,
});
return response;
}
catch (error) {
if ((0, axios_1.isAxiosError)(error)) {
throw new Error(`Error (${error.response?.status || "unknown"}): ${error.response?.data || "Request failed"}`);
}
throw error;
}
}
}
const archonInstance = new Archon();
exports.Archon = archonInstance;