milkio 0.7.0-alpha.3 → 0.7.0-alpha.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/kernel/context.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { ExecuteId, Logger, MilkioHttpResponse } from "..";
1
+ import type { ExecuteId, Logger, MilkioHttpResponse, Mixin, ToEmptyObject } from "..";
2
2
 
3
3
  export type MilkioContext = {
4
4
  path: string;
@@ -10,7 +10,8 @@ export type MilkioContext = {
10
10
  * These are usually only fully implemented when called by an Http server
11
11
  * During testing or when calling between microservices, some or all of the values may be undefined
12
12
  */
13
- detail: Partial<FrameworkHttpDetail>;
13
+ detail: FrameworkHttpDetail;
14
+ step: Steps<{}>['step'];
14
15
  };
15
16
 
16
17
  export type FrameworkHttpDetail = {
@@ -21,3 +22,28 @@ export type FrameworkHttpDetail = {
21
22
  request: Request;
22
23
  response: MilkioHttpResponse;
23
24
  };
25
+
26
+ export type Steps<StageT extends Record<any, any>> = {
27
+ step: StepFunction<StageT>,
28
+ run: <HandlerT extends (stage: StageT) => Record<any, any> | Promise<Record<any, any>>>(handler: HandlerT) => Promise<Awaited<ReturnType<HandlerT>>>,
29
+ }
30
+
31
+ type StepFunction<StageT extends Record<any, any>> = <HandlerT extends ((stage: Readonly<StageT>) => Record<any, any> | Promise<Record<any, any>>) >(handler: HandlerT) => Steps<Mixin<StageT, ToEmptyObject<Awaited<ReturnType<HandlerT>>>>>
32
+
33
+ export const createStep = () => {
34
+ const stepController = {
35
+ _steps: [] as Array<(stage: any) => Promise<any>>,
36
+ step(handler: (stage: any) => Promise<any>) {
37
+ this._steps.push(handler);
38
+ return this;
39
+ },
40
+ async run(handler: any) {
41
+ let stage = {};
42
+ for (const step of this._steps) {
43
+ stage = { ...stage, ...(await step(stage)) }
44
+ }
45
+ return await handler(stage);
46
+ }
47
+ }
48
+ return stepController.step as any as Steps<{}>['step'];
49
+ };
package/kernel/execute.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import type { Context } from "../../../src/context";
2
2
  import { failCode } from "../../../src/fail-code";
3
3
  import schema from "../../../generated/api-schema";
4
- import { type ExecuteId, type ExecuteOptions, type ExecuteResult, createUlid, useLogger, runtime, loggerPushTags, headerToPlainObject, loggerSubmit, type ExecuteCoreOptions, TSON, MiddlewareEvent, reject, _validate, ExecuteStreamResult } from "..";
4
+ import { type ExecuteId, type ExecuteOptions, type ExecuteResult, createUlid, useLogger, runtime, loggerPushTags, headerToPlainObject, loggerSubmit, type ExecuteCoreOptions, TSON, MiddlewareEvent, reject, _validate, ExecuteStreamResult, createStep } from "..";
5
5
  import { handleCatchError } from "../utils/handle-catch-error";
6
6
 
7
7
  const apis = new Map<string, any>();
@@ -55,7 +55,8 @@ export async function _call(
55
55
  path: path as string,
56
56
  headers,
57
57
  logger: options.logger,
58
- detail: options?.detail ?? {},
58
+ detail: options?.detail ?? {} as any,
59
+ step: createStep(),
59
60
  };
60
61
 
61
62
  let result: { value: unknown };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "milkio",
3
3
  "type": "module",
4
4
  "module": "index.ts",
5
- "version": "0.7.0-alpha.3",
5
+ "version": "0.7.0-alpha.5",
6
6
  "peerDependencies": {
7
7
  "typescript": "^5.4.2"
8
8
  },
@@ -237,9 +237,11 @@ export default async () => {
237
237
 
238
238
  console.time(`Client Stage`);
239
239
  try {
240
- if (process.platform !== "win32") await $`bun run ./node_modules/typescript/bin/tsc --outDir './packages/client/project'`.quiet();
241
- else await $`powershell.exe -command "bun run ./node_modules/typescript/bin/tsc --outDir './packages/client/project'"`.quiet();
242
- } catch (error) { }
240
+ if (process.platform !== "win32") await $`bun run ./node_modules/typescript/bin/tsc --outDir './packages/client/project'`;
241
+ else await $`powershell.exe -command "bun run ./node_modules/typescript/bin/tsc --outDir './packages/client/project'"`;
242
+ } catch (error) {
243
+ console.log(error);
244
+ }
243
245
  await Bun.build({
244
246
  entrypoints: ["./packages/client/index.ts"],
245
247
  outdir: "./packages/client/dist",
package/types.ts CHANGED
@@ -93,3 +93,10 @@ export type ExecuteCoreOptions = Mixin<
93
93
  >;
94
94
 
95
95
  export type MilkioEvent<Result> = Awaited<GeneratorGeneric<ExecuteResultSuccess<Result>["data"] extends { $type: any } ? ExecuteResultSuccess<Result>["data"]["$type"] : never>>;
96
+
97
+
98
+ export type ToEmptyObject<T> = T extends undefined | null | never
99
+ ? {}
100
+ : T extends object
101
+ ? T
102
+ : {};
@@ -5,7 +5,7 @@ import { configMilkio } from "../../../src/config/milkio";
5
5
  export function handleCatchError(error: any, executeId: ExecuteId): ExecuteResult<any> {
6
6
  const logger = useLogger(executeId);
7
7
 
8
- if (configMilkio.debug === true) {
8
+ if (configMilkio.debug !== false) {
9
9
  logger.error(`Error Data: ${JSON.stringify(error)}`);
10
10
  if (error.stack) logger.error("Error Stack: ", error.stack);
11
11
  else logger.error("Error Stack: ", error);
@@ -14,7 +14,7 @@ export function handleCatchError(error: any, executeId: ExecuteId): ExecuteResul
14
14
  let result: ExecuteResult<any>;
15
15
 
16
16
  if (error.name !== "MilkioReject") {
17
- if (configMilkio.debug === true) {
17
+ if (configMilkio.debug !== false) {
18
18
  // If it is not MilkioReject, it is considered an internal server error that should not be exposed
19
19
  logger.error(`FailCode: INTERNAL_SERVER_ERROR`);
20
20
  }
@@ -29,7 +29,7 @@ export function handleCatchError(error: any, executeId: ExecuteId): ExecuteResul
29
29
  },
30
30
  };
31
31
  } else {
32
- if (configMilkio.debug === true) {
32
+ if (configMilkio.debug !== false) {
33
33
  logger.error(`FailCode: ${error.code}`);
34
34
  }
35
35