export type CreateApiRemoteRpcRequestArgs = {service_name: string, port: number, timeout_ms?: number | undefined, base_url?: string | undefined, body: BodyInit}; export type CreateApiRemoteRpcRequestOptions = {baseUrl: string, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; /** * Execute Remote RPC * * POST /remote_rpc_request */ export const createApiRemoteRpcRequest = async (args: CreateApiRemoteRpcRequestArgs, options: CreateApiRemoteRpcRequestOptions) => { const {baseUrl, fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/octet-stream") if (args.service_name != null) query.append("service_name", String(args.service_name)) if (args.port != null) query.append("port", String(args.port)) if (args.timeout_ms != null) query.append("timeout_ms", String(args.timeout_ms)) if (args.base_url != null) query.append("base_url", String(args.base_url)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/remote_rpc_request${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: args.body}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return undefined };