/** * API client utilities for CLI commands */ import type { ContractRouterClient } from "@orpc/contract"; import type { contract } from "@reviq/api-contract"; import { createORPCClient } from "@orpc/client"; import { RPCLink } from "@orpc/client/fetch"; import { readConfig } from "./config.js"; export type ApiClient = ContractRouterClient; /** * Create an oRPC API client with provided credentials */ export function createApiClient(apiUrl: string, token: string): ApiClient; /** * Create an oRPC API client with the stored credentials * Throws an error if not logged in */ export function createApiClient(): Promise; export function createApiClient( apiUrl?: string, token?: string, ): ApiClient | Promise { // If both arguments are provided, create client directly if (apiUrl !== undefined && token !== undefined) { const link = new RPCLink({ url: `${apiUrl}/api/v1/rpc`, headers: { "X-API-Key": token, }, }); return createORPCClient(link) as unknown as ApiClient; } // Otherwise, read from config asynchronously return (async (): Promise => { const config = await readConfig(); if (!config) { throw new Error( "Not logged in. Run 'reviq bootstrap' or 'reviq auth login' first.", ); } const link = new RPCLink({ url: `${config.apiUrl}/api/v1/rpc`, headers: { "X-API-Key": config.token, }, }); return createORPCClient(link) as unknown as ApiClient; })(); }