@thestarweb/star-framework-app-web 1.1.4 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +39 -0
- package/dist/abort-signal/browser.js +10 -0
- package/dist/abort-signal/browser.js.map +1 -0
- package/dist/abort-signal/index.js +25 -0
- package/dist/abort-signal/index.js.map +1 -0
- package/dist/abort-signal/server.js +15 -0
- package/dist/abort-signal/server.js.map +1 -0
- package/dist/browser/index.js +3 -0
- package/dist/browser/index.js.map +1 -1
- package/dist/cli/dev-server.js +42 -15
- package/dist/cli/dev-server.js.map +1 -1
- package/dist/cli/index.js +10 -11
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/package-info.js +13 -0
- package/dist/cli/package-info.js.map +1 -0
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/params/index.js +3 -0
- package/dist/params/index.js.map +1 -1
- package/dist/request/browser.js +28 -0
- package/dist/request/browser.js.map +1 -0
- package/dist/request/index.js +13 -44
- package/dist/request/index.js.map +1 -1
- package/dist/request/server.js +55 -0
- package/dist/request/server.js.map +1 -0
- package/dist/request/types.js +53 -0
- package/dist/request/types.js.map +1 -0
- package/dist/server/dump-api-type.js +53 -8
- package/dist/server/dump-api-type.js.map +1 -1
- package/dist/server/index.js +96 -69
- package/dist/server/index.js.map +1 -1
- package/dist/view/layout.js +4 -3
- package/dist/view/layout.js.map +1 -1
- package/dist/view/render/sf-v-dom-render.js +1 -1
- package/dist/view/types.js +3 -1
- package/dist/view/utils/get-render/client.js +18 -3
- package/dist/view/utils/get-render/client.js.map +1 -1
- package/dist/view/utils/get-render/common.js +18 -11
- package/dist/view/utils/get-render/common.js.map +1 -1
- package/dist/view/utils/get-render/server.js +12 -3
- package/dist/view/utils/get-render/server.js.map +1 -1
- package/dist/view/view.js +4 -4
- package/dist/view/view.js.map +1 -1
- package/package.json +4 -4
- package/src/abort-signal/browser.ts +7 -0
- package/src/abort-signal/index.ts +16 -0
- package/src/abort-signal/server.ts +15 -0
- package/src/browser/index.ts +10 -7
- package/src/cli/dev-server.ts +71 -17
- package/src/cli/index.ts +12 -12
- package/src/cli/package-info.ts +12 -0
- package/src/cli/types.ts +8 -7
- package/src/index.ts +2 -2
- package/src/params/index.ts +17 -14
- package/src/request/browser.ts +34 -0
- package/src/request/index.ts +15 -65
- package/src/request/server.ts +58 -0
- package/src/request/types.ts +130 -0
- package/src/server/dump-api-type.ts +119 -64
- package/src/server/index.ts +124 -77
- package/src/view/layout.ts +22 -20
- package/src/view/render/sf-v-dom-render.ts +34 -34
- package/src/view/types.ts +24 -18
- package/src/view/utils/get-render/client.ts +23 -6
- package/src/view/utils/get-render/common.ts +127 -113
- package/src/view/utils/get-render/server.ts +13 -4
- package/src/view/view.ts +12 -7
- package/src/server/utils/pass-through-readable.ts +0 -10
|
@@ -1,64 +1,119 @@
|
|
|
1
|
-
import { getConfig } from "@thestarweb/star-framework-cli";
|
|
2
|
-
import { getFlatRoute } from "../route/index.js";
|
|
3
|
-
import { dirname, relative } from "path";
|
|
4
|
-
import { writeFile } from "fs/promises";
|
|
5
|
-
import { paramMeta } from "../params/index.js";
|
|
6
|
-
import {
|
|
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
|
-
}
|
|
1
|
+
import { getConfig } from "@thestarweb/star-framework-cli";
|
|
2
|
+
import { getFlatRoute } from "../route/index.js";
|
|
3
|
+
import { dirname, relative } from "path";
|
|
4
|
+
import { mkdir, writeFile } from "fs/promises";
|
|
5
|
+
import { paramMeta } from "../params/index.js";
|
|
6
|
+
import {
|
|
7
|
+
dumpTSType,
|
|
8
|
+
toParamType,
|
|
9
|
+
} from "@thestarweb/star-framework-param-helper";
|
|
10
|
+
import {
|
|
11
|
+
ReflectionClass,
|
|
12
|
+
ReflectionKind,
|
|
13
|
+
TypeFunction,
|
|
14
|
+
typeOf,
|
|
15
|
+
} from "@deepkit/type";
|
|
16
|
+
|
|
17
|
+
export async function dumpRouteType(
|
|
18
|
+
file: string,
|
|
19
|
+
obj: object,
|
|
20
|
+
packageName?: string,
|
|
21
|
+
) {
|
|
22
|
+
const routes = getFlatRoute(obj);
|
|
23
|
+
const config = getConfig()!;
|
|
24
|
+
const data = [];
|
|
25
|
+
let rPath = relative(dirname(file), config.entry).replaceAll("\\", "/");
|
|
26
|
+
if (!rPath.startsWith(".")) {
|
|
27
|
+
rPath = `./${rPath}`;
|
|
28
|
+
}
|
|
29
|
+
if(!packageName){
|
|
30
|
+
data.push(`import App from ${JSON.stringify(rPath)};`);
|
|
31
|
+
}
|
|
32
|
+
data.push(`declare module '@thestarweb/star-framework-app-web'{`);
|
|
33
|
+
data.push(`\tinterface ${packageName ? "AllApi" : "ProjectApi"}{`);
|
|
34
|
+
if (packageName) {
|
|
35
|
+
data.push(`\t${JSON.stringify(packageName)}:{`);
|
|
36
|
+
}
|
|
37
|
+
Object.keys(routes).forEach((type) => {
|
|
38
|
+
if (type === "__SF:NotFind") return;
|
|
39
|
+
data.push(`\t\t${type}:{`);
|
|
40
|
+
routes[type as keyof typeof routes]!.forEach((meta, path) => {
|
|
41
|
+
if (!meta.finalRoute) return;
|
|
42
|
+
data.push(`\t\t\t${JSON.stringify(path)}:{`);
|
|
43
|
+
const { obj, propertyKey } = meta.finalRoute;
|
|
44
|
+
if (typeof (obj as any)[propertyKey] === "function") {
|
|
45
|
+
data.push(`\t\t\t\tprop:{`);
|
|
46
|
+
const param = paramMeta.getParamMetadata(
|
|
47
|
+
meta.finalRoute.obj,
|
|
48
|
+
meta.finalRoute.propertyKey,
|
|
49
|
+
);
|
|
50
|
+
if (param.types.body) {
|
|
51
|
+
data.push(
|
|
52
|
+
`\t\t\t\t\tbody${param.types.body.required ? "" : "?"}:${dumpTSType(param.types.body.type)},`,
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
if (param.types.query) {
|
|
56
|
+
data.push(
|
|
57
|
+
`\t\t\t\t\tquery${param.types.query.required ? "" : "?"}:${dumpTSType(param.types.query.type)},`,
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
if (param.types.header) {
|
|
61
|
+
data.push(
|
|
62
|
+
`\t\t\t\t\theader${param.types.header.required ? "" : "?"}:${dumpTSType(param.types.header.type)},`,
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
data.push(`\t\t\t\t},`);
|
|
66
|
+
|
|
67
|
+
if (packageName) {
|
|
68
|
+
let type = "any";
|
|
69
|
+
try {
|
|
70
|
+
const classMeta = ReflectionClass.from(meta.finalRoute.obj as any);
|
|
71
|
+
const fnMeta = classMeta
|
|
72
|
+
.getMethod(meta.finalRoute.propertyKey)
|
|
73
|
+
.getReturnType();
|
|
74
|
+
let t = fnMeta
|
|
75
|
+
? fnMeta
|
|
76
|
+
: (
|
|
77
|
+
classMeta
|
|
78
|
+
.getProperty(meta.finalRoute.propertyKey)
|
|
79
|
+
.getType() as TypeFunction
|
|
80
|
+
).return;
|
|
81
|
+
while (t.kind === ReflectionKind.promise) t = t.type;
|
|
82
|
+
type = dumpTSType(toParamType(t));
|
|
83
|
+
} catch {}
|
|
84
|
+
data.push(`\t\t\t\treturn:${type}`);
|
|
85
|
+
} else {
|
|
86
|
+
data.push(
|
|
87
|
+
`\t\t\t\treturn:ReturnType<App${meta.allRoute.map((r) => `[${JSON.stringify(r.propertyKey)}]`).join("")}>`,
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
} else {
|
|
91
|
+
data.push(`\t\t\t\tprop:{},`);
|
|
92
|
+
if (packageName) {
|
|
93
|
+
let type = "any";
|
|
94
|
+
try {
|
|
95
|
+
const classMeta = ReflectionClass.from(meta.finalRoute.obj as any);
|
|
96
|
+
const t = classMeta
|
|
97
|
+
.getProperty(meta.finalRoute.propertyKey)
|
|
98
|
+
.getType();
|
|
99
|
+
type = dumpTSType(toParamType(t));
|
|
100
|
+
} catch {}
|
|
101
|
+
data.push(`\t\t\t\treturn:${type}`);
|
|
102
|
+
} else {
|
|
103
|
+
data.push(
|
|
104
|
+
`\t\t\t\treturn:App${meta.allRoute.map((r) => `[${JSON.stringify(r.propertyKey)}]`).join("")}`,
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
data.push(`\t\t\t},`);
|
|
109
|
+
});
|
|
110
|
+
data.push(`\t\t}`);
|
|
111
|
+
});
|
|
112
|
+
if (packageName) {
|
|
113
|
+
data.push(`\t}`);
|
|
114
|
+
}
|
|
115
|
+
data.push(`\t}`);
|
|
116
|
+
data.push(`}`);
|
|
117
|
+
await mkdir(dirname(file), { recursive: true });
|
|
118
|
+
await writeFile(file, data.join("\n"));
|
|
119
|
+
}
|
package/src/server/index.ts
CHANGED
|
@@ -2,113 +2,160 @@ import { PassThrough, Readable } from "stream";
|
|
|
2
2
|
import { parse } from "qs";
|
|
3
3
|
import { paramMeta } from "../params/index.js";
|
|
4
4
|
import { ResponseWithMeta } from "../return-types/index.js";
|
|
5
|
-
import { PassThroughReadable } from "./utils/pass-through-readable.js";
|
|
6
5
|
import { getInterceptors, Interceptor } from "../interceptor/index.js";
|
|
7
6
|
import { parseRoute } from "../route/index.js";
|
|
8
7
|
import { createServerRender } from "../view/utils/index.js";
|
|
9
8
|
import { CallProp } from "./types.js";
|
|
10
9
|
import {
|
|
10
|
+
RequestProp,
|
|
11
11
|
ServerInstance,
|
|
12
12
|
ServerRuntimeContext,
|
|
13
|
-
} from "@thestarweb/star-framework-web-runtime
|
|
13
|
+
} from "@thestarweb/star-framework-web-runtime";
|
|
14
14
|
import { Method } from "../route/types.js";
|
|
15
15
|
import { JSONBody } from "./body/json.js";
|
|
16
|
+
import { setSfRequest } from "../request/index.js";
|
|
17
|
+
import { ServerSideRequest } from "../request/server.js";
|
|
18
|
+
import {
|
|
19
|
+
mergeAbortSignal,
|
|
20
|
+
setRequestAbortSignalFn,
|
|
21
|
+
} from "../abort-signal/index.js";
|
|
22
|
+
import {
|
|
23
|
+
getRequestAbortSignal,
|
|
24
|
+
runWithSignal,
|
|
25
|
+
} from "../abort-signal/server.js";
|
|
26
|
+
|
|
27
|
+
const textEncoder = new TextEncoder();
|
|
16
28
|
|
|
17
29
|
export function createServerFactory(object: any) {
|
|
30
|
+
setRequestAbortSignalFn(getRequestAbortSignal);
|
|
18
31
|
const handles = [JSONBody];
|
|
19
32
|
return (context: ServerRuntimeContext) => {
|
|
20
33
|
const routes = parseRoute(object);
|
|
21
34
|
const serverRender = createServerRender(context);
|
|
22
35
|
const ins: ServerInstance = {
|
|
23
|
-
async onRequest(
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
36
|
+
async onRequest({
|
|
37
|
+
method,
|
|
38
|
+
url,
|
|
39
|
+
header: reqHeader,
|
|
40
|
+
body: rawBody,
|
|
41
|
+
signal,
|
|
42
|
+
ip,
|
|
43
|
+
}: RequestProp) {
|
|
44
|
+
const ctrl = new AbortController();
|
|
45
|
+
try {
|
|
46
|
+
return await runWithSignal(
|
|
47
|
+
mergeAbortSignal(signal, ctrl.signal),
|
|
48
|
+
async () => {
|
|
49
|
+
const urlObj =
|
|
50
|
+
typeof url === "string"
|
|
51
|
+
? new URL(url, "http://127.0.0.1/")
|
|
52
|
+
: url;
|
|
53
|
+
const route = routes(method as Method, urlObj.pathname);
|
|
54
|
+
|
|
55
|
+
const interceptors: Interceptor[] = route.allRoute.flatMap(
|
|
56
|
+
(info) => getInterceptors(info.obj),
|
|
57
|
+
);
|
|
27
58
|
|
|
28
|
-
|
|
29
|
-
getInterceptors(info.obj),
|
|
30
|
-
);
|
|
59
|
+
interceptors.unshift(serverRender);
|
|
31
60
|
|
|
32
|
-
|
|
61
|
+
let body: any = null;
|
|
62
|
+
let call: (
|
|
63
|
+
prop: CallProp,
|
|
64
|
+
) => ResponseWithMeta | Promise<ResponseWithMeta> = () =>
|
|
65
|
+
new ResponseWithMeta(null, { code: 404 });
|
|
33
66
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
prop: CallProp,
|
|
37
|
-
) => ResponseWithMeta | Promise<ResponseWithMeta> = () =>
|
|
38
|
-
new ResponseWithMeta(null, { code: 404 });
|
|
67
|
+
if (route.finalRoute) {
|
|
68
|
+
const { obj, propertyKey } = route.finalRoute;
|
|
39
69
|
|
|
40
|
-
|
|
41
|
-
const { obj, propertyKey } = route.finalRoute;
|
|
70
|
+
interceptors.push(...getInterceptors(obj, propertyKey));
|
|
42
71
|
|
|
43
|
-
|
|
72
|
+
if (typeof (obj as any)[propertyKey] === "function") {
|
|
73
|
+
const paramType = paramMeta.getParamMetadata(
|
|
74
|
+
obj,
|
|
75
|
+
propertyKey,
|
|
76
|
+
);
|
|
77
|
+
if (paramType.types.body) {
|
|
78
|
+
for (const h of handles) {
|
|
79
|
+
const handle = h(reqHeader, paramType.types.body.type);
|
|
80
|
+
if (handle) {
|
|
81
|
+
body = await handle(rawBody);
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
44
86
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
87
|
+
call = async (req: CallProp) => {
|
|
88
|
+
const rawData = await paramMeta.call(obj, propertyKey, req);
|
|
89
|
+
return ResponseWithMeta.isResponseWithMeta(rawData)
|
|
90
|
+
? rawData
|
|
91
|
+
: new ResponseWithMeta(rawData);
|
|
92
|
+
};
|
|
93
|
+
} else {
|
|
94
|
+
call = () => new ResponseWithMeta((obj as any)[propertyKey]);
|
|
53
95
|
}
|
|
54
96
|
}
|
|
55
|
-
}
|
|
56
97
|
|
|
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
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
98
|
+
const handle = interceptors.reduceRight<
|
|
99
|
+
(prop: CallProp) => ResponseWithMeta | Promise<ResponseWithMeta>
|
|
100
|
+
>((next, handle) => (req) => handle(req, next), call);
|
|
101
|
+
const {
|
|
102
|
+
data,
|
|
103
|
+
code = 200,
|
|
104
|
+
header = {},
|
|
105
|
+
} = await handle({
|
|
106
|
+
body,
|
|
107
|
+
header: reqHeader,
|
|
108
|
+
query: parse(urlObj.search.substring(1)),
|
|
109
|
+
url: urlObj,
|
|
110
|
+
method: method as Method,
|
|
111
|
+
route,
|
|
112
|
+
param: route.params,
|
|
113
|
+
context: { ip },
|
|
114
|
+
});
|
|
115
|
+
let res: ReadableStream | null;
|
|
116
|
+
if (data instanceof ReadableStream) {
|
|
117
|
+
res = data;
|
|
118
|
+
} else if (data instanceof TransformStream) {
|
|
119
|
+
res = data.readable;
|
|
120
|
+
} else if (data instanceof Readable) {
|
|
121
|
+
res = Readable.toWeb(data) as ReadableStream;
|
|
122
|
+
} else if (data instanceof PassThrough) {
|
|
123
|
+
res = Readable.toWeb(data) as ReadableStream;
|
|
124
|
+
} else if (data === null || typeof data === "undefined") {
|
|
125
|
+
res = null;
|
|
126
|
+
} else if (header["content-type"]?.includes?.("text")) {
|
|
127
|
+
res = new ReadableStream({
|
|
128
|
+
start(ctrl) {
|
|
129
|
+
ctrl.enqueue(textEncoder.encode(data?.toString() || ""));
|
|
130
|
+
ctrl.close();
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
} else {
|
|
134
|
+
if (!header["content-type"]) {
|
|
135
|
+
header["content-type"] = "application/json";
|
|
136
|
+
}
|
|
137
|
+
res = new ReadableStream({
|
|
138
|
+
start(ctrl) {
|
|
139
|
+
ctrl.enqueue(textEncoder.encode(JSON.stringify(data)));
|
|
140
|
+
ctrl.close();
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
code,
|
|
146
|
+
header,
|
|
147
|
+
data: res,
|
|
148
|
+
};
|
|
149
|
+
},
|
|
150
|
+
);
|
|
151
|
+
} finally {
|
|
152
|
+
ctrl.abort();
|
|
102
153
|
}
|
|
103
|
-
return {
|
|
104
|
-
code,
|
|
105
|
-
header,
|
|
106
|
-
res,
|
|
107
|
-
};
|
|
108
154
|
},
|
|
109
155
|
// @ts-ignore
|
|
110
156
|
_rawObject: object,
|
|
111
157
|
};
|
|
158
|
+
setSfRequest(new ServerSideRequest(ins, context.innerCall));
|
|
112
159
|
return ins;
|
|
113
160
|
};
|
|
114
161
|
}
|
package/src/view/layout.ts
CHANGED
|
@@ -1,20 +1,22 @@
|
|
|
1
|
-
import { getMetadata, initMetadata } from "@thestarweb/star-framework-utils";
|
|
2
|
-
import { IRender } from "./render/render.js";
|
|
3
|
-
import { ViewInfo, ViewMeta } from "./types.js";
|
|
4
|
-
|
|
5
|
-
const mateKey = "$sf:app-web:view:layout";
|
|
6
|
-
|
|
7
|
-
export function Layout(
|
|
8
|
-
render: IRender,
|
|
9
|
-
meta?: ViewInfo<ViewMeta>[
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
1
|
+
import { getMetadata, initMetadata } from "@thestarweb/star-framework-utils";
|
|
2
|
+
import { IRender } from "./render/render.js";
|
|
3
|
+
import { DataHandle, ViewInfo, ViewMeta } from "./types.js";
|
|
4
|
+
|
|
5
|
+
const mateKey = "$sf:app-web:view:layout";
|
|
6
|
+
|
|
7
|
+
export function Layout(
|
|
8
|
+
render: IRender,
|
|
9
|
+
meta?: ViewInfo<ViewMeta>['meta'],
|
|
10
|
+
dataHandle?:DataHandle
|
|
11
|
+
): ClassDecorator {
|
|
12
|
+
return function (target) {
|
|
13
|
+
initMetadata<ViewInfo<ViewMeta>[]>(target, mateKey, undefined, []).push({
|
|
14
|
+
render,
|
|
15
|
+
meta,
|
|
16
|
+
dataHandle,
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export function getLayouts(target: any) {
|
|
21
|
+
return getMetadata<ViewInfo<ViewMeta>[]>(target, mateKey, undefined, []);
|
|
22
|
+
}
|
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
import { RenderDataContext } from "../data-context/index.js";
|
|
2
|
-
import { renderVDomToString } from "../html/render-v-dom-to-string.js";
|
|
3
|
-
import { HTMLVNode } from "../html/types.js";
|
|
4
|
-
import { IRender, RenderContext } from "./render.js";
|
|
5
|
-
|
|
6
|
-
export class SFVDomRender implements IRender {
|
|
7
|
-
constructor(
|
|
8
|
-
private readonly domFn:
|
|
9
|
-
| HTMLVNode
|
|
10
|
-
| HTMLVNode[]
|
|
11
|
-
| (() => Promise<
|
|
12
|
-
HTMLVNode | HTMLVNode[] | { default: HTMLVNode | HTMLVNode[] }
|
|
13
|
-
>),
|
|
14
|
-
) {}
|
|
15
|
-
private dom: HTMLVNode | HTMLVNode[] = [];
|
|
16
|
-
async fetchResource() {
|
|
17
|
-
if (typeof this.domFn === "function") {
|
|
18
|
-
const res = await this.domFn();
|
|
19
|
-
this.dom =
|
|
20
|
-
typeof res === "object" && "default" in res ? res.default : res;
|
|
21
|
-
} else {
|
|
22
|
-
this.dom = this.domFn;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
renderToString(context: RenderDataContext, prop: any, child: RenderContext) {
|
|
26
|
-
return renderVDomToString(this.dom, {
|
|
27
|
-
customizeChildrenRender: (el) => {
|
|
28
|
-
if (el.tagName === "
|
|
29
|
-
return () => child.warpRenderToString(context.user);
|
|
30
|
-
}
|
|
31
|
-
},
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
}
|
|
1
|
+
import { RenderDataContext } from "../data-context/index.js";
|
|
2
|
+
import { renderVDomToString } from "../html/render-v-dom-to-string.js";
|
|
3
|
+
import { HTMLVNode } from "../html/types.js";
|
|
4
|
+
import { IRender, RenderContext } from "./render.js";
|
|
5
|
+
|
|
6
|
+
export class SFVDomRender implements IRender {
|
|
7
|
+
constructor(
|
|
8
|
+
private readonly domFn:
|
|
9
|
+
| HTMLVNode
|
|
10
|
+
| HTMLVNode[]
|
|
11
|
+
| (() => Promise<
|
|
12
|
+
HTMLVNode | HTMLVNode[] | { default: HTMLVNode | HTMLVNode[] }
|
|
13
|
+
>),
|
|
14
|
+
) {}
|
|
15
|
+
private dom: HTMLVNode | HTMLVNode[] = [];
|
|
16
|
+
async fetchResource() {
|
|
17
|
+
if (typeof this.domFn === "function") {
|
|
18
|
+
const res = await this.domFn();
|
|
19
|
+
this.dom =
|
|
20
|
+
typeof res === "object" && "default" in res ? res.default : res;
|
|
21
|
+
} else {
|
|
22
|
+
this.dom = this.domFn;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
renderToString(context: RenderDataContext, prop: any, child: RenderContext) {
|
|
26
|
+
return renderVDomToString(this.dom, {
|
|
27
|
+
customizeChildrenRender: (el) => {
|
|
28
|
+
if (el.tagName === "sf-route") {
|
|
29
|
+
return () => child.warpRenderToString(context.user);
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
}
|
package/src/view/types.ts
CHANGED
|
@@ -1,18 +1,24 @@
|
|
|
1
|
-
import { IRender } from "./render/render.js";
|
|
2
|
-
|
|
3
|
-
export interface ViewMeta {
|
|
4
|
-
title: string;
|
|
5
|
-
/**
|
|
6
|
-
* html的meta标签,通常用于seo,当前仅再ssr时生效,浏览器页面切换时不修改
|
|
7
|
-
*/
|
|
8
|
-
htmlMeta?: Record<string, string>;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface ViewInfo<T = any> {
|
|
12
|
-
render: IRender;
|
|
13
|
-
meta?: ViewMeta | ((data: T) => ViewMeta);
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
1
|
+
import { IRender } from "./render/render.js";
|
|
2
|
+
|
|
3
|
+
export interface ViewMeta {
|
|
4
|
+
title: string;
|
|
5
|
+
/**
|
|
6
|
+
* html的meta标签,通常用于seo,当前仅再ssr时生效,浏览器页面切换时不修改
|
|
7
|
+
*/
|
|
8
|
+
htmlMeta?: Record<string, string>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface ViewInfo<T = any> {
|
|
12
|
+
render: IRender;
|
|
13
|
+
meta?: ViewMeta | ((data: T, viewProp: any) => ViewMeta);
|
|
14
|
+
dataHandle?: DataHandle;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface RenderConfig {
|
|
18
|
+
baseUrl: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface DataHandle {
|
|
22
|
+
handleSelfData?: (data: any) => any;
|
|
23
|
+
handleParentData?: (data: any) => any;
|
|
24
|
+
}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import { setRequestAbortSignal } from "../../../abort-signal/browser.js";
|
|
2
|
+
import { BrowserSideRequest } from "../../../request/browser.js";
|
|
3
|
+
import { setSfRequest } from "../../../request/index.js";
|
|
4
|
+
import { ResponseWithMeta } from "../../../return-types/response-with-meta.js";
|
|
1
5
|
import { parseRoute } from "../../../route/index.js";
|
|
2
|
-
import {
|
|
3
|
-
import { RenderContext } from "../../render/index.js";
|
|
4
|
-
import { RenderConfig, ViewMeta } from "../../types.js";
|
|
6
|
+
import { ViewMeta } from "../../types.js";
|
|
5
7
|
import { findRouteDom } from "../find-route-dom.js";
|
|
6
8
|
import { LoadingProgress } from "../loading-progress.js";
|
|
7
9
|
import {
|
|
@@ -28,9 +30,16 @@ function handleMeta(meta: ViewMeta) {
|
|
|
28
30
|
export const clientRender = async (routes: ReturnType<typeof parseRoute>) => {
|
|
29
31
|
const { config, pageData } = (window as any)[ssrToBrowserDataKey];
|
|
30
32
|
delete (window as any)[ssrToBrowserDataKey];
|
|
33
|
+
setSfRequest(new BrowserSideRequest(config.baseUrl));
|
|
31
34
|
const route = routes("GET", location.pathname);
|
|
35
|
+
let abortCtrl = new AbortController();
|
|
36
|
+
setRequestAbortSignal(abortCtrl.signal);
|
|
32
37
|
const getData = async () => {
|
|
33
|
-
const res = await fetch(location.href, {
|
|
38
|
+
const res = await fetch(location.href, {
|
|
39
|
+
signal: abortCtrl.signal,
|
|
40
|
+
redirect: "manual",
|
|
41
|
+
headers: { "x-requested-with": "sf-page-render" },
|
|
42
|
+
});
|
|
34
43
|
if (res.type === "opaqueredirect") {
|
|
35
44
|
console.log(
|
|
36
45
|
"this page is redirect, however, we can not get the redirect url, so we can only reload the page for redirect from server",
|
|
@@ -41,13 +50,21 @@ export const clientRender = async (routes: ReturnType<typeof parseRoute>) => {
|
|
|
41
50
|
return new Promise(() => {});
|
|
42
51
|
}
|
|
43
52
|
if (res.headers.get("content-type")?.includes("json")) {
|
|
44
|
-
|
|
53
|
+
const data = await res.json();
|
|
54
|
+
return new ResponseWithMeta(data.data, data);
|
|
45
55
|
}
|
|
46
56
|
return undefined;
|
|
47
57
|
};
|
|
48
|
-
const context = await createRootRenderContext(
|
|
58
|
+
const context = await createRootRenderContext(
|
|
59
|
+
config,
|
|
60
|
+
route,
|
|
61
|
+
() => new ResponseWithMeta(pageData.data, pageData),
|
|
62
|
+
);
|
|
49
63
|
handleMeta(context.meta);
|
|
50
64
|
listenerRoute(async () => {
|
|
65
|
+
abortCtrl.abort();
|
|
66
|
+
abortCtrl = new AbortController();
|
|
67
|
+
setRequestAbortSignal(abortCtrl.signal);
|
|
51
68
|
const route = routes("GET", location.pathname);
|
|
52
69
|
LoadingProgress.instance.start();
|
|
53
70
|
const c = await updateRootRenderContext(context, route, getData);
|