tRPC and existing api
Hello, I have external API that I use on my project. I need to hide my api key so I need to create api routes as wrapper (to run serverside). Could I just use trpc for it instead of the normal api route way on nextjs? So on trpc get external api and return result and client fetches from trpc
7 Replies
yes
thats an excellent use case for trpc
Is there any examples for it, I am thinking about the typing. Do I need to use zod for input and output validation to get typings work correctly? So on my procedure I use axios to fetch my api and then its untyped. Then I parse it to get typings or somehow type output. Is this correct idea?
that idea is good
what does axios offer for this?
https://trpc.io/docs/output-validation this seems to be the way. Axios to fetch data instead of default fetch
Output Validation | tRPC
tRPC gives you automatic type-safety of outputs without the need of adding a validator; however, it can be useful at times to strictly define the output type in order to prevent sensitive data of being leaked.
yeah i get that you're using axios instead of fetch, but im wondering why. i haven't used axios much but i havent seen anything it offers that fetch doesn't, cuz it used to just be a convenient wrapper around XMLHttpRequest before fetch came into existence
the only thing i know of is that you can get percentage updates on huge file download/uploads i think
If somebody is also interested I managed to do it like this and using it on client with types: