feat(server): plugins can add middlewares and routes (#1455)
Co-authored-by: RoeHH <git@roeh.ch> Co-authored-by: Marvin Hagemeister <marvin@deno.com>
This commit is contained in:
co-authored by
RoeHH
Marvin Hagemeister
parent
7dee138bee
commit
50db296fb7
+47
-1
@@ -27,6 +27,8 @@ import {
|
||||
MiddlewareModule,
|
||||
MiddlewareRoute,
|
||||
Plugin,
|
||||
PluginMiddleware,
|
||||
PluginRoute,
|
||||
RenderFunction,
|
||||
RenderOptions,
|
||||
Route,
|
||||
@@ -176,7 +178,14 @@ export class ServerContext {
|
||||
let app: AppModule = DEFAULT_APP;
|
||||
let notFound: UnknownPage = DEFAULT_NOT_FOUND;
|
||||
let error: ErrorPage = DEFAULT_ERROR;
|
||||
for (const [self, module] of Object.entries(manifest.routes)) {
|
||||
const allRoutes = [
|
||||
...Object.entries(manifest.routes),
|
||||
...Object.entries(getMiddlewareRoutesFromPlugins(opts.plugins || [])),
|
||||
...Object.entries(getRoutesFromPlugins(opts.plugins || [])),
|
||||
];
|
||||
for (
|
||||
const [self, module] of allRoutes
|
||||
) {
|
||||
const url = new URL(self, baseUrl).href;
|
||||
if (!url.startsWith(baseUrl + "routes")) {
|
||||
throw new TypeError("Page is not a child of the basepath.");
|
||||
@@ -1133,6 +1142,43 @@ async function readDenoConfig(
|
||||
}
|
||||
}
|
||||
|
||||
function getMiddlewareRoutesFromPlugins(
|
||||
plugins: Plugin[],
|
||||
): Record<string, MiddlewareModule> {
|
||||
return (Object.assign(
|
||||
{},
|
||||
...[
|
||||
...new Set(
|
||||
([] as PluginMiddleware[]).concat(
|
||||
...plugins.map((p) => p.middlewares || []),
|
||||
),
|
||||
),
|
||||
]
|
||||
.map((middleware: PluginMiddleware) => ({
|
||||
[`./routes${middleware.path}_middleware.ts`]: {
|
||||
handler: middleware.middleware.handler,
|
||||
},
|
||||
})) || [],
|
||||
));
|
||||
}
|
||||
|
||||
function getRoutesFromPlugins(plugins: Plugin[]): Record<string, RouteModule> {
|
||||
return (Object.assign(
|
||||
{},
|
||||
...[
|
||||
...new Set(
|
||||
([] as PluginRoute[]).concat(...plugins.map((p) => p.routes || [])),
|
||||
),
|
||||
]
|
||||
.map((route: PluginRoute) => ({
|
||||
[`./routes${route.path}.ts`]: {
|
||||
default: route.component,
|
||||
handler: route.handler,
|
||||
} as RouteModule,
|
||||
})) || [],
|
||||
));
|
||||
}
|
||||
|
||||
function sendResponse(
|
||||
resp: [string, ContentSecurityPolicy | undefined],
|
||||
options: {
|
||||
|
||||
@@ -337,6 +337,10 @@ export interface Plugin {
|
||||
* propagate state between the render hook and the renderer.
|
||||
*/
|
||||
renderAsync?(ctx: PluginAsyncRenderContext): Promise<PluginRenderResult>;
|
||||
|
||||
routes?: PluginRoute[];
|
||||
|
||||
middlewares?: PluginMiddleware[];
|
||||
}
|
||||
|
||||
export interface PluginRenderContext {
|
||||
@@ -387,3 +391,20 @@ export interface PluginRenderFunctionResult {
|
||||
* client. */
|
||||
requiresHydration: boolean;
|
||||
}
|
||||
|
||||
export interface PluginMiddleware {
|
||||
/** A path in the format of a filename path without filetype */
|
||||
path: string;
|
||||
|
||||
middleware: Middleware;
|
||||
}
|
||||
|
||||
export interface PluginRoute {
|
||||
/** A path in the format of a filename path without filetype */
|
||||
path: string;
|
||||
|
||||
component?: ComponentType<PageProps> | ComponentType<AppProps>;
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
handler?: Handler<any, any> | Handlers<any, any>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user