milkio 1.0.0-alpha.4 → 1.0.0-alpha.6

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/context/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type MilkioHttpRequest, type MilkioHttpResponse, type $types, type Logger, Steps } from "..";
1
+ import { type MilkioHttpRequest, type MilkioHttpResponse, type $types, type Logger, type Steps, type Execute } from "..";
2
2
 
3
3
  export interface $context {
4
4
  executeId: string;
@@ -6,6 +6,7 @@ export interface $context {
6
6
  logger: Logger;
7
7
  http?: ContextHttp<Record<any, any>>;
8
8
  step: Steps<{}>["step"];
9
+ execute: Execute;
9
10
  }
10
11
 
11
12
  export type ContextHttp<ParamsParsed = any> = {
package/execute/index.ts CHANGED
@@ -13,6 +13,7 @@ export const __initExecuter = <MilkioRuntime extends MilkioRuntimeInit<MilkioRun
13
13
  path: string;
14
14
  headers: Record<string, string> | Headers;
15
15
  mixinContext: Record<any, any> | undefined;
16
+ extendContext?: $context;
16
17
  } & (
17
18
  | {
18
19
  params: Record<any, any>;
@@ -60,13 +61,16 @@ export const __initExecuter = <MilkioRuntime extends MilkioRuntimeInit<MilkioRun
60
61
  params = { ...paramsRand, ...params };
61
62
  }
62
63
  if (options.mixinContext?.http?.params?.string) options.mixinContext.http.params.parsed = params; // listen でパースしたパラメータを渡す
63
- const context = {
64
- ...(options.mixinContext ? options.mixinContext : {}),
65
- path: options.path,
66
- logger: options.createdLogger,
67
- executeId: options.createdExecuteId,
68
- step: createStep(),
69
- } as unknown as $context;
64
+ const context =
65
+ options.extendContext ??
66
+ ({
67
+ ...(options.mixinContext ? options.mixinContext : {}),
68
+ path: options.path,
69
+ logger: options.createdLogger,
70
+ executeId: options.createdExecuteId,
71
+ execute: (path: any, options: any) => runtime.runtime.app.execute(path, options, context),
72
+ step: createStep(),
73
+ } as unknown as $context);
70
74
  const results: Results<unknown> = { value: undefined };
71
75
 
72
76
  if (runtime.develop) {
@@ -81,20 +85,21 @@ export const __initExecuter = <MilkioRuntime extends MilkioRuntimeInit<MilkioRun
81
85
  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.` });
82
86
  }
83
87
 
84
- await runtime.emit("milkio:executeBefore", { executeId: options.createdExecuteId, logger: options.createdLogger, path: options.path, context });
88
+ if (!options.extendContext) await runtime.emit("milkio:executeBefore", { executeId: options.createdExecuteId, logger: options.createdLogger, path: options.path, context });
85
89
 
86
90
  results.value = await module.default.handler(context, params);
87
91
 
88
- await runtime.emit("milkio:executeAfter", { executeId: options.createdExecuteId, logger: options.createdLogger, path: options.path, context, results });
92
+ if (!options.extendContext) await runtime.emit("milkio:executeAfter", { executeId: options.createdExecuteId, logger: options.createdLogger, path: options.path, context, results });
89
93
 
90
94
  return { executeId, headers, params, results, context, meta, type: module.$milkioType };
91
95
  };
92
96
 
93
- const execute = async (path: string, options?: ExecuteOptions): Promise<any> => {
97
+ const execute = async (path: string, options?: ExecuteOptions, context?: $context): Promise<any> => {
94
98
  if (!options) options = {};
95
- const executeId = createId();
96
- const logger = createLogger(runtime, path, executeId);
97
- runtime.runtime.request.set(executeId, { logger: logger });
99
+
100
+ const executeId = context?.executeId ?? createId();
101
+ const logger = context?.logger ?? createLogger(runtime, path, executeId);
102
+ if (!context?.logger) runtime.runtime.request.set(executeId, { logger: logger });
98
103
 
99
104
  try {
100
105
  const routeSchema = generated.routeSchema.routes.get(path);
@@ -110,6 +115,7 @@ export const __initExecuter = <MilkioRuntime extends MilkioRuntimeInit<MilkioRun
110
115
  mixinContext: {},
111
116
  params: options.params ?? {},
112
117
  paramsType: "raw",
118
+ extendContext: context,
113
119
  });
114
120
 
115
121
  if (routeSchema.type === "stream") {
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.4",
5
+ "version": "1.0.0-alpha.6",
6
6
  "peerDependencies": {
7
7
  "typescript": "^5.4.2"
8
8
  },
package/types/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type $rejectCode } from "..";
1
+ import { type $context, type $rejectCode } from "..";
2
2
 
3
3
  export interface $types {
4
4
  [key: string]: Record<any, any>;
@@ -10,7 +10,19 @@ 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 ToEmptyObject<T> = T extends undefined | null | never ? {} : T extends object ? T : {};
13
+ export type DBCreate<T, K extends keyof T | never = never> = Omit<
14
+ {
15
+ [K in keyof T]: T[K] extends null ? undefined : T[K];
16
+ },
17
+ K
18
+ >;
19
+
20
+ export type DBUpdate<T, K extends keyof T | never = never> = Omit<
21
+ Partial<{
22
+ [K in keyof T]: T[K] extends null ? undefined : T[K];
23
+ }>,
24
+ K
25
+ >;
14
26
 
15
27
  export type ExecuteId = string | "global";
16
28
 
package/world/index.ts CHANGED
@@ -25,6 +25,7 @@ export type MilkioRuntimeInit<T extends MilkioInit> = Mixin<
25
25
  executeId: (request: Request) => string | Promise<string>;
26
26
  runtime: {
27
27
  request: Map<ExecuteId, { logger: Logger }>;
28
+ app: any;
28
29
  };
29
30
  on: Awaited<ReturnType<typeof __initEventManager>>["on"];
30
31
  off: Awaited<ReturnType<typeof __initEventManager>>["off"];
@@ -71,6 +72,8 @@ export const createWorld = async <MilkioOptions extends MilkioInit>(generated: G
71
72
  listener,
72
73
  };
73
74
 
75
+ runtime.app = app;
76
+
74
77
  return app as MilkioWorld<MilkioOptions>;
75
78
  };
76
79