Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.
Go aialertsdashboarddockerdocker-monitoringgohomelabinfrastructure-monitoringkubernetesmonitoringmspproxmoxproxmox-backup-serverproxmox-mail-gatewayproxmox-veself-hostedtruenastypescriptvspherewebhooks
import type { PortalBootstrapData, PortalAccessMember } from './types';
import type { ProviderPlanAPI, ProviderPlanRefreshResult, ProviderPlanState } from './provider_plan';
interface PortalAPIContext {
getBootstrap(): PortalBootstrapData;
}
export class PortalAPIError extends Error {
status: number;
payload: unknown;
constructor(message: string, status = 0, payload: unknown = null) {
super(message);
this.name = 'PortalAPIError';
this.status = status;
this.payload = payload;
}
}
export interface PortalBillingResponse {
url?: string;
}
export interface PortalMagicLinkResponse {
message?: string;
}
export interface PortalWorkspaceCreateRequest {
display_name: string;
}
export interface PortalWorkspaceCreateResponse {
id?: string;
display_name?: string;
state?: string;
}
export interface PortalMemberInviteRequest {
email: string;
role: string;
}
export interface PortalMemberInviteResponse {
subject_id?: string;
user_id?: string;
state?: 'active' | 'pending';
}
export interface PortalMemberRoleRequest {
role: string;
}
export interface PortalAPI extends ProviderPlanAPI {
fetchBootstrap(): Promise<PortalBootstrapData>;
requestMagicLink(email: string): Promise<PortalMagicLinkResponse>;
logout(): Promise<void>;
getCommercialJSON<T>(path: string): Promise<T>;
postCommercialJSON<T>(path: string, body: Record<string, unknown>): Promise<T>;
createWorkspace(accountID: string, body: PortalWorkspaceCreateRequest): Promise<PortalWorkspaceCreateResponse>;
suspendWorkspace(accountID: string, tenantID: string): Promise<void>;
deleteWorkspace(accountID: string, tenantID: string): Promise<void>;
openBilling(accountID: string): Promise<PortalBillingResponse>;
listMembers(accountID: string): Promise<PortalAccessMember[]>;
inviteMember(accountID: string, body: PortalMemberInviteRequest): Promise<PortalMemberInviteResponse>;
updateMemberRole(accountID: string, userID: string, body: PortalMemberRoleRequest): Promise<void>;
removeMember(accountID: string, userID: string): Promise<void>;
}
export function createPortalAPI(context: PortalAPIContext): PortalAPI {
function bootstrap() {
return context.getBootstrap();
}
async function readPayload(response: Response): Promise<unknown> {
// Read the body exactly once. Calling response.json() speculatively and
// falling back to response.text() consumed the stream, so non-JSON error
// bodies (e.g. plain-text http.Error responses) were silently dropped.
var contentType = response.headers && typeof response.headers.get === 'function'
? response.headers.get('content-type') || ''
: '';
if (contentType.includes('application/json') && typeof response.json === 'function') {
try {
return await response.json();
} catch {
return null;
}
}
if (typeof response.text === 'function') {
var text = '';
try {
text = await response.text();
} catch {
return null;
}
if (!text) return null;
try {
return JSON.parse(text);
} catch {
return text;
}
}
if (typeof response.json === 'function') {
try {
return await response.json();
} catch {
return null;
}
}
return null;
}
// Control plane errors carry a machine code in "error" and, when there is
// something to tell the person, a sentence in "message". Show the sentence;
// older handlers put their only human text in "error", so fall back to it.
function messageFromPayload(payload: unknown, fallback: string): string {
if (payload && typeof payload === 'object') {
var message = (payload as { message?: unknown }).message;
if (typeof message === 'string' && message.trim()) {
return message;
}
var errorMessage = (payload as { error?: unknown }).error;
if (typeof errorMessage === 'string' && errorMessage.trim()) {
return errorMessage;
}
}
if (typeof payload === 'string' && payload.trim()) {
return payload;
}
return fallback;
}
async function request<T>(input: string, init: RequestInit, fallbackMessage: string): Promise<T> {
var response: Response;
try {
if (Object.keys(init).length > 0) {
response = await fetch(input, init);
} else {
response = await fetch(input);
}
} catch {
throw new PortalAPIError(fallbackMessage, 0, null);
}
var payload = await readPayload(response);
if (!response.ok) {
throw new PortalAPIError(messageFromPayload(payload, fallbackMessage), response.status, payload);
}
return payload as T;
}
function accountURL(accountID: string, suffix = ''): string {
return bootstrap().account_api_base_path + '/' + encodeURIComponent(accountID) + suffix;
}
return {
fetchBootstrap: function() {
return request<PortalBootstrapData>(bootstrap().bootstrap_path, {
headers: { Accept: 'application/json' },
}, 'Failed to refresh account state.');
},
requestMagicLink: function(email: string) {
return request<PortalMagicLinkResponse>(bootstrap().magic_link_request_path, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email: email, target: 'portal' }),
}, 'Failed to send magic link.');
},
logout: function() {
return request<void>(bootstrap().logout_path, {
method: 'POST',
}, 'Failed to sign out.');
},
getCommercialJSON: function<T>(path: string) {
return request<T>(bootstrap().commercial_api_base_url + path, {
headers: { Accept: 'application/json' },
}, 'Commercial request failed.');
},
postCommercialJSON: function<T>(path: string, body: Record<string, unknown>) {
return request<T>(bootstrap().commercial_api_base_url + path, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
}, 'Commercial request failed.');
},
createWorkspace: function(accountID: string, body: PortalWorkspaceCreateRequest) {
return request<PortalWorkspaceCreateResponse>(accountURL(accountID, '/tenants'), {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
}, 'Failed to create workspace.');
},
suspendWorkspace: function(accountID: string, tenantID: string) {
return request<void>(accountURL(accountID, '/tenants/' + encodeURIComponent(tenantID)), {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ state: 'suspended' }),
}, 'Failed to suspend workspace.');
},
deleteWorkspace: function(accountID: string, tenantID: string) {
return request<void>(accountURL(accountID, '/tenants/' + encodeURIComponent(tenantID)), {
method: 'DELETE',
}, 'Failed to delete workspace.');
},
openBilling: function(accountID: string) {
return request<PortalBillingResponse>(bootstrap().portal_api_base_path + '/billing?account_id=' + encodeURIComponent(accountID), {
method: 'POST',
}, 'Failed to open billing portal.');
},
listMembers: function(accountID: string) {
return request<PortalAccessMember[]>(accountURL(accountID, '/members'), {}, 'Failed to load access roster.');
},
inviteMember: function(accountID: string, body: PortalMemberInviteRequest) {
return request<PortalMemberInviteResponse>(accountURL(accountID, '/members'), {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
}, 'Failed to invite member.');
},
updateMemberRole: function(accountID: string, userID: string, body: PortalMemberRoleRequest) {
return request<void>(accountURL(accountID, '/members/' + encodeURIComponent(userID)), {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
}, 'Failed to update role.');
},
removeMember: function(accountID: string, userID: string) {
return request<void>(accountURL(accountID, '/members/' + encodeURIComponent(userID)), {
method: 'DELETE',
}, 'Failed to remove member.');
},
fetchPlan: function(accountID: string) {
return request<ProviderPlanState>(accountURL(accountID, '/provider-msp/plan'), {
headers: { Accept: 'application/json' },
}, 'Your plan could not be loaded.');
},
startCheckout: function(accountID: string, planVersion: string, billingCycle: string) {
return request<{ url?: string }>(accountURL(accountID, '/provider-msp/checkout'), {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ plan_version: planVersion, billing_cycle: billingCycle }),
}, 'Checkout is unavailable right now.');
},
openBillingPortal: function(accountID: string) {
return request<{ url?: string }>(accountURL(accountID, '/provider-msp/billing-portal'), {
method: 'POST',
}, 'Billing is unavailable right now.');
},
refreshLicense: function(accountID: string) {
return request<ProviderPlanRefreshResult>(accountURL(accountID, '/provider-msp/license/refresh'), {
method: 'POST',
}, 'The licence could not be refreshed right now.');
},
};
}