Install using bunx shadcn@latest add @remix-utils/json-hash.
When returning a json from a loader function, you may need to get data from different DB queries or API requests, typically you would something like this
exportasyncfunctionloader({ params }: LoaderData) { letpostId = z.string().parse(params.postId); let [post, comments] = awaitPromise.all([getPost(), getComments()]); returnjson({ post, comments });
Install using
bunx shadcn@latest add @remix-utils/json-hash.When returning a
jsonfrom aloaderfunction, you may need to get data from different DB queries or API requests, typically you would something like thisThe
jsonHashfunction lets you define those functions directly in thejson, reducing the need to create extra functions and variables.It also calls your functions using
Promise.allso you can be sure the data is retrieved in parallel.Additionally, you can pass non-async functions, values and promises.
The result of
jsonHashis aTypedResponseand it's correctly typed so using it withtypeof loaderworks flawlessly.Author
Sergio Xalambrí