milkio 1.0.0-alpha.25 → 1.0.0-alpha.27

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.
@@ -8,8 +8,10 @@ export interface $rejectCode {
8
8
  REQUEST_TIMEOUT: { timeout: number; message: string };
9
9
  NOT_FOUND: { path: string };
10
10
  PARAMS_TYPE_INCORRECT: { path: string; expected: string; value: any; message: string } | null;
11
+ RESULTS_TYPE_INCORRECT: { path: string; expected: string; value: any; message: string } | null;
11
12
  UNACCEPTABLE: { expected: string; message: string };
12
13
  PARAMS_TYPE_NOT_SUPPORTED: { expected: string };
14
+ RESULTS_TYPE_NOT_SUPPORTED: { expected: string };
13
15
  INTERNAL_SERVER_ERROR: undefined;
14
16
  }
15
17
 
package/execute/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type IValidation } from "typia";
1
+ import typia, { type IValidation } from "typia";
2
2
  import { TSON } from "@southern-aurora/tson";
3
3
  import { reject, type $context, type $meta, type Logger, type Results, type GeneratedInit, getConfig } from "..";
4
4
  import { headersToJSON } from "../utils/headers-to-json";
@@ -24,7 +24,7 @@ export const __initExecuter = (generated: GeneratedInit, runtime: any) => {
24
24
  paramsType: "string";
25
25
  }
26
26
  ),
27
- ): Promise<{ executeId: string; headers: Headers; params: Record<any, unknown>; results: Results<any>; context: $context; meta: Readonly<$meta>; type: "action" | "stream" }> => {
27
+ ): Promise<{ executeId: string; headers: Headers; params: Record<any, unknown>; results: Results<any>; context: $context; meta: Readonly<$meta>; type: "action" | "stream"; emptyResult: boolean; resultsTypeSafety: boolean }> => {
28
28
  const executeId: string = options.createdExecuteId;
29
29
  env = env ?? {};
30
30
  envMode = envMode ?? "production";
@@ -73,7 +73,7 @@ export const __initExecuter = (generated: GeneratedInit, runtime: any) => {
73
73
  getConfig: (namespace: string) => getConfig(generated, env, envMode, namespace),
74
74
  call: (module: any, options: any) => __call(context, module, options),
75
75
  } as unknown as $context;
76
- const results: Results<unknown> = { value: undefined };
76
+ const results: Results<any> = { value: undefined };
77
77
 
78
78
  if (runtime.develop) {
79
79
  options.createdLogger.request(`headers - ${TSON.stringify(headers.toJSON())}`, `\nparams - ${TSON.stringify(params)}`);
@@ -82,7 +82,7 @@ export const __initExecuter = (generated: GeneratedInit, runtime: any) => {
82
82
  const module = await routeSchema.module();
83
83
  let meta = (module.meta ? module.meta : {}) as unknown as Readonly<$meta>;
84
84
 
85
- if (!meta.typeSafety || meta.typeSafety === true) {
85
+ if (meta.typeSafety === undefined || meta.typeSafety === true) {
86
86
  const validation = routeSchema.validateParams(params) as IValidation<any>;
87
87
  if (!validation.success) throw reject("PARAMS_TYPE_INCORRECT", { ...validation.errors[0], message: `The value '${validation.errors[0].path}' is '${validation.errors[0].value}', which does not meet '${validation.errors[0].expected}' requirements.` });
88
88
  }
@@ -90,13 +90,25 @@ export const __initExecuter = (generated: GeneratedInit, runtime: any) => {
90
90
  await runtime.emit("milkio:executeBefore", { executeId: options.createdExecuteId, logger: options.createdLogger, path: options.path, context });
91
91
 
92
92
  results.value = await module.default.handler(context, params);
93
- if (results.value === undefined || results.value === null || results.value === "") results.value = {};
94
- else if (Array.isArray(results.value)) throw reject("FAIL", "The return type of the handler must be an object, which is currently an array.");
93
+
94
+ let resultsTypeSafety = false;
95
+ if (results?.value?.$milkioType === "type-safety") {
96
+ resultsTypeSafety = true;
97
+ const validation = routeSchema.validateResults(results.value.value) as IValidation<any>;
98
+ if (!validation.success) throw reject("RESULTS_TYPE_INCORRECT", { ...validation.errors[0], message: `The value '${validation.errors[0].path}' is '${validation.errors[0].value}', which does not meet '${validation.errors[0].expected}' requirements.` });
99
+ results.value = results.value.value;
100
+ }
101
+
102
+ let emptyResult = false;
103
+ if (results.value === undefined || results.value === null || results.value === "") {
104
+ emptyResult = true;
105
+ results.value = {};
106
+ } else if (Array.isArray(results.value)) throw reject("FAIL", "The return type of the handler must be an object, which is currently an array.");
95
107
  else if (typeof results.value !== "object") throw reject("FAIL", "The return type of the handler must be an object, which is currently a primitive type.");
96
108
 
97
109
  await runtime.emit("milkio:executeAfter", { executeId: options.createdExecuteId, logger: options.createdLogger, path: options.path, context, results });
98
110
 
99
- return { executeId, headers, params, results, context, meta, type: module.$milkioType };
111
+ return { executeId, headers, params, results, context, meta, type: module.$milkioType, emptyResult, resultsTypeSafety };
100
112
  };
101
113
 
102
114
  const __call = async (context: $context, module: { default: any }, params?: any): Promise<any> => {
package/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from "./type-safety";
1
2
  export * from "@southern-aurora/tson";
2
3
  export * from "./types";
3
4
  export * from "./config";
package/listener/index.ts CHANGED
@@ -108,7 +108,15 @@ export const __initListener = (generated: GeneratedInit, runtime: any, executer:
108
108
  paramsType: "string",
109
109
  });
110
110
 
111
- if (response.body === "" && executed.results.value !== undefined) response.body = TSON.stringify({ success: true, data: executed.results.value, executeId } satisfies MilkioResponseSuccess<any>);
111
+ if (response.body === "" && executed.results.value !== undefined) {
112
+ if (executed.emptyResult) {
113
+ response.body = `{"data":{},"executeId":"${executeId}","success":true}`;
114
+ } else if (executed.resultsTypeSafety) {
115
+ response.body = `{"data":${routeSchema.resultsToJSON(executed.results.value)},"executeId":"${executeId}","success":true}`;
116
+ } else {
117
+ response.body = `{"data":${TSON.stringify(executed.results.value)},"executeId":"${executeId}","success":true}`;
118
+ }
119
+ }
112
120
 
113
121
  await runtime.emit("milkio:httpResponse", { executeId, logger, path: http.path.string as string, http, context: executed.context });
114
122
 
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "milkio",
3
3
  "type": "module",
4
4
  "module": "index.ts",
5
- "version": "1.0.0-alpha.25",
5
+ "version": "1.0.0-alpha.27",
6
6
  "peerDependencies": {
7
7
  "typescript": "^5.4.2"
8
8
  },
@@ -13,7 +13,7 @@
13
13
  "@southern-aurora/tson": "*",
14
14
  "chalk": "^5.3.0",
15
15
  "date-fns": "^4.1.0",
16
- "typia": "6.9.0"
16
+ "typia": "6.11.3"
17
17
  },
18
18
  "devDependencies": {
19
19
  "@types/bun": "latest",
@@ -0,0 +1,16 @@
1
+ export type TypeSafety<Value extends Record<any, any>> = (value: Value) => TypeSafetyValue;
2
+
3
+ export type TypeSafetyValue = {
4
+ type: <Type extends Record<any, any>>() => Type;
5
+ };
6
+
7
+ export type TypeSafetyType<Type extends Record<any, any>> = {
8
+ $milkioType: "type-safety";
9
+ value: Type;
10
+ };
11
+
12
+ export function typeSafety<Value extends Record<any, any>>(value: Value): TypeSafetyValue {
13
+ return {
14
+ type: () => ({ $milkioType: "type-safety", value }),
15
+ } as any;
16
+ }
package/types/index.ts CHANGED
@@ -10,6 +10,8 @@ export type Mixin<T, U> = U & Omit<T, keyof U>;
10
10
 
11
11
  export type GeneratorGeneric<T> = T extends AsyncGenerator<infer I> ? I : never;
12
12
 
13
+ export type DBSelect<T, K> = Exclude<T, K>;
14
+
13
15
  export type DBCreate<T, KO extends keyof T | never = never, TO extends Omit<T, KO> = Omit<T, KO>> = {
14
16
  [K in keyof TO]?: TO[K] extends null ? undefined : Exclude<TO[K], null>;
15
17
  };