prim-rpc icon indicating copy to clipboard operation
prim-rpc copied to clipboard

Optional compiler for inline RPC

Open doseofted opened this issue 1 year ago • 2 comments

[!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:

  • This build tools integrates with an existing Prim+RPC server and client:
    • Inline functions become a virtual module, provided to your server
    • Your Prim+RPC client is passed to this plugin, will be used in place of function calls
  • Parent context cannot be shared with inline RPC function
    • Context sharing may be unexpected by developer and easily leads to messy mistakes
    • The exception here includes globals for the given environment and static imports
  • You can use this function alongside a module defined only on the server
    • Only functions marked "inline" will be transformed
  • Inline RPC functions should generally be async since client calls to it will be awaited
  • Exported inline RPC in a user codebase should use import attribute with { type: "rpc" } to clarify for other developers that an imported function will be transformed into an RPC.

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.

doseofted avatar Feb 29 '24 17:02 doseofted

⚠️ 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

changeset-bot[bot] avatar Feb 29 '24 17:02 changeset-bot[bot]

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

vercel[bot] avatar Feb 29 '24 17:02 vercel[bot]