Mugetsu
How to get a TRPCClientError shape as in initTRPC errorFormatter for testing
I want to test some UI logic handling errors from trpc but I struggle how to setup the error shape in the test so it triggers proper branches while test.
here is my initTrpc
Handler I want to test:
And How can I test it???
4 replies
How to get mutation onError typescript type
I have two mutations on my frontend that should handle the response in the same way. So I extract the onError function so these two mutations can re-use the logic but I have a problem on how to type the error so the in function handling is pleased with typescript and the onError handler is also pleased.
I have tried like
error: TRPCClientError<AppRouter>
this does satisfy the function and internal of it but doesn't comply with the onError handlers onError: onApiError
2 replies
Value attached to all queries
Is it possible to add a value the all queries so its preset within every procedure call on the client?? Lets say I have a cookie which Is determied on the server and I want it to be added to every response or the trpc query calls. Is it possible to add it in a single place rather adding it in every procedure I make making it very prone to error because I can forget it ?
2 replies
onError callback type
I want to have a callback onError passed from parent component to the child which has mutation call. onError should be passed directly to the mutation options but also accept plan Error type and undefined. But I struggle how to type it correctly.
6 replies
LoggerLink logging only via server logger in prod
I have a logger link setup as declared in https://trpc.io/docs/client/links/loggerLink#usage
This is logging to console.log by default as I understand correctly. I have a specific logger instance using pino which can be used only on the server side. How do I acommodate this loggerlink to log errors via my logger instance only if it ERRORS in PRODUCTION?
3 replies
Express tRPC NextJS
I've custom expressjs server with Nextjs + im using internal package for handling OneLogin (OIDC) hooked as express middleware which runs. before the Nextjs trpc. I encounter a problem where I do fire a request api call via trpc query or mutation and the OIDC middleware catches that the request is 401 trpc throws on me that
TRPCClientError: Unexpected token 'U', "Unauthorized" is not valid JSON
if I change the response of the middleware as a json instead of text something like
Then I get app-index.js:31 TRPCClientError: Unexpected non-whitespace character after JSON at position 14 (line 1 column 15)
with tRPC. What do i need to do to handle the 401 via trpc and be able to redirect the user to login "client side redirect at this point"1 replies
Stack trace in Client?
I've just found that in production I can see stack trace for TRPCErrors. Isin't it supposed to not show it?
Checking on development build with
isDev
for initTRPC
doesn't seem to work at all. I've set it to false but I can see still stack trace too.
Also is it good idea to have loggerLink enabled in production?6 replies
Sequential batch mutation
How one would make a mutation as a sequential mutation batch call?
Say I have to add 10 apples to the basket. My API allows only to add one at a time so I need to make 10 mutations one after another plus I would need to catch which one succeeded and which failed.
4 replies
'req' of undefined in onError of express middleware
I've recently noticed that I get a bunch of errors regarding req object missing in ctx for onError property in trpc express middleware. Can't figure out how req obj might be undefined at the point of onerror handler??
15 replies
Enigmatic INTERNAL SERVER ERROR
Im having a problem finding out about what is causing the INTERNAL SERVER ERROR from tRPC in my production.
This is the error 👀
Are there any recommendation on how to handle onError handler for production ??
This is my current approach which seems to swallow some information. Can't really tell where it originates and how to possibly overcome it
3 replies
Can I force metadata with the createCaller?
I have custom metadata which I check within the middleware. Im trying to write tests for this. Currently no single procedure is using the meta and I wonder If I can test this somehow. Can I add meta on the fly to the procedure with createCaller??
6 replies
How to enforce usequery as NonNullable
I have a custom hook with infinite query. I check for undefined at the app first render and then it is reused so I know by then, the type should not be undefined. I would like to be able to infer its type without undefined conditionally. Can I do that? How to if so??
8 replies
Sharing Enum
On the backend: I have colocated files with types and procedures (index.ts - procedures, schemas.ts - zod/ts types).
If I define an Enum on the backend in the schemas.ts and would like to use it on the client(frontend) does it meat that importing that enum would boundle this file in my client? With type I can just do
import type { SomeType } from '../backend/schemas'
and that would be stripped If I understand correctly. But what about enum?4 replies
application/octet-stream response
Im refactoring my old backend to trpc so far it was a pleasure and fairly straight forward process ❤️ Im amazed.
Now I faced the issue with the files. I have one endpoint from external API that returns the data as
application/octet-stream
. (csv/xlsx) Can I handle this with trpc?? Probably I can't pass directly the response from the API throught trpc procedure and read it on client. I was thinking if I could parse response on backend (read it as text) and send text to client and download it? Would that work? Is it some kind of security? One problem could occur if the file would be big I guess??4 replies
Fetching different server url than defined in config
Is it possible to access the reactQuery instance and fetch different server url? I would like to use reactQuery to get data from my server which is at say
/address-middleware
instead of hitting /trpc/....
Can I do that? I dont want to setup separate reactquery provider or something just to call one separate endpoint4 replies