@thestarweb/star-framework-app-web 1.1.3 → 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.
- package/CHANGELOG.md +36 -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 +14 -12
- 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 +3 -3
- 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/return-types/index.js +1 -0
- package/dist/return-types/index.js.map +1 -1
- package/dist/return-types/location-to.js +9 -0
- package/dist/return-types/location-to.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/const.js +1 -0
- package/dist/view/const.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 +28 -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 +16 -3
- package/dist/view/utils/get-render/server.js.map +1 -1
- package/dist/view/utils/loading-progress.js +61 -0
- package/dist/view/utils/loading-progress.js.map +1 -0
- 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 +5 -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
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {
|
|
2
|
+
RequestProp,
|
|
3
|
+
ResponseData,
|
|
4
|
+
ServerInstance,
|
|
5
|
+
} from "@thestarweb/star-framework-web-runtime";
|
|
6
|
+
import { Method } from "../route/types.js";
|
|
7
|
+
import { Prop, SFWebRequest } from "./types.js";
|
|
8
|
+
|
|
9
|
+
const textEncoder = new TextEncoder();
|
|
10
|
+
|
|
11
|
+
export class ServerSideRequest extends SFWebRequest {
|
|
12
|
+
constructor(
|
|
13
|
+
private serverIns: ServerInstance,
|
|
14
|
+
private innerCall: (prop: RequestProp) => Promise<ResponseData>,
|
|
15
|
+
) {
|
|
16
|
+
super();
|
|
17
|
+
}
|
|
18
|
+
protected async handleInnerCall(
|
|
19
|
+
app: string,
|
|
20
|
+
method: Method,
|
|
21
|
+
url: URL,
|
|
22
|
+
signal: AbortSignal | undefined,
|
|
23
|
+
prop: Prop,
|
|
24
|
+
) {
|
|
25
|
+
const res = await (
|
|
26
|
+
app === "self" ? this.serverIns.onRequest : this.innerCall
|
|
27
|
+
)({
|
|
28
|
+
method,
|
|
29
|
+
url,
|
|
30
|
+
header: prop.header || {},
|
|
31
|
+
context: {},
|
|
32
|
+
appName: app,
|
|
33
|
+
body: new ReadableStream({
|
|
34
|
+
start(ctrl) {
|
|
35
|
+
if (prop.body) {
|
|
36
|
+
ctrl.enqueue(textEncoder.encode(prop.body));
|
|
37
|
+
}
|
|
38
|
+
ctrl.close();
|
|
39
|
+
},
|
|
40
|
+
}),
|
|
41
|
+
signal: signal || new AbortController().signal,
|
|
42
|
+
ip: '127.0.0.1',
|
|
43
|
+
port: 0,
|
|
44
|
+
});
|
|
45
|
+
const reader = res.data?.getReader();
|
|
46
|
+
return new Response(
|
|
47
|
+
new ReadableStream({
|
|
48
|
+
async pull(ctrl) {
|
|
49
|
+
if (!reader) ctrl.close();
|
|
50
|
+
const res = await reader!.read();
|
|
51
|
+
if (res.value) ctrl.enqueue(res.value);
|
|
52
|
+
if (res.done) ctrl.close();
|
|
53
|
+
},
|
|
54
|
+
}),
|
|
55
|
+
{ status: res.code, headers: res.header },
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AllApi,
|
|
3
|
+
ProjectApi,
|
|
4
|
+
ResponseWithMeta,
|
|
5
|
+
} from "@thestarweb/star-framework-app-web";
|
|
6
|
+
import { Method } from "../route/types.js";
|
|
7
|
+
import { stringify } from "qs";
|
|
8
|
+
import { StringKeyOf } from "@thestarweb/ts-helper";
|
|
9
|
+
import {
|
|
10
|
+
getRequestAbortSignal,
|
|
11
|
+
mergeAbortSignal,
|
|
12
|
+
} from "../abort-signal/index.js";
|
|
13
|
+
|
|
14
|
+
export interface UserConfig {
|
|
15
|
+
signal?: AbortSignal;
|
|
16
|
+
/**
|
|
17
|
+
* 默认请求在离开页面(对于前端)或接口响应结束(对于后端)时会取消请求,如果需要保持请求则需要将其设置为true
|
|
18
|
+
*/
|
|
19
|
+
keepRequest?: boolean;
|
|
20
|
+
}
|
|
21
|
+
export interface UserProp extends UserConfig {
|
|
22
|
+
body?: string;
|
|
23
|
+
query?: any;
|
|
24
|
+
header?: Record<string, string>;
|
|
25
|
+
param?: any;
|
|
26
|
+
}
|
|
27
|
+
export interface Prop {
|
|
28
|
+
body?: any;
|
|
29
|
+
query?: any;
|
|
30
|
+
header?: Record<string, string>;
|
|
31
|
+
param?: any;
|
|
32
|
+
}
|
|
33
|
+
interface ApiDefine {
|
|
34
|
+
prop: Prop;
|
|
35
|
+
return: any;
|
|
36
|
+
}
|
|
37
|
+
interface ApisDefine {
|
|
38
|
+
[k: string]: ApiDefine;
|
|
39
|
+
}
|
|
40
|
+
type APIType = Partial<Record<Method, ApisDefine>>;
|
|
41
|
+
|
|
42
|
+
declare module "@thestarweb/star-framework-app-web" {
|
|
43
|
+
interface ProjectApi extends APIType {}
|
|
44
|
+
interface AllApi {
|
|
45
|
+
self: ProjectApi;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
type AllUrl<Root extends Record<string, APIType>, M extends Method> = {
|
|
49
|
+
[K1 in keyof Root & string]: {
|
|
50
|
+
[K3 in keyof Root[K1][M] & string]: `${K1}:${K3}`;
|
|
51
|
+
}[keyof Root[K1][M] & string];
|
|
52
|
+
}[keyof Root & string];
|
|
53
|
+
type UrlToProp<
|
|
54
|
+
Root extends Record<string, APIType>,
|
|
55
|
+
M extends Method,
|
|
56
|
+
P extends string,
|
|
57
|
+
> = P extends `${infer K1}:${infer K3}`
|
|
58
|
+
? K1 extends keyof Root
|
|
59
|
+
? Root[K1] extends Record<M, infer C>
|
|
60
|
+
? K3 extends keyof C
|
|
61
|
+
? C[K3]
|
|
62
|
+
: never
|
|
63
|
+
: never
|
|
64
|
+
: never
|
|
65
|
+
: never;
|
|
66
|
+
export abstract class SFWebRequest<
|
|
67
|
+
All extends Record<keyof All, Partial<APIType>> = AllApi,
|
|
68
|
+
> {
|
|
69
|
+
constructor() {}
|
|
70
|
+
protected abstract handleInnerCall(
|
|
71
|
+
app: string,
|
|
72
|
+
method: Method,
|
|
73
|
+
url: URL,
|
|
74
|
+
signal: AbortSignal | undefined,
|
|
75
|
+
prop: Prop,
|
|
76
|
+
): Promise<Response>;
|
|
77
|
+
private async _request(
|
|
78
|
+
method: Method,
|
|
79
|
+
path: string,
|
|
80
|
+
prop: UserProp,
|
|
81
|
+
): Promise<any> {
|
|
82
|
+
const url = new URL(path);
|
|
83
|
+
if (!prop.header) prop.header = {};
|
|
84
|
+
if (!prop.header["content-type"])
|
|
85
|
+
prop.header["content-type"] = "application/json";
|
|
86
|
+
const body = JSON.stringify(prop.body);
|
|
87
|
+
let signal = prop.keepRequest ? undefined : getRequestAbortSignal();
|
|
88
|
+
if (prop.signal)
|
|
89
|
+
signal = signal ? mergeAbortSignal(signal, prop.signal) : prop.signal;
|
|
90
|
+
const data = await (url.protocol === "https" || url.protocol === "http"
|
|
91
|
+
? fetch(url, {
|
|
92
|
+
...prop,
|
|
93
|
+
body,
|
|
94
|
+
})
|
|
95
|
+
: this.handleInnerCall(
|
|
96
|
+
url.protocol.substring(0, url.protocol.length - 1),
|
|
97
|
+
method,
|
|
98
|
+
url,
|
|
99
|
+
signal,
|
|
100
|
+
{ ...prop, body },
|
|
101
|
+
));
|
|
102
|
+
if (!data.ok) throw new Error();
|
|
103
|
+
return data.json();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async get<P extends AllUrl<All, "GET">>(
|
|
107
|
+
path: P,
|
|
108
|
+
prop: UrlToProp<All, "GET", P>["prop"] & UserConfig,
|
|
109
|
+
): Promise<UrlToProp<All, "GET", P>["return"]> {
|
|
110
|
+
return await this._request("GET", path, prop);
|
|
111
|
+
}
|
|
112
|
+
async post<P extends AllUrl<All, "POST">>(
|
|
113
|
+
path: P,
|
|
114
|
+
prop: UrlToProp<All, "POST", P>["prop"] & UserConfig,
|
|
115
|
+
): Promise<UrlToProp<All, "POST", P>["return"]> {
|
|
116
|
+
return await this._request("POST", path as string, prop);
|
|
117
|
+
}
|
|
118
|
+
async put<P extends AllUrl<All, "PUT">>(
|
|
119
|
+
path: P,
|
|
120
|
+
prop: UrlToProp<All, "PUT", P>["prop"] & UserConfig,
|
|
121
|
+
): Promise<UrlToProp<All, "PUT", P>["return"]> {
|
|
122
|
+
return await this._request("PUT", path as string, prop);
|
|
123
|
+
}
|
|
124
|
+
async delete<P extends AllUrl<All, "DELETE">>(
|
|
125
|
+
path: P,
|
|
126
|
+
prop: UrlToProp<All, "DELETE", P>["prop"] & UserConfig,
|
|
127
|
+
): Promise<UrlToProp<All, "DELETE", P>["return"]> {
|
|
128
|
+
return await this._request("PUT", path as string, prop);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
@@ -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
|
+
}
|