2022-12-26 03:52:48 +00:00
|
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
2022-12-26 05:12:23 +00:00
|
|
|
import { createTRPCReact, httpLink, loggerLink } from '@trpc/react-query';
|
2023-02-02 07:46:29 +00:00
|
|
|
import React, { useState } from 'react';
|
2022-12-26 03:52:48 +00:00
|
|
|
import fetch from 'isomorphic-fetch';
|
2023-02-02 07:46:29 +00:00
|
|
|
import superjson from 'superjson';
|
2022-12-26 03:52:48 +00:00
|
|
|
|
|
|
|
import type { AppRouter } from '../src/server/routers/_app';
|
|
|
|
|
|
|
|
export const trpc = createTRPCReact<AppRouter>({
|
|
|
|
unstable_overrides: {
|
|
|
|
useMutation: {
|
|
|
|
async onSuccess(opts) {
|
|
|
|
await opts.originalFn();
|
|
|
|
await opts.queryClient.invalidateQueries();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export function TRPCTestClientProvider(props: { children: React.ReactNode }) {
|
|
|
|
const { children } = props;
|
2023-02-02 07:46:29 +00:00
|
|
|
const [queryClient] = useState(
|
|
|
|
() =>
|
|
|
|
new QueryClient({
|
|
|
|
defaultOptions: {
|
|
|
|
queries: {
|
|
|
|
retry: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
const [trpcClient] = useState(() =>
|
|
|
|
trpc.createClient({
|
|
|
|
transformer: superjson,
|
|
|
|
links: [
|
|
|
|
loggerLink({
|
|
|
|
enabled: () => false,
|
|
|
|
}),
|
|
|
|
httpLink({
|
|
|
|
url: 'http://localhost:3000/api/trpc',
|
|
|
|
fetch: async (input, init?) =>
|
2023-02-12 17:44:10 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore - isomorphic-fetch is missing the `signal` option
|
2023-02-02 07:46:29 +00:00
|
|
|
fetch(input, {
|
|
|
|
...init,
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
);
|
2022-12-26 03:52:48 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<trpc.Provider client={trpcClient} queryClient={queryClient}>
|
|
|
|
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
|
|
|
</trpc.Provider>
|
|
|
|
);
|
|
|
|
}
|