@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
package/dist/view/view.js
CHANGED
|
@@ -2,9 +2,9 @@ import { Get } from "../route/index.js";
|
|
|
2
2
|
/*@ts-ignore*/
|
|
3
3
|
import { __ΩIRender } from "./render/render.js";
|
|
4
4
|
/*@ts-ignore*/
|
|
5
|
-
import { __ΩViewInfo } from "./types.js";
|
|
6
|
-
export function View(path, render, meta) {
|
|
7
|
-
return Get(path, { view: { render, meta } });
|
|
5
|
+
import { __ΩViewInfo, __ΩDataHandle } from "./types.js";
|
|
6
|
+
export function View(path, render, meta, dataHandle) {
|
|
7
|
+
return Get(path, { view: { render, meta, dataHandle } });
|
|
8
8
|
}
|
|
9
|
-
View.__type = ['path', () => __ΩIRender, 'render', () => __ΩViewInfo, "meta", 'meta', 'View', 'P&2!n"2#n$.%f2&8"
|
|
9
|
+
View.__type = ['path', () => __ΩIRender, 'render', () => __ΩViewInfo, "meta", 'meta', () => __ΩDataHandle, 'dataHandle', 'View', 'P&2!n"2#n$.%f2&8n\'2(8"/)'];
|
|
10
10
|
//# sourceMappingURL=view.js.map
|
package/dist/view/view.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"view.js","sourceRoot":"","sources":["../../src/view/view.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;;2BAChB,oBAAoB;;
|
|
1
|
+
{"version":3,"file":"view.js","sourceRoot":"","sources":["../../src/view/view.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;;2BAChB,oBAAoB;;2CACP,YAAY;AAEjD,MAAM,UAAU,IAAI,CACnB,IAAY,EACZ,MAAe,EACf,IAAuB,EACvB,UAAuB;IAEvB,OAAO,GAAG,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,EAAE,EAAE,CAAC,CAAC;AAC1D,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thestarweb/star-framework-app-web",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "Star Framework Web App",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "src/index.ts",
|
|
@@ -16,11 +16,11 @@
|
|
|
16
16
|
"dependencies": {
|
|
17
17
|
"@deepkit/type": "^1.0.19",
|
|
18
18
|
"@deepkit/type-compiler": "^1.0.0",
|
|
19
|
-
"@thestarweb/star-framework-cli": "^1.1.
|
|
19
|
+
"@thestarweb/star-framework-cli": "^1.1.4",
|
|
20
20
|
"@thestarweb/star-framework-param-helper": "^1.0.1",
|
|
21
21
|
"@thestarweb/star-framework-route": "^1.0.3",
|
|
22
22
|
"@thestarweb/star-framework-utils": "^1.0.1",
|
|
23
|
-
"@thestarweb/star-framework-web-runtime
|
|
23
|
+
"@thestarweb/star-framework-web-runtime": "^1.1.0",
|
|
24
24
|
"node-mocks-http": "^1.18.1",
|
|
25
25
|
"qs": "6.15.3",
|
|
26
26
|
"tslib": "^2.8.1",
|
|
@@ -44,5 +44,5 @@
|
|
|
44
44
|
"README.md"
|
|
45
45
|
],
|
|
46
46
|
"license": "ISC",
|
|
47
|
-
"gitHead": "
|
|
47
|
+
"gitHead": "3ad6494fe473a94115ad8e02a4fc621969b9bf5c"
|
|
48
48
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
let relGetAbortSignal = () => new AbortController().signal;
|
|
2
|
+
export function setRequestAbortSignalFn(fn: typeof relGetAbortSignal) {
|
|
3
|
+
relGetAbortSignal = fn;
|
|
4
|
+
}
|
|
5
|
+
export function getRequestAbortSignal() {
|
|
6
|
+
return relGetAbortSignal();
|
|
7
|
+
}
|
|
8
|
+
export function mergeAbortSignal(...signals: AbortSignal[]) {
|
|
9
|
+
const ctrl = new AbortController();
|
|
10
|
+
function handle(ev: Event) {
|
|
11
|
+
ctrl.abort(ev);
|
|
12
|
+
signals.forEach((i) => i.removeEventListener("abort", handle));
|
|
13
|
+
}
|
|
14
|
+
signals.forEach((i) => i.addEventListener("abort", handle));
|
|
15
|
+
return ctrl.signal;
|
|
16
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
2
|
+
|
|
3
|
+
const reqStorage = new AsyncLocalStorage<AbortSignal>({
|
|
4
|
+
defaultValue: new AbortController().signal,
|
|
5
|
+
});
|
|
6
|
+
export function runWithSignal<T extends (...arg: any) => any>(
|
|
7
|
+
signal: AbortSignal,
|
|
8
|
+
fn: T,
|
|
9
|
+
...arg: Parameters<T>
|
|
10
|
+
): ReturnType<T> {
|
|
11
|
+
return reqStorage.run(signal, fn, ...arg);
|
|
12
|
+
}
|
|
13
|
+
export function getRequestAbortSignal() {
|
|
14
|
+
return reqStorage.getStore()!;
|
|
15
|
+
}
|
package/src/browser/index.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { getRequestAbortSignal } from "../abort-signal/browser.js";
|
|
2
|
+
import { setRequestAbortSignalFn } from "../abort-signal/index.js";
|
|
3
|
+
import { parseRoute } from "../route/index.js";
|
|
4
|
+
import { clientRender } from "../view/index.js";
|
|
5
|
+
|
|
6
|
+
export function createBrowserInstance(object: any) {
|
|
7
|
+
setRequestAbortSignalFn(getRequestAbortSignal);
|
|
8
|
+
const routes = parseRoute(object);
|
|
9
|
+
clientRender(routes);
|
|
10
|
+
}
|
package/src/cli/dev-server.ts
CHANGED
|
@@ -8,32 +8,88 @@ import { middlewareToInterceptor } from "../interceptor/index.js";
|
|
|
8
8
|
import type { ViteDevServer } from "vite";
|
|
9
9
|
import {
|
|
10
10
|
createHttpServer,
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
RequestProp,
|
|
12
|
+
ResolvedServerInstance,
|
|
13
|
+
ResponseData,
|
|
14
|
+
ServerConfig,
|
|
15
|
+
ServerInstance,
|
|
16
|
+
ServerRuntimeContext,
|
|
17
|
+
} from "@thestarweb/star-framework-web-runtime";
|
|
13
18
|
import { dumpRouteType } from "../server/dump-api-type.js";
|
|
14
|
-
import { join } from "path";
|
|
19
|
+
import { isAbsolute, join } from "path";
|
|
20
|
+
import { handleRef } from "@thestarweb/star-framework-web-runtime";
|
|
15
21
|
|
|
16
|
-
export
|
|
22
|
+
export interface DevServerConfig extends Partial<ServerConfig> {
|
|
23
|
+
/**
|
|
24
|
+
* 输出API类型定义的默认位置
|
|
25
|
+
* 以便项目内能获得Request中具体API的类型提示
|
|
26
|
+
* @default ./route-type.ts
|
|
27
|
+
*/
|
|
28
|
+
apiTypeDefinePath?: false | string;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 输出带有命名空间的API类型定义文件位置,如果项目需要供其他项目使用,则需要设置,以便将类型定义分发给其他程序使用
|
|
32
|
+
*/
|
|
33
|
+
apiTypeNamespaceDefinePath?: false | string;
|
|
34
|
+
/**
|
|
35
|
+
* @default 3000
|
|
36
|
+
*/
|
|
37
|
+
port?: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function devServer(
|
|
41
|
+
html: string,
|
|
42
|
+
{
|
|
43
|
+
apiTypeDefinePath = "./route-type.ts",
|
|
44
|
+
apiTypeNamespaceDefinePath,
|
|
45
|
+
port = 3000,
|
|
46
|
+
instances = [],
|
|
47
|
+
...serverConfig
|
|
48
|
+
}: DevServerConfig,
|
|
49
|
+
): SFDevHook {
|
|
17
50
|
let server: Awaited<ReturnType<typeof createHttpServer>>;
|
|
18
51
|
let viteInterceptor: Interceptor;
|
|
19
52
|
let _vite: ViteDevServer;
|
|
20
|
-
|
|
53
|
+
let packageName = "";
|
|
54
|
+
const context: ServerRuntimeContext = {
|
|
21
55
|
root: process.cwd(),
|
|
22
56
|
static: "",
|
|
23
57
|
indexHtml: html,
|
|
58
|
+
innerCall: function (prop: RequestProp): Promise<ResponseData> {
|
|
59
|
+
return handleRef.value(prop);
|
|
60
|
+
},
|
|
24
61
|
};
|
|
25
|
-
|
|
62
|
+
let rawIns: ServerInstance | undefined;
|
|
63
|
+
const ins: ResolvedServerInstance = {
|
|
26
64
|
...context,
|
|
27
|
-
|
|
65
|
+
name: "default",
|
|
66
|
+
matchRequest: {},
|
|
67
|
+
onRequest: function (prop: RequestProp): Promise<ResponseData> {
|
|
68
|
+
if (rawIns) {
|
|
69
|
+
return rawIns.onRequest(prop);
|
|
70
|
+
}
|
|
71
|
+
throw new Error("Function not implemented.");
|
|
72
|
+
},
|
|
28
73
|
};
|
|
29
74
|
const devInterceptor: Interceptor = (req, next) => {
|
|
30
75
|
return viteInterceptor(req, next);
|
|
31
76
|
};
|
|
32
77
|
function handleIns(obj: any) {
|
|
33
78
|
addInterceptor(devInterceptor, obj._rawObject);
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
79
|
+
if (apiTypeDefinePath) {
|
|
80
|
+
const file = isAbsolute(apiTypeDefinePath)
|
|
81
|
+
? apiTypeDefinePath
|
|
82
|
+
: join(process.cwd(), apiTypeDefinePath);
|
|
83
|
+
_vite?.watcher.unwatch(file);
|
|
84
|
+
dumpRouteType(file, obj._rawObject);
|
|
85
|
+
}
|
|
86
|
+
if (apiTypeNamespaceDefinePath) {
|
|
87
|
+
const file = isAbsolute(apiTypeNamespaceDefinePath)
|
|
88
|
+
? apiTypeNamespaceDefinePath
|
|
89
|
+
: join(process.cwd(), apiTypeNamespaceDefinePath);
|
|
90
|
+
_vite?.watcher.unwatch(file);
|
|
91
|
+
dumpRouteType(file, obj._rawObject, packageName);
|
|
92
|
+
}
|
|
37
93
|
return obj;
|
|
38
94
|
}
|
|
39
95
|
return {
|
|
@@ -46,19 +102,17 @@ export function devServer(html: string): SFDevHook {
|
|
|
46
102
|
context.indexHtml = await vite.transformIndexHtml("index.html", html);
|
|
47
103
|
},
|
|
48
104
|
async start(main) {
|
|
49
|
-
|
|
105
|
+
const { info } = await import("./package-info.js");
|
|
106
|
+
packageName = info.name;
|
|
107
|
+
rawIns = handleIns(main(context));
|
|
50
108
|
server = await createHttpServer({
|
|
51
|
-
instances: [
|
|
52
|
-
{
|
|
53
|
-
package: ins,
|
|
54
|
-
},
|
|
55
|
-
],
|
|
109
|
+
instances: [...instances, ins],
|
|
56
110
|
port: 3000,
|
|
57
111
|
});
|
|
58
112
|
},
|
|
59
113
|
async hotReload(main) {
|
|
60
114
|
if (server) {
|
|
61
|
-
|
|
115
|
+
rawIns = handleIns(main(context));
|
|
62
116
|
console.log("hot updated");
|
|
63
117
|
}
|
|
64
118
|
},
|
package/src/cli/index.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { type SFPluging } from "@thestarweb/star-framework-cli";
|
|
2
|
-
import { devServer } from "./dev-server.js";
|
|
2
|
+
import { devServer, DevServerConfig } from "./dev-server.js";
|
|
3
3
|
import { dirname, join, relative } from "path";
|
|
4
4
|
import { writeFile } from "fs/promises";
|
|
5
5
|
import { readFileSync } from "fs";
|
|
6
|
-
import { PackageInfo } from "@thestarweb/star-framework-web-runtime
|
|
6
|
+
import { PackageInfo } from "@thestarweb/star-framework-web-runtime";
|
|
7
7
|
import { fileURLToPath } from "url";
|
|
8
8
|
import { parseHtmlToVDom, renderVDomToString } from "../view/html/index.js";
|
|
9
9
|
|
|
@@ -14,6 +14,7 @@ const __dir = dirname(__file);
|
|
|
14
14
|
export function SFWebPlugin({
|
|
15
15
|
distPath = "./dist/web/",
|
|
16
16
|
htmlPath = join(__dir, "../../resource/default-index.html"),
|
|
17
|
+
devServer: devServerConfig = {} as DevServerConfig,
|
|
17
18
|
} = {}): SFPluging {
|
|
18
19
|
const fullDistPath = join(process.cwd(), distPath);
|
|
19
20
|
const fullUnpackDistPath = join(fullDistPath, ".unpack");
|
|
@@ -42,7 +43,7 @@ export function SFWebPlugin({
|
|
|
42
43
|
mode: {
|
|
43
44
|
server: {
|
|
44
45
|
side: ["server"],
|
|
45
|
-
dev: devServer(html),
|
|
46
|
+
dev: devServer(html, devServerConfig),
|
|
46
47
|
environments: {
|
|
47
48
|
build: { outDir: join(fullUnpackDistPath, "server") },
|
|
48
49
|
},
|
|
@@ -85,6 +86,13 @@ export function SFWebPlugin({
|
|
|
85
86
|
removeMode: { prop: true, body: "throw" },
|
|
86
87
|
},
|
|
87
88
|
},
|
|
89
|
+
{
|
|
90
|
+
import: "@thestarweb/star-framework-app-web",
|
|
91
|
+
name: ["AddInterceptor"],
|
|
92
|
+
side: "server",
|
|
93
|
+
type: "include",
|
|
94
|
+
whenDecorator: { enable: true, removeSelfOny: true },
|
|
95
|
+
},
|
|
88
96
|
],
|
|
89
97
|
vitePlugin: {
|
|
90
98
|
name: "sf-web-html",
|
|
@@ -104,19 +112,11 @@ export function SFWebPlugin({
|
|
|
104
112
|
async onAllModeBuildEnd(modes, res) {
|
|
105
113
|
const configFile = join(fullUnpackDistPath, "package-info.json");
|
|
106
114
|
console.log("正在写入包信息");
|
|
107
|
-
let packageName = "sf-web",
|
|
108
|
-
packageVersion = "1.0.0";
|
|
109
|
-
try {
|
|
110
|
-
const pJson = JSON.parse(readFileSync("package.json", "utf-8"));
|
|
111
|
-
pJson.name ?? (packageName = pJson.name);
|
|
112
|
-
pJson.version ?? (packageVersion = pJson.version);
|
|
113
|
-
} catch {}
|
|
114
115
|
await writeFile(
|
|
115
116
|
configFile,
|
|
116
117
|
JSON.stringify({
|
|
117
118
|
"sf-formate": "1",
|
|
118
|
-
|
|
119
|
-
version: packageVersion,
|
|
119
|
+
...(await import("./package-info.js")).info,
|
|
120
120
|
buildAt: Date.now(),
|
|
121
121
|
server: {
|
|
122
122
|
dir: relative(fullUnpackDistPath, res.server.distPath),
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { readFile } from "fs/promises";
|
|
2
|
+
|
|
3
|
+
const info = {
|
|
4
|
+
name: "sf-web",
|
|
5
|
+
version: "1.0.0",
|
|
6
|
+
};
|
|
7
|
+
try {
|
|
8
|
+
const pJson = JSON.parse(await readFile("package.json", "utf-8"));
|
|
9
|
+
pJson.name && (info.name = pJson.name);
|
|
10
|
+
pJson.version && (info.version = pJson.version);
|
|
11
|
+
} catch {}
|
|
12
|
+
export { info };
|
package/src/cli/types.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
declare global {
|
|
2
|
-
interface StarFrameworkSideConfig {
|
|
3
|
-
server: {};
|
|
4
|
-
browser: {};
|
|
5
|
-
client: {};
|
|
6
|
-
}
|
|
7
|
-
}
|
|
1
|
+
declare global {
|
|
2
|
+
interface StarFrameworkSideConfig {
|
|
3
|
+
server: {};
|
|
4
|
+
browser: {};
|
|
5
|
+
client: {};
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export {}
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { Get, Post, Put, Delete, SubRoute } from "./route/index.js";
|
|
2
|
-
export { Body, Query, Header, Param } from "./params/index.js";
|
|
2
|
+
export { Body, Query, Header, Param, Ip, Context } from "./params/index.js";
|
|
3
3
|
// @side-only server
|
|
4
4
|
export { SFWebPlugin } from "./cli/index.js";
|
|
5
5
|
export { ResponseWithMeta, LocationTo } from "./return-types/index.js";
|
|
@@ -21,4 +21,4 @@ export { createServerFactory } from "./server/index.js";
|
|
|
21
21
|
export { createBrowserInstance } from "./browser/index.js";
|
|
22
22
|
// @side-only server
|
|
23
23
|
export * from "./cli-helper.js";
|
|
24
|
-
export { SFWebRequest } from "./request/index.js";
|
|
24
|
+
export { SFWebRequest, getSfRequest } from "./request/index.js";
|
package/src/params/index.ts
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ParamMetaHelper,
|
|
3
|
-
RootParamType,
|
|
4
|
-
} from "@thestarweb/star-framework-param-helper";
|
|
5
|
-
export const paramMeta = new ParamMetaHelper("http", {
|
|
6
|
-
body: RootParamType.Any,
|
|
7
|
-
header: RootParamType.SingleObject,
|
|
8
|
-
query: RootParamType.Any,
|
|
9
|
-
param: RootParamType.SingleObject,
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
export const
|
|
13
|
-
export const
|
|
14
|
-
export const
|
|
1
|
+
import {
|
|
2
|
+
ParamMetaHelper,
|
|
3
|
+
RootParamType,
|
|
4
|
+
} from "@thestarweb/star-framework-param-helper";
|
|
5
|
+
export const paramMeta = new ParamMetaHelper("http", {
|
|
6
|
+
body: RootParamType.Any,
|
|
7
|
+
header: RootParamType.SingleObject,
|
|
8
|
+
query: RootParamType.Any,
|
|
9
|
+
param: RootParamType.SingleObject,
|
|
10
|
+
context: RootParamType.Object,
|
|
11
|
+
});
|
|
12
|
+
export const Body = paramMeta.createDecorator("body");
|
|
13
|
+
export const Header = paramMeta.createDecorator("header");
|
|
14
|
+
export const Query = paramMeta.createDecorator("query");
|
|
15
|
+
export const Param = paramMeta.createDecorator("param");
|
|
16
|
+
export const Context = paramMeta.createDecorator("context");
|
|
17
|
+
export const Ip = () => Context("ip");
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Method } from "../route/types.js";
|
|
2
|
+
import { Prop, SFWebRequest } from "./types.js";
|
|
3
|
+
import { AllApi } from "@thestarweb/star-framework-app-web";
|
|
4
|
+
|
|
5
|
+
export class BrowserSideRequest extends SFWebRequest<Pick<AllApi, "self">> {
|
|
6
|
+
constructor(public readonly baseUrl: string = "/") {
|
|
7
|
+
super();
|
|
8
|
+
}
|
|
9
|
+
protected async handleInnerCall(
|
|
10
|
+
app: string,
|
|
11
|
+
method: Method,
|
|
12
|
+
url: URL,
|
|
13
|
+
signal: AbortSignal,
|
|
14
|
+
prop: Prop,
|
|
15
|
+
) {
|
|
16
|
+
if (app === "self") {
|
|
17
|
+
return fetch(
|
|
18
|
+
new URL(
|
|
19
|
+
url.pathname + url.search,
|
|
20
|
+
new URL(this.baseUrl, location.origin),
|
|
21
|
+
),
|
|
22
|
+
{
|
|
23
|
+
...prop,
|
|
24
|
+
headers: prop.header,
|
|
25
|
+
//@ts-ignore
|
|
26
|
+
duplex: "half",
|
|
27
|
+
method,
|
|
28
|
+
signal,
|
|
29
|
+
},
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
throw new Error("当前仅支持self访问本程序");
|
|
33
|
+
}
|
|
34
|
+
}
|
package/src/request/index.ts
CHANGED
|
@@ -1,65 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
constructor(public readonly baseUrl: string = "/") {}
|
|
17
|
-
private async _request(
|
|
18
|
-
method: Method,
|
|
19
|
-
path: string,
|
|
20
|
-
prop: Prop,
|
|
21
|
-
): Promise<any> {
|
|
22
|
-
let url = path.startsWith("self:")
|
|
23
|
-
? path.replace(/^self:\//, this.baseUrl)
|
|
24
|
-
: path;
|
|
25
|
-
if (prop.query) {
|
|
26
|
-
url += stringify(prop.query);
|
|
27
|
-
}
|
|
28
|
-
const res = await fetch(url, {
|
|
29
|
-
method,
|
|
30
|
-
body: JSON.stringify(prop.body),
|
|
31
|
-
headers: { "content-type": "application/json", ...prop.header },
|
|
32
|
-
});
|
|
33
|
-
if (!res.ok) {
|
|
34
|
-
throw new Error("request fail");
|
|
35
|
-
}
|
|
36
|
-
if (res.headers.get("content-type") === "application/json") {
|
|
37
|
-
return await res.json();
|
|
38
|
-
}
|
|
39
|
-
return res;
|
|
40
|
-
}
|
|
41
|
-
async get<P extends string & keyof API["GET"]>(
|
|
42
|
-
path: `self:${P}`,
|
|
43
|
-
prop: API["GET"][P]["prop"],
|
|
44
|
-
): Promise<API["GET"][P]["return"]> {
|
|
45
|
-
return await this._request("GET", path as string, prop);
|
|
46
|
-
}
|
|
47
|
-
async post<P extends keyof ProjectApi["POST"]>(
|
|
48
|
-
path: `self:${P}`,
|
|
49
|
-
prop: API["POST"][P]["prop"],
|
|
50
|
-
): Promise<API["POST"][P]["return"]> {
|
|
51
|
-
return await this._request("POST", path as string, prop);
|
|
52
|
-
}
|
|
53
|
-
async put<P extends keyof ProjectApi["PUT"]>(
|
|
54
|
-
path: `self:${P}`,
|
|
55
|
-
prop: API["PUT"][P]["prop"],
|
|
56
|
-
): Promise<API["PUT"][P]["return"]> {
|
|
57
|
-
return await this._request("PUT", path as string, prop);
|
|
58
|
-
}
|
|
59
|
-
async delete<P extends keyof ProjectApi["DELETE"]>(
|
|
60
|
-
path: `self:${P}`,
|
|
61
|
-
prop: API["DELETE"][P]["prop"],
|
|
62
|
-
): Promise<API["DELETE"][P]["return"]> {
|
|
63
|
-
return await this._request("PUT", path as string, prop);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
1
|
+
import { SFWebRequest } from "./types.js";
|
|
2
|
+
export { SFWebRequest } from "./types.js";
|
|
3
|
+
|
|
4
|
+
let request: SFWebRequest | undefined;
|
|
5
|
+
export function getSfRequest() {
|
|
6
|
+
if (!request) {
|
|
7
|
+
throw new Error(
|
|
8
|
+
"request还未初始化,只有在主程序加载完成后才能调用request。如主程序已经完成加载,请检查是否正确",
|
|
9
|
+
);
|
|
10
|
+
}
|
|
11
|
+
return request;
|
|
12
|
+
}
|
|
13
|
+
export function setSfRequest(request2: SFWebRequest) {
|
|
14
|
+
request = request2;
|
|
15
|
+
}
|
|
@@ -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
|
+
}
|