36 lines
1.2 KiB
TypeScript
36 lines
1.2 KiB
TypeScript
import type { HttpResult, HttpSpec, Transport, WsSession, WsSpec } from "@opsxdb/core";
|
|
import { browserTransport } from "./browser";
|
|
|
|
/**
|
|
* HTTP goes through Gin POST /v1/proxy/http.
|
|
* WS (MVP): falls back to browser WebSocket — custom headers require
|
|
* desktop/Gin tunnel later (Week 2).
|
|
*/
|
|
export function ginProxyTransport(baseUrl: string): Transport {
|
|
const root = baseUrl.replace(/\/$/, "");
|
|
|
|
return {
|
|
async http(spec: HttpSpec): Promise<HttpResult> {
|
|
const res = await fetch(`${root}/v1/proxy/http`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(spec),
|
|
});
|
|
if (!res.ok) {
|
|
const text = await res.text();
|
|
throw new Error(`gin-proxy HTTP ${res.status}: ${text}`);
|
|
}
|
|
return (await res.json()) as HttpResult;
|
|
},
|
|
|
|
ws(spec: WsSpec): WsSession {
|
|
// TODO(week2): custom headers need desktop/Gin WS tunnel.
|
|
// For MVP, fall back to browser WebSocket (no custom headers).
|
|
console.warn(
|
|
"[opsxdb/transport] gin-proxy WS falls back to browser WebSocket; custom headers need desktop/Gin tunnel later",
|
|
);
|
|
return browserTransport.ws(spec);
|
|
},
|
|
};
|
|
}
|