@asterai/client
Version:
client library for asterai.io
70 lines (69 loc) • 2.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AsteraiTeam = void 0;
const config_1 = require("./config");
/**
* Represents an asterai team.
*
* This can be used to fetch data from and control
* a team programmatically.
*
* The team is authenticated with the account's API key,
* therefore this should not be used in untrusted clients such as
* the client side of a public web app.
*/
class AsteraiTeam {
constructor(id, name, accountApiKey, apiBaseUrl) {
this.apiBaseUrl = config_1.DEFAULT_API_BASE_URL;
this.id = id;
this.name = name;
this.accountApiKey = accountApiKey;
if (apiBaseUrl) {
this.apiBaseUrl = apiBaseUrl;
}
}
static async fetch(args) {
const teams = await AsteraiTeam.list({
accountApiKey: args.accountApiKey,
apiBaseUrl: args.apiBaseUrl
});
return teams.find(t => t.id == args.teamId);
}
/**
* List teams from an account.
*/
static async list(args) {
const apiBaseUrl = args.apiBaseUrl ?? config_1.DEFAULT_API_BASE_URL;
let url = `${apiBaseUrl}/teams`;
const response = await fetch(url, {
method: "GET",
headers: {
"Authorization": `Bearer ${args.accountApiKey}`
},
});
const json = await response.json();
return json.teams.map(team => new AsteraiTeam(team.id, team.name, args.accountApiKey, args.apiBaseUrl));
}
async listAgents() {
let url = `${this.apiBaseUrl}/team/${this.id}/apps`;
const response = await fetch(url, {
method: "GET",
headers: {
"Authorization": `Bearer ${this.accountApiKey}`
},
});
const json = await response.json();
return json.apps;
}
async fetchAgent(args) {
let url = `${this.apiBaseUrl}/app/${args.agentId}/info`;
const response = await fetch(url, {
method: "GET",
headers: {
"Authorization": `Bearer ${this.accountApiKey}`
},
});
return await response.json();
}
}
exports.AsteraiTeam = AsteraiTeam;