chore: update types in tests, init, update (#2122)
Of particular note is the change to
`tests/fixture_plugin/routes/test.tsx`. Note how I do this:
```diff
- async GET(_req, ctx: HandlerContext<unknown, { test: string }>) {
+ async GET(_req, ctx: FreshContext<{ test: string }, unknown>) {
```
I find it strange and confusing that the order of the type parameters
has "changed". This makes it impossible to do a simple search and
replace, because people now have to change order as well.
Most of the types have been declared as `<T, S>`, i.e. data before
state:
* `PageProps<T = any, S = Record<string, unknown>>`
* `RouteContext<T = any, S = Record<string, unknown>>`
* `Handler<T = any, State = Record<string, unknown>>`
* `Handlers<T = any, State = Record<string, unknown>> `
* `AsyncRoute<T = any, S = Record<string, unknown>>`
* `PageComponent<T = any, S = Record<string, unknown>> `
* `AsyncLayout<T = any, S = Record<string, unknown>>`
And then `FreshContext` comes around like
```
export interface FreshContext<
State = Record<string, unknown>,
// deno-lint-ignore no-explicit-any
Data = any,
NotFoundData = Data,
>
```
and switches it all up.
Not a huge issue, but just something to be aware of that others might
report.
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
import { FreshContext } from "$fresh/server.ts";
|
||||
|
||||
// handlers are supposed to return something, so in order to make type checker on the manifest happy, we'll use any to escape it
|
||||
export function handler(
|
||||
_req: Request,
|
||||
_ctx: MiddlewareHandlerContext,
|
||||
_ctx: FreshContext,
|
||||
// deno-lint-ignore no-explicit-any
|
||||
): any {
|
||||
throw new Deno.errors.NotFound();
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { UnknownPageProps } from "$fresh/server.ts";
|
||||
import { PageProps } from "$fresh/server.ts";
|
||||
|
||||
type Data = { hello: string };
|
||||
type State = { root: string };
|
||||
|
||||
export default function NotFoundPage(
|
||||
{ data, state, url }: UnknownPageProps<Data | undefined, State>,
|
||||
{ data, state, url }: PageProps<Data | undefined, State>,
|
||||
) {
|
||||
// Checks that we have the correct type for state
|
||||
state.root satisfies string;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ErrorPageProps } from "$fresh/server.ts";
|
||||
import { PageProps } from "$fresh/server.ts";
|
||||
|
||||
export default function Error500Page({ error }: ErrorPageProps) {
|
||||
export default function Error500Page({ error }: PageProps) {
|
||||
return <p>500 internal error: {(error as Error).message}</p>;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { Head } from "$fresh/runtime.ts";
|
||||
import { AppProps } from "$fresh/server.ts";
|
||||
import { PageProps } from "$fresh/server.ts";
|
||||
|
||||
export type TestState = {
|
||||
root: string;
|
||||
stateInProps: string;
|
||||
};
|
||||
|
||||
export default function App(props: AppProps<unknown, TestState>) {
|
||||
export default function App(props: PageProps<unknown, TestState>) {
|
||||
const statefulValue = props.state?.root === "root_mw"
|
||||
? "The freshest framework!"
|
||||
: "";
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { HandlerContext } from "$fresh/server.ts";
|
||||
import { FreshContext } from "$fresh/server.ts";
|
||||
|
||||
export default function Page() {
|
||||
return <div>This is HTML</div>;
|
||||
}
|
||||
|
||||
export const handler = {
|
||||
GET(req: Request, { render }: HandlerContext) {
|
||||
GET(req: Request, { render }: FreshContext) {
|
||||
if (req.headers.get("accept")?.includes("text/html")) {
|
||||
return render();
|
||||
} else {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
import { FreshContext } from "$fresh/server.ts";
|
||||
|
||||
export async function handler(_req: Request, ctx: MiddlewareHandlerContext) {
|
||||
export async function handler(_req: Request, ctx: FreshContext) {
|
||||
ctx.state.layer1 = "layer1_mw";
|
||||
const resp = await ctx.next();
|
||||
resp.headers.set("server", "fresh test server layer1");
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
import { FreshContext } from "$fresh/server.ts";
|
||||
|
||||
export async function handler(_req: Request, ctx: MiddlewareHandlerContext) {
|
||||
export async function handler(_req: Request, ctx: FreshContext) {
|
||||
const resp = await ctx.next();
|
||||
resp.headers.set("middlewareParams_inner", JSON.stringify(ctx.params));
|
||||
return resp;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
import { FreshContext } from "$fresh/server.ts";
|
||||
|
||||
export async function handler(_req: Request, ctx: MiddlewareHandlerContext) {
|
||||
export async function handler(_req: Request, ctx: FreshContext) {
|
||||
const resp = await ctx.next();
|
||||
resp.headers.set("middlewareParams_outer", JSON.stringify(ctx.params));
|
||||
return resp;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
import { FreshContext } from "$fresh/server.ts";
|
||||
|
||||
interface State {
|
||||
root: string;
|
||||
@@ -8,7 +8,7 @@ interface State {
|
||||
|
||||
export async function handler(
|
||||
_req: Request,
|
||||
ctx: MiddlewareHandlerContext<State>,
|
||||
ctx: FreshContext<State>,
|
||||
) {
|
||||
ctx.state.layer2 = "layer2_mw";
|
||||
const resp = await ctx.next();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
import { FreshContext } from "$fresh/server.ts";
|
||||
|
||||
export async function handler(_req: Request, ctx: MiddlewareHandlerContext) {
|
||||
export async function handler(_req: Request, ctx: FreshContext) {
|
||||
ctx.state.layer3 = "layer3_mw";
|
||||
const resp = await ctx.next();
|
||||
resp.headers.set("server", "fresh test server layer3");
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
import { FreshContext } from "$fresh/server.ts";
|
||||
|
||||
export async function handler(_req: Request, ctx: MiddlewareHandlerContext) {
|
||||
export async function handler(_req: Request, ctx: FreshContext) {
|
||||
ctx.state.middlewareNestingOrder += "3";
|
||||
const resp = await ctx.next();
|
||||
return resp;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
import { FreshContext } from "$fresh/server.ts";
|
||||
|
||||
export async function handler(_req: Request, ctx: MiddlewareHandlerContext) {
|
||||
export async function handler(_req: Request, ctx: FreshContext) {
|
||||
ctx.state.middlewareNestingOrder += "2";
|
||||
const resp = await ctx.next();
|
||||
return resp;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
import { FreshContext } from "$fresh/server.ts";
|
||||
|
||||
export async function handler(_req: Request, ctx: MiddlewareHandlerContext) {
|
||||
export async function handler(_req: Request, ctx: FreshContext) {
|
||||
ctx.state.middlewareNestingOrder = "1";
|
||||
const resp = await ctx.next();
|
||||
return resp;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
import { FreshContext } from "$fresh/server.ts";
|
||||
|
||||
export async function handler(
|
||||
_req: Request,
|
||||
ctx: MiddlewareHandlerContext,
|
||||
ctx: FreshContext,
|
||||
) {
|
||||
try {
|
||||
ctx.state.flag = true;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { HandlerContext, RouteConfig } from "$fresh/server.ts";
|
||||
import { FreshContext, RouteConfig } from "$fresh/server.ts";
|
||||
|
||||
export const handler = {
|
||||
GET(_req: Request, { params }: HandlerContext) {
|
||||
GET(_req: Request, { params }: FreshContext) {
|
||||
return new Response(params.path);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { LayoutProps } from "$fresh/server.ts";
|
||||
import { PageProps } from "$fresh/server.ts";
|
||||
|
||||
export default function BarLayout({ Component }: LayoutProps) {
|
||||
export default function BarLayout({ Component }: PageProps) {
|
||||
return (
|
||||
<div>
|
||||
<p class="baz-layout">Baz layout</p>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { LayoutProps } from "$fresh/server.ts";
|
||||
import { PageProps } from "$fresh/server.ts";
|
||||
|
||||
export default function BarLayout({ Component }: LayoutProps) {
|
||||
export default function BarLayout({ Component }: PageProps) {
|
||||
return (
|
||||
<div>
|
||||
<p class="bar-layout">Bar layout</p>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { LayoutProps } from "$fresh/server.ts";
|
||||
import { PageProps } from "$fresh/server.ts";
|
||||
|
||||
export default function FooLayout({ Component }: LayoutProps) {
|
||||
export default function FooLayout({ Component }: PageProps) {
|
||||
return (
|
||||
<div>
|
||||
<p class="foo-layout">Foo layout</p>
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
import { FreshContext } from "$fresh/server.ts";
|
||||
import { TestState } from "../_app.tsx";
|
||||
|
||||
export async function handler(
|
||||
_req: Request,
|
||||
ctx: MiddlewareHandlerContext<TestState>,
|
||||
ctx: FreshContext<TestState>,
|
||||
) {
|
||||
ctx.state.stateInProps = "look, i am set from middleware";
|
||||
const resp = await ctx.next();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
import { FreshContext } from "$fresh/server.ts";
|
||||
|
||||
export const handler = (_req: Request, ctx: MiddlewareHandlerContext) => {
|
||||
export const handler = (_req: Request, ctx: FreshContext) => {
|
||||
ctx.state = { handler1: "it works" };
|
||||
return ctx.next();
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
import { FreshContext } from "$fresh/server.ts";
|
||||
|
||||
export const handler = (_req: Request, ctx: MiddlewareHandlerContext) => {
|
||||
export const handler = (_req: Request, ctx: FreshContext) => {
|
||||
ctx.state.handler2 = "it works";
|
||||
return ctx.next();
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { HandlerContext, PageProps } from "$fresh/server.ts";
|
||||
import { FreshContext, PageProps } from "$fresh/server.ts";
|
||||
|
||||
export const handler = (_req: Request, ctx: HandlerContext) => {
|
||||
export const handler = (_req: Request, ctx: FreshContext) => {
|
||||
ctx.state.handler3 = "it works";
|
||||
return ctx.render();
|
||||
};
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { HandlerContext } from "$fresh/server.ts";
|
||||
import { FreshContext } from "$fresh/server.ts";
|
||||
|
||||
export default function Page() {
|
||||
return <div>This is HTML</div>;
|
||||
}
|
||||
|
||||
export const handler = {
|
||||
GET(req: Request, { render }: HandlerContext) {
|
||||
GET(req: Request, { render }: FreshContext) {
|
||||
return render(undefined, {
|
||||
status: 401,
|
||||
headers: { "x-some-header": "foo" },
|
||||
|
||||
Reference in New Issue
Block a user