Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
httpSubscriptionLink,
retryLink,
splitLink,
TRPCClientError,
} from "@trpc/client";
import { Spinner } from "@std/cli/unstable-spinner";
import { error } from "./util.ts";
Expand All @@ -35,8 +36,10 @@ export function createTrpcClient(deployUrl: string) {
return createTRPCClient<any>({
links: [
retryLink({
retry() {
// TODO: check its an auth error
retry({ error }) {
if (error.message !== "Unauthorized") {
return false;
}

if (typeof retryPromise !== "undefined") {
return false;
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down