Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

trpc-bun-adapter

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

trpc-bun-adapter

TRPC adapter for bun js runtime

  • 1.0.0-rc.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
637
increased by90.15%
Maintainers
1
Weekly downloads
 
Created
Source

TRPC Bun Adapter

Description

Leverage TRPC on Bun with ease.

Supports both HTTP and WebSockets transports.

Quick Start

Install the package:

bun install @trpc/server trpc-bun-adapter

Paste the following code into server.ts:

import {createBunServeHandler} from 'trpc-bun-adapter';
import {router} from './router';

Bun.serve(createBunServeHandler({ router }));

Run the server with HTTP and WebSocket transports:

bun run server.ts

API Reference

createBunServeHandler

Creates a Bun serve handler for HTTP and WebSocket transports:

import {createBunServeHandler} from 'trpc-bun-adapter';
import {router} from './router';

const createContext = (opts: { req: Request }) => ({
    user: 1,
});

Bun.serve(
    createBunServeHandler(
        {
            router,
            // optional arguments:
            endpoint: '/trpc', // Default to ""
            createContext,
            onError: console.error,
            responseMeta(opts) {
                return {
                    status: 202,
                    headers: {},
                }
            },
            batching: {
                enabled: true,
            },
        },
        {
            // Bun serve options
            port: 3001,
            fetch(request, server) {
                // will be fired if it's not a TRPC request
                return new Response("Hello world");
            },
        },
    ),
);

Arguments are:

  • options - TRPC options
  • bunOptions - Bun serve options

createBunHttpHandler

Creates a Bun HTTP handler for HTTP transport:

import {createBunHttpHandler} from 'trpc-bun-adapter';
import {router} from './router';

const createContext = (opts: { req: Request }) => ({
    user: 1,
});

const bunHandler = createBunHttpHandler({
    router,
    // optional arguments:
    endpoint: '/trpc', // Default to ""
    createContext,
    onError: console.error,
    responseMeta(opts) {
        return {
            status: 202,
            headers: {},
        }
    },
    batching: {
        enabled: true,
    },
    emitWsUpgrades: false, // pass true to upgrade to WebSocket
});

Bun.serve({
    fetch(request, response) {
        return bunHandler(request, response) ?? new Response("Not found", {status: 404});
    }
});

createBunWsHandler

Creates a Bun WebSocket handler for WebSocket transport:

import {ServerWebSocket} from "bun";
import {createBunWSHandler, BunWSClientCtx} from './src';
import {router} from './router';

const createContext = (opts: { req: Request, client: ServerWebSocket<BunWSClientCtx> }) => ({
    user: 1,
});

const websocket = createBunWSHandler({
    router,
    // optional arguments:
    createContext,
    onError: console.error,
    batching: {
        enabled: true,
    },
});

Bun.serve({
    fetch(request, server) {
        if (server.upgrade(request, {data: {req: request}})) {
            return;
        }

        return new Response("Please use websocket protocol", {status: 404});
    },
    websocket,
});

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Feel free to open issues and pull requests.

Keywords

FAQs

Package last updated on 30 Dec 2023

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc