milkio 0.0.3 → 0.0.5
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/c.ts +5 -15
- package/defines/define-http-handler.ts +13 -13
- package/kernel/config.ts +3 -4
- package/kernel/context.ts +2 -2
- package/kernel/fail.ts +2 -2
- package/kernel/milkio.ts +3 -3
- package/kernel/validate.ts +1 -1
- package/package.json +5 -5
- package/scripts/build-dto.ts +2 -2
- package/scripts/generate-database.ts +2 -2
- package/scripts/generate-partial.ts +2 -2
- package/scripts/generate.ts +2 -2
- package/types.ts +0 -2
- package/utils/handle-catch-error.ts +3 -3
package/c.ts
CHANGED
|
@@ -30,23 +30,13 @@ const commands = {
|
|
|
30
30
|
async test(files?: string, reserved?: string) {
|
|
31
31
|
const run = async () => {
|
|
32
32
|
const packageJson = await JSON.parse((await readFile(join(rootPath, "package.json"))).toString());
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
// milkio_RUN_MODE: "API_TEST",
|
|
38
|
-
// milkio_TEST: files ?? "1"
|
|
39
|
-
// });
|
|
40
|
-
//
|
|
41
|
-
await exec(rootPath, ["bash", "-c", packageJson.scripts.start as string], {
|
|
42
|
-
env: {
|
|
43
|
-
...env,
|
|
44
|
-
milkio_RUN_MODE: "API_TEST",
|
|
45
|
-
milkio_TEST: files ?? "1"
|
|
46
|
-
}
|
|
33
|
+
await $`${{ raw: packageJson.scripts.start }}`.env({
|
|
34
|
+
...env,
|
|
35
|
+
MILKIO_RUN_MODE: "API_TEST",
|
|
36
|
+
MILKIO_TEST: files ?? "1"
|
|
47
37
|
});
|
|
48
38
|
};
|
|
49
|
-
if (!env.
|
|
39
|
+
if (!env.MILKIO_TEST_RESERVED && reserved !== "1") {
|
|
50
40
|
// Normal test
|
|
51
41
|
await run();
|
|
52
42
|
} else {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/* eslint-disable no-console */
|
|
2
|
-
import {
|
|
2
|
+
import { configMilkio, loggerPushTags, loggerSubmit, useLogger, loggerSubmitAll, runtime, MiddlewareEvent } from "..";
|
|
3
3
|
import type { ExecuteId, MilkioApp, Mixin } from "..";
|
|
4
4
|
import { hanldeCatchError } from "../utils/handle-catch-error";
|
|
5
5
|
import { routerHandler } from "../../../src/router";
|
|
@@ -33,7 +33,7 @@ export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOpti
|
|
|
33
33
|
exit(1);
|
|
34
34
|
});
|
|
35
35
|
|
|
36
|
-
const fetch = async (request:
|
|
36
|
+
const fetch = async (request: MilkioHTTPRequest) => {
|
|
37
37
|
const fullurl = new URL(request.request.url, `http://${request.request.headers.get("host") ?? "localhost"}`);
|
|
38
38
|
const executeId = (options?.executeIdGenerator ? await options.executeIdGenerator(request.request) : createUlid()) as ExecuteId;
|
|
39
39
|
runtime.execute.executeIds.add(executeId);
|
|
@@ -50,14 +50,14 @@ export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOpti
|
|
|
50
50
|
timein: new Date().getTime()
|
|
51
51
|
});
|
|
52
52
|
|
|
53
|
-
const response:
|
|
53
|
+
const response: MilkioHTTPResponse = {
|
|
54
54
|
body: "",
|
|
55
55
|
status: 200,
|
|
56
56
|
headers: {
|
|
57
57
|
"Content-Type": "application/json",
|
|
58
|
-
"Access-Control-Allow-Methods":
|
|
59
|
-
"Access-Control-Allow-Headers":
|
|
60
|
-
"Access-Control-Allow-Origin":
|
|
58
|
+
"Access-Control-Allow-Methods": configMilkio.corsAllowMethods ?? "*",
|
|
59
|
+
"Access-Control-Allow-Headers": configMilkio.corsAllowHeaders ?? "*",
|
|
60
|
+
"Access-Control-Allow-Origin": configMilkio.corsAllowOrigin ?? "*"
|
|
61
61
|
}
|
|
62
62
|
};
|
|
63
63
|
|
|
@@ -69,9 +69,9 @@ export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOpti
|
|
|
69
69
|
|
|
70
70
|
return new Response("", {
|
|
71
71
|
headers: {
|
|
72
|
-
"Access-Control-Allow-Methods":
|
|
73
|
-
"Access-Control-Allow-Headers":
|
|
74
|
-
"Access-Control-Allow-Origin":
|
|
72
|
+
"Access-Control-Allow-Methods": configMilkio.corsAllowMethods ?? "*",
|
|
73
|
+
"Access-Control-Allow-Headers": configMilkio.corsAllowHeaders ?? "*",
|
|
74
|
+
"Access-Control-Allow-Origin": configMilkio.corsAllowOrigin ?? "*"
|
|
75
75
|
}
|
|
76
76
|
});
|
|
77
77
|
}
|
|
@@ -80,7 +80,7 @@ export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOpti
|
|
|
80
80
|
|
|
81
81
|
// Compatible with API gateway's ability to differentiate versions by path
|
|
82
82
|
// see: /src/config/ConfigProgram.ts in "ignorePathLevel"
|
|
83
|
-
if (
|
|
83
|
+
if (configMilkio.ignorePathLevel !== 0) path = path.slice(configMilkio.ignorePathLevel);
|
|
84
84
|
|
|
85
85
|
let pathstr = path.join("/") as keyof (typeof schema)["apiMethodsSchema"];
|
|
86
86
|
|
|
@@ -92,7 +92,7 @@ export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOpti
|
|
|
92
92
|
loggerPushTags(executeId, {
|
|
93
93
|
body: rawbody || "no body"
|
|
94
94
|
});
|
|
95
|
-
response.body = `{"executeId":"${executeId}","success":false,"fail":{"code":"NOT_FOUND","message":${JSON.stringify(failCode
|
|
95
|
+
response.body = `{"executeId":"${executeId}","success":false,"fail":{"code":"NOT_FOUND","message":${JSON.stringify(failCode.NOT_FOUND())}}}`;
|
|
96
96
|
|
|
97
97
|
loggerPushTags(executeId, {
|
|
98
98
|
status: response.status,
|
|
@@ -188,11 +188,11 @@ export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOpti
|
|
|
188
188
|
return fetch;
|
|
189
189
|
}
|
|
190
190
|
|
|
191
|
-
export type
|
|
191
|
+
export type MilkioHTTPRequest = {
|
|
192
192
|
request: Request;
|
|
193
193
|
};
|
|
194
194
|
|
|
195
|
-
export type
|
|
195
|
+
export type MilkioHTTPResponse = Mixin<
|
|
196
196
|
ResponseInit,
|
|
197
197
|
{
|
|
198
198
|
//
|
package/kernel/config.ts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import { cwd, env } from "node:process";
|
|
2
2
|
import { envToBoolean, envToNumber, envToString } from "..";
|
|
3
3
|
|
|
4
|
-
export const
|
|
4
|
+
export const configMilkio = {
|
|
5
5
|
cwd: cwd(),
|
|
6
6
|
port: envToNumber(env.PORT, 9000),
|
|
7
|
-
milkioRunMode: envToString(env.
|
|
8
|
-
milkioTest: envToString(env.
|
|
7
|
+
milkioRunMode: envToString(env.MILKIO_RUN_MODE, "DEFAULT"),
|
|
8
|
+
milkioTest: envToString(env.MILKIO_TEST, ""),
|
|
9
9
|
debug: envToBoolean(env.DEBUG, false),
|
|
10
|
-
redisUrl: envToString(env.REDIS_URL, "redis://:123456@your-not-redis-url:6379"),
|
|
11
10
|
ignorePathLevel: envToNumber(env.IGNORE_PATH_LEVEL, 0),
|
|
12
11
|
corsAllowMethods: envToString(env.CORS_ALLOW_METHODS, "*"),
|
|
13
12
|
corsAllowHeaders: envToString(env.CORS_ALLOW_HEADERS, "*"),
|
package/kernel/context.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { URL } from "node:url";
|
|
2
|
-
import type { ExecuteId, Logger,
|
|
2
|
+
import type { ExecuteId, Logger, MilkioHTTPResponse } from "..";
|
|
3
3
|
|
|
4
4
|
export type MilkioContext = {
|
|
5
5
|
path: string;
|
|
@@ -20,5 +20,5 @@ export type FrameworkHTTPDetail = {
|
|
|
20
20
|
fullurl: URL;
|
|
21
21
|
ip: string;
|
|
22
22
|
request: Request;
|
|
23
|
-
response:
|
|
23
|
+
response: MilkioHTTPResponse;
|
|
24
24
|
};
|
package/kernel/fail.ts
CHANGED
|
@@ -4,7 +4,7 @@ export function reject<Code extends keyof typeof failCode, FailData extends (typ
|
|
|
4
4
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument
|
|
5
5
|
const message = failCode[code]?.(data as any) ?? "";
|
|
6
6
|
const error = {
|
|
7
|
-
name: "
|
|
7
|
+
name: "MilkioReject",
|
|
8
8
|
code,
|
|
9
9
|
message,
|
|
10
10
|
data,
|
|
@@ -16,7 +16,7 @@ export function reject<Code extends keyof typeof failCode, FailData extends (typ
|
|
|
16
16
|
return error;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
export type
|
|
19
|
+
export type MilkioReject = ReturnType<typeof reject>;
|
|
20
20
|
|
|
21
21
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
22
22
|
export type MilkioFailCode = Record<string, (arg: any) => string>;
|
package/kernel/milkio.ts
CHANGED
|
@@ -47,7 +47,7 @@ export async function createMilkioApp(MilkioAppOptions: MilkioAppOptions = {}) {
|
|
|
47
47
|
|
|
48
48
|
if (MilkioAppOptions.enableMaxRunningTimeoutLimit && MilkioAppOptions.enableMaxRunningTimeoutLimit >= 1) {
|
|
49
49
|
setTimeout(() => {
|
|
50
|
-
console.log('❌
|
|
50
|
+
console.log('❌ Milkio reached the limit of "maxRunningTimeout" in the options and automatically exited.');
|
|
51
51
|
exit(0);
|
|
52
52
|
}, MilkioAppOptions.enableMaxRunningTimeoutLimit * 60 * 1000);
|
|
53
53
|
runtime.maxRunningTimeout.enable = true;
|
|
@@ -137,7 +137,7 @@ async function _executeCore<Path extends keyof (typeof schema)["apiMethodsTypeSc
|
|
|
137
137
|
|
|
138
138
|
if (runtime.maxRequest.enable) {
|
|
139
139
|
if (runtime.maxRequest.counter >= runtime.maxRequest.expected) {
|
|
140
|
-
console.log("❌
|
|
140
|
+
console.log("❌ Milkio reached the limit of 'maxRequest' in the options and automatically exited.");
|
|
141
141
|
exit(0);
|
|
142
142
|
}
|
|
143
143
|
runtime.maxRequest.counter++;
|
|
@@ -149,7 +149,7 @@ async function _executeCore<Path extends keyof (typeof schema)["apiMethodsTypeSc
|
|
|
149
149
|
success: false,
|
|
150
150
|
fail: {
|
|
151
151
|
code: "NOT_FOUND",
|
|
152
|
-
message: failCode
|
|
152
|
+
message: failCode.NOT_FOUND(),
|
|
153
153
|
data: undefined
|
|
154
154
|
}
|
|
155
155
|
} satisfies ExecuteResult<Result>;
|
package/kernel/validate.ts
CHANGED
|
@@ -5,7 +5,7 @@ export function _validate(validator: IValidation.IFailure | IValidation.ISuccess
|
|
|
5
5
|
if (validator.success) return;
|
|
6
6
|
const error = validator.errors[0];
|
|
7
7
|
|
|
8
|
-
throw reject("
|
|
8
|
+
throw reject("TYPE_SAFE_ERROR", {
|
|
9
9
|
path: error.path,
|
|
10
10
|
expected: error.expected,
|
|
11
11
|
value: error.value
|
package/package.json
CHANGED
|
@@ -2,20 +2,20 @@
|
|
|
2
2
|
"name": "milkio",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"module": "index.ts",
|
|
5
|
-
"version": "0.0.
|
|
5
|
+
"version": "0.0.5",
|
|
6
6
|
"peerDependencies": {
|
|
7
7
|
"typescript": "^5.4.2"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
10
|
"@poech/camel-hump-under": "^1.1.0",
|
|
11
11
|
"@southern-aurora/tson": "2.0.2",
|
|
12
|
-
"ulidx": "^2.3.0"
|
|
12
|
+
"ulidx": "^2.3.0",
|
|
13
|
+
"walk-sync": "^3.0.0",
|
|
14
|
+
"typia": "5.5.5",
|
|
15
|
+
"ejs": "^3.1.9"
|
|
13
16
|
},
|
|
14
17
|
"devDependencies": {
|
|
15
18
|
"@types/bun": "latest",
|
|
16
|
-
"walk-sync": "^3.0.0",
|
|
17
|
-
"typia": "5.5.5",
|
|
18
|
-
"ejs": "^3.1.9",
|
|
19
19
|
"@types/ejs": "^3.1.5",
|
|
20
20
|
"@types/js-beautify": "^1.14.3",
|
|
21
21
|
"ts-patch": "^3.1.2",
|
package/scripts/build-dto.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { join } from "node:path";
|
|
|
6
6
|
import { copyFile, mkdir } from "node:fs/promises";
|
|
7
7
|
|
|
8
8
|
export async function buildDTO() {
|
|
9
|
-
console.log("🧊
|
|
9
|
+
console.log("🧊 Milkio DTO Building..");
|
|
10
10
|
|
|
11
11
|
removeDir(join(cwd(), "packages", "dto", "dist"));
|
|
12
12
|
removeDir(join(cwd(), "packages", "dto", "generate"));
|
|
@@ -36,7 +36,7 @@ export async function buildDTO() {
|
|
|
36
36
|
|
|
37
37
|
const root = join(cwd(), "packages", "dto");
|
|
38
38
|
|
|
39
|
-
console.log("🧊
|
|
39
|
+
console.log("🧊 Milkio DTO Build Finished");
|
|
40
40
|
console.log("\x1B[2m");
|
|
41
41
|
console.log("Now, your latest code (including changes to your interface) is built to the latest version and waiting for your release!");
|
|
42
42
|
console.log("");
|
|
@@ -14,10 +14,10 @@ export async function generate() {
|
|
|
14
14
|
await generateDatabase();
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
console.log("
|
|
17
|
+
console.log("Milkio Database Generating..");
|
|
18
18
|
|
|
19
19
|
await generate();
|
|
20
20
|
|
|
21
|
-
console.log("\n✅
|
|
21
|
+
console.log("\n✅ Milkio Database Generated!\n");
|
|
22
22
|
|
|
23
23
|
exit(0);
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import { argv, exit } from "node:process";
|
|
4
4
|
import { generateApp } from "./generate/generate-app";
|
|
5
|
-
console.log("
|
|
5
|
+
console.log("Milkio Quick Generating..");
|
|
6
6
|
|
|
7
7
|
export async function generatePartial(path?: string) {
|
|
8
8
|
await generateApp();
|
|
@@ -10,6 +10,6 @@ export async function generatePartial(path?: string) {
|
|
|
10
10
|
|
|
11
11
|
await generatePartial(...(argv.slice(3) as any));
|
|
12
12
|
|
|
13
|
-
console.log("\n✅
|
|
13
|
+
console.log("\n✅ Milkio Generated!");
|
|
14
14
|
|
|
15
15
|
exit(0);
|
package/scripts/generate.ts
CHANGED
|
@@ -14,10 +14,10 @@ export async function generate() {
|
|
|
14
14
|
await generateApp();
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
console.log("
|
|
17
|
+
console.log("Milkio Generating..");
|
|
18
18
|
|
|
19
19
|
await generate();
|
|
20
20
|
|
|
21
|
-
console.log("\n✅
|
|
21
|
+
console.log("\n✅ Milkio Generated!");
|
|
22
22
|
|
|
23
23
|
exit(0);
|
package/types.ts
CHANGED
|
@@ -35,8 +35,6 @@ export type CookbookItem = {
|
|
|
35
35
|
}>;
|
|
36
36
|
};
|
|
37
37
|
|
|
38
|
-
export type DatabaseType<Table extends { findFirst: () => unknown }, Override = {}> = Mixin<NonNullable<Awaited<ReturnType<Table["findFirst"]>>>, Override>;
|
|
39
|
-
|
|
40
38
|
export type Override<P, S> = Omit<P, keyof S> & S;
|
|
41
39
|
|
|
42
40
|
export type Mixin<T, U> = U & Omit<T, keyof U>;
|
|
@@ -9,8 +9,8 @@ export function hanldeCatchError(error: any, executeId: ExecuteId): ExecuteResul
|
|
|
9
9
|
if (error.stack) logger.error("\nError Stack: ", error.stack);
|
|
10
10
|
else logger.error("\nError Stack: ", error);
|
|
11
11
|
|
|
12
|
-
if (error.name !== "
|
|
13
|
-
// If it is not
|
|
12
|
+
if (error.name !== "MilkioReject") {
|
|
13
|
+
// If it is not MilkioReject, it is considered an internal server error that should not be exposed
|
|
14
14
|
logger.error(`FailCode: INTERNAL_SERVER_ERROR`);
|
|
15
15
|
|
|
16
16
|
return {
|
|
@@ -18,7 +18,7 @@ export function hanldeCatchError(error: any, executeId: ExecuteId): ExecuteResul
|
|
|
18
18
|
success: false,
|
|
19
19
|
fail: {
|
|
20
20
|
code: "INTERNAL_SERVER_ERROR",
|
|
21
|
-
message: failCode
|
|
21
|
+
message: failCode.INTERNAL_SERVER_ERROR(),
|
|
22
22
|
data: undefined
|
|
23
23
|
}
|
|
24
24
|
};
|