npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, πŸ‘‹, I’m Ryan HefnerΒ  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you πŸ™

Β© 2024 – Pkg Stats / Ryan Hefner

@chord-ts/rpc

v1.0.0-beta.21

Published

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

Downloads

261

Readme

Chord - RPC Framework

The revolution in the world of client-server communication. Type-safe RPC on top of JSON-RPC v2 protocol.

βœ… Used inside

πŸ₯ Why?

Because client-server communication becomes complex with the growth of project. Hundreds of REST API endpoints destroy Developer Experience. On the other hand GraphQL seems like a cumbersome and suboptimal solution, and gRPC works only for server-to-server

That's where Chord comes to solve accumulated problems of modern software development

✨ Features

  • Protocol agnostic
  • Framework agnostic
  • Type safe exchange
  • Accelerated development
  • IDE hinting out-of-box
  • Client size ~1.5kb

βš™οΈ Installation

Chord can be used with any backend library. Install it freely from npm via your favorite package manager

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

Chord uses decorators and reflection under the hood, to construct 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 are using Vite as bundler of your project, you have to note, that ESbuild that is used under the hood, does not support emitDecoratorMetadata flag at the moment

Thus, you have to use additional plugins for Vite. I personally recommend to try out SWC. It fixes this issue and doesn't impact on rebuilding performance

Then add SWC 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 framework, but you can try your own on any other preferred framework like Next or Nuxt

πŸ“ Implement the Class

Then implement defined interface inside your controller. In SvelteKit it's +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));
}

What we did in this listing is defined everything we need to handle requests. The first three steps are the same for any framework you will use, while the fourth is depended of it

You can implement a middleware for your backend framework which must extract body of request

πŸ–ΌοΈ Use RPC on frontend

Now we are ready to call the method on frontend. As we use SvelteKit, we have a full power of Svelte for our 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

πŸ“š Further reading

We have finished a basic example of using Chord. But it's the tip of the iceberg of possibilities that framework unlocks


Visit the Documentation(Coming Soon) to dive deeper