This covers the server-side Fetch handler. There’s also a client-specific Fetch plugin available to use.
Prim+RPC supports the Fetch API as used in modern JavaScript runtimes like Bun and Deno. Node can also use the Fetch API
through the @whatwg-node/*
ponyfill and server adapter.
First, you’ll need some module with functions that you’d like to expose to the client:
export function hello() {
return "Hi from Prim+RPC!"
}
hello.rpc = true
export default hello
Now we can configure our Prim+RPC server with our module using the Fetch API:
import { createPrimServer } from "@doseofted/prim-rpc"
import { primFetch } from "@doseofted/prim-rpc-plugins/server-fetch"
import * as module from "./functions"
const prim = createPrimServer({ module })
const fetch = primFetch({ prim })
The Fetch function will be used with the server of your choice. Choose your runtime:
import { createPrimServer } from "@doseofted/prim-rpc"
import { primFetch } from "@doseofted/prim-rpc-plugins/server-fetch"
import * as module from "./functions"
const prim = createPrimServer({ module })
const fetch = primFetch({ prim })
// Serves http://localhost:8000/prim
Deno.serve(fetch)
import { createPrimServer } from "@doseofted/prim-rpc"
import { primFetch } from "@doseofted/prim-rpc-plugins/server-fetch"
import * as module from "./functions"
const prim = createPrimServer({ module })
const fetch = primFetch({ prim })
// Serves http://localhost:3000/prim
Bun.serve({ fetch })
import { createPrimServer } from "@doseofted/prim-rpc"
import { primFetch } from "@doseofted/prim-rpc-plugins/server-fetch"
import { createServerAdapter } from "@whatwg-node/server"
import * as module from "./functions"
const prim = createPrimServer({ module })
const fetch = primFetch({ prim })
// Serves http://localhost:3000/prim
const fetchAdapter = createServerAdapter(fetch)
const server = createServer(fetchAdapter).listen(3000)
Your server should now be available on the address show above, unless you have changed settings. Take note of the
address as will become the client’s endpoint. The /prim
prefix may also be different if you’ve configured it in
your Prim+RPC server. You can test it out like so:
# Remember to change the address depending on your server
curl --request GET --url "http://localhost:3000/prim"
You may also choose a compatible method plugin:
Report an IssueNo sections available.
Prim+RPC: a project by Ted Klingenberg
Anonymous analytics collected with Ackee