milkio 0.2.0 → 0.2.2

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/api-test/index.ts CHANGED
@@ -1,5 +1,3 @@
1
- /* eslint-disable no-constant-condition, @typescript-eslint/no-unsafe-argument, no-console, @typescript-eslint/no-explicit-any */
2
-
3
1
  import schema from "../../../generated/api-schema";
4
2
  import { useLogger, type MilkioApp } from "..";
5
3
 
package/c.ts CHANGED
@@ -1,7 +1,5 @@
1
1
  #!/usr/bin/env bun
2
2
 
3
- /* eslint-disable no-constant-condition, @typescript-eslint/no-misused-promises, no-console, @typescript-eslint/no-explicit-any */
4
-
5
3
  import { argv, exit } from "node:process";
6
4
  import { $ } from "bun";
7
5
 
@@ -1,4 +1,3 @@
1
- /* eslint-disable no-console, @typescript-eslint/no-explicit-any */
2
1
  import type { MilkioApp } from "..";
3
2
 
4
3
  export type CommandOptions = {
@@ -1,4 +1,3 @@
1
- /* eslint-disable no-console */
2
1
  import { loggerPushTags, loggerSubmit, useLogger, runtime, MiddlewareEvent } from "..";
3
2
  import type { ExecuteId, MilkioApp, Mixin } from "..";
4
3
  import { hanldeCatchError } from "../utils/handle-catch-error";
@@ -22,7 +21,7 @@ export type ExecuteHttpServerOptions = {
22
21
  };
23
22
 
24
23
  export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOptions = {}) {
25
- const fetch = async (request: MilkioHTTPRequest) => {
24
+ const fetch = async (request: MilkioHttpRequest) => {
26
25
  const fullurl = new URL(request.request.url, `http://${request.request.headers.get("host") ?? "localhost"}`);
27
26
  const executeId = (options?.executeIdGenerator ? await options.executeIdGenerator(request.request) : createUlid()) as ExecuteId;
28
27
  runtime.execute.executeIds.add(executeId);
@@ -40,7 +39,7 @@ export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOpti
40
39
  timein: new Date().getTime(),
41
40
  });
42
41
 
43
- const response: MilkioHTTPResponse = {
42
+ const response: MilkioHttpResponse = {
44
43
  body: "",
45
44
  status: 200,
46
45
  headers: {
@@ -74,6 +73,15 @@ export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOpti
74
73
 
75
74
  let pathstr = path.join("/") as keyof (typeof schema)["apiMethodsSchema"];
76
75
 
76
+ const detail = {
77
+ path: pathstr,
78
+ ip,
79
+ executeId,
80
+ fullurl,
81
+ request: request.request,
82
+ response,
83
+ };
84
+
77
85
  // Special processing: do not run middleware when encountering 404 and return quickly
78
86
  if (!(pathstr in schema.apiMethodsSchema) || pathstr.startsWith("$/")) {
79
87
  // @ts-ignore
@@ -83,19 +91,22 @@ export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOpti
83
91
  loggerPushTags(executeId, {
84
92
  body: rawbody || "no body",
85
93
  });
86
- if (!response.body) response.body = `{"executeId":"${executeId}","success":false,"fail":{"code":"NOT_FOUND","message":${JSON.stringify(failCode.NOT_FOUND())}}}`;
94
+
95
+ if (!detail.response.body) detail.response.body = `{"executeId":"${executeId}","success":false,"fail":{"code":"NOT_FOUND","message":${JSON.stringify(failCode.NOT_FOUND())}}}`;
96
+ await MiddlewareEvent.handle("httpNotFound", [detail]);
87
97
 
88
98
  loggerPushTags(executeId, {
89
- status: response.status,
90
- responseHeaders: response.headers,
99
+ status: detail.response.status,
100
+ responseHeaders: detail.response.headers,
91
101
  timeout: new Date().getTime(),
92
102
  });
93
103
 
94
104
  await loggerSubmit(executeId);
95
105
  runtime.execute.executeIds.delete(executeId);
96
106
 
97
- return new Response(response.body, response);
107
+ return new Response(detail.response.body, detail.response);
98
108
  }
109
+
99
110
  pathstr = redirectPath as typeof pathstr;
100
111
  }
101
112
 
@@ -103,18 +114,9 @@ export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOpti
103
114
  path: pathstr,
104
115
  });
105
116
 
106
- const detail = {
107
- path: pathstr,
108
- ip,
109
- executeId,
110
- fullurl,
111
- request: request.request,
112
- response,
113
- };
114
-
115
117
  // execute api
116
118
  // after request middleware
117
- await MiddlewareEvent.handle("afterHTTPRequest", [headers, detail]);
119
+ await MiddlewareEvent.handle("afterHttpRequest", [headers, detail]);
118
120
 
119
121
  const rawbody = await request.request.text();
120
122
  loggerPushTags(executeId, {
@@ -149,17 +151,19 @@ export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOpti
149
151
 
150
152
  // @ts-ignore
151
153
  // eslint-disable-next-line @typescript-eslint/no-confusing-void-expression, @typescript-eslint/no-explicit-any
152
- if (!response.body) response.body = result;
154
+ if (!detail.response.body) detail.response.body = result;
153
155
 
154
156
  // before response middleware
155
157
  const middlewareResponse = {
156
- value: response.body,
158
+ value: detail.response.body,
157
159
  };
158
- await MiddlewareEvent.handle("beforeHTTPResponse", [middlewareResponse, detail]);
160
+ await MiddlewareEvent.handle("beforeHttpResponse", [middlewareResponse, detail]);
159
161
 
160
- if (!response.body) response.body = middlewareResponse.value;
162
+ if (!detail.response.headers["Cache-Control"]) detail.response.headers["Cache-Control"] = "no-cache";
163
+ if (!detail.response.body) detail.response.body = middlewareResponse.value;
161
164
  } catch (error) {
162
165
  const result = hanldeCatchError(error, executeId);
166
+ if (!response.headers["Cache-Control"]) response.headers["Cache-Control"] = "no-cache";
163
167
  if (!response.body) response.body = TSON.stringify(result);
164
168
  }
165
169
 
@@ -180,11 +184,11 @@ export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOpti
180
184
  return fetch;
181
185
  }
182
186
 
183
- export type MilkioHTTPRequest = {
187
+ export type MilkioHttpRequest = {
184
188
  request: Request;
185
189
  };
186
190
 
187
- export type MilkioHTTPResponse = Mixin<
191
+ export type MilkioHttpResponse = Mixin<
188
192
  ResponseInit,
189
193
  {
190
194
  body: string | BodyInit;
package/kernel/context.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { ExecuteId, Logger, MilkioHTTPResponse } from "..";
1
+ import type { ExecuteId, Logger, MilkioHttpResponse } from "..";
2
2
 
3
3
  export type MilkioContext = {
4
4
  path: string;
@@ -7,17 +7,17 @@ export type MilkioContext = {
7
7
  logger: Logger;
8
8
  /**
9
9
  * Additional information about the request
10
- * These are usually only fully implemented when called by an HTTP server
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: Partial<FrameworkHttpDetail>;
14
14
  };
15
15
 
16
- export type FrameworkHTTPDetail = {
16
+ export type FrameworkHttpDetail = {
17
17
  path: string;
18
18
  executeId: ExecuteId;
19
19
  fullurl: URL;
20
20
  ip: string;
21
21
  request: Request;
22
- response: MilkioHTTPResponse;
22
+ response: MilkioHttpResponse;
23
23
  };
package/kernel/logger.ts CHANGED
@@ -1,5 +1,3 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
-
3
1
  import { runtime, type ExecuteId } from "..";
4
2
  import { loggerOptions } from "../../../src/logger";
5
3
 
@@ -1,20 +1,21 @@
1
- /* eslint-disable @typescript-eslint/no-unnecessary-type-constraint, @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument */
2
-
3
1
  import type { Context } from "../../../src/context";
4
- import type { FrameworkHTTPDetail } from "./context";
2
+ import type { FrameworkHttpDetail } from "./context";
5
3
  import type { MilkioApp } from "milkio";
6
4
 
7
5
  export type BootstrapMiddleware = (milkio: MilkioApp) => Promise<void> | void;
8
6
  export type BeforeExecuteMiddleware = (context: Context) => Promise<void> | void;
9
7
  export type AfterExecuteMiddleware = (context: Context, response: { value: unknown }) => Promise<void> | void;
10
- export type AfterHTTPRequestMiddleware = (headers: Headers, detail: FrameworkHTTPDetail) => Promise<void> | void;
11
- export type BeforeHTTPResponseMiddleware = (response: { value: string }, detail: FrameworkHTTPDetail) => Promise<void> | void;
8
+ export type AfterHttpRequestMiddleware = (headers: Headers, detail: FrameworkHttpDetail) => Promise<void> | void;
9
+ export type BeforeHttpResponseMiddleware = (response: { value: string }, detail: FrameworkHttpDetail) => Promise<void> | void;
10
+ export type HttpNotFoundMiddleware = (detail: FrameworkHttpDetail) => Promise<void> | void;
11
+
12
12
  export type MiddlewareOptions = {
13
13
  bootstrap?: BootstrapMiddleware;
14
14
  beforeExecute?: BeforeExecuteMiddleware;
15
15
  afterExecute?: AfterExecuteMiddleware;
16
- afterHTTPRequest?: AfterHTTPRequestMiddleware;
17
- beforeHTTPResponse?: BeforeHTTPResponseMiddleware;
16
+ afterHttpRequest?: AfterHttpRequestMiddleware;
17
+ beforeHttpResponse?: BeforeHttpResponseMiddleware;
18
+ httpNotFound?: HttpNotFoundMiddleware;
18
19
  } & Record<string, (...args: Array<any>) => Promise<void> | void>;
19
20
  export type MiddlewareFn = (...args: Array<any>) => Promise<void> | void;
20
21
  export type MiddlewareT<T extends MiddlewareFn = MiddlewareFn> = { id: string; index: number; middleware: T };
package/kernel/milkio.ts CHANGED
@@ -1,4 +1,3 @@
1
- /* eslint-disable no-console, @typescript-eslint/no-invalid-void-type, @typescript-eslint/await-thenable, @typescript-eslint/ban-types, @typescript-eslint/no-explicit-any */
2
1
  import { type MiddlewareOptions, _middlewares, MiddlewareEvent } from "./middleware";
3
2
  import schema from "../../../generated/api-schema";
4
3
  import type { Context } from "../../../src/context";
@@ -9,7 +8,6 @@ import { type Mixin, type ExecuteId, type Fail, type FailEnumerates, loggerPushT
9
8
  import { hanldeCatchError } from "../utils/handle-catch-error";
10
9
  import { createUlid } from "../utils/create-ulid";
11
10
  import { _validate } from "./validate";
12
- import { exit } from "node:process";
13
11
 
14
12
  export type MilkioAppOptions = {
15
13
  /**
@@ -62,8 +60,9 @@ export async function createMilkioApp(MilkioAppOptions: MilkioAppOptions = {}) {
62
60
  MiddlewareEvent.define("bootstrap", (a, b) => a.index - b.index);
63
61
  MiddlewareEvent.define("beforeExecute", (a, b) => a.index - b.index);
64
62
  MiddlewareEvent.define("afterExecute", (a, b) => b.index - a.index);
65
- MiddlewareEvent.define("afterHTTPRequest", (a, b) => a.index - b.index);
66
- MiddlewareEvent.define("beforeHTTPResponse", (a, b) => b.index - a.index);
63
+ MiddlewareEvent.define("afterHttpRequest", (a, b) => a.index - b.index);
64
+ MiddlewareEvent.define("beforeHttpResponse", (a, b) => b.index - a.index);
65
+ MiddlewareEvent.define("httpNotFound", (a, b) => a.index - b.index);
67
66
 
68
67
  const middlewares = MilkioAppOptions.middlewares();
69
68
 
@@ -272,7 +271,7 @@ export type ExecuteOptions = {
272
271
  executeId?: string;
273
272
  /**
274
273
  * Additional information about the request
275
- * These are usually only fully implemented when called by an HTTP server
274
+ * These are usually only fully implemented when called by an Http server
276
275
  * During testing or when calling between microservices, some or all of the values may be undefined
277
276
  */
278
277
  detail?: MilkioContext["detail"];
@@ -1,5 +1,3 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
-
3
1
  import type { IValidation } from "typia";
4
2
  import { reject } from "../kernel/fail";
5
3
 
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "milkio",
3
3
  "type": "module",
4
4
  "module": "index.ts",
5
- "version": "0.2.0",
5
+ "version": "0.2.2",
6
6
  "peerDependencies": {
7
7
  "typescript": "^5.4.2"
8
8
  },
@@ -1,5 +1,3 @@
1
- /* eslint-disable no-console, @typescript-eslint/no-dynamic-delete */
2
-
3
1
  import { $ } from "bun";
4
2
  import { join } from "node:path";
5
3
  import { cwd } from "node:process";
@@ -248,6 +246,7 @@ export default async () => {
248
246
  minify: true,
249
247
  });
250
248
  await copyFile(join(cwd(), "src", "fail-code.ts"), join(cwd(), "packages", "client", "project", "src", "fail-code.ts"));
249
+ await $`bun i`.cwd(join(cwd(), "packages", "client"));
251
250
  console.timeEnd(`Client Stage`);
252
251
  console.log(``);
253
252
 
@@ -1,5 +1,3 @@
1
- /* eslint-disable no-console */
2
-
3
1
  import ejs from "ejs";
4
2
  import { join } from "node:path";
5
3
  import { existsSync, mkdirSync } from "node:fs";
package/types.ts CHANGED
@@ -1,50 +1,48 @@
1
- /* eslint-disable @typescript-eslint/ban-types, @typescript-eslint/no-explicit-any */
1
+ import type { createMilkioApp } from ".";
2
+ import type { failCode } from "../../src/fail-code";
2
3
 
3
- import { type createMilkioApp } from "."
4
- import type { failCode } from "../../src/fail-code"
4
+ export type MilkioApp = Awaited<ReturnType<typeof createMilkioApp>>;
5
5
 
6
- export type MilkioApp = Awaited<ReturnType<typeof createMilkioApp>>
6
+ export type ExecuteId = string | "global";
7
7
 
8
- export type ExecuteId = string | "global"
8
+ export type FailEnumerates = typeof failCode;
9
9
 
10
- export type FailEnumerates = typeof failCode
10
+ export type HttpRequest = Request;
11
11
 
12
- export type HTTPRequest = Request
13
-
14
- export type HTTPResponse = Override<ResponseInit & { body: string | null | undefined }, { headers: NonNullable<ResponseInit["headers"]> }>
12
+ export type HttpResponse = Override<ResponseInit & { body: string | null | undefined }, { headers: NonNullable<ResponseInit["headers"]> }>;
15
13
 
16
14
  export type Fail<FailCode extends keyof FailEnumerates> = {
17
- code: FailCode
18
- message: string
19
- data: Parameters<FailEnumerates[FailCode]>[0]
20
- }
15
+ code: FailCode;
16
+ message: string;
17
+ data: Parameters<FailEnumerates[FailCode]>[0];
18
+ };
21
19
 
22
20
  export type MilkioMeta = {
23
- //
24
- }
21
+ //
22
+ };
25
23
 
26
- export type Cookbook = Record<string, CookbookItem>
24
+ export type Cookbook = Record<string, CookbookItem>;
27
25
 
28
26
  export type CookbookItem = {
29
- title?: string;
30
- desc?: string;
31
- params: string;
32
- cases: Array<{
33
- name: string;
34
- handler: string;
35
- }>
36
- }
27
+ title?: string;
28
+ desc?: string;
29
+ params: string;
30
+ cases: Array<{
31
+ name: string;
32
+ handler: string;
33
+ }>;
34
+ };
37
35
 
38
- export type Override<P, S> = Omit<P, keyof S> & S
36
+ export type Override<P, S> = Omit<P, keyof S> & S;
39
37
 
40
- export type Mixin<T, U> = U & Omit<T, keyof U>
38
+ export type Mixin<T, U> = U & Omit<T, keyof U>;
41
39
 
42
40
  export type MilkioConfig = {
43
- generate?: {
44
- significant?: Array<string>;
45
- insignificant?: Array<string>;
46
- },
47
- menubar?: {
48
- commands?: Array<{ name?: string, script?: string, icon?: string }>;
49
- }
50
- };
41
+ generate?: {
42
+ significant?: Array<string>;
43
+ insignificant?: Array<string>;
44
+ };
45
+ menubar?: {
46
+ commands?: Array<{ name?: string; script?: string; icon?: string }>;
47
+ };
48
+ };