@beignet/web
v0.0.3
Published
Web Fetch adapter for Beignet
Maintainers
Readme
@beignet/web
Web Fetch adapter for Beignet's framework runtime.
Use this package when your runtime accepts a standard Request and returns a
standard Response, such as Cloudflare Workers, Bun, Deno, Node fetch servers,
or tests that should avoid a framework-specific adapter.
Installation
npm install @beignet/webQuick start
import { createFetchServer } from "@beignet/web";
import { routes } from "./server/routes";
export const server = await createFetchServer({
ports,
routes,
createContext: ({ ports, req }) => ({
requestId: req.headers.get("x-request-id") ?? crypto.randomUUID(),
ports,
}),
mapUnhandledError: () => ({
status: 500,
body: {
code: "INTERNAL_SERVER_ERROR",
message: "Internal server error",
},
}),
});
export default {
fetch: server.fetch,
};Bun
import { server } from "./server";
Bun.serve({
fetch: server.fetch,
});Lower-level helpers
import {
createFetchHandler,
toRequestLike,
toWebResponse,
} from "@beignet/web";createFetchHandler(server)adapts a Beignet server instance or API handler to(req: Request) => Promise<Response>.toRequestLike(req)converts a standardRequestto Beignet's framework-neutral request shape.toWebResponse(response)converts a Beignet response to a standardResponse.
Native Response instances returned by handlers are passed through unchanged.
Plain Beignet responses are serialized as JSON unless the body is undefined
or null.
Testing
Use @beignet/web/testing to exercise routes through the same Web Fetch
adapter without opening a network port.
import { createTestApp } from "@beignet/web/testing";
import { getTodo } from "./features/todos/contracts";
import { routes } from "./server/routes";
const app = await createTestApp({
ports,
routes,
createContext: ({ ports }) => ({
requestId: "test-request",
ports,
}),
});
const todo = await app.request(getTodo, {
path: { id: "todo_1" },
});app.request(contract, args) uses the same typed call arguments as
@beignet/core/client, while app.safeRequest(contract, args) returns a typed
success/error result instead of throwing.
