-
Notifications
You must be signed in to change notification settings - Fork 7
/
types.ts
91 lines (74 loc) · 2.65 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import type { StreamInjectorHooks } from '@ssrx/streaming';
import type { SSRx } from './namespace.ts';
type ConfigBuiltIn = {
jsxElement: unknown;
};
export type Config = ConfigBuiltIn & SSRx.Config;
export type RenderToStreamFn<O extends object = object> = (props: {
app: () => Config['jsxElement'];
req: Request;
injectToStream?: StreamInjectorHooks | StreamInjectorHooks[];
opts?: O;
}) => Promise<{ stream: ReadableStream; statusCode: () => number }>;
export type ServerRenderer = {
renderToStream: RenderToStreamFn;
};
type BaseHandlerOpts = {
RootLayout?: null | false | ((props: { children: Config['jsxElement'] }) => Config['jsxElement']);
appRenderer?: (props: {
req: Request;
renderProps: SSRx.RenderProps;
meta?: SSRx.ReqMeta;
}) => (() => Config['jsxElement']) | Promise<() => Config['jsxElement']>;
};
export type ClientHandlerOpts<P extends RenderPlugin<any>[]> = BaseHandlerOpts & {
plugins?: P;
};
export type ClientHandlerFn = (props?: { renderProps?: SSRx.RenderProps }) => Promise<() => Config['jsxElement']>;
export type ServerHandlerOpts<P extends RenderPlugin<any>[]> = BaseHandlerOpts & {
renderer: ServerRenderer;
plugins?: P;
};
export type ServerHandlerFn = (props: {
req: Request;
renderProps?: SSRx.RenderProps;
meta?: SSRx.ReqMeta;
}) => Promise<{ stream: ReadableStream<Uint8Array>; statusCode: () => number }>;
export type CommonHooks<AC extends Record<string, unknown> = Record<string, unknown>> = {
/**
* Extend the app ctx object with additional properties. The app ctx object is made available
* to the end application on the server and the client, and to subsequent plugins.
*/
extendCtx?: () => AC;
/**
* Wrap the app component with a higher-order component. This is useful for wrapping the app with providers, etc.
*/
wrapApp?: () => (props: { children: () => Config['jsxElement'] }) => Config['jsxElement'];
/**
* Render the final inner-most app component. Only one plugin may do this - usually a routing plugin.
*/
renderApp?: () => (() => Config['jsxElement']) | Promise<() => Config['jsxElement']>;
};
export type ServerHooks = StreamInjectorHooks;
export type RenderPlugin<AC extends Record<string, unknown>> = {
id: Readonly<string>;
hooksForReq?: (props: {
req: Request;
meta?: SSRx.ReqMeta;
renderProps: SSRx.RenderProps;
ctx: Record<string, unknown>;
}) =>
| null
| {
common?: CommonHooks<AC>;
server?: ServerHooks;
}
| Promise<null | {
common?: CommonHooks<AC>;
server?: ServerHooks;
}>;
};
/**
* Some types useful to downstream consumers.
*/
export type { SetOptional } from 'type-fest';