Menu

Post image 1
Post image 2
1 / 2
0

A Function Should Know Where It Runs

DEV Community·Viktor Lázár·about 1 month ago
#zj4J8Ve0
#api#architecture#server#function#code#user
Reading 0:00
15s threshold

There is an obvious appeal to a server function you can call from anywhere. The old version of the same idea was not pleasant. You wrote an endpoint, then a client helper for that endpoint, then some shared schema to keep the two sides honest, then error handling in both places, and eventually a small pile of files whose main job was to move one value from the browser to the server and another value back again. So when a framework lets you write the server part as a normal function and call it as a normal function, it feels like the right kind of progress. export const getUser = createServerFn (). handler ( async () => { return db . user . findCurrent () }) Enter fullscreen mode Exit fullscreen mode Somewhere else: const user = await getUser () Enter fullscreen mode Exit fullscreen mode This is much nicer than wiring an endpoint by hand. The function is typed. The caller is typed. Refactors have a path through the codebase instead of disappearing into a string URL.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More