@ruiapp/rapid-core 0.1.0 → 0.1.2
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/dist/core/request.d.ts +4 -3
- package/dist/index.js +49 -26
- package/package.json +1 -1
- package/src/core/request.ts +30 -16
- package/src/core/routesBuilder.ts +12 -10
- package/src/plugins/authManager/mod.ts +2 -2
- package/src/plugins/dataManager/mod.ts +5 -1
- package/src/server.ts +3 -1
package/dist/core/request.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export declare const GlobalRequest: {
|
|
|
3
3
|
prototype: Request;
|
|
4
4
|
};
|
|
5
5
|
export interface RapidRequestBody {
|
|
6
|
-
type: "form-data" | "json";
|
|
6
|
+
type: "form-data" | "json" | "form";
|
|
7
7
|
value: any;
|
|
8
8
|
}
|
|
9
9
|
export declare class RapidRequest {
|
|
@@ -11,7 +11,8 @@ export declare class RapidRequest {
|
|
|
11
11
|
method: string;
|
|
12
12
|
url: URL;
|
|
13
13
|
headers: Headers;
|
|
14
|
-
hasBody: boolean;
|
|
15
14
|
constructor(req: Request);
|
|
16
|
-
|
|
15
|
+
parseBody(): Promise<void>;
|
|
16
|
+
get rawRequest(): Request;
|
|
17
|
+
get body(): RapidRequestBody;
|
|
17
18
|
}
|
package/dist/index.js
CHANGED
|
@@ -1842,7 +1842,8 @@ async function registerHttpHandlers$3(server) {
|
|
|
1842
1842
|
server.registerHttpHandler(_plugin$3, queryDatabase);
|
|
1843
1843
|
}
|
|
1844
1844
|
async function configureRoutes$3(server, applicationConfig) {
|
|
1845
|
-
const {
|
|
1845
|
+
const { models } = applicationConfig;
|
|
1846
|
+
const routes = [];
|
|
1846
1847
|
models.forEach((model) => {
|
|
1847
1848
|
const { namespace, singularCode, pluralCode } = model;
|
|
1848
1849
|
routeConfigs.forEach((routeConfig) => {
|
|
@@ -1865,6 +1866,7 @@ async function configureRoutes$3(server, applicationConfig) {
|
|
|
1865
1866
|
});
|
|
1866
1867
|
});
|
|
1867
1868
|
});
|
|
1869
|
+
server.appendApplicationConfig({ routes });
|
|
1868
1870
|
}
|
|
1869
1871
|
async function onApplicationLoaded$4(server, application) {
|
|
1870
1872
|
console.log("[dataManager.onApplicationLoaded]");
|
|
@@ -1983,17 +1985,19 @@ async function buildRoutes(server, applicationConfig) {
|
|
|
1983
1985
|
const input = Object.assign({}, params, query);
|
|
1984
1986
|
const requestMethod = request.method;
|
|
1985
1987
|
if ((requestMethod === "POST" || requestMethod === "PUT" ||
|
|
1986
|
-
requestMethod === "PATCH")
|
|
1987
|
-
const body = request.body
|
|
1988
|
-
if (body
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1988
|
+
requestMethod === "PATCH")) {
|
|
1989
|
+
const body = request.body;
|
|
1990
|
+
if (body) {
|
|
1991
|
+
if (body.type === "form-data") {
|
|
1992
|
+
const formDataReader = body.value;
|
|
1993
|
+
const formDataBody = await formDataReader.read({ maxFileSize: 1073741824 /* 1GB */ });
|
|
1994
|
+
Object.assign(input, {
|
|
1995
|
+
formData: formDataBody
|
|
1996
|
+
});
|
|
1997
|
+
}
|
|
1998
|
+
else {
|
|
1999
|
+
Object.assign(input, body.value);
|
|
2000
|
+
}
|
|
1997
2001
|
}
|
|
1998
2002
|
}
|
|
1999
2003
|
// Normalize input value
|
|
@@ -2709,10 +2713,10 @@ async function registerHttpHandlers$1(server) {
|
|
|
2709
2713
|
async function registerEventHandlers$1(server) {
|
|
2710
2714
|
}
|
|
2711
2715
|
async function configureModels$1(server, applicationConfig) {
|
|
2712
|
-
|
|
2716
|
+
server.appendApplicationConfig({ models: pluginModels });
|
|
2713
2717
|
}
|
|
2714
2718
|
async function configureRoutes$1(server, applicationConfig) {
|
|
2715
|
-
|
|
2719
|
+
server.appendApplicationConfig({ routes: pluginRoutes });
|
|
2716
2720
|
}
|
|
2717
2721
|
async function onApplicationLoaded$1(server, applicationConfig) {
|
|
2718
2722
|
console.log("authManager.onApplicationLoaded");
|
|
@@ -2944,36 +2948,53 @@ class RapidRequest {
|
|
|
2944
2948
|
method;
|
|
2945
2949
|
url;
|
|
2946
2950
|
headers;
|
|
2947
|
-
hasBody;
|
|
2948
2951
|
constructor(req) {
|
|
2949
2952
|
this.#raw = req;
|
|
2950
2953
|
this.method = req.method;
|
|
2951
2954
|
this.url = new URL(req.url);
|
|
2952
2955
|
this.headers = req.headers;
|
|
2953
|
-
this.hasBody = false;
|
|
2954
2956
|
}
|
|
2955
|
-
|
|
2957
|
+
async parseBody() {
|
|
2956
2958
|
if (this.#bodyParsed) {
|
|
2957
|
-
|
|
2959
|
+
console.warn("Request body has been parsed. 'parseBody()' method should not be called more than once.");
|
|
2960
|
+
return;
|
|
2958
2961
|
}
|
|
2959
|
-
|
|
2960
|
-
if (
|
|
2961
|
-
|
|
2962
|
+
const requestMethod = this.method;
|
|
2963
|
+
if (requestMethod === "POST" || requestMethod === "PUT" || requestMethod === "PATCH") {
|
|
2964
|
+
const req = this.#raw;
|
|
2962
2965
|
const contentType = this.headers.get("Content-Type");
|
|
2963
2966
|
if (contentType.includes("json")) {
|
|
2964
2967
|
this.#body = {
|
|
2965
2968
|
type: "json",
|
|
2966
|
-
value:
|
|
2969
|
+
value: await req.json(),
|
|
2967
2970
|
};
|
|
2968
2971
|
}
|
|
2969
|
-
else if (contentType.
|
|
2972
|
+
else if (contentType.startsWith("application/x-www-form-urlencoded")) {
|
|
2973
|
+
const bodyText = await req.text();
|
|
2974
|
+
this.#body = {
|
|
2975
|
+
type: "form",
|
|
2976
|
+
value: qs__default["default"].parse(bodyText),
|
|
2977
|
+
};
|
|
2978
|
+
}
|
|
2979
|
+
else if (contentType.startsWith("multipart/form-data")) {
|
|
2970
2980
|
this.#body = {
|
|
2971
2981
|
type: "form-data",
|
|
2972
|
-
value:
|
|
2982
|
+
value: await req.formData(),
|
|
2973
2983
|
};
|
|
2974
2984
|
}
|
|
2975
2985
|
}
|
|
2976
|
-
|
|
2986
|
+
else {
|
|
2987
|
+
this.#body = null;
|
|
2988
|
+
}
|
|
2989
|
+
this.#bodyParsed = true;
|
|
2990
|
+
}
|
|
2991
|
+
get rawRequest() {
|
|
2992
|
+
return this.#raw;
|
|
2993
|
+
}
|
|
2994
|
+
get body() {
|
|
2995
|
+
if (!this.#bodyParsed) {
|
|
2996
|
+
throw new Error("Request body not parsed, you should call 'parseBody()' method before getting the body.");
|
|
2997
|
+
}
|
|
2977
2998
|
return this.#body;
|
|
2978
2999
|
}
|
|
2979
3000
|
}
|
|
@@ -3653,7 +3674,9 @@ class RapidServer {
|
|
|
3653
3674
|
return this.#middlewares;
|
|
3654
3675
|
}
|
|
3655
3676
|
async handleRequest(request, next) {
|
|
3656
|
-
const
|
|
3677
|
+
const rapidRequest = new RapidRequest(request);
|
|
3678
|
+
await rapidRequest.parseBody();
|
|
3679
|
+
const routeContext = new RouteContext(rapidRequest);
|
|
3657
3680
|
console.log('routeContext', routeContext);
|
|
3658
3681
|
await this.#buildedRoutes(routeContext, next);
|
|
3659
3682
|
return routeContext.response.getResponse();
|
package/package.json
CHANGED
package/src/core/request.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import qs from "qs";
|
|
3
2
|
|
|
3
|
+
export const GlobalRequest = global.Request;
|
|
4
4
|
|
|
5
5
|
export interface RapidRequestBody {
|
|
6
|
-
type: "form-data" | "json";
|
|
6
|
+
type: "form-data" | "json" | "form";
|
|
7
7
|
value: any;
|
|
8
8
|
}
|
|
9
9
|
|
|
@@ -14,41 +14,55 @@ export class RapidRequest {
|
|
|
14
14
|
method: string;
|
|
15
15
|
url: URL;
|
|
16
16
|
headers: Headers;
|
|
17
|
-
hasBody: boolean;
|
|
18
17
|
|
|
19
18
|
constructor(req: Request) {
|
|
20
19
|
this.#raw = req;
|
|
21
20
|
this.method = req.method;
|
|
22
21
|
this.url = new URL(req.url);
|
|
23
22
|
this.headers = req.headers;
|
|
24
|
-
this.hasBody = false;
|
|
25
23
|
}
|
|
26
24
|
|
|
27
|
-
|
|
25
|
+
async parseBody(): Promise<void> {
|
|
28
26
|
if (this.#bodyParsed) {
|
|
29
|
-
|
|
27
|
+
console.warn("Request body has been parsed. 'parseBody()' method should not be called more than once.");
|
|
28
|
+
return;
|
|
30
29
|
}
|
|
31
30
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
this.hasBody = true;
|
|
36
|
-
|
|
31
|
+
const requestMethod = this.method;
|
|
32
|
+
if (requestMethod === "POST" || requestMethod === "PUT" || requestMethod === "PATCH") {
|
|
33
|
+
const req = this.#raw;
|
|
37
34
|
const contentType = this.headers.get("Content-Type");
|
|
38
35
|
if (contentType.includes("json")) {
|
|
39
36
|
this.#body = {
|
|
40
37
|
type: "json",
|
|
41
|
-
value:
|
|
38
|
+
value: await req.json(),
|
|
42
39
|
};
|
|
43
|
-
} else if (contentType.
|
|
40
|
+
} else if (contentType.startsWith("application/x-www-form-urlencoded")) {
|
|
41
|
+
const bodyText = await req.text();
|
|
42
|
+
this.#body = {
|
|
43
|
+
type: "form",
|
|
44
|
+
value: qs.parse(bodyText),
|
|
45
|
+
}
|
|
46
|
+
} else if (contentType.startsWith("multipart/form-data")) {
|
|
44
47
|
this.#body = {
|
|
45
48
|
type: "form-data",
|
|
46
|
-
value:
|
|
49
|
+
value: await req.formData(),
|
|
47
50
|
}
|
|
48
51
|
}
|
|
52
|
+
} else {
|
|
53
|
+
this.#body = null;
|
|
49
54
|
}
|
|
55
|
+
this.#bodyParsed = true;
|
|
56
|
+
}
|
|
50
57
|
|
|
51
|
-
|
|
58
|
+
get rawRequest(): Request {
|
|
59
|
+
return this.#raw;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
get body(): RapidRequestBody {
|
|
63
|
+
if (!this.#bodyParsed) {
|
|
64
|
+
throw new Error("Request body not parsed, you should call 'parseBody()' method before getting the body.")
|
|
65
|
+
}
|
|
52
66
|
return this.#body;
|
|
53
67
|
}
|
|
54
68
|
}
|
|
@@ -43,17 +43,19 @@ export async function buildRoutes(
|
|
|
43
43
|
const requestMethod = request.method;
|
|
44
44
|
if (
|
|
45
45
|
(requestMethod === "POST" || requestMethod === "PUT" ||
|
|
46
|
-
requestMethod === "PATCH")
|
|
46
|
+
requestMethod === "PATCH")
|
|
47
47
|
) {
|
|
48
|
-
const body = request.body
|
|
49
|
-
if (body
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
48
|
+
const body = request.body;
|
|
49
|
+
if (body) {
|
|
50
|
+
if (body.type === "form-data") {
|
|
51
|
+
const formDataReader = body.value;
|
|
52
|
+
const formDataBody = await formDataReader.read({ maxFileSize: 1073741824 /* 1GB */});
|
|
53
|
+
Object.assign(input, {
|
|
54
|
+
formData: formDataBody
|
|
55
|
+
});
|
|
56
|
+
} else {
|
|
57
|
+
Object.assign(input, body.value);
|
|
58
|
+
}
|
|
57
59
|
}
|
|
58
60
|
}
|
|
59
61
|
|
|
@@ -38,14 +38,14 @@ export async function configureModels(
|
|
|
38
38
|
server: IRpdServer,
|
|
39
39
|
applicationConfig: RpdApplicationConfig,
|
|
40
40
|
) {
|
|
41
|
-
|
|
41
|
+
server.appendApplicationConfig({ models: pluginModels });
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
export async function configureRoutes(
|
|
45
45
|
server: IRpdServer,
|
|
46
46
|
applicationConfig: RpdApplicationConfig,
|
|
47
47
|
) {
|
|
48
|
-
|
|
48
|
+
server.appendApplicationConfig({ routes: pluginRoutes });
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
export async function onApplicationLoaded(
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
IPluginInstance,
|
|
10
10
|
RpdApplicationConfig,
|
|
11
11
|
RpdHttpMethod,
|
|
12
|
+
RpdRoute,
|
|
12
13
|
RunEntityHttpHandlerOptions,
|
|
13
14
|
} from "~/types";
|
|
14
15
|
|
|
@@ -115,7 +116,8 @@ export async function configureRoutes(
|
|
|
115
116
|
server: IRpdServer,
|
|
116
117
|
applicationConfig: RpdApplicationConfig,
|
|
117
118
|
) {
|
|
118
|
-
const {
|
|
119
|
+
const { models } = applicationConfig;
|
|
120
|
+
const routes: RpdRoute[] = [];
|
|
119
121
|
|
|
120
122
|
models.forEach((model) => {
|
|
121
123
|
const { namespace, singularCode, pluralCode } = model;
|
|
@@ -140,6 +142,8 @@ export async function configureRoutes(
|
|
|
140
142
|
});
|
|
141
143
|
});
|
|
142
144
|
});
|
|
145
|
+
|
|
146
|
+
server.appendApplicationConfig({ routes });
|
|
143
147
|
}
|
|
144
148
|
|
|
145
149
|
export async function onApplicationLoaded(
|
package/src/server.ts
CHANGED
|
@@ -220,7 +220,9 @@ export class RapidServer implements IRpdServer {
|
|
|
220
220
|
}
|
|
221
221
|
|
|
222
222
|
async handleRequest(request: Request, next: Next) {
|
|
223
|
-
const
|
|
223
|
+
const rapidRequest = new RapidRequest(request);
|
|
224
|
+
await rapidRequest.parseBody();
|
|
225
|
+
const routeContext = new RouteContext(rapidRequest);
|
|
224
226
|
console.log('routeContext', routeContext)
|
|
225
227
|
await this.#buildedRoutes(routeContext, next);
|
|
226
228
|
return routeContext.response.getResponse();
|