Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(client): setup tRPC in front-end #15

Merged
merged 3 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions client/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_API_URL = http://localhost:3000
19 changes: 18 additions & 1 deletion client/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
import App from './app.tsx';
import { StrictMode } from 'react';
import { trpc } from './utils/trpc.ts';
import { createRoot } from 'react-dom/client';
import { httpBatchLink } from '@trpc/react-query';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';

const queryClient = new QueryClient();

const trpcClient = trpc.createClient({
links: [
httpBatchLink({
url: import.meta.env.VITE_API_URL,
}),
],
});

createRoot(document.getElementById('root')!).render(
<StrictMode>
<App />
<trpc.Provider client={ trpcClient } queryClient={ queryClient }>
<QueryClientProvider client={ queryClient }>
<App />
</QueryClientProvider>
</trpc.Provider>
</StrictMode>
);
4 changes: 4 additions & 0 deletions client/src/utils/trpc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { AppRouter } from '@server/index.ts';
import { createTRPCReact } from '@trpc/react-query';

export const trpc = createTRPCReact<AppRouter>();
6 changes: 4 additions & 2 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ const appRouter = router({

const server = createHTTPServer({
router: appRouter,
middleware: cors(),
middleware: cors({ origin: '*' }),
});

server.listen(process.env.PORT || 3001);
server.listen(process.env.PORT || 3000, () => {
console.log(`🚀 Server ready at http://localhost:${ process.env.PORT || 3000 }`);
});
2 changes: 1 addition & 1 deletion server/utils/fetchers/fetchers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fetchExternalData from '../../fetch-external-data';
import fetchExternalData from './fetch-external-data';

type ExternalSearchResponse = Array<{
id: number;
Expand Down
Loading