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

trpc-bun-adapter

v1.2.2

Published

TRPC adapter for bun js runtime

Downloads

2,548

Readme

tRPC Bun Adapter

npm version License

Description

trpc-bun-adapter is a tRPC adapter for Bun.

Start both HTTP and WebSockets transports with ease.

Quick Start

Install packages:

bun install @trpc/server trpc-bun-adapter

Create a server.ts file with the following content:

import {initTRPC} from '@trpc/server';
import {createBunServeHandler} from 'trpc-bun-adapter';

const t = initTRPC.create();

export const router = t.router({
    ping: t.procedure.query(() => "pong"),
});

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

To start the server, run:

bun run server.ts
bun run --watch server.ts # to restart on file changes

Check that it works:

curl http://localhost:3000/ping

Example

for a full example, see the example directory.

API Reference

Ensure you have created a router.ts file as outlined in the tRPC documentation: Define Routers.

createBunServeHandler

Creates a Bun serve handler:

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

const createContext = (opts: CreateBunContextOptions) => ({
    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 executed if it's not a TRPC request
                return new Response("Hello world");
            },
        },
    ),
);

To add response headers like Cross-origin resource sharing (CORS) use responseMeta option:

Bun.serve(
   createBunServeHandler({
         router: appRouter,
         responseMeta(opts) {
            return {
               status: 200,
               headers: {
                  "Access-Control-Allow-Origin": "*",
                  "Access-Control-Allow-Methods": "GET, POST, OPTIONS",
                  "Access-Control-Allow-Headers": "Content-Type, Authorization"
               }
            };
         }
      }
   )
);

createBunHttpHandler

Creates a Bun HTTP handler for tRPC HTTP requests:

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

const createContext = (opts: CreateBunContextOptions) => ({
    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 tRPC websocket requests:

import { createBunWSHandler, CreateBunContextOptions } from './src';
import { router } from './router';

const createContext = (opts: CreateBunContextOptions) => ({
    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,
});

CreateBunContextOptions

To ensure your router recognizes the context type, define a createContext function utilizing the CreateBunContextOptions type:

import { initTRPC } from '@trpc/server';
import type { CreateBunContextOptions } from "src/createBunHttpHandler";

export const createContext = async (opts: CreateBunContextOptions) => {
    return {
        authorization: req.headers.get('Authorization')
    };
};

With createContext defined, you can use it in your router to access the context, such as the authorization information:

const t = initTRPC.context<typeof createContext>().create();

export const router = t.router({
    session: t.procedure.query(({ ctx }) => ctx.authorization),
});

Finally, pass your createContext function besides router to createBunHttpHandler. This integrates your custom context into the HTTP handler setup:

createBunHttpHandler({
    router,
    createContext,
})

Read more documentation about tRPC contexts here: Contexts

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.