RPC Docs.

Prim+RPC is prerelease software. It may be unstable and functionality may change prior to full release.

Prim+RPC Tools

Prim+RPC includes additional (and optional) tooling that can make working with Prim+RPC even easier.

Table of Contents

Prevent Import Utility

Since Prim+RPC allows you to import type definitions from your server, there's the possibility (although unlikely) that some developer makes the mistake of importing a function from the server. Generally these errors can be caught fairly quickly because many server-side tools will throw errors on the client however when you're using isomorphic/universal tools that work on both server and client this may be harder to catch.

The Prevent Import Utility is very simple plugin for the build tool of your choice (using unplugin) that just prevents imports of a specified directory or file, like those from the server. You can use it like so (we'll use Vite as an example but you could use Webpack or others):


import path from "node:path"
import { defineConfig } from "vite"
import preventImport, { BuildModifyMethod } from "@doseofted/prim-rpc-tooling/build"
export default defineConfig({
plugins: [
preventImport.vite({
name: path.join(__dirname, "server"),
method: BuildModifyMethod.BuildTimeEmptyExport,
}),
],
})

In this example we assume that server-related code is located in the same directory as the Vite config (./server).

Now if we try to import a function directly from the server (the code itself, not the types as used with Prim+RPC) that function call will fail because we selected the BuildTimeEmptyExport method. The plugin will replace all server imports with an empty export (export {}) during the build process.

There are two other methods available: RunTimeWindowCheck and RunTimeProcessCheck. These two options are very similar and will simply prepend a conditional error only if imported on the client. The RunTimeWindowCheck method, for instance, will prepend if (typeof window !== "undefined") { throw new Error("...") } to the top of the file (with your chosen error message). While this does not prevent the import itself, it should flag some errors in your testing tools.

Documentation Generator

The Documentation Generator is a tool that reads TypeDoc documentation and creates a simpler RPC-specific documentation for your code.

This RPC documentation process is intended become more streamlined in the future.

First, it's suggested that you document your code using TypeDoc's format. Next, you can generate TypeDoc with its CLI (this is not Prim+RPC related). We'll pretend your code lives at src/index.ts.


npx typedoc src/index.ts --json dist/docs.json

Now we can use this generated dist/docs.json file to generate RPC-specific documentation.

Below is a simple script that creates the documentation.


import preventImport from "@doseofted/prim-rpc-tooling/docs"
import typeDoc from "./dist/docs.json"
const rpcDocs = createDocsForModule(typeDoc)

In the future, basic JavaScript types will be turned into JSON Schema for typed usage outside of TypeScript. It's not planned to support all TypeScript types in RPC documentation since it doesn't make sense to use them outside of TypeScript but basic JSON Schema support could be useful for using Prim+RPC outside of JavaScript environments.

The resulting documentation is a simpler version of the TypeDoc that relates to RPC. This output could be used for a documentation website or fed to another tool like HTTPsnippet to generate requests for other clients outside of JavaScript.

Command Line Tool

Documentation in Progress

Prim+RPC: a project by Ted Klingenberg

Dose of Ted

Anonymous analytics collected with Ackee