2026-10-10 10:42:56 +00:00
|
|
|
import { csrfFetch } from "@metis/core/client";
|
2026-10-06 16:44:21 +00:00
|
|
|
import { z } from "zod/v4";
|
|
|
|
|
|
2026-10-08 17:25:02 +00:00
|
|
|
import type { RemoteConnectionInput } from "../../../../common/remote-connections";
|
2026-10-06 16:44:21 +00:00
|
|
|
import {
|
|
|
|
|
RemoteConnectionResponseSchema,
|
|
|
|
|
RemoteConnectionsResponseSchema,
|
2026-10-08 17:25:02 +00:00
|
|
|
} from "../../../../common/remote-connections";
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-06 16:44:21 +00:00
|
|
|
export const remoteKeys = { company: (id: string) => ["remote-connections", id] };
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-06 16:44:21 +00:00
|
|
|
export class RemoteApiError extends Error {
|
|
|
|
|
constructor(public readonly code: string) {
|
|
|
|
|
super(code);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-10 10:42:56 +00:00
|
|
|
async function request(companyId: string, values?: RemoteConnectionInput, id?: string) {
|
|
|
|
|
const response = await csrfFetch(
|
|
|
|
|
`/api/remote-connections/${companyId}${id ? `/${id}/update` : ""}`,
|
|
|
|
|
{
|
|
|
|
|
method: values ? "POST" : "GET",
|
|
|
|
|
credentials: "same-origin",
|
|
|
|
|
headers: values ? { "content-type": "application/json" } : {},
|
|
|
|
|
...(values ? { body: JSON.stringify({ values }) } : {}),
|
|
|
|
|
}
|
|
|
|
|
);
|
2026-10-06 16:44:21 +00:00
|
|
|
const body: unknown = await response.json();
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-06 16:44:21 +00:00
|
|
|
if (!response.ok) {
|
|
|
|
|
const error = z.object({ code: z.string() }).safeParse(body);
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-06 16:44:21 +00:00
|
|
|
throw new RemoteApiError(error.success ? error.data.code : "INTERNAL_ERROR");
|
|
|
|
|
}
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-06 16:44:21 +00:00
|
|
|
return body;
|
|
|
|
|
}
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-06 16:44:21 +00:00
|
|
|
export const remoteApi = {
|
|
|
|
|
async get(companyId: string) {
|
|
|
|
|
return RemoteConnectionsResponseSchema.parse(await request(companyId));
|
|
|
|
|
},
|
2026-10-10 10:42:56 +00:00
|
|
|
async save(companyId: string, values: RemoteConnectionInput, id?: string) {
|
|
|
|
|
return RemoteConnectionResponseSchema.parse(await request(companyId, values, id));
|
2026-10-06 16:44:21 +00:00
|
|
|
},
|
|
|
|
|
};
|