elysiajs / elysia-trpc

A plugin for Elysia that add support for using tRPC
MIT License
20 stars 17 forks source link

readme example fails #12

Open JosephClay opened 11 months ago

JosephClay commented 11 months ago

Tried the code from the trpc example and it failed to greet from the client (404s). Only major difference from the example was adding cors (for local development/recreation)

Bun 1.0.2

server.ts

import { cors } from '@elysiajs/cors';
import { Elysia, t } from 'elysia';
import { trpc, compile as c } from '@elysiajs/trpc';
import { initTRPC } from '@trpc/server';

const r = initTRPC.create();

const router = r.router({
  greet: r.procedure.input(c(t.String())).query(({ input }) => input)
});

export type Router = typeof router

const app = new Elysia()
  .use(cors())
  .use(trpc(router))
  .listen(8080)

client.ts

import { createTRPCProxyClient, httpBatchLink } from '@trpc/client';
import type { Router } from './server';

const trpc = createTRPCProxyClient<App>({
  links: [
    httpBatchLink({
      url: 'http://localhost:8080',
    }),
  ],
});

const result = await trpc.greet.query('Hello Elysia');
console.log(result);

Confirmed that the client wasn't the issue by using a barebones trpc server file

server.trpc.ts

import { initTRPC } from '@trpc/server';
import { createHTTPServer } from '@trpc/server/adapters/standalone';
import cors from 'cors';
import { z } from 'zod';

export const t = initTRPC.create();

const appRouter = t.router({
  greet: t.procedure
    .input(z.object({ message: z.string() }))
    .query(({ input }) => input)
});

export type Router = typeof appRouter;

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

server.listen(8080);
olgertse commented 10 months ago

Just ran into the same issue and found a solution. By default, elysia-trpc uses /trpc endpoint; pointing your client to <your_url>/trpc sould resolve the issue, or you can pass a different configuration to the trpc() function.

smaccoun commented 8 months ago

Thanks @olgertse that worked for me. It'd be nice to have this documented