Skip to content
chord-tsPublic

About

πŸ’Ž Cutting edge transport framework vanishing borders between frontend and backend

Topics

Resources

Stars

59 stars

Watchers

3 watching

Forks

Latest commit

Β 

History

80 Commits

Folders and files

Repository files navigation

Chord β€” type-safe RPC on JSON-RPC v2: write a typed method once on the server, call it from the client like a local function.

TypeScript SvelteKit Express JSON-RPC

download stats version license maintainability

βœ… Used inside

πŸ₯ Why?

As a project grows, client-server communication becomes a maintenance burden. Hundreds of REST API endpoints erode Developer Experience. GraphQL feels heavy for many use cases, and gRPC is built for server-to-server, not the browser.

That's where Chord comes in: you write a typed method once, then call it from the client as if it lived in the same file.

✨ Features

  • Protocol-agnostic β€” speaks JSON-RPC v2, so you own the transport
  • Framework-agnostic β€” ready middlewares for SvelteKit and Express, and any backend can plug in
  • Type-safe end-to-end β€” one typed method shared by server and client
  • IDE autocompletion out of the box β€” no codegen, no schema sync
  • Tiny client β€” under 2 KB gzipped
  • Batteries included β€” batch requests, caching, middleware, and Zod validation

βš™οΈ Installation

Chord works with any backend library. Install it from npm with your favorite package manager

npm install @chord-ts/rpc
# or
pnpm install @chord-ts/rpc

Chord uses decorators and reflection under the hood to build the server and client. So you need to configure your tsconfig.json first

./tsconfig.json

{
  compilerOptions: {
    // Other stuff...

    target: 'ESNext',
    experimentalDecorators: true,
    emitDecoratorMetadata: true
  }
}

⚠️ Caveats

If you use Vite as your bundler, note that the ESbuild underneath does not support the emitDecoratorMetadata flag yet

The workaround is SWC: it fixes the issue without adding noticeable rebuild overhead. Add the plugin to Vite:

./vite.config.ts

import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vitest/config';
import swc from 'unplugin-swc';

export default defineConfig({
  plugins: [sveltekit(), swc.vite()],
  test: {
    include: ['src/**/*.{test,spec}.{js,ts}']
  }
});

πŸ› οΈ Usage

The example below uses SvelteKit, but the same pattern works on Next.js, Nuxt, or any other framework

πŸ“ Implement the Class

Implement your controller as a class. In SvelteKit that's a +server.ts file

./src/routes/+server.ts

import { json } from '@sveltejs/kit';
import { Composer, rpc, type Composed } from '@chord-ts/rpc'; // Main components of Chord we will use
import { sveltekitMiddleware } from '@chord-ts/rpc/middlewares'; // Middleware to process RequestEvent object

// 1. Implement the class containing RPC methods
export class Say {
  @rpc() // Use decorator to register callable method
  hello(name: string): string {
    return `Hello, ${name}!`;
  }
}

// 2. Init Composer instance that will handle requests
export const composer = Composer.init({ Say: new Say() });

// 3. Create a type that will be used on frontend
export type Client = typeof composer.clientType;

composer.use(sveltekitMiddleware()); // Use middleware to process SvelteKit RequestEvent

// 4. SvelteKit syntax to define POST endpoint
export async function POST(event) {
  // Execute request in place and return result of execution
  return json(await composer.exec(event));
}

The listing defines everything needed to handle requests. The first three steps are the same for any framework; only the fourth changes. For other backends, write a middleware that extracts the request body.

πŸ–ΌοΈ Use RPC on frontend

Now call the method from the client. As we use SvelteKit, we have the full power of Svelte for the UI

./src/routes/+page.svelte

<script lang="ts">
  import { client } from '@chord-ts/rpc/client';
  import { onMount } from 'svelte';

  // Import our Contract
  import type { Client } from './+server';

  // Init dynamic client with type checking
  // Use Contract as Generic to get type safety and hints from IDE
  // dynamicClient means that RPC will be created during code execution
  // and executed when the function call statement is found
  const rpc = client<Client>({ endpoint: '/' });

  let res;
  // Called after Page mount. The same as useEffect(..., [])
  onMount(async () => {
    // Call method defined on backend with type-hinting
    res = await rpc.Say.hello('world');
    console.log(res);
  });
</script>

<h1>Chord call Test</h1>
<p>Result: {res}</p>

πŸ“¦ Try it yourself

Ready to run sandbox with prepared environment for your experiments

Open in SvelteLab

πŸ“š Further reading

The example above is only the beginning. The documentation walks through validation, middleware, caching, and batch requests β€” and the full reference lives in /docs.

About

πŸ’Ž Cutting edge transport framework vanishing borders between frontend and backend

Topics

Resources

Stars

59 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages