fix(server): Improve MiddlewareHandler type for plugins (#1534)
This commit is contained in:
+4
-4
@@ -325,7 +325,7 @@ export interface Island {
|
||||
|
||||
// --- PLUGINS ---
|
||||
|
||||
export interface Plugin {
|
||||
export interface Plugin<State = Record<string, unknown>> {
|
||||
/** The name of the plugin. Must be snake-case. */
|
||||
name: string;
|
||||
|
||||
@@ -359,7 +359,7 @@ export interface Plugin {
|
||||
|
||||
routes?: PluginRoute[];
|
||||
|
||||
middlewares?: PluginMiddleware[];
|
||||
middlewares?: PluginMiddleware<State>[];
|
||||
}
|
||||
|
||||
export interface PluginRenderContext {
|
||||
@@ -411,11 +411,11 @@ export interface PluginRenderFunctionResult {
|
||||
requiresHydration: boolean;
|
||||
}
|
||||
|
||||
export interface PluginMiddleware {
|
||||
export interface PluginMiddleware<State = Record<string, unknown>> {
|
||||
/** A path in the format of a filename path without filetype */
|
||||
path: string;
|
||||
|
||||
middleware: Middleware;
|
||||
middleware: Middleware<State>;
|
||||
}
|
||||
|
||||
export interface PluginRoute {
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import { Handlers, PageProps } from "../../../server.ts";
|
||||
import { HandlerContext, Handlers, PageProps } from "../../../server.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET(_req, ctx) {
|
||||
export const handler: Handlers<unknown, { test: string }> = {
|
||||
async GET(_req, ctx: HandlerContext<unknown, { test: string }>) {
|
||||
const resp = await ctx.render();
|
||||
return resp;
|
||||
},
|
||||
};
|
||||
|
||||
export default function Home(props: PageProps) {
|
||||
const value = props.state.test as string;
|
||||
export default function Home(props: PageProps<unknown, { test: string }>) {
|
||||
const value = props.state.test;
|
||||
return (
|
||||
<div>
|
||||
<h1>{value}</h1>
|
||||
|
||||
@@ -7,7 +7,9 @@ interface Options {
|
||||
title: string;
|
||||
}
|
||||
|
||||
export default function routePlugin(options: Options): Plugin {
|
||||
export default function routePlugin(
|
||||
options: Options,
|
||||
): Plugin<{ test: string }> {
|
||||
return {
|
||||
name: "routePlugin",
|
||||
middlewares: [{
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
|
||||
export async function handler(_req: Request, ctx: MiddlewareHandlerContext) {
|
||||
export async function handler(
|
||||
_req: Request,
|
||||
ctx: MiddlewareHandlerContext<{ test: string }>,
|
||||
) {
|
||||
ctx.state.test = "look, i'm set from a plugin!";
|
||||
const resp = await ctx.next();
|
||||
return resp;
|
||||
|
||||
Reference in New Issue
Block a user