Skip to content

Commit c3d7d0e

Browse files
authored
fix: make trpc retry only occur for unauthorized case (#11)
1 parent 9a7f727 commit c3d7d0e

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

auth.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
httpSubscriptionLink,
1010
retryLink,
1111
splitLink,
12+
TRPCClientError,
1213
} from "@trpc/client";
1314
import { Spinner } from "@std/cli/unstable-spinner";
1415
import { error } from "./util.ts";
@@ -35,8 +36,10 @@ export function createTrpcClient(deployUrl: string) {
3536
return createTRPCClient<any>({
3637
links: [
3738
retryLink({
38-
retry() {
39-
// TODO: check its an auth error
39+
retry({ error }) {
40+
if (error.message !== "Unauthorized") {
41+
return false;
42+
}
4043

4144
if (typeof retryPromise !== "undefined") {
4245
return false;
@@ -54,6 +57,18 @@ export function createTrpcClient(deployUrl: string) {
5457
condition: (op) => op.type === "subscription",
5558
false: httpBatchStreamLink({
5659
url: deployUrl + "/api",
60+
fetch: async (url, options) => {
61+
// deno-lint-ignore no-explicit-any
62+
const response = await fetch(url, options as any);
63+
if (response.status === 401) {
64+
throw TRPCClientError.from({
65+
message: "Unauthorized",
66+
code: -32004,
67+
data: { httpStatus: 401, code: "NOT_AUTHENTICATED" },
68+
});
69+
}
70+
return response;
71+
},
5772
async headers() {
5873
if (retryPromise) {
5974
await retryPromise;
@@ -133,8 +148,6 @@ export async function interactive(deployUrl: string): Promise<
133148
Deno.exit(1);
134149
}
135150

136-
console.log(`${deployUrl}/auth/interactive`);
137-
138151
const body = await res.json();
139152

140153
return {

0 commit comments

Comments
 (0)