Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: rename FreshContext.req to FreshContext.request #2593

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/canary/examples/migration-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion init/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface FreshContext<State = unknown> {
readonly config: ResolvedFreshConfig;
readonly state: State;
/** The original incoming `Request` object` */
readonly req: Request;
readonly request: Request;
/**
* The request url parsed into an `URL` instance. This is typically used
* to apply logic based on the pathname of the incoming url or when
Expand Down Expand Up @@ -90,7 +90,7 @@ export class FreshReqContext<State>
implements FreshContext<State>, PageProps<unknown, State> {
config: ResolvedFreshConfig;
url: URL;
req: Request;
request: Request;
params: Record<string, string>;
state: State = {} as State;
data: unknown = undefined;
Expand All @@ -110,7 +110,7 @@ export class FreshReqContext<State>
}

constructor(
req: Request,
request: Request,
url: URL,
info: Deno.ServeHandlerInfo,
params: Record<string, string>,
Expand All @@ -120,7 +120,7 @@ export class FreshReqContext<State>
buildCache: BuildCache,
) {
this.url = url;
this.req = req;
this.request = request;
this.info = info;
this.params = params;
this.config = config;
Expand Down
2 changes: 1 addition & 1 deletion src/dev/middlewares/error_overlay/middleware.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function devErrorOverlay<T>(): MiddlewareFn<T> {
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;
Expand Down
6 changes: 3 additions & 3 deletions src/dev/middlewares/live_reload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ export function liveReload<T>(): MiddlewareFn<T> {
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 });
}

Expand All @@ -20,7 +20,7 @@ export function liveReload<T>(): MiddlewareFn<T> {
// 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(
Expand Down
2 changes: 1 addition & 1 deletion src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export function page<T>(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!`);
* });
* ```
*/
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
* });
Expand Down
8 changes: 4 additions & 4 deletions src/middlewares/static_files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { getBuildCache } from "../context.ts";
*/
export function staticFiles<T>(): MiddlewareFn<T> {
return async function freshStaticFiles(ctx) {
const { req, url, config } = ctx;
const { request, url, config } = ctx;
const buildCache = getBuildCache(ctx);

let pathname = decodeURIComponent(url.pathname);
Expand All @@ -34,7 +34,7 @@ export function staticFiles<T>(): MiddlewareFn<T> {
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 });
}
Expand Down Expand Up @@ -67,7 +67,7 @@ export function staticFiles<T>(): MiddlewareFn<T> {
"no-cache, no-store, max-age=0, must-revalidate",
);
} else {
const ifNoneMatch = req.headers.get("If-None-Match");
const ifNoneMatch = request.headers.get("If-None-Match");
if (
ifNoneMatch !== null &&
(ifNoneMatch === etag || ifNoneMatch === `W/"${etag}"`)
Expand All @@ -84,7 +84,7 @@ export function staticFiles<T>(): MiddlewareFn<T> {
}

headers.set("Content-Length", String(file.size));
if (req.method === "HEAD") {
if (request.method === "HEAD") {
file.close();
return new Response(null, { status: 200, headers });
}
Expand Down
10 changes: 5 additions & 5 deletions tests/partials_test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1795,7 +1795,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(
Expand Down Expand Up @@ -1854,7 +1854,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, {
Expand Down Expand Up @@ -1901,7 +1901,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(
Expand Down Expand Up @@ -1962,7 +1962,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(
Expand Down Expand Up @@ -2025,7 +2025,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(
Expand Down
2 changes: 1 addition & 1 deletion update/src/maybePrependReqVar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export function maybePrependReqVar(
declarationKind: tsmorph.VariableDeclarationKind.Const,
declarations: [{
name: paramName,
initializer: "ctx.req",
initializer: "ctx.request",
}],
});
}
Expand Down
2 changes: 1 addition & 1 deletion update/src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ function maybePrependReqVar(
declarationKind: tsmorph.VariableDeclarationKind.Const,
declarations: [{
name: paramName,
initializer: "ctx.req",
initializer: "ctx.request",
}],
});
}
Expand Down
40 changes: 20 additions & 20 deletions update/src/update_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ interface State {
export async function handler(
ctx: FreshContext<State>,
) {
const req = ctx.req;
const req = ctx.request;

ctx.state.data = "myData";
ctx.state.url = req.url;
Expand All @@ -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);
}`);
Expand Down Expand Up @@ -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"])
Expand All @@ -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"])
Expand Down Expand Up @@ -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);
},
Expand All @@ -361,15 +361,15 @@ 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);
},
};`);
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);
},
Expand Down Expand Up @@ -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);
},
Expand Down Expand Up @@ -473,23 +473,23 @@ 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;
});`);
expect(files["/routes/_layout.tsx"])
.toEqual(`import { defineLayout } from "fresh/compat";

export default defineLayout(async (ctx) => {
const req = ctx.req;
const req = ctx.request;

return null;
});`);
expect(files["/routes/foo.tsx"])
.toEqual(`import { defineRoute } from "fresh/compat";

export default defineRoute(async (ctx) => {
const req = ctx.req;
const req = ctx.request;

return null;
});`);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion www/routes/_middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export async function handler(
throw e;
} finally {
ga4(
ctx.req,
ctx.request,
ctx,
res!,
start,
Expand Down
8 changes: 4 additions & 4 deletions www/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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}`, {
Expand All @@ -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, {
Expand Down
2 changes: 1 addition & 1 deletion www/routes/raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions www/routes/update.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
Expand Down
Loading