milkio 0.0.30 → 0.0.32

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/c.ts CHANGED
@@ -3,7 +3,6 @@
3
3
  /* eslint-disable no-constant-condition, @typescript-eslint/no-misused-promises, no-console, @typescript-eslint/no-explicit-any */
4
4
 
5
5
  import { argv, exit } from "node:process"
6
- import process from "process"
7
6
  import { $ } from "bun"
8
7
 
9
8
  const method = argv[2] as keyof typeof commands
@@ -0,0 +1,46 @@
1
+ /* eslint-disable no-console, @typescript-eslint/no-explicit-any */
2
+ import type { MilkioApp } from ".."
3
+
4
+ export type CommandOptions = {
5
+ notFoundHandler?: (event: { name: string, path: string, commands: Array<string>, options: Record<string, string | true> }) => void | Promise<void>;
6
+ }
7
+
8
+ export function defineCommandHandler(app: MilkioApp, options: CommandOptions = {}) {
9
+ const call = async (argv: Array<string>) => {
10
+ const params = {
11
+ path: 'default',
12
+ commands: [] as Array<string>,
13
+ options: {} as Record<string, string | true>
14
+ }
15
+ for (const v of argv.slice(3)) {
16
+ if (v.startsWith("--") && v.includes('=')) {
17
+ const vSplited = v.split("=")
18
+ params.options[vSplited[0].slice(2)] = vSplited.slice(1, vSplited.length).join("=")
19
+ } else if (v.startsWith("--")) {
20
+ params.options[v.slice(2)] = true
21
+ } else if (v.startsWith("-") && v.includes('=')) {
22
+ const vSplited = v.split("=")
23
+ params.options[vSplited[0].slice(1)] = vSplited.slice(1, vSplited.length).join("=")
24
+ } else if (v.startsWith("-")) {
25
+ params.options[v.slice(1)] = true
26
+ } else {
27
+ params.commands.push(v)
28
+ }
29
+ }
30
+ if (argv.length === 2) params.path = `$/default`
31
+ else params.path = `$/${argv[2]}`
32
+
33
+ // @ts-ignore
34
+ const result = await app.execute(params.path as any, params)
35
+ if (!result.success) {
36
+ if (result.fail.code === 'NOT_FOUND') {
37
+ if (options.notFoundHandler) await options.notFoundHandler({ ...params, name: argv.length === 2 ? 'default' : argv[2] })
38
+ return
39
+ }
40
+ console.log(result.fail.message)
41
+ }
42
+ else if (result.data) console.log(result.data)
43
+ }
44
+
45
+ return call
46
+ }
@@ -8,6 +8,7 @@ import { failCode } from "../../../src/fail-code"
8
8
  import { TSON } from "@southern-aurora/tson"
9
9
  import { createUlid } from "../utils/create-ulid"
10
10
  import { configMilkio } from "../../../src/config/milkio"
11
+ import { headerToPlainObject } from "../utils/header-to-plain-object"
11
12
 
12
13
  export type ExecuteHttpServerOptions = {
13
14
  /**
@@ -35,7 +36,7 @@ export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOpti
35
36
  ip,
36
37
  method: request.request.method,
37
38
  // @ts-ignore
38
- requestHeaders: request.request.headers.toJSON(),
39
+ requestHeaders: headerToPlainObject(request.request.headers),
39
40
  timein: new Date().getTime()
40
41
  })
41
42
 
@@ -74,7 +75,7 @@ export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOpti
74
75
  let pathstr = path.join("/") as keyof (typeof schema)["apiMethodsSchema"]
75
76
 
76
77
  // Special processing: do not run middleware when encountering 404 and return quickly
77
- if (!(pathstr in schema.apiMethodsSchema)) {
78
+ if (!(pathstr in schema.apiMethodsSchema) || pathstr.startsWith('$/')) {
78
79
  // @ts-ignore
79
80
  const redirectPath = await routerHandler(pathstr, fullurl)
80
81
  if (!redirectPath) {
@@ -172,7 +173,7 @@ export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOpti
172
173
 
173
174
  await loggerSubmit(executeId)
174
175
  runtime.execute.executeIds.delete(executeId)
175
-
176
+
176
177
  return new Response(response.body, response)
177
178
  }
178
179
 
package/index.ts CHANGED
@@ -8,6 +8,7 @@ export * from "./utils/env-to-string"
8
8
  export * from "./utils/env-to-number"
9
9
  export * from "./utils/env-to-boolean"
10
10
  export * from "./utils/create-template"
11
+ export * from "./utils/header-to-plain-object"
11
12
 
12
13
  // defines
13
14
  export * from "./defines/define-use"
@@ -26,6 +27,7 @@ export * from "./kernel/middleware"
26
27
 
27
28
  // handler
28
29
  export * from "./defines/define-http-handler"
30
+ export * from "./defines/define-command-handler"
29
31
 
30
32
  // api test
31
33
  export * from "./api-test/index"
package/kernel/milkio.ts CHANGED
@@ -4,6 +4,7 @@ import schema from "../../../generated/api-schema"
4
4
  import type { Context } from "../../../src/context"
5
5
  import { failCode } from "../../../src/fail-code"
6
6
  import type { MilkioContext } from "./context"
7
+ import { headerToPlainObject } from "../utils/header-to-plain-object"
7
8
  import { type Mixin, type ExecuteId, type Fail, type FailEnumerates, loggerPushTags, loggerSubmit, runtime, TSON, type Logger, useLogger } from ".."
8
9
  import { hanldeCatchError } from "../utils/handle-catch-error"
9
10
  import { createUlid } from "../utils/create-ulid"
@@ -98,7 +99,7 @@ async function _execute<Path extends keyof (typeof schema)["apiMethodsTypeSchema
98
99
  logger,
99
100
  onAfterHeaders: (headers) => {
100
101
  loggerPushTags(executeId, {
101
- headers: headers.toJSON()
102
+ headers: headerToPlainObject(headers)
102
103
  })
103
104
  }
104
105
  })
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "milkio",
3
3
  "type": "module",
4
4
  "module": "index.ts",
5
- "version": "0.0.30",
5
+ "version": "0.0.32",
6
6
  "peerDependencies": {
7
7
  "typescript": "^5.4.2"
8
8
  },
@@ -60,12 +60,12 @@ export default async () => {
60
60
  if (module?.api?.isApi === true) {
61
61
  // Exclude disallowed characters
62
62
  if (path.includes("_")) {
63
- console.error(`\n\nPath: ` + `"${path}"`)
63
+ console.error(`\n\nPath: ` + `"${path.slice(0, -3)}"`)
64
64
  console.error(`Do not use "_" in the path. If you want to add a separator between words, please use "-".\n`)
65
65
  exit(1)
66
66
  }
67
- if (!/^[a-z0-9/-]+$/.test(path.slice(0, -3))) {
68
- console.error(`\n\nPath: ` + `"${path}"`)
67
+ if (!/^[a-z0-9/$/-]+$/.test(path.slice(0, -3))) {
68
+ console.error(`\n\nPath: ` + `"${path.slice(0, -3)}"`)
69
69
  console.error(`The path can only contain lowercase letters, numbers, and "-".\n`)
70
70
  exit(1)
71
71
  }
@@ -0,0 +1,8 @@
1
+ export const headerToPlainObject = (headers: Headers) => {
2
+ if (headers.toJSON) return headers.toJSON()
3
+ const plainHeaders: Record<string, string> = {}
4
+ headers.forEach((value, key) => {
5
+ plainHeaders[key] = value
6
+ })
7
+ return headers
8
+ }