quasar
144 строки · 4.0 Кб
1import { Express, Request, RequestHandler, Response, NextFunction } from "express";
2import { Server } from "http";
3import { Server as HttpsServer } from "https";
4import { ServeStaticOptions } from "serve-static";
5
6interface RenderParams {
7req: Request;
8res: Response;
9}
10
11interface RenderVueParams extends RenderParams, Record<string, any> {}
12
13interface RenderError extends Error {
14code: Response["statusCode"];
15url: Request["url"];
16}
17
18interface RenderErrorParams extends RenderParams {
19err: RenderError;
20}
21
22interface SsrMiddlewareServe {
23/**
24* It's essentially a wrapper over express.static() with a few convenient tweaks:
25* - the pathFromPublicFolder is a path resolved to the "public" folder out of the box
26* - the opts are the same as for express.static()
27* - opts.maxAge is used by default, taking into account the quasar.config file > ssr > maxAge configuration; this sets how long the respective file(s) can live in browser's cache
28*/
29static(
30path: string,
31options?: ServeStaticOptions<Response>
32): RequestHandler<Response>;
33/**
34* Displays a wealth of useful debug information (including the stack trace).
35* Warning: It's available only in development and NOT in production.
36*/
37error(ssrContext: RenderErrorParams): void;
38}
39
40type SsrMiddlewareRender = (ssrContext: RenderVueParams) => Promise<string>;
41
42interface SsrMiddlewareFolders {
43root: string;
44public: string;
45}
46
47interface SsrMiddlewareResolve {
48/**
49* Whenever you define a route (with app.use(), app.get(), app.post() etc), you should use the resolve.urlPath() method so that you'll also keep into account the configured publicPath (quasar.config file > build > publicPath).
50*/
51urlPath(url: string): string;
52/**
53* Resolve folder path to the root (of the project in dev and of the distributables in production). Under the hood, it does a path.join()
54* @param paths paths to join
55*/
56root(...paths: string[]): string;
57/**
58* Resolve folder path to the "public" folder. Under the hood, it does a path.join()
59* @param paths paths to join
60*/
61public(...paths: string[]): string;
62}
63
64interface SsrCreateParams {
65/**
66* Terminal PORT env var or the default configured port
67* for the SSR webserver
68*/
69port: number;
70resolve: SsrMiddlewareResolve;
71publicPath: string;
72folders: SsrMiddlewareFolders;
73/**
74* Uses Vue and Vue Router to render the requested URL path.
75* @returns the rendered HTML string to return to the client
76*/
77render: SsrMiddlewareRender;
78serve: SsrMiddlewareServe;
79}
80
81export type SsrCreateCallback = (
82params: SsrCreateParams
83) => Express | any;
84
85interface SsrMiddlewareParams extends SsrCreateParams {
86app: Express;
87}
88
89export type SsrMiddlewareCallback = (
90params: SsrMiddlewareParams
91) => void | Promise<void>;
92
93interface SsrHandlerParams {
94req: Request;
95res: Response;
96next: NextFunction;
97}
98
99interface SsrListenParams extends SsrMiddlewareParams {
100/**
101* Wait for app to be initialized (run all SSR middlewares)
102* before starting to listen for clients
103*/
104isReady(): Promise<void>;
105/**
106* If you use HTTPS in development, this will be the
107* actual server that listens for clients.
108* It is a Node https.Server instance wrapper over the original "app".
109*/
110devHttpsApp?: HttpsServer;
111/**
112* SSR handler to be used on production should
113* you wish to use a serverless approach.
114*/
115ssrHandler(
116params: SsrHandlerParams
117): Promise<void>;
118}
119
120export type SsrListenCallback = (
121params: SsrListenParams
122) => Promise<Server>;
123
124interface SsrCloseParams extends SsrListenParams {
125listenResult: Server;
126}
127
128export type SsrCloseCallback = (
129params: SsrCloseParams
130) => void;
131
132export type SsrServeStaticContentCallback = (
133path: string,
134opts?: ServeStaticOptions<Response>
135) => RequestHandler<Response>;
136
137interface SsrRenderPreloadTagCallbackOptions {
138ssrContext: RenderVueParams;
139}
140
141export type SsrRenderPreloadTagCallback = (
142file: string,
143options: SsrRenderPreloadTagCallbackOptions
144) => string;
145