[RFC] Server Middleware & Server Context #5639
Replies: 4 comments 15 replies
-
|
Beta Was this translation helpful? Give feedback.
-
Looks Great! And I have some topics to discuss
|
Beta Was this translation helpful? Give feedback.
-
I have a idea, why we can't get something from dataLoader.context, like this: type LoaderFunctionArgs<C extends Record<string, any> = {}> = {
context: C;
};
interface Context {
name: string;
age: number;
}
const loader = ({ context }: LoaderFunctionArgs<Context>) => {
const name = context.name;
const age = context.age;
}; Have any problems like this? |
Beta Was this translation helpful? Give feedback.
-
ConclusionAfter discuss, we confirm some design. So Make a summarize. Enable new server MiddlewareTo avoid breaking change, we will appoint file export to open this feature. // server/index.ts
export unstableMiddleware: UnstableMiddleware[] = [mid1, mid2]; Middleware Typestype UnstableMiddlewareContext<Var> = {
request: Request;
response: Response;
header: (name: string, value: string) => void;
status: (code: number) => void;
get: (key: string) => unknown;
set: (key: string, value: unknown) => void;
redirect: (location: string, status?: number) => Response;
body: (data: Body, init?: ResponseInit) => Response;
html: (
data: string | Promise<string>,
init?: ResponseInit,
) => Response | Promise<Response>;
} dataLoaderdataLoader get payload from server middleware by using See Examples. // server/index.ts
import type { Env } from '@shared/index';
export const middleware: Middleware = async (ctx<Env>, next) => {
const user = await getUserInfo();
if(!user) {
return redirect('/login');
}
ctx.set('user', user);
await next();
} // loader.data.ts
import type { Env } from '@shared/index'
export const loader = ({ context }: LoaderFunctionArgs<Env>) {
const user = context.get('user');
} dataLoader TypesEnsure dataLoader can extend types by others plugins, we will provide a interface to extend types. See specific implement. export interface RequestPayload {
[key: string]: unknown;
}
interface Get<P extends Record<string, unknown>> {
<Key extends keyof P>(key: Key): P[Key];
}
interface Set<P extends Record<string, unknown>> {
<Key extends keyof P>(key: Key, value: P[Key]): void;
}
export type RequestContext<P extends Record<string, unknown> = {}> = {
get: Get<P & RequestPayload>;
set: Set<P & RequestPayload>;
}; Deprecated
|
Beta Was this translation helpful? Give feedback.
-
Summary
Support server middleware like koa-compose.
Background
Currently, Modern.js can't give a way to control the render process.
Using middleware, we can extend servers conveniently. But now, modern.js server middleware doesn't like koa-compose , can't do something after render.
On the other hand, we can't pass context to runtime from server middleware. Doing some auth work in middleware is very frequent. But the user can't pass user info to runtime for now.
Detailed Design
Future Flag
To avoid breaking change, we would import a
future flag
to open this featureMiddleware Types
Examples
Authentication
Log Render Timing
Affect Response
Deprecated
We can get a reporter beforehand from data loader
We would support this usage, but would tag this as a
deprecated
status.We use
context.get('reporter')
instead ofcontext.get(reporterCtx)
Conclusion
Beta Was this translation helpful? Give feedback.
All reactions