diff --git a/auth.ts b/auth.ts index 25dfbb4..a608947 100644 --- a/auth.ts +++ b/auth.ts @@ -9,6 +9,7 @@ import { httpSubscriptionLink, retryLink, splitLink, + TRPCClientError, } from "@trpc/client"; import { Spinner } from "@std/cli/unstable-spinner"; import { error } from "./util.ts"; @@ -35,8 +36,10 @@ export function createTrpcClient(deployUrl: string) { return createTRPCClient({ links: [ retryLink({ - retry() { - // TODO: check its an auth error + retry({ error }) { + if (error.message !== "Unauthorized") { + return false; + } if (typeof retryPromise !== "undefined") { return false; @@ -54,6 +57,18 @@ export function createTrpcClient(deployUrl: string) { condition: (op) => op.type === "subscription", false: httpBatchStreamLink({ url: deployUrl + "/api", + fetch: async (url, options) => { + // deno-lint-ignore no-explicit-any + const response = await fetch(url, options as any); + if (response.status === 401) { + throw TRPCClientError.from({ + message: "Unauthorized", + code: -32004, + data: { httpStatus: 401, code: "NOT_AUTHENTICATED" }, + }); + } + return response; + }, async headers() { if (retryPromise) { await retryPromise; @@ -133,8 +148,6 @@ export async function interactive(deployUrl: string): Promise< Deno.exit(1); } - console.log(`${deployUrl}/auth/interactive`); - const body = await res.json(); return {