Coder Social home page Coder Social logo

Comments (6)

Ehesp avatar Ehesp commented on June 15, 2024 2

I don't think this package is needed with v10 right? I got it working like so:

// pages/api/trpc/[trpc].ts

import { APIRoute } from 'astro';
import { fetchRequestHandler } from '@trpc/server/adapters/fetch';
import { initTRPC } from '@trpc/server';

const t = initTRPC.create();
 
const router = t.router({
  // ... routes
});

export const all: APIRoute = ({ request }) => {
  return fetchRequestHandler({
    req: request,
    endpoint: '/api/trpc',
    router,
  });
};

export type AppRouter = typeof router;
// client file

export const trpc = createTRPCProxyClient<AppRouter>({
  links: [
    httpBatchLink({
      url: 'http://localhost:3000/api/trpc',
    }),
  ],
});

from astro-trpc.

carlos8v avatar carlos8v commented on June 15, 2024 2

I manage to fix a SSR error where the client request did not made to the createTRPCProxyClient in the astro frontmatter by intercepting query/mutation context and sending to the httpBatchLink.

// src/lib/client.ts
import type { AnyRouter } from '@trpc/server'

import { createTRPCProxyClient, httpBatchLink, TRPCLink } from '@trpc/client'
import type { AppRouter } from '../pages/api/trpc/[trpc]'

function astroSSRLink<TRouter extends AnyRouter>(): TRPCLink<TRouter> {
  return (runtime) => {
    return (props) => {
      const incomingReq = props.op.context?.incomingReq as Request

      return httpBatchLink({
        url: 'http://localhost:3000/api/trpc',
        fetch: (url, options) => fetch(url, {
          ...options,
          headers: incomingReq?.headers ? incomingReq.headers : options?.headers,
          credentials: 'same-origin'
        })
      })(runtime)(props)
    }
  }
}

export const trpc = createTRPCProxyClient<AppRouter>({
  links: [astroSSRLink()]
})

That way you can use trpc context to get client headers and cookies like that:

---
// src/pages/index.astro
import { trpc } from '@lib/client';

const user = await trpc.me.query(undefined, {
  context: { incomingReq: Astro.request } // <-- Redirect client request to trpc context
})
---

<UserComponent user={user} />
// src/pages/api/trpc/[trpc].ts
import type { APIContext } from 'astro'
import { initTRPC } from '@trpc/server'

const t = initTRPC.create()

const appRouter = t.router({
  me: t.procedure.query(async ({ ctx }) => {
    const { cookies } = ctx as APIContext
    if (!cookies.has('sessionId')) return null
    // ....
  })
})

export const all: APIRoute = (apiContext) => {
  return fetchRequestHandler({
    req: apiContext.request,
    endpoint: '/api/trpc',
    router: appRouter,
    createContext: () => apiContext
  })
}

from astro-trpc.

MoustaphaDev avatar MoustaphaDev commented on June 15, 2024 2

@Ehesp and @carlos8v Thank you so much for your feedback. I'll try your suggestions and see if it's time to deprecate this package since tRPC is that easy to setup now with the fetch adapter and performance is increased.

from astro-trpc.

MoustaphaDev avatar MoustaphaDev commented on June 15, 2024

Hi @unbiased-dev, thanks for reporting this. I'll update when I have some free time.

from astro-trpc.

Ehesp avatar Ehesp commented on June 15, 2024

Oh nice that's a neat way. I've currently made a caller via createCaller api which accepts a Request, and uses that to create my context object.

from astro-trpc.

MoustaphaDev avatar MoustaphaDev commented on June 15, 2024

It's been a while, didn't have enough time to consider this but that's it!
Thanks again!

from astro-trpc.

Related Issues (2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.