@ruiapp/rapid-core 0.1.0 → 0.1.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/dist/core/request.d.ts +4 -2
- package/dist/index.js +46 -24
- package/package.json +1 -1
- package/src/core/request.ts +29 -15
- 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 {
|
|
@@ -13,5 +13,7 @@ export declare class RapidRequest {
|
|
|
13
13
|
headers: Headers;
|
|
14
14
|
hasBody: boolean;
|
|
15
15
|
constructor(req: Request);
|
|
16
|
-
|
|
16
|
+
parseBody(): Promise<void>;
|
|
17
|
+
get rawRequest(): Request;
|
|
18
|
+
get body(): RapidRequestBody;
|
|
17
19
|
}
|
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");
|
|
@@ -2950,30 +2954,46 @@ class RapidRequest {
|
|
|
2950
2954
|
this.method = req.method;
|
|
2951
2955
|
this.url = new URL(req.url);
|
|
2952
2956
|
this.headers = req.headers;
|
|
2953
|
-
this.hasBody = false;
|
|
2954
2957
|
}
|
|
2955
|
-
|
|
2958
|
+
async parseBody() {
|
|
2956
2959
|
if (this.#bodyParsed) {
|
|
2957
|
-
|
|
2960
|
+
console.warn("Request body has been parsed. 'parseBody()' method should not be called more than once.");
|
|
2961
|
+
return;
|
|
2958
2962
|
}
|
|
2959
|
-
|
|
2960
|
-
if (
|
|
2961
|
-
|
|
2963
|
+
const requestMethod = this.method;
|
|
2964
|
+
if (requestMethod === "POST" || requestMethod === "PUT" || requestMethod === "PATCH") {
|
|
2965
|
+
const req = this.#raw;
|
|
2962
2966
|
const contentType = this.headers.get("Content-Type");
|
|
2963
2967
|
if (contentType.includes("json")) {
|
|
2964
2968
|
this.#body = {
|
|
2965
2969
|
type: "json",
|
|
2966
|
-
value:
|
|
2970
|
+
value: await req.json(),
|
|
2971
|
+
};
|
|
2972
|
+
}
|
|
2973
|
+
else if (contentType.startsWith("application/x-www-form-urlencoded")) {
|
|
2974
|
+
const bodyText = await req.text();
|
|
2975
|
+
this.#body = {
|
|
2976
|
+
type: "form",
|
|
2977
|
+
value: qs__default["default"].parse(bodyText),
|
|
2967
2978
|
};
|
|
2968
2979
|
}
|
|
2969
|
-
else if (contentType.
|
|
2980
|
+
else if (contentType.startsWith("multipart/form-data")) {
|
|
2970
2981
|
this.#body = {
|
|
2971
2982
|
type: "form-data",
|
|
2972
|
-
value:
|
|
2983
|
+
value: await req.formData(),
|
|
2973
2984
|
};
|
|
2974
2985
|
}
|
|
2975
2986
|
}
|
|
2976
2987
|
this.#body = null;
|
|
2988
|
+
this.#bodyParsed = true;
|
|
2989
|
+
}
|
|
2990
|
+
get rawRequest() {
|
|
2991
|
+
return this.#raw;
|
|
2992
|
+
}
|
|
2993
|
+
get body() {
|
|
2994
|
+
if (!this.#bodyParsed) {
|
|
2995
|
+
throw new Error("Request body not parsed, you should call 'parseBody()' method before getting the body.");
|
|
2996
|
+
}
|
|
2977
2997
|
return this.#body;
|
|
2978
2998
|
}
|
|
2979
2999
|
}
|
|
@@ -3653,7 +3673,9 @@ class RapidServer {
|
|
|
3653
3673
|
return this.#middlewares;
|
|
3654
3674
|
}
|
|
3655
3675
|
async handleRequest(request, next) {
|
|
3656
|
-
const
|
|
3676
|
+
const rapidRequest = new RapidRequest(request);
|
|
3677
|
+
await rapidRequest.parseBody();
|
|
3678
|
+
const routeContext = new RouteContext(rapidRequest);
|
|
3657
3679
|
console.log('routeContext', routeContext);
|
|
3658
3680
|
await this.#buildedRoutes(routeContext, next);
|
|
3659
3681
|
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
|
|
|
@@ -21,34 +21,48 @@ export class RapidRequest {
|
|
|
21
21
|
this.method = req.method;
|
|
22
22
|
this.url = new URL(req.url);
|
|
23
23
|
this.headers = req.headers;
|
|
24
|
-
this.hasBody = false;
|
|
25
24
|
}
|
|
26
25
|
|
|
27
|
-
|
|
26
|
+
async parseBody(): Promise<void> {
|
|
28
27
|
if (this.#bodyParsed) {
|
|
29
|
-
|
|
28
|
+
console.warn("Request body has been parsed. 'parseBody()' method should not be called more than once.");
|
|
29
|
+
return;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
this.hasBody = true;
|
|
36
|
-
|
|
32
|
+
const requestMethod = this.method;
|
|
33
|
+
if (requestMethod === "POST" || requestMethod === "PUT" || requestMethod === "PATCH") {
|
|
34
|
+
const req = this.#raw;
|
|
37
35
|
const contentType = this.headers.get("Content-Type");
|
|
38
36
|
if (contentType.includes("json")) {
|
|
39
37
|
this.#body = {
|
|
40
38
|
type: "json",
|
|
41
|
-
value:
|
|
39
|
+
value: await req.json(),
|
|
42
40
|
};
|
|
43
|
-
} else if (contentType.
|
|
41
|
+
} else if (contentType.startsWith("application/x-www-form-urlencoded")) {
|
|
42
|
+
const bodyText = await req.text();
|
|
43
|
+
this.#body = {
|
|
44
|
+
type: "form",
|
|
45
|
+
value: qs.parse(bodyText),
|
|
46
|
+
}
|
|
47
|
+
} else if (contentType.startsWith("multipart/form-data")) {
|
|
44
48
|
this.#body = {
|
|
45
49
|
type: "form-data",
|
|
46
|
-
value:
|
|
50
|
+
value: await req.formData(),
|
|
47
51
|
}
|
|
48
52
|
}
|
|
49
53
|
}
|
|
50
|
-
|
|
51
54
|
this.#body = null;
|
|
55
|
+
this.#bodyParsed = true;
|
|
56
|
+
}
|
|
57
|
+
|
|
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();
|