@thestarweb/star-framework-app-web 1.1.4 → 1.2.0

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.
Files changed (68) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/dist/abort-signal/browser.js +10 -0
  3. package/dist/abort-signal/browser.js.map +1 -0
  4. package/dist/abort-signal/index.js +25 -0
  5. package/dist/abort-signal/index.js.map +1 -0
  6. package/dist/abort-signal/server.js +15 -0
  7. package/dist/abort-signal/server.js.map +1 -0
  8. package/dist/browser/index.js +3 -0
  9. package/dist/browser/index.js.map +1 -1
  10. package/dist/cli/dev-server.js +42 -15
  11. package/dist/cli/dev-server.js.map +1 -1
  12. package/dist/cli/index.js +3 -11
  13. package/dist/cli/index.js.map +1 -1
  14. package/dist/cli/package-info.js +13 -0
  15. package/dist/cli/package-info.js.map +1 -0
  16. package/dist/index.js +2 -2
  17. package/dist/index.js.map +1 -1
  18. package/dist/params/index.js +3 -0
  19. package/dist/params/index.js.map +1 -1
  20. package/dist/request/browser.js +28 -0
  21. package/dist/request/browser.js.map +1 -0
  22. package/dist/request/index.js +13 -44
  23. package/dist/request/index.js.map +1 -1
  24. package/dist/request/server.js +55 -0
  25. package/dist/request/server.js.map +1 -0
  26. package/dist/request/types.js +53 -0
  27. package/dist/request/types.js.map +1 -0
  28. package/dist/server/dump-api-type.js +53 -8
  29. package/dist/server/dump-api-type.js.map +1 -1
  30. package/dist/server/index.js +96 -69
  31. package/dist/server/index.js.map +1 -1
  32. package/dist/view/layout.js +4 -3
  33. package/dist/view/layout.js.map +1 -1
  34. package/dist/view/render/sf-v-dom-render.js +1 -1
  35. package/dist/view/types.js +3 -1
  36. package/dist/view/utils/get-render/client.js +18 -3
  37. package/dist/view/utils/get-render/client.js.map +1 -1
  38. package/dist/view/utils/get-render/common.js +18 -11
  39. package/dist/view/utils/get-render/common.js.map +1 -1
  40. package/dist/view/utils/get-render/server.js +12 -3
  41. package/dist/view/utils/get-render/server.js.map +1 -1
  42. package/dist/view/view.js +4 -4
  43. package/dist/view/view.js.map +1 -1
  44. package/package.json +4 -4
  45. package/src/abort-signal/browser.ts +7 -0
  46. package/src/abort-signal/index.ts +16 -0
  47. package/src/abort-signal/server.ts +15 -0
  48. package/src/browser/index.ts +10 -7
  49. package/src/cli/dev-server.ts +71 -17
  50. package/src/cli/index.ts +5 -12
  51. package/src/cli/package-info.ts +12 -0
  52. package/src/cli/types.ts +8 -7
  53. package/src/index.ts +2 -2
  54. package/src/params/index.ts +17 -14
  55. package/src/request/browser.ts +34 -0
  56. package/src/request/index.ts +15 -65
  57. package/src/request/server.ts +58 -0
  58. package/src/request/types.ts +130 -0
  59. package/src/server/dump-api-type.ts +119 -64
  60. package/src/server/index.ts +124 -77
  61. package/src/view/layout.ts +22 -20
  62. package/src/view/render/sf-v-dom-render.ts +34 -34
  63. package/src/view/types.ts +24 -18
  64. package/src/view/utils/get-render/client.ts +23 -6
  65. package/src/view/utils/get-render/common.ts +127 -113
  66. package/src/view/utils/get-render/server.ts +13 -4
  67. package/src/view/view.ts +12 -7
  68. 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 { dumpTSType } from "@thestarweb/star-framework-param-helper";
7
-
8
- export async function dumpRouteType(file: string, obj: object) {
9
- const routes = getFlatRoute(obj);
10
- const config = getConfig()!;
11
- const data = [];
12
- let rPath = relative(dirname(file), config.entry).replaceAll("\\", "/");
13
- if (!rPath.startsWith(".")) {
14
- rPath = `./${rPath}`;
15
- }
16
- data.push(`import App from ${JSON.stringify(rPath)};`);
17
- data.push(`declare module '@thestarweb/star-framework-app-web'{`);
18
- data.push(`\tinterface ProjectApi{`);
19
- Object.keys(routes).forEach((type) => {
20
- if (type === "__SF:NotFind") return;
21
- data.push(`\t\t${type}:{`);
22
- routes[type as keyof typeof routes]!.forEach((meta, path) => {
23
- if (!meta.finalRoute) return;
24
- data.push(`\t\t\t${JSON.stringify(path)}:{`);
25
- const { obj, propertyKey } = meta.finalRoute;
26
- if (typeof (obj as any)[propertyKey] === "function") {
27
- data.push(`\t\t\t\tprop:{`);
28
- const param = paramMeta.getParamMetadata(
29
- meta.finalRoute.obj,
30
- meta.finalRoute.propertyKey,
31
- );
32
- if (param.types.body) {
33
- data.push(
34
- `\t\t\t\t\tbody${param.types.body.required ? "" : "?"}:${dumpTSType(param.types.body.type)},`,
35
- );
36
- }
37
- if (param.types.query) {
38
- data.push(
39
- `\t\t\t\t\tquery${param.types.query.required ? "" : "?"}:${dumpTSType(param.types.query.type)},`,
40
- );
41
- }
42
- if (param.types.header) {
43
- data.push(
44
- `\t\t\t\t\theader${param.types.header.required ? "" : "?"}:${dumpTSType(param.types.header.type)},`,
45
- );
46
- }
47
- data.push(`\t\t\t\t},`);
48
- data.push(
49
- `\t\t\t\treturn:ReturnType<App${meta.allRoute.map((r) => `[${JSON.stringify(r.propertyKey)}]`).join("")}>`,
50
- );
51
- } else {
52
- data.push(`\t\t\t\tprop:{},`);
53
- data.push(
54
- `\t\t\t\treturn:App${meta.allRoute.map((r) => `[${JSON.stringify(r.propertyKey)}]`).join("")}`,
55
- );
56
- }
57
- data.push(`\t\t\t},`);
58
- });
59
- data.push(`\t\t}`);
60
- });
61
- data.push(`\t}`);
62
- data.push(`}`);
63
- await writeFile(file, data.join("\n"));
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
+ }
@@ -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-node";
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(method, url, reqHeader, rawBody, context) {
24
- const urlObj =
25
- typeof url === "string" ? new URL(url, "http://127.0.0.1/") : url;
26
- const route = routes(method as Method, urlObj.pathname);
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
- const interceptors: Interceptor[] = route.allRoute.flatMap((info) =>
29
- getInterceptors(info.obj),
30
- );
59
+ interceptors.unshift(serverRender);
31
60
 
32
- interceptors.unshift(serverRender);
61
+ let body: any = null;
62
+ let call: (
63
+ prop: CallProp,
64
+ ) => ResponseWithMeta | Promise<ResponseWithMeta> = () =>
65
+ new ResponseWithMeta(null, { code: 404 });
33
66
 
34
- let body: any = null;
35
- let call: (
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
- if (route.finalRoute) {
41
- const { obj, propertyKey } = route.finalRoute;
70
+ interceptors.push(...getInterceptors(obj, propertyKey));
42
71
 
43
- interceptors.push(...getInterceptors(obj, propertyKey));
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
- if (typeof (obj as any)[propertyKey] === "function") {
46
- const paramType = paramMeta.getParamMetadata(obj, propertyKey);
47
- if (paramType.types.body) {
48
- for (const h of handles) {
49
- const handle = h(reqHeader, paramType.types.body.type);
50
- if (handle) {
51
- body = await handle(rawBody);
52
- break;
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
- call = async (req: CallProp) => {
58
- const rawData = await paramMeta.call(obj, propertyKey, req);
59
- return ResponseWithMeta.isResponseWithMeta(rawData)
60
- ? rawData
61
- : new ResponseWithMeta(rawData);
62
- };
63
- } else {
64
- call = () => new ResponseWithMeta((obj as any)[propertyKey]);
65
- }
66
- }
67
-
68
- const handle = interceptors.reduceRight<
69
- (prop: CallProp) => ResponseWithMeta | Promise<ResponseWithMeta>
70
- >((next, handle) => (req) => handle(req, next), call);
71
- const {
72
- data,
73
- code = 200,
74
- header = {},
75
- } = await handle({
76
- body,
77
- header: reqHeader,
78
- query: parse(urlObj.search),
79
- url: urlObj,
80
- method: method as Method,
81
- route,
82
- param: route.params,
83
- });
84
- let res: ReadableStream | Readable | null;
85
- if (data instanceof ReadableStream) {
86
- res = data;
87
- } else if (data instanceof TransformStream) {
88
- res = data.readable;
89
- } else if (data instanceof Readable) {
90
- res = data;
91
- } else if (data instanceof PassThrough) {
92
- res = new PassThroughReadable(data);
93
- } else if (data === null || typeof data === "undefined") {
94
- res = null;
95
- } else if (header["content-type"]?.includes?.("text")) {
96
- res = Readable.from(data?.toString() || "");
97
- } else {
98
- if (!header["content-type"]) {
99
- header["content-type"] = "application/json";
100
- }
101
- res = Readable.from(JSON.stringify(data));
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
  }
@@ -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>["meta"],
10
- ): ClassDecorator {
11
- return function (target) {
12
- initMetadata<ViewInfo<ViewMeta>[]>(target, mateKey, undefined, []).push({
13
- render,
14
- meta,
15
- });
16
- };
17
- }
18
- export function getLayouts(target: any) {
19
- return getMetadata<ViewInfo<ViewMeta>[]>(target, mateKey, undefined, []);
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 === "pageData") {
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
- export interface RenderConfig {
17
- baseUrl: string;
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 { redirectStatusCode } from "../../const.js";
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, { redirect: "manual" });
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
- return await res.json();
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(config, route, () => pageData);
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);