Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | 332x 4134x 4134x 5456x 5456x 5198x 248x 5198x 332x 332x 4134x 66992x 4134x 5456x 5454x 4134x 4134x 248x 248x 6437x 332x | import type { QueryClientConfig } from "@tanstack/react-query";
import type { TRPCLink } from "@trpc/client";
import {
TRPCClientError,
httpBatchStreamLink,
httpLink,
splitLink,
} from "@trpc/client";
import type { AnyTRPCRouter } from "@trpc/server";
import { observable } from "@trpc/server/observable";
import { createTRPCContext } from "@trpc/tanstack-react-query";
import { omitBy } from "remeda";
import type { AppRouter } from "~app/trpc";
import { transformer } from "~utils/transformer";
type UnexpectedErrorLinkOptions<Router extends AnyTRPCRouter> = {
mapper: (error: TRPCClientError<Router>) => TRPCClientError<Router>;
};
const mapError =
<Router extends AnyTRPCRouter>({
mapper,
}: UnexpectedErrorLinkOptions<Router>): TRPCLink<Router> =>
() =>
({ next, op }) =>
observable((observer) =>
next(op).subscribe({
next: (value) => observer.next(value),
error: (error) => observer.error(mapper(error)),
complete: () => observer.complete(),
}),
);
declare module "@trpc/client" {
// oxlint-disable-next-line typescript/consistent-type-definitions
interface OperationContext {
batch: symbol;
}
}
export const noBatchContext = { batch: Symbol("no-batch") };
export type SimpleHeaders = Record<string, string>;
export type GetLinksOptions = {
url: string;
debug?: boolean;
useBatch?: boolean;
keepError?: boolean;
headers?: SimpleHeaders;
fetch?: Parameters<typeof httpLink>[0]["fetch"];
captureError: (error: Error) => string;
source: // Client-side rendering
| "csr"
// Client-side rendering via loader
| "csr-loader"
// Server-side rendering
| "ssr"
// Server-side rendering via loader
| "ssr-loader"
// React Native environment
| "native"
// Originated from artificial testing environment
| "test"
// API call from server-side code
| "api"
// Default unset value in context
| "unset";
};
export const getLinks = ({
url,
debug,
useBatch,
keepError,
source,
captureError,
headers: extraHeaders,
fetch,
}: GetLinksOptions): TRPCLink<AppRouter>[] => {
// we omit to not let stringified "undefined" get passed to the server
const headers = omitBy(
{
...extraHeaders,
"x-debug": debug ? "true" : undefined,
"x-source": source,
},
(value) => value === undefined,
);
const splitLinkInstance = splitLink({
condition: (op) => op.input instanceof FormData,
true: httpLink({ url, fetch, headers, transformer }),
false: splitLink({
condition: (op) =>
Boolean(useBatch && op.context.batch !== noBatchContext.batch),
true: httpBatchStreamLink({
url,
fetch,
headers,
transformer,
// Experimentally: 15k is ok
maxURLLength: 14_000,
}),
false: httpLink({ url, fetch, headers, transformer }),
}),
});
Iif (keepError) {
return [splitLinkInstance];
}
return [
mapError({
mapper: (error) => {
Iif (
error instanceof TRPCClientError &&
error.meta?.response instanceof Response &&
error.meta.response.status !== 200
) {
const transactionId = captureError(error);
return TRPCClientError.from(
new Error(
[
"Internal server error",
`Error fingerprint "${transactionId}"`,
...(import.meta.env.MODE === "test" ? [error.message] : []),
].join("\n"),
),
{ meta: error.meta },
);
}
return error;
},
}),
splitLinkInstance,
];
};
export const getQueryClientConfig = (): QueryClientConfig => ({
defaultOptions: {
queries: {
retry: false,
retryOnMount: true,
staleTime: Temporal.Duration.from({ minutes: 1 }).total("milliseconds"),
refetchOnWindowFocus: !import.meta.env.DEV,
},
},
});
export const { TRPCProvider, useTRPC } = createTRPCContext<AppRouter>();
|