milkio 0.0.2 → 0.0.3
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/defines/define-api-test-handler.ts +2 -2
- package/defines/define-http-handler.ts +3 -3
- package/kernel/context.ts +1 -1
- package/kernel/fail.ts +1 -1
- package/kernel/middleware.ts +2 -2
- package/kernel/milkio.ts +17 -17
- package/kernel/validate.ts +1 -1
- package/package.json +1 -1
- package/types.ts +2 -2
- package/utils/handle-catch-error.ts +3 -3
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unsafe-argument, no-console, @typescript-eslint/no-explicit-any */
|
|
2
2
|
import { exit } from "node:process";
|
|
3
3
|
import schema from "../../../generate/api-schema";
|
|
4
|
-
import { type
|
|
4
|
+
import { type MilkioApp } from "..";
|
|
5
5
|
|
|
6
|
-
export const defineApiTestHandler = async <Paths extends Array<keyof (typeof schema)["apiTestsSchema"]>>(app:
|
|
6
|
+
export const defineApiTestHandler = async <Paths extends Array<keyof (typeof schema)["apiTestsSchema"]>>(app: MilkioApp, paths: Paths | string | 1 | undefined) => {
|
|
7
7
|
console.log(`🧊 Milkio Api Testing..\n`);
|
|
8
8
|
|
|
9
9
|
if (!paths) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/* eslint-disable no-console */
|
|
2
2
|
import { configFramework, loggerPushTags, loggerSubmit, useLogger, loggerSubmitAll, runtime, MiddlewareEvent } from "..";
|
|
3
|
-
import type { ExecuteId,
|
|
3
|
+
import type { ExecuteId, MilkioApp, Mixin } from "..";
|
|
4
4
|
import { hanldeCatchError } from "../utils/handle-catch-error";
|
|
5
5
|
import { routerHandler } from "../../../src/router";
|
|
6
6
|
import schema from "../../../generate/api-schema";
|
|
@@ -20,7 +20,7 @@ export type ExecuteHttpServerOptions = {
|
|
|
20
20
|
executeIdGenerator?: (request: Request) => string | Promise<string>;
|
|
21
21
|
};
|
|
22
22
|
|
|
23
|
-
export function defineHttpHandler(app:
|
|
23
|
+
export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOptions = {}) {
|
|
24
24
|
// If an unexpected error occurs, exit the process.
|
|
25
25
|
// For modern production environments such as Serverless, Kubernetes, or Docker Compose:
|
|
26
26
|
// The process will automatically restart after exiting.
|
|
@@ -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":"
|
|
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,
|
package/kernel/context.ts
CHANGED
package/kernel/fail.ts
CHANGED
|
@@ -19,4 +19,4 @@ export function reject<Code extends keyof typeof failCode, FailData extends (typ
|
|
|
19
19
|
export type milkioReject = ReturnType<typeof reject>;
|
|
20
20
|
|
|
21
21
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
22
|
-
export type
|
|
22
|
+
export type MilkioFailCode = Record<string, (arg: any) => string>;
|
package/kernel/middleware.ts
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
import type { Context } from "../../../src/context";
|
|
4
4
|
import type { FrameworkHTTPDetail } from "./context";
|
|
5
|
-
import { type
|
|
5
|
+
import { type MilkioApp } from "milkio";
|
|
6
6
|
|
|
7
|
-
export type BootstrapMiddleware = (milkio:
|
|
7
|
+
export type BootstrapMiddleware = (milkio: MilkioApp) => Promise<void> | void;
|
|
8
8
|
export type BeforeExecuteMiddleware = (context: Context) => Promise<void> | void;
|
|
9
9
|
export type AfterExecuteMiddleware = (context: Context, response: { value: unknown }) => Promise<void> | void;
|
|
10
10
|
export type AfterHTTPRequestMiddleware = (headers: Headers, detail: FrameworkHTTPDetail) => Promise<void> | void;
|
package/kernel/milkio.ts
CHANGED
|
@@ -3,14 +3,14 @@ import { type MiddlewareOptions, _middlewares, MiddlewareEvent } from "./middlew
|
|
|
3
3
|
import schema from "../../../generate/api-schema";
|
|
4
4
|
import type { Context } from "../../../src/context";
|
|
5
5
|
import { failCode } from "../../../src/fail-code";
|
|
6
|
-
import type {
|
|
6
|
+
import type { MilkioContext } from "./context";
|
|
7
7
|
import { type Mixin, type ExecuteId, type Fail, type FailEnumerates, loggerPushTags, loggerSubmit, runtime, TSON, type Logger, useLogger } from "..";
|
|
8
8
|
import { hanldeCatchError } from "../utils/handle-catch-error";
|
|
9
9
|
import { _validate } from "./validate";
|
|
10
10
|
import { cwd, exit } from "node:process";
|
|
11
11
|
import { createUlid } from "../utils/create-ulid";
|
|
12
12
|
|
|
13
|
-
export type
|
|
13
|
+
export type MilkioAppOptions = {
|
|
14
14
|
/**
|
|
15
15
|
* bootstraps
|
|
16
16
|
* @description
|
|
@@ -39,39 +39,39 @@ export type milkioAppOptions = {
|
|
|
39
39
|
enableMaxRunningTimeoutLimit?: number | null | undefined;
|
|
40
40
|
};
|
|
41
41
|
|
|
42
|
-
export async function createMilkioApp(
|
|
43
|
-
if (
|
|
44
|
-
runtime.maxRequest.expected =
|
|
42
|
+
export async function createMilkioApp(MilkioAppOptions: MilkioAppOptions = {}) {
|
|
43
|
+
if (MilkioAppOptions?.enableMaxRequestLimit && MilkioAppOptions.enableMaxRequestLimit >= 1) {
|
|
44
|
+
runtime.maxRequest.expected = MilkioAppOptions.enableMaxRequestLimit;
|
|
45
45
|
runtime.maxRequest.enable = true;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
if (
|
|
48
|
+
if (MilkioAppOptions.enableMaxRunningTimeoutLimit && MilkioAppOptions.enableMaxRunningTimeoutLimit >= 1) {
|
|
49
49
|
setTimeout(() => {
|
|
50
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;
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
const
|
|
56
|
+
const MilkioApp = {
|
|
57
57
|
execute: _execute,
|
|
58
58
|
executeToJson: _executeToJson,
|
|
59
59
|
_executeCore,
|
|
60
60
|
_executeCoreToJson
|
|
61
61
|
};
|
|
62
62
|
|
|
63
|
-
if (
|
|
64
|
-
await Promise.all(
|
|
63
|
+
if (MilkioAppOptions.bootstraps) {
|
|
64
|
+
await Promise.all(MilkioAppOptions.bootstraps());
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
if (
|
|
67
|
+
if (MilkioAppOptions.middlewares) {
|
|
68
68
|
MiddlewareEvent.define("bootstrap", (a, b) => a.index - b.index);
|
|
69
69
|
MiddlewareEvent.define("beforeExecute", (a, b) => a.index - b.index);
|
|
70
70
|
MiddlewareEvent.define("afterExecute", (a, b) => b.index - a.index);
|
|
71
71
|
MiddlewareEvent.define("afterHTTPRequest", (a, b) => a.index - b.index);
|
|
72
72
|
MiddlewareEvent.define("beforeHTTPResponse", (a, b) => b.index - a.index);
|
|
73
73
|
|
|
74
|
-
const middlewares =
|
|
74
|
+
const middlewares = MilkioAppOptions.middlewares();
|
|
75
75
|
|
|
76
76
|
for (let index = 0; index < middlewares.length; index++) {
|
|
77
77
|
const middlewareOptions = middlewares[index];
|
|
@@ -87,12 +87,12 @@ export async function createMilkioApp(milkioAppOptions: milkioAppOptions = {}) {
|
|
|
87
87
|
}
|
|
88
88
|
MiddlewareEvent._sort();
|
|
89
89
|
|
|
90
|
-
await MiddlewareEvent.handle("bootstrap", [
|
|
90
|
+
await MiddlewareEvent.handle("bootstrap", [MilkioApp]);
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
console.log(`🧊 Milkio is running on : ${cwd()}`);
|
|
94
94
|
|
|
95
|
-
return
|
|
95
|
+
return MilkioApp;
|
|
96
96
|
}
|
|
97
97
|
|
|
98
98
|
async function _execute<Path extends keyof (typeof schema)["apiMethodsTypeSchema"], Result extends Awaited<ReturnType<(typeof schema)["apiMethodsTypeSchema"][Path]["api"]["action"]>>>(path: Path, params: Parameters<(typeof schema)["apiMethodsTypeSchema"][Path]["api"]["action"]>[0] | string, headersInit: Record<string, string> | Headers = {}, options?: ExecuteOptions): Promise<ExecuteResult<Result>> {
|
|
@@ -148,8 +148,8 @@ async function _executeCore<Path extends keyof (typeof schema)["apiMethodsTypeSc
|
|
|
148
148
|
executeId,
|
|
149
149
|
success: false,
|
|
150
150
|
fail: {
|
|
151
|
-
code: "
|
|
152
|
-
message: failCode["
|
|
151
|
+
code: "NOT_FOUND",
|
|
152
|
+
message: failCode["NOT_FOUND"](),
|
|
153
153
|
data: undefined
|
|
154
154
|
}
|
|
155
155
|
} satisfies ExecuteResult<Result>;
|
|
@@ -257,7 +257,7 @@ export type ExecuteOptions = {
|
|
|
257
257
|
* These are usually only fully implemented when called by an HTTP server
|
|
258
258
|
* During testing or when calling between microservices, some or all of the values may be undefined
|
|
259
259
|
*/
|
|
260
|
-
detail?:
|
|
260
|
+
detail?: MilkioContext["detail"];
|
|
261
261
|
};
|
|
262
262
|
|
|
263
263
|
export type ExecuteCoreOptions = Mixin<
|
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("PARAMS_TYPE_ERROR", {
|
|
9
9
|
path: error.path,
|
|
10
10
|
expected: error.expected,
|
|
11
11
|
value: error.value
|
package/package.json
CHANGED
package/types.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import { type createMilkioApp } from ".";
|
|
4
4
|
import type { failCode } from "../../src/fail-code";
|
|
5
5
|
|
|
6
|
-
export type
|
|
6
|
+
export type MilkioApp = Awaited<ReturnType<typeof createMilkioApp>>;
|
|
7
7
|
|
|
8
8
|
export type ExecuteId = string | "global";
|
|
9
9
|
|
|
@@ -19,7 +19,7 @@ export type Fail<FailCode extends keyof FailEnumerates> = {
|
|
|
19
19
|
data: Parameters<FailEnumerates[FailCode]>[0];
|
|
20
20
|
};
|
|
21
21
|
|
|
22
|
-
export type
|
|
22
|
+
export type MilkioMeta = {
|
|
23
23
|
//
|
|
24
24
|
};
|
|
25
25
|
|
|
@@ -11,14 +11,14 @@ export function hanldeCatchError(error: any, executeId: ExecuteId): ExecuteResul
|
|
|
11
11
|
|
|
12
12
|
if (error.name !== "milkioReject") {
|
|
13
13
|
// If it is not milkioReject, it is considered an internal server error that should not be exposed
|
|
14
|
-
logger.error(`FailCode:
|
|
14
|
+
logger.error(`FailCode: INTERNAL_SERVER_ERROR`);
|
|
15
15
|
|
|
16
16
|
return {
|
|
17
17
|
executeId,
|
|
18
18
|
success: false,
|
|
19
19
|
fail: {
|
|
20
|
-
code: "
|
|
21
|
-
message: failCode["
|
|
20
|
+
code: "INTERNAL_SERVER_ERROR",
|
|
21
|
+
message: failCode["INTERNAL_SERVER_ERROR"](),
|
|
22
22
|
data: undefined
|
|
23
23
|
}
|
|
24
24
|
};
|