Skip to content

Commit

Permalink
chore: fill new runtime properties
Browse files Browse the repository at this point in the history
  • Loading branch information
magne4000 committed Dec 4, 2024
1 parent 7e3a0be commit 6aa56c5
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 18 deletions.
3 changes: 3 additions & 0 deletions packages/adapter-cloudflare/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,13 @@ export function getRuntime(
): RuntimeAdapter {
const isContext = args.length === 1;

const key = isContext ? "cloudflare-pages" : "cloudflare-worker";

return getAdapterRuntime(
isContext ? "cloudflare-pages" : "cloudflare-worker",
{
params: isContext ? ((args[0].params as Record<string, string>) ?? undefined) : undefined,
[key]: isContext ? args[0] : { env: args[0], ctx: args[1] },
},
{
env: isContext ? args[0].env : args[0],
Expand Down
1 change: 1 addition & 0 deletions packages/adapter-elysia/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export function getRuntime(elysiaContext: ElysiaContext): RuntimeAdapter {
"elysia",
{
params,
elysia: elysiaContext,
},
cloudflareContext,
);
Expand Down
4 changes: 4 additions & 0 deletions packages/adapter-express/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,9 @@ export function getRuntime(request: DecoratedRequest, response: DecoratedServerR
params: request.params,
req: request as IncomingMessage,
res: response,
express: Object.freeze({
req: request as IncomingMessage,
res: response,
}),
});
}
4 changes: 4 additions & 0 deletions packages/adapter-fastify/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,5 +255,9 @@ export function getRuntime(request: FastifyRequest, reply: FastifyReply): Runtim
params: request.params as Record<string, string> | undefined,
req: request.raw as IncomingMessage,
res: reply.raw,
fastify: Object.freeze({
request: request,
reply: reply,
}),
});
}
1 change: 1 addition & 0 deletions packages/adapter-h3/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export function getRuntime(event: H3Event): RuntimeAdapter {
"h3",
{
params: event.context.params,
h3: event,
},
{
req: event.node.req,
Expand Down
1 change: 1 addition & 0 deletions packages/adapter-hattip/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export function getRuntime(context: AdapterRequestContext): RuntimeAdapter {
"hattip",
{
params: "params" in context ? (context.params as Record<string, string>) : undefined,
hattip: context,
},
{
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
Expand Down
1 change: 1 addition & 0 deletions packages/adapter-hono/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export function getRuntime(honoContext: HonoContext): RuntimeAdapter {
"hono",
{
params,
hono: honoContext,
},
{
env: honoContext.env,
Expand Down
31 changes: 18 additions & 13 deletions packages/adapter-vercel/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,23 @@ export function getRuntime(request: Request | IncomingMessage, response?: Server
? request.headers.get("x-now-route-matches")
: (request.headers["x-now-route-matches"] as string | undefined);

return getAdapterRuntime(
request instanceof Request ? "vercel-edge" : "vercel-node",
{
params: Object.fromEntries(
new URLSearchParams(routeMatches ?? new URL(request.url ?? "", "http://localhost").search).entries(),
),
},
response
? {
req: request,
res: response,
}
: {},
const key = request instanceof Request ? "vercel-edge" : "vercel-node";
const params = Object.fromEntries(
new URLSearchParams(routeMatches ?? new URL(request.url ?? "", "http://localhost").search).entries(),
);
const args = response
? {
req: request,
res: response,
}
: {};

const value = response
? {
params,
[key]: Object.freeze(args),
}
: { params };

return getAdapterRuntime(key, value, args);
}
1 change: 1 addition & 0 deletions packages/adapter-webroute/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,6 @@ export async function getRuntime(ctx: RequestCtx | undefined): Promise<RuntimeAd

return getAdapterRuntime("webroute", {
params,
webroute: ctx,
});
}
5 changes: 5 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@hattip/core": "catalog:",
"@webroute/route": "catalog:",
"elysia": "catalog:",
"fastify": "catalog:",
"h3": "catalog:",
"hono": "catalog:",
"rimraf": "catalog:",
Expand All @@ -40,6 +41,7 @@
"@hattip/core": "catalog:",
"@webroute/route": "catalog:",
"elysia": "catalog:",
"fastify": "catalog:",
"h3": "catalog:",
"hono": "catalog:"
},
Expand All @@ -56,6 +58,9 @@
"elysia": {
"optional": true
},
"fastify": {
"optional": true
},
"h3": {
"optional": true
},
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Adapter, Runtime } from "./types";
import { getRuntime } from "./runtime";
import type { Adapter, Runtime } from "./types";

export function getAdapter<K extends Adapter["adapter"]>(
key: K,
Expand All @@ -19,5 +19,5 @@ export function getAdapterRuntime<K extends Adapter["adapter"]>(
const a = getAdapter(adapter, adapterArgs);
const r = getRuntime(runtimeArgs);

return { ...r, ...a };
return Object.freeze({ ...r, ...a });
}
7 changes: 4 additions & 3 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { AdapterRequestContext as HattipContext } from "@hattip/core";
import type { RequestCtx as WebrouteContext } from "@webroute/route";
import type { Server as BunServer } from "bun";
import type { Context as ElysiaContext } from "elysia";
import type { FastifyReply, FastifyRequest } from "fastify";
import type { H3Event } from "h3";
import type { Context as HonoContext } from "hono";
import type { universalSymbol } from "./const";
Expand Down Expand Up @@ -115,8 +116,8 @@ export interface FastifyAdapter {
res: ServerResponse;

fastify: {
req: IncomingMessage;
res: ServerResponse;
request: FastifyRequest;
reply: FastifyReply;
};
}

Expand Down Expand Up @@ -190,7 +191,7 @@ export interface WebrouteAdapter {
adapter: "webroute";
params: Record<string, string> | undefined;

webroute: WebrouteContext;
webroute?: WebrouteContext;
}

export interface OtherAdapter {
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6aa56c5

Please sign in to comment.