From 3352a3ea393849550573deff8d774ba6bf149471 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Wed, 24 Apr 2024 11:37:35 +0100 Subject: build(cli): Prepare for publishing CLI to npm --- apps/cli/src/lib/globals.ts | 17 +++++++++++++++++ apps/cli/src/lib/trpc.ts | 22 ++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 apps/cli/src/lib/globals.ts create mode 100644 apps/cli/src/lib/trpc.ts (limited to 'apps/cli/src/lib') diff --git a/apps/cli/src/lib/globals.ts b/apps/cli/src/lib/globals.ts new file mode 100644 index 00000000..771136da --- /dev/null +++ b/apps/cli/src/lib/globals.ts @@ -0,0 +1,17 @@ +export interface GlobalOptions { + apiKey: string; + serverAddr: string; +} + +export let globalOpts: GlobalOptions | undefined = undefined; + +export function setGlobalOptions(opts: GlobalOptions) { + globalOpts = opts; +} + +export function getGlobalOptions() { + if (!globalOpts) { + throw new Error("Global options are not initalized yet"); + } + return globalOpts; +} diff --git a/apps/cli/src/lib/trpc.ts b/apps/cli/src/lib/trpc.ts new file mode 100644 index 00000000..aa7aec21 --- /dev/null +++ b/apps/cli/src/lib/trpc.ts @@ -0,0 +1,22 @@ +import { getGlobalOptions } from "@/lib/globals"; +import { createTRPCClient, httpBatchLink } from "@trpc/client"; +import superjson from "superjson"; + +import type { AppRouter } from "@hoarder/trpc/routers/_app"; + +export function getAPIClient() { + const globals = getGlobalOptions(); + return createTRPCClient({ + links: [ + httpBatchLink({ + url: `${globals.serverAddr}/api/trpc`, + transformer: superjson, + headers() { + return { + authorization: `Bearer ${globals.apiKey}`, + }; + }, + }), + ], + }); +} -- cgit v1.2.3-70-g09d2