Library to make API route fetching easier #21323
Unanswered
Janpot
asked this question in
Show and tell
Replies: 1 comment
-
@Janpot I like this idea. This remembers me on blitz.js. However, I like that this is a lib not a whole framework. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, all. Decided to share a little project I've been working on lately.
next-rpc
makes exported functions from API routes accessible in the browser. Just import your API function and call it anywhere you want. This reduces boilerplate and makes working with API routes and typescript effortless.Example
Define your API routes as follows:
Now in your components you can just import getName and call it client side:
Under the hood,
next-rpc
will generate an API route handler for all your exports and afetch
call for all your imports in the browser.Context
But wouldn't this hide the next.js context? How would I do authentication?
next-rpc/context
, a second part of this library makes the next.js context available everywhere by leveraging Node.js newAsyncLocalStorage
. It wraps allgetServerSideProps
,getInitialProps
and API handlers with anAsyncLocalStorage
and stores the request context. This context can then be used in all downstream code:Installation
Just wrap your next.config.js with
next-rpc
and you're good to goBeta Was this translation helpful? Give feedback.
All reactions