Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
💎 Cutting edge transport framework vanishing borders between frontend and backend
The revolution in the world of client-server communication. Type-safe RPC on top of JSON-RPC v2 protocol.
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
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
}
}
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
./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}']
}
});
The example below uses SvelteKit framework, but you can try your own on any other preferred framework like Next or Nuxt
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 HelloRPC {
@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({ HelloRPC: new HelloRPC() });
// 3. Create a type that will be used on frontend
export type Wrapped = 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
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 { dynamicClient } from '@chord-ts/rpc';
import { onMount } from 'svelte';
// Import our Contract
import type { Wrapped } 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 = dynamicClient<Wrapped>({ endpoint: '/' });
let res;
// Called after Page mount. The same as useEffect(..., [])
onMount(async () => {
// Call method defined on backend with type-hinting
res = await rpc.HelloRPC.hello('world');
console.log(res);
});
</script>
<h1>Chord call Test</h1>
<p>Result: {res}</p>
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
FAQs
💎 Cutting edge transport framework vanishing borders between frontend and backend
The npm package chord-rpc receives a total of 0 weekly downloads. As such, chord-rpc popularity was classified as not popular.
We found that chord-rpc demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.