diff --git a/docs/canary/examples/migration-guide.md b/docs/canary/examples/migration-guide.md index 1366f3c747c..54e80cd8833 100644 --- a/docs/canary/examples/migration-guide.md +++ b/docs/canary/examples/migration-guide.md @@ -196,7 +196,7 @@ have a trailing slash at the end or that they will never have one. Middleware, handler and route component signatures have been unified to all look the same. Instead of receiving two arguments, they receive one. The `Request` -object is stored on the context object as `ctx.req`. +object is stored on the context object as `ctx.request`. ```diff - const middleware = (req, ctx) => new Response("ok"); diff --git a/init/src/init.ts b/init/src/init.ts index c8d44df7f91..2b278b21967 100644 --- a/init/src/init.ts +++ b/init/src/init.ts @@ -416,7 +416,7 @@ app.get("/api2/:name", (ctx) => { // this can also be defined via a file. feel free to delete this! const exampleLoggerMiddleware = define.middleware((ctx) => { - console.log(\`\${ctx.req.method} \${ctx.req.url}\`); + console.log(\`\${ctx.request.method} \${ctx.request.url}\`); return ctx.next(); }); app.use(exampleLoggerMiddleware); diff --git a/src/context.ts b/src/context.ts index 49d6d91c9a7..0ea43103126 100644 --- a/src/context.ts +++ b/src/context.ts @@ -35,6 +35,8 @@ export interface FreshContext { readonly config: ResolvedFreshConfig; readonly state: State; /** The original incoming `Request` object` */ + readonly request: Request; + /** @deprecated This is an alias for internal use only. Use {@linkcode FreshContext[request]} instead. */ readonly req: Request; /** * The request url parsed into an `URL` instance. This is typically used @@ -92,6 +94,8 @@ export class FreshReqContext implements FreshContext, PageProps { config: ResolvedFreshConfig; url: URL; + request: Request; + /** @deprecated This is an alias for internal use only. Use {@linkcode FreshReqContext[request]} instead. */ req: Request; params: Record; state: State = {} as State; @@ -112,7 +116,7 @@ export class FreshReqContext } constructor( - req: Request, + request: Request, url: URL, info: Deno.ServeHandlerInfo, params: Record, @@ -122,7 +126,8 @@ export class FreshReqContext buildCache: BuildCache, ) { this.url = url; - this.req = req; + this.request = request; + this.req = request; this.info = info; this.params = params; this.config = config; diff --git a/src/dev/middlewares/error_overlay/middleware.tsx b/src/dev/middlewares/error_overlay/middleware.tsx index b4aa91ab219..c1f2283e06d 100644 --- a/src/dev/middlewares/error_overlay/middleware.tsx +++ b/src/dev/middlewares/error_overlay/middleware.tsx @@ -14,7 +14,7 @@ export function devErrorOverlay(): MiddlewareFn { try { return await ctx.next(); } catch (err) { - if (ctx.req.headers.get("accept")?.includes("text/html")) { + if (ctx.request.headers.get("accept")?.includes("text/html")) { let init: ResponseInit | undefined; if (err instanceof HttpError) { if (err.status < 500) throw err; diff --git a/src/dev/middlewares/live_reload.ts b/src/dev/middlewares/live_reload.ts index 8e182e420d4..fd33bc45908 100644 --- a/src/dev/middlewares/live_reload.ts +++ b/src/dev/middlewares/live_reload.ts @@ -6,12 +6,12 @@ export function liveReload(): MiddlewareFn { const revision = Date.now(); return (ctx) => { - const { config, req, url } = ctx; + const { config, request, url } = ctx; const aliveUrl = config.basePath + ALIVE_URL; if (url.pathname === aliveUrl) { - if (req.headers.get("upgrade") !== "websocket") { + if (request.headers.get("upgrade") !== "websocket") { return new Response(null, { status: 501 }); } @@ -20,7 +20,7 @@ export function liveReload(): MiddlewareFn { // the client to know when the server is back up. Once we // have HMR we'll actively start sending messages back // and forth. - const { response, socket } = Deno.upgradeWebSocket(req); + const { response, socket } = Deno.upgradeWebSocket(request); socket.addEventListener("open", () => { socket.send( diff --git a/src/handlers.ts b/src/handlers.ts index e459595170a..407831474eb 100644 --- a/src/handlers.ts +++ b/src/handlers.ts @@ -89,7 +89,7 @@ export function page(data?: T, options?: { * * ```ts * export const handlers = define.handlers((ctx) => { - * return new Response(`Hello from a ${ctx.req.method} request!`); + * return new Response(`Hello from a ${ctx.request.method} request!`); * }); * ``` */ diff --git a/src/middlewares/mod.ts b/src/middlewares/mod.ts index e6ae6cefd1a..e389477d9c2 100644 --- a/src/middlewares/mod.ts +++ b/src/middlewares/mod.ts @@ -41,7 +41,7 @@ import type { Define as _Define } from "../define.ts"; * // checking and code completion. It does not register the middleware with the * // app. * const loggerMiddleware = define.middleware((ctx) => { - * console.log(`${ctx.req.method} ${ctx.req.url}`); + * console.log(`${ctx.request.method} ${ctx.request.url}`); * // Call the next middleware * return ctx.next(); * }); diff --git a/src/middlewares/static_files.ts b/src/middlewares/static_files.ts index c4f08ec60ff..c63b86f85de 100644 --- a/src/middlewares/static_files.ts +++ b/src/middlewares/static_files.ts @@ -15,7 +15,7 @@ import { tracer } from "../otel.ts"; */ export function staticFiles(): MiddlewareFn { return async function freshStaticFiles(ctx) { - const { req, url, config } = ctx; + const { request, url, config } = ctx; const buildCache = getBuildCache(ctx); let pathname = decodeURIComponent(url.pathname); @@ -36,7 +36,7 @@ export function staticFiles(): MiddlewareFn { return ctx.next(); } - if (req.method !== "GET" && req.method !== "HEAD") { + if (request.method !== "GET" && request.method !== "HEAD") { file.close(); return new Response("Method Not Allowed", { status: 405 }); } @@ -71,7 +71,7 @@ export function staticFiles(): MiddlewareFn { vary: "If-None-Match", }); - const ifNoneMatch = req.headers.get("If-None-Match"); + const ifNoneMatch = request.headers.get("If-None-Match"); if ( ifNoneMatch !== null && (ifNoneMatch === etag || ifNoneMatch === `W/"${etag}"`) @@ -101,7 +101,7 @@ export function staticFiles(): MiddlewareFn { } headers.set("Content-Length", String(file.size)); - if (req.method === "HEAD") { + if (request.method === "HEAD") { file.close(); return new Response(null, { status: 200, headers }); } diff --git a/tests/partials_test.tsx b/tests/partials_test.tsx index cb71f70cedd..6637d5c21c8 100644 --- a/tests/partials_test.tsx +++ b/tests/partials_test.tsx @@ -1797,7 +1797,7 @@ Deno.test({ fn: async () => { const app = testApp() .post("/partial", async (ctx) => { - const data = await ctx.req.formData(); + const data = await ctx.request.formData(); const name = data.get("name"); const submitter = data.get("submitter"); return ctx.render( @@ -1856,7 +1856,7 @@ Deno.test({ ); }) .post("/partial", async (ctx) => { - const data = await ctx.req.formData(); + const data = await ctx.request.formData(); const name = String(data.get("name")); return new Response(null, { @@ -1903,7 +1903,7 @@ Deno.test({ fn: async () => { const app = testApp() .post("/partial", async (ctx) => { - const data = await ctx.req.formData(); + const data = await ctx.request.formData(); const name = data.get("name"); const submitter = data.get("submitter"); return ctx.render( @@ -1964,7 +1964,7 @@ Deno.test({ fn: async () => { const app = testApp() .post("/partial", async (ctx) => { - const data = await ctx.req.formData(); + const data = await ctx.request.formData(); const name = data.get("name"); const submitter = data.get("submitter"); return ctx.render( @@ -2027,7 +2027,7 @@ Deno.test({ fn: async () => { const app = testApp() .post("/partial", async (ctx) => { - const data = await ctx.req.formData(); + const data = await ctx.request.formData(); const name = data.get("name"); const submitter = data.get("submitter"); return ctx.render( diff --git a/update/src/maybePrependReqVar.ts b/update/src/maybePrependReqVar.ts index c9da8770938..f99ee6d6081 100644 --- a/update/src/maybePrependReqVar.ts +++ b/update/src/maybePrependReqVar.ts @@ -64,7 +64,7 @@ export function maybePrependReqVar( declarationKind: tsmorph.VariableDeclarationKind.Const, declarations: [{ name: paramName, - initializer: "ctx.req", + initializer: "ctx.request", }], }); } diff --git a/update/src/update.ts b/update/src/update.ts index 8924ceafc80..5f0bb0382b1 100644 --- a/update/src/update.ts +++ b/update/src/update.ts @@ -365,7 +365,7 @@ function maybePrependReqVar( declarationKind: tsmorph.VariableDeclarationKind.Const, declarations: [{ name: paramName, - initializer: "ctx.req", + initializer: "ctx.request", }], }); } diff --git a/update/src/update_test.ts b/update/src/update_test.ts index ec1d8540fab..98f9f197358 100644 --- a/update/src/update_test.ts +++ b/update/src/update_test.ts @@ -150,7 +150,7 @@ interface State { export async function handler( ctx: FreshContext, ) { - const req = ctx.req; + const req = ctx.request; ctx.state.data = "myData"; ctx.state.url = req.url; @@ -177,7 +177,7 @@ Deno.test("update - 1.x project middlewares one arg", async () => { .toEqual(`import { FreshContext } from "fresh"; export async function handler(ctx: FreshContext) { - const req = ctx.req; + const req = ctx.request; return new Response("hello world from: " + req.url); }`); @@ -258,19 +258,19 @@ export const handler: Handlers = { export const handler: Handlers = { async GET(ctx) { - const req = ctx.req; + const req = ctx.request; }, async POST(ctx) { - const req = ctx.req; + const req = ctx.request; }, async PATCH(ctx) { - const req = ctx.req; + const req = ctx.request; }, async PUT(ctx) { - const req = ctx.req; + const req = ctx.request; }, async DELETE(ctx) { - const req = ctx.req; + const req = ctx.request; }, };`); expect(files["/routes/foo.tsx"]) @@ -289,19 +289,19 @@ export const handler: Handlers = { export const handler: Handlers = { async GET(ctx) { - const request = ctx.req; + const request = ctx.request; }, async POST(ctx) { - const request = ctx.req; + const request = ctx.request; }, async PATCH(ctx) { - const request = ctx.req; + const request = ctx.request; }, async PUT(ctx) { - const request = ctx.req; + const request = ctx.request; }, async DELETE(ctx) { - const request = ctx.req; + const request = ctx.request; }, };`); expect(files["/routes/name-unused.tsx"]) @@ -334,7 +334,7 @@ Deno.test( expect(files["/routes/index.tsx"]) .toEqual(`export const handler: Handlers = { GET(ctx) { - const req = ctx.req; + const req = ctx.request; return Response.redirect(req.url); }, @@ -361,7 +361,7 @@ Deno.test.ignore( expect(files["/routes/index.tsx"]) .toEqual(`export const handler: Handlers = { GET: (ctx) => { - const req = ctx.req; + const req = ctx.request; return Response.redirect(req.url); }, @@ -369,7 +369,7 @@ Deno.test.ignore( expect(files["/routes/foo.tsx"]) .toEqual(`export const handler: Handlers = { GET: (ctx) => { - const req = ctx.req; + const req = ctx.request; return Response.redirect(req.url); }, @@ -397,7 +397,7 @@ Deno.test( export const handler = { GET(ctx: FreshContext) { - const req = ctx.req; + const req = ctx.request; return Response.redirect(req.url); }, @@ -473,7 +473,7 @@ export default defineRoute(async (req, ctx) => { .toEqual(`import { defineApp } from "fresh/compat"; export default defineApp(async (ctx) => { - const req = ctx.req; + const req = ctx.request; return null; });`); @@ -481,7 +481,7 @@ export default defineApp(async (ctx) => { .toEqual(`import { defineLayout } from "fresh/compat"; export default defineLayout(async (ctx) => { - const req = ctx.req; + const req = ctx.request; return null; });`); @@ -489,7 +489,7 @@ export default defineLayout(async (ctx) => { .toEqual(`import { defineRoute } from "fresh/compat"; export default defineRoute(async (ctx) => { - const req = ctx.req; + const req = ctx.request; return null; });`); @@ -520,7 +520,7 @@ Deno.test( .toEqual(`import { FreshContext } from "fresh"; export default async function Index(ctx: FreshContext) { - const req = ctx.req; + const req = ctx.request; if (true) { return ctx.throw(404); diff --git a/www/routes/_middleware.ts b/www/routes/_middleware.ts index d407000e76f..f5245ea4a6e 100644 --- a/www/routes/_middleware.ts +++ b/www/routes/_middleware.ts @@ -106,7 +106,7 @@ export async function handler( throw e; } finally { ga4( - ctx.req, + ctx.request, ctx, res!, start, diff --git a/www/routes/index.tsx b/www/routes/index.tsx index 031eda5c278..ba523c6bbe5 100644 --- a/www/routes/index.tsx +++ b/www/routes/index.tsx @@ -16,9 +16,9 @@ import { define } from "../utils/state.ts"; export const handler = define.handlers({ GET(ctx) { - const { req } = ctx; - const accept = req.headers.get("accept"); - const userAgent = req.headers.get("user-agent"); + const { request } = ctx; + const accept = request.headers.get("accept"); + const userAgent = request.headers.get("user-agent"); if (userAgent?.includes("Deno/") && !accept?.includes("text/html")) { const path = `https://deno.land/x/fresh@${VERSIONS[0]}/init.ts`; return new Response(`Redirecting to ${path}`, { @@ -37,7 +37,7 @@ export const handler = define.handlers({ }, async POST(ctx) { const headers = new Headers(); - const form = await ctx.req.formData(); + const form = await ctx.request.formData(); const treat = form.get("treat"); headers.set("location", `/thanks?vote=${treat}`); return new Response(null, { diff --git a/www/routes/raw.ts b/www/routes/raw.ts index 503020a0273..fb393e57a51 100644 --- a/www/routes/raw.ts +++ b/www/routes/raw.ts @@ -18,7 +18,7 @@ const contentTypes = new Map([ export const handler = define.handlers({ async GET(ctx) { - const accept = ctx.req.headers.get("Accept"); + const accept = ctx.request.headers.get("Accept"); const isHTML = accept?.includes("text/html"); const { version, path } = ctx.params; diff --git a/www/routes/update.tsx b/www/routes/update.tsx index b36862b99d2..bd1a16399d3 100644 --- a/www/routes/update.tsx +++ b/www/routes/update.tsx @@ -2,8 +2,8 @@ import { define } from "../utils/state.ts"; import VERSIONS from "../../versions.json" with { type: "json" }; export const handler = define.handlers({ - GET({ req }) { - const accept = req.headers.get("accept"); + GET({ request }) { + const accept = request.headers.get("accept"); let path = "/docs/concepts/updating"; if (accept && !accept.includes("text/html")) { path = `https://deno.land/x/fresh@${VERSIONS[0]}/update.ts`;