Skip to content

Commit

Permalink
feature(client): setup tRPC in front-end (#15)
Browse files Browse the repository at this point in the history
* fix(server): update import path for fetchExternalData

* wip

* feature(client): set-up `tRPC` in front-end
  • Loading branch information
IdoBouskila authored Nov 29, 2024
1 parent 5195a6d commit 1806777
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 4 deletions.
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 }`);
});
File renamed without changes.
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

0 comments on commit 1806777

Please sign in to comment.