Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
cah4a committed Jan 24, 2024
1 parent 5cd978e commit 20c48bd
Show file tree
Hide file tree
Showing 6 changed files with 622 additions and 581 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4

[*.{js,ts,mjs,jsx,tsx}]
indent_style = space
insert_final_newline = true
indent_style = space
trim_trailing_whitespace = true
6 changes: 6 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"formatter": {
"indentStyle": "space",
"indentWidth": 4
}
}
48 changes: 24 additions & 24 deletions src/createBunHttpHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,34 @@ import type { HTTPBaseHandlerOptions } from "@trpc/server/http";
export type CreateBunContextOptions = { req: Request };

export type BunHttpHandlerOptions<TRouter extends AnyRouter> =
HTTPBaseHandlerOptions<TRouter, Request> & {
endpoint?: string;
createContext?: (
opts: CreateBunContextOptions,
) => inferRouterContext<TRouter> | Promise<inferRouterContext<TRouter>>;
};
HTTPBaseHandlerOptions<TRouter, Request> & {
endpoint?: string;
createContext?: (
opts: CreateBunContextOptions,
) => inferRouterContext<TRouter> | Promise<inferRouterContext<TRouter>>;
};

export function createBunHttpHandler<TRouter extends AnyRouter>(
opts: BunHttpHandlerOptions<TRouter> & { emitWsUpgrades?: boolean },
opts: BunHttpHandlerOptions<TRouter> & { emitWsUpgrades?: boolean },
) {
return (request: Request, server: Server) => {
const url = new URL(request.url);
return (request: Request, server: Server) => {
const url = new URL(request.url);

if (opts.endpoint && !url.pathname.startsWith(opts.endpoint)) {
return;
}
if (opts.endpoint && !url.pathname.startsWith(opts.endpoint)) {
return;
}

if (
opts.emitWsUpgrades &&
server.upgrade(request, { data: { req: request } })
) {
return new Response(null, { status: 101 });
}
if (
opts.emitWsUpgrades &&
server.upgrade(request, { data: { req: request } })
) {
return new Response(null, { status: 101 });
}

return fetchRequestHandler({
endpoint: opts.endpoint ?? "",
...opts,
req: request,
});
};
return fetchRequestHandler({
endpoint: opts.endpoint ?? "",
...opts,
req: request,
});
};
}
38 changes: 19 additions & 19 deletions src/createBunServeHandler.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import type { ServeOptions, Server } from "bun";
import { createBunWSHandler } from "./createBunWSHandler";
import {
BunHttpHandlerOptions,
createBunHttpHandler,
BunHttpHandlerOptions,
createBunHttpHandler,
} from "./createBunHttpHandler";
import type { AnyRouter } from "@trpc/server";

type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;

export function createBunServeHandler<TRouter extends AnyRouter>(
opts: BunHttpHandlerOptions<TRouter>,
serveOptions?: Optional<ServeOptions, "fetch">,
opts: BunHttpHandlerOptions<TRouter>,
serveOptions?: Optional<ServeOptions, "fetch">,
) {
const trpcHandler = createBunHttpHandler({
...opts,
emitWsUpgrades: true,
});
const trpcHandler = createBunHttpHandler({
...opts,
emitWsUpgrades: true,
});

return {
...serveOptions,
async fetch(req: Request, server: Server) {
const trpcReponse = trpcHandler(req, server);
return {
...serveOptions,
async fetch(req: Request, server: Server) {
const trpcReponse = trpcHandler(req, server);

if (trpcReponse) {
return trpcReponse;
}
if (trpcReponse) {
return trpcReponse;
}

return serveOptions?.fetch?.call(server, req, server);
},
websocket: createBunWSHandler(opts),
};
return serveOptions?.fetch?.call(server, req, server);
},
websocket: createBunWSHandler(opts),
};
}

0 comments on commit 20c48bd

Please sign in to comment.