#Is AstroGlobal defined in non-astro files (Typescript)?

6 messages · Page 1 of 1 (latest)

bitter stratus
#

Hi, i'm trying to get the Astro.request object inside a typescript file - is this possible?
I'm using nodeJs and server mode (no static builds)

Use case: I want to use tRPC middlewares with Astro (without the astro-trpc plugin which works only for very narrow use cases)

How it works:
I have queries and mutations defined with trpc, and i'm building a trpc server instance to be able to use queries and mutations in SSR (instead of using a client instance, which i'm using on client side components)

https://trpc.io/docs/v10/server-side-calls

I've got everything working and a workaround would be to wrap router.createCaller(context) in a function

const rpcServer = (request: AstroRequest) => {
 // extract data from request object as const rpcContext = ....
 return router.createCaller(rpcContext)
}

But i'd like to avoid having to pass the request object whenever i want to use trpc calls inside .astro files

Edit: For clarification, this typescript file is only used server-side!

You may need to call your procedure(s) directly from the server, createCaller() function returns you an instance of RouterCaller able to execute query(ies) and mutation(s).

ionic heron
#

The Astro global is only defined in .astro files

ionic heron
#

I'm the creator of the astro-trpc tRPC adapter. To be honest I didn't use trpc a lot, when building the adapter I had one thing in mind: to be able to use tRPC in astro API routes.
If you have any suggestions please file an issue or if you want to help, submit a PR, it will be very welcome 🙂

bitter stratus
bitter stratus
#

i guess i’ll just go with the workaround