doseofted / prim-rpc

Easy-to-understand, type-safe, transport-agnostic RPC/IPC for JavaScript, supporting callbacks, batching, file handling, custom serialization, and more.
https://prim.doseofted.me
Apache License 2.0
98 stars 1 forks source link

Optional compiler for inline RPC #82

Open doseofted opened 8 months ago

doseofted commented 8 months ago

[!WARNING]

This is very experimental and incomplete. A WIP.

This plugin allows server-side functions to be replaced with RPC on the client and moved to a new module on the server. It integrates with an existing Prim+RPC server/client and will work with your existing server/client frameworks and the build tool of your choice.

This is the Prim+RPC take on "server actions" or "data loaders/actions" in frameworks like Next.js, Solid Start, Qwik, and others. An example of what this feature will look like:

// written code
async function sayHello(x = "Backend", y = "Frontend") {
    return `${x}, meet ${y}.`
}
sayHello.rpc = "inline"
document.getElementById("app").innerText = await sayHello()
// compiled client code (an intermediate step for readability)
import client from "virtual:prim-rpc?client"
document.getElementById("app").innerText = await client.sayHello()

A few key points:

Prim+RPC is still intended to be used without a compiler step in most cases: having a separate server and client generally results in better structured code, fewer chances to expose sensitive information, and easier review. The compiler step will be optional.

But sometimes inline RPC can be useful: prototyping, reducing the payload of a server result, other app-specific logic that doesn't need to be part of an API itself.

changeset-bot[bot] commented 8 months ago

⚠️ No Changeset found

Latest commit: e2879a94ecda0804a330ac048cc9f46885449672

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

vercel[bot] commented 8 months ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
prim-rpc ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 4, 2024 3:40pm