33 lines
2.9 KiB
TypeScript
33 lines
2.9 KiB
TypeScript
export type B2Contour = { id: string; key: string; name: string; access?: string };
|
|
export type B2Application = { id: string; key: string; name: string; authority: 'host' | 'contours';
|
|
grants: Record<string, string>; credentials: Array<{ client_id: string; enabled: boolean }> };
|
|
export type B2Status = { connected: boolean; endpoint?: string; authority?: 'host' | 'contours';
|
|
contours?: B2Contour[]; applications?: B2Application[];
|
|
links?: Array<{ contourId: string; projectRef: string }> };
|
|
export type B2Device = { id: string; identifier: { masked: string }; name: string; active: boolean; number: number; contourId: string };
|
|
const messages: Record<string, string> = {
|
|
b2_infrastructure_unavailable: 'Подключение ARNavi пока не настроено на сервере Device Core.',
|
|
b2_host_unavailable: 'VPS не отвечает. Проверьте доступность сервера.',
|
|
b2_host_not_connected: 'Сначала подключите VPS.',
|
|
b2_access_denied: 'Ключ отозван или не даёт доступа к этому действию.',
|
|
b2_key_rejected: 'VPS не принял ключ подключения.',
|
|
b2_conflict: 'На VPS уже есть запись с такими данными. Обновите список и проверьте результат.',
|
|
b2_contour_already_linked: 'Этот контур уже связан с другим проектом.',
|
|
b2_connection_identity_conflict: 'Этот хост уже подключён другим приложением или ключом сервера.',
|
|
b2_certificate_mismatch: 'Сертификат VPS не соответствует ключу подключения.',
|
|
b2_private_storage_unavailable: 'Закрытое хранилище ключей ещё не настроено на Device Core.',
|
|
b2_connection_invalid: 'Не удалось прочитать ключ подключения ARNavi VPS.',
|
|
b2_company_mismatch: 'Выберите проект той же компании.',
|
|
device_owner_scope_access_denied: 'Настройки общего VPS доступны администратору компании.',
|
|
};
|
|
export async function b2Request<T>(projectRef: string, hostRef: string, action: string, fields: Record<string, unknown> = {}, requestId = crypto.randomUUID().replaceAll('-', '')): Promise<T> {
|
|
const response = await fetch('/api/device-manager/b2/infrastructure', {
|
|
method: 'POST', credentials: 'same-origin',
|
|
headers: { 'Content-Type': 'application/json', 'Idempotency-Key': requestId },
|
|
body: JSON.stringify({ projectRef, hostRef, action, ...fields }),
|
|
});
|
|
const body = await response.json().catch(() => null);
|
|
if (!response.ok || body?.ok !== true) throw new Error(messages[body?.error] || 'Не удалось выполнить действие на VPS. Обновите сведения о подключении.');
|
|
return body.result as T;
|
|
}
|