milkio 1.0.0-alpha.42 → 1.0.0-alpha.43
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/.co.toml +2 -2
- package/README.md +1 -1
- package/action/index.ts +16 -16
- package/command/index.ts +41 -37
- package/config/index.ts +16 -16
- package/context/index.ts +20 -20
- package/events/index.ts +26 -26
- package/exception/index.ts +31 -29
- package/execute/execute-id-generator.ts +5 -5
- package/execute/index.ts +81 -70
- package/index.ts +16 -16
- package/listener/index.ts +134 -124
- package/logger/index.ts +51 -51
- package/meta/index.ts +2 -2
- package/package.json +2 -2
- package/step/index.ts +20 -20
- package/stream/index.ts +16 -16
- package/tsconfig.json +18 -18
- package/type-safety/index.ts +10 -10
- package/types/index.ts +39 -39
- package/utils/create-id.ts +3 -3
- package/utils/headers-to-json.ts +3 -3
- package/utils/send-cookbook-event.ts +17 -16
- package/world/index.ts +52 -52
package/.co.toml
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
[
|
|
2
|
-
includes = ["co:bun"]
|
|
1
|
+
[general]
|
|
2
|
+
includes = [ "co:bun" ]
|
package/README.md
CHANGED
package/action/index.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { $context, $meta } from '..'
|
|
2
2
|
|
|
3
|
-
export
|
|
4
|
-
const action = init as unknown as Action<ActionInitT
|
|
5
|
-
action.$milkioType =
|
|
6
|
-
if (action.meta === undefined) action.meta = {}
|
|
7
|
-
return action
|
|
8
|
-
}
|
|
3
|
+
export function action<ActionInitT extends ActionInit>(init: ActionInitT): Action<ActionInitT> {
|
|
4
|
+
const action = init as unknown as Action<ActionInitT>
|
|
5
|
+
action.$milkioType = 'action'
|
|
6
|
+
if (action.meta === undefined) action.meta = {}
|
|
7
|
+
return action
|
|
8
|
+
}
|
|
9
9
|
|
|
10
|
-
export
|
|
11
|
-
meta?: $meta
|
|
12
|
-
handler: (context: $context, params: any) => Promise<unknown
|
|
13
|
-
}
|
|
10
|
+
export interface ActionInit {
|
|
11
|
+
meta?: $meta
|
|
12
|
+
handler: (context: $context, params: any) => Promise<unknown>
|
|
13
|
+
}
|
|
14
14
|
|
|
15
|
-
export
|
|
16
|
-
$milkioType:
|
|
17
|
-
meta: ActionInitT[
|
|
18
|
-
handler: ActionInitT[
|
|
19
|
-
}
|
|
15
|
+
export interface Action<ActionInitT extends ActionInit> {
|
|
16
|
+
$milkioType: 'action'
|
|
17
|
+
meta: ActionInitT['meta'] extends undefined ? {} : ActionInitT['meta']
|
|
18
|
+
handler: ActionInitT['handler']
|
|
19
|
+
}
|
package/command/index.ts
CHANGED
|
@@ -1,53 +1,57 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { GeneratedInit } from '..'
|
|
2
2
|
|
|
3
|
-
export
|
|
4
|
-
const command = init as unknown as Command<CommandInitT
|
|
5
|
-
command.$milkioType =
|
|
6
|
-
return command
|
|
7
|
-
}
|
|
3
|
+
export function command<CommandInitT extends CommandInit>(init: CommandInitT): Command<CommandInitT> {
|
|
4
|
+
const command = init as unknown as Command<CommandInitT>
|
|
5
|
+
command.$milkioType = 'command'
|
|
6
|
+
return command
|
|
7
|
+
}
|
|
8
8
|
|
|
9
|
-
export
|
|
10
|
-
handler: (commands: Array<string>, options: Record<string, string>) => Promise<unknown
|
|
11
|
-
}
|
|
9
|
+
export interface CommandInit {
|
|
10
|
+
handler: (commands: Array<string>, options: Record<string, string>) => Promise<unknown>
|
|
11
|
+
}
|
|
12
12
|
|
|
13
|
-
export
|
|
14
|
-
$milkioType:
|
|
15
|
-
handler: CommandInitT[
|
|
16
|
-
}
|
|
13
|
+
export interface Command<CommandInitT extends CommandInit> {
|
|
14
|
+
$milkioType: 'command'
|
|
15
|
+
handler: CommandInitT['handler']
|
|
16
|
+
}
|
|
17
17
|
|
|
18
|
-
export
|
|
18
|
+
export function __initCommander(generated: GeneratedInit, runtime: any) {
|
|
19
19
|
const commander = async (argv: Array<string>, options?: { onNotFound?: () => any }) => {
|
|
20
20
|
const params = {
|
|
21
|
-
path:
|
|
21
|
+
path: 'index',
|
|
22
22
|
commands: [] as Array<string>,
|
|
23
23
|
options: {} as Record<string, string | true>,
|
|
24
|
-
}
|
|
24
|
+
}
|
|
25
25
|
for (const v of argv.slice(3)) {
|
|
26
|
-
if (v.startsWith(
|
|
27
|
-
const vSplited = v.split(
|
|
28
|
-
params.options[vSplited[0].slice(2)] = vSplited.slice(1, vSplited.length).join(
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
params.options[
|
|
36
|
-
}
|
|
37
|
-
|
|
26
|
+
if (v.startsWith('--') && v.includes('=')) {
|
|
27
|
+
const vSplited = v.split('=')
|
|
28
|
+
params.options[vSplited[0].slice(2)] = vSplited.slice(1, vSplited.length).join('=')
|
|
29
|
+
}
|
|
30
|
+
else if (v.startsWith('--')) {
|
|
31
|
+
params.options[v.slice(2)] = '1'
|
|
32
|
+
}
|
|
33
|
+
else if (v.startsWith('-') && v.includes('=')) {
|
|
34
|
+
const vSplited = v.split('=')
|
|
35
|
+
params.options[vSplited[0].slice(1)] = vSplited.slice(1, vSplited.length).join('=')
|
|
36
|
+
}
|
|
37
|
+
else if (v.startsWith('-')) {
|
|
38
|
+
params.options[v.slice(1)] = '1'
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
params.commands.push(v)
|
|
38
42
|
}
|
|
39
43
|
}
|
|
40
|
-
if (argv.length === 2) params.path = `index
|
|
41
|
-
else params.path = `${argv[2] ??
|
|
44
|
+
if (argv.length === 2) params.path = `index`
|
|
45
|
+
else params.path = `${argv[2] ?? 'index'}`
|
|
42
46
|
|
|
43
47
|
if (!(params.path in generated.commandSchema.commands)) {
|
|
44
|
-
if (options?.onNotFound) return await options.onNotFound()
|
|
45
|
-
console.log(`\x1B[44m UwU \x1B[0m \x1B[2mCommand not found:\x1B[0m \x1B[32m${params.path}\x1B[0m`)
|
|
46
|
-
return undefined
|
|
48
|
+
if (options?.onNotFound) return await options.onNotFound()
|
|
49
|
+
console.log(`\x1B[44m UwU \x1B[0m \x1B[2mCommand not found:\x1B[0m \x1B[32m${params.path}\x1B[0m`)
|
|
50
|
+
return undefined
|
|
47
51
|
}
|
|
48
52
|
|
|
49
|
-
return await generated.commandSchema.commands[params.path].module.handler(params.commands, params.options)
|
|
50
|
-
}
|
|
53
|
+
return await generated.commandSchema.commands[params.path].module.handler(params.commands, params.options)
|
|
54
|
+
}
|
|
51
55
|
|
|
52
|
-
return commander
|
|
53
|
-
}
|
|
56
|
+
return commander
|
|
57
|
+
}
|
package/config/index.ts
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
export
|
|
2
|
-
return config
|
|
3
|
-
}
|
|
1
|
+
export function config<ConfigT extends Config>(config: ConfigT): ConfigT {
|
|
2
|
+
return config
|
|
3
|
+
}
|
|
4
4
|
|
|
5
|
-
export type Config = (mode: string) => Promise<Record<string, unknown>> | Record<string, unknown
|
|
5
|
+
export type Config = (mode: string) => Promise<Record<string, unknown>> | Record<string, unknown>
|
|
6
6
|
|
|
7
|
-
export
|
|
8
|
-
[key: string]: (env: Record<string, string>) => Partial<Awaited<ReturnType<T>>> | Promise<Partial<Awaited<ReturnType<T
|
|
9
|
-
}
|
|
7
|
+
export interface ConfigEnvironments<T extends Config> {
|
|
8
|
+
[key: string]: (env: Record<string, string>) => Partial<Awaited<ReturnType<T>>> | Promise<Partial<Awaited<ReturnType<T>>>>
|
|
9
|
+
}
|
|
10
10
|
|
|
11
11
|
export function envToString(value: string | number | undefined, defaultValue: string) {
|
|
12
|
-
if (value === undefined) return defaultValue
|
|
12
|
+
if (value === undefined) return defaultValue
|
|
13
13
|
|
|
14
|
-
return `${value}
|
|
14
|
+
return `${value}`
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
export function envToNumber(value: string | undefined, defaultValue: number) {
|
|
18
|
-
if (value === undefined) return defaultValue
|
|
18
|
+
if (value === undefined) return defaultValue
|
|
19
19
|
|
|
20
|
-
return Number.parseInt(value, 10)
|
|
20
|
+
return Number.parseInt(value, 10)
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
export function envToBoolean(value: string | number | undefined, defaultValue: boolean) {
|
|
24
|
-
if (value ===
|
|
24
|
+
if (value === 'true') return true
|
|
25
25
|
|
|
26
|
-
if (value ===
|
|
26
|
+
if (value === 'false') return false
|
|
27
27
|
|
|
28
|
-
if (value ===
|
|
28
|
+
if (value === '') return false
|
|
29
29
|
|
|
30
|
-
if (undefined === value) return defaultValue
|
|
30
|
+
if (undefined === value) return defaultValue
|
|
31
31
|
|
|
32
|
-
return Boolean(value)
|
|
32
|
+
return Boolean(value)
|
|
33
33
|
}
|
package/context/index.ts
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { MilkioHttpRequest, MilkioHttpResponse, $types, Logger, Action } from '..'
|
|
2
2
|
|
|
3
3
|
export interface $context {
|
|
4
|
-
develop: boolean
|
|
5
|
-
executeId: string
|
|
6
|
-
environment: string
|
|
7
|
-
path: string
|
|
8
|
-
logger: Logger
|
|
9
|
-
http: ContextHttp<Record<any, any
|
|
10
|
-
config: Readonly<Awaited<ReturnType<$types[
|
|
11
|
-
call: <Module extends Promise<{ default: Action<any> }>>(module: Module, params: Parameters<Awaited<Module>[
|
|
4
|
+
develop: boolean
|
|
5
|
+
executeId: string
|
|
6
|
+
environment: string
|
|
7
|
+
path: string
|
|
8
|
+
logger: Logger
|
|
9
|
+
http: ContextHttp<Record<any, any>>
|
|
10
|
+
config: Readonly<Awaited<ReturnType<$types['configSchema']['get']>>>
|
|
11
|
+
call: <Module extends Promise<{ default: Action<any> }>>(module: Module, params: Parameters<Awaited<Module>['default']['handler']>[1]) => Promise<ReturnType<Awaited<Module>['default']['handler']>>
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
export
|
|
15
|
-
url: URL
|
|
16
|
-
ip: string
|
|
17
|
-
path: { string: keyof $types[
|
|
14
|
+
export interface ContextHttp<ParamsParsed = any> {
|
|
15
|
+
url: URL
|
|
16
|
+
ip: string
|
|
17
|
+
path: { string: keyof $types['generated']['routeSchema'], array: Array<string> }
|
|
18
18
|
params: {
|
|
19
|
-
string: string
|
|
20
|
-
parsed: ParamsParsed
|
|
21
|
-
}
|
|
22
|
-
request: MilkioHttpRequest
|
|
23
|
-
response: MilkioHttpResponse
|
|
24
|
-
}
|
|
19
|
+
string: string
|
|
20
|
+
parsed: ParamsParsed
|
|
21
|
+
}
|
|
22
|
+
request: MilkioHttpRequest
|
|
23
|
+
response: MilkioHttpResponse
|
|
24
|
+
}
|
|
25
25
|
|
|
26
|
-
export type ContextCreatedHandler = (context: $context) => Promise<void> | void
|
|
26
|
+
export type ContextCreatedHandler = (context: $context) => Promise<void> | void
|
package/events/index.ts
CHANGED
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { $context, ContextHttp, Results, Logger, $meta } from '..'
|
|
2
2
|
|
|
3
3
|
export interface $events {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
'milkio:httpRequest': { executeId: string, path: string, logger: Logger, http: ContextHttp<Record<string, any>> }
|
|
5
|
+
'milkio:httpResponse': { executeId: string, path: string, logger: Logger, http: ContextHttp<Record<string, any>>, context: $context }
|
|
6
|
+
'milkio:httpNotFound': { executeId: string, path: string, logger: Logger, http: ContextHttp<Record<string, any>> }
|
|
7
|
+
'milkio:executeBefore': { executeId: string, path: string, logger: Logger, meta: $meta, context: $context }
|
|
8
|
+
'milkio:executeAfter': { executeId: string, path: string, logger: Logger, meta: $meta, context: $context, results: Results<any> }
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
export
|
|
12
|
-
const handlers = new Map<(event: any) => void, string>()
|
|
13
|
-
const indexed = new Map<string, Set<(event: any) => void>>()
|
|
11
|
+
export function __initEventManager() {
|
|
12
|
+
const handlers = new Map<(event: any) => void, string>()
|
|
13
|
+
const indexed = new Map<string, Set<(event: any) => void>>()
|
|
14
14
|
|
|
15
15
|
const eventManager = {
|
|
16
16
|
on: <Key extends keyof $events, Handler extends (event: $events[Key]) => void>(key: Key, handler: Handler) => {
|
|
17
|
-
handlers.set(handler, key as string)
|
|
17
|
+
handlers.set(handler, key as string)
|
|
18
18
|
if (indexed.has(key as string) === false) {
|
|
19
|
-
indexed.set(key as string, new Set())
|
|
19
|
+
indexed.set(key as string, new Set())
|
|
20
20
|
}
|
|
21
|
-
const set = indexed.get(key as string)
|
|
22
|
-
set.add(handler)
|
|
23
|
-
handlers.set(handler, key as string)
|
|
21
|
+
const set = indexed.get(key as string)!
|
|
22
|
+
set.add(handler)
|
|
23
|
+
handlers.set(handler, key as string)
|
|
24
24
|
|
|
25
25
|
return () => {
|
|
26
|
-
handlers.delete(handler)
|
|
27
|
-
set.delete(handler)
|
|
28
|
-
}
|
|
26
|
+
handlers.delete(handler)
|
|
27
|
+
set.delete(handler)
|
|
28
|
+
}
|
|
29
29
|
},
|
|
30
30
|
off: <Key extends keyof $events, Handler extends (event: $events[Key]) => void>(key: Key, handler: Handler) => {
|
|
31
|
-
const set = indexed.get(key as string)
|
|
32
|
-
if (!set) return
|
|
33
|
-
handlers.delete(handler)
|
|
34
|
-
set.delete(handler)
|
|
31
|
+
const set = indexed.get(key as string)
|
|
32
|
+
if (!set) return
|
|
33
|
+
handlers.delete(handler)
|
|
34
|
+
set.delete(handler)
|
|
35
35
|
},
|
|
36
36
|
emit: async <Key extends keyof $events, Value extends $events[Key]>(key: Key, value: Value): Promise<void> => {
|
|
37
|
-
const h = indexed.get(key as string)
|
|
37
|
+
const h = indexed.get(key as string)
|
|
38
38
|
if (h) {
|
|
39
39
|
for (const handler of h) {
|
|
40
|
-
await handler(value)
|
|
40
|
+
await handler(value)
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
},
|
|
44
|
-
}
|
|
44
|
+
}
|
|
45
45
|
|
|
46
|
-
return eventManager
|
|
47
|
-
}
|
|
46
|
+
return eventManager
|
|
47
|
+
}
|
package/exception/index.ts
CHANGED
|
@@ -1,46 +1,48 @@
|
|
|
1
|
-
import { TSON } from
|
|
2
|
-
import {
|
|
1
|
+
import { TSON } from '@southern-aurora/tson'
|
|
2
|
+
import type { MilkioResponseReject, Logger } from '..'
|
|
3
3
|
|
|
4
4
|
export interface $rejectCode {
|
|
5
|
-
FAIL: string
|
|
6
|
-
REQUEST_FAIL: any
|
|
7
|
-
NOT_DEVELOP_MODE: string
|
|
8
|
-
REQUEST_TIMEOUT: { timeout: number
|
|
9
|
-
NOT_FOUND: { path: string }
|
|
10
|
-
PARAMS_TYPE_INCORRECT: { path: string
|
|
11
|
-
RESULTS_TYPE_INCORRECT: { path: string
|
|
12
|
-
UNACCEPTABLE: { expected: string
|
|
13
|
-
PARAMS_TYPE_NOT_SUPPORTED: { expected: string }
|
|
14
|
-
RESULTS_TYPE_NOT_SUPPORTED: { expected: string }
|
|
15
|
-
INTERNAL_SERVER_ERROR: undefined
|
|
5
|
+
FAIL: string
|
|
6
|
+
REQUEST_FAIL: any
|
|
7
|
+
NOT_DEVELOP_MODE: string
|
|
8
|
+
REQUEST_TIMEOUT: { timeout: number, message: string }
|
|
9
|
+
NOT_FOUND: { path: string }
|
|
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
|
|
12
|
+
UNACCEPTABLE: { expected: string, message: string }
|
|
13
|
+
PARAMS_TYPE_NOT_SUPPORTED: { expected: string }
|
|
14
|
+
RESULTS_TYPE_NOT_SUPPORTED: { expected: string }
|
|
15
|
+
INTERNAL_SERVER_ERROR: undefined
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
export function reject<Code extends keyof $rejectCode, RejectData extends $rejectCode[Code]>(code: Code, data: RejectData): MilkioRejectError<Code, RejectData> {
|
|
19
|
-
const error = { $milkioReject: true, code
|
|
20
|
-
Error.captureStackTrace(error)
|
|
21
|
-
return error
|
|
19
|
+
const error = { $milkioReject: true, code, data } as MilkioRejectError<Code, RejectData>
|
|
20
|
+
Error.captureStackTrace(error)
|
|
21
|
+
return error
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
export
|
|
24
|
+
export interface MilkioRejectError<Code extends keyof $rejectCode = keyof $rejectCode, RejectData extends $rejectCode[Code] = $rejectCode[Code]> { code: Code, data: RejectData, stack: string, $milkioReject: true }
|
|
25
25
|
|
|
26
26
|
export function exceptionHandler(executeId: string, logger: Logger, error: MilkioRejectError<any, any> | any): MilkioResponseReject {
|
|
27
|
-
const name = error?.code ?? error?.name ?? error?.constructor?.name ??
|
|
27
|
+
const name = error?.code ?? error?.name ?? error?.constructor?.name ?? 'Unnamed Exception'
|
|
28
28
|
|
|
29
|
-
if (error?.$milkioReject === true && error?.code ===
|
|
30
|
-
logger.info(name, error?.data?.path ??
|
|
31
|
-
}
|
|
29
|
+
if (error?.$milkioReject === true && error?.code === 'NOT_FOUND') {
|
|
30
|
+
logger.info(name, error?.data?.path ?? 'Unknown path')
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
32
33
|
try {
|
|
33
|
-
const stack = error?.$milkioReject ? (error?.stack ??
|
|
34
|
-
logger.error(name, `\n${TSON.stringify(error?.data)}`, `\n${stack}\n`)
|
|
35
|
-
}
|
|
36
|
-
|
|
34
|
+
const stack = error?.$milkioReject ? (error?.stack ?? '').split('\n').slice(2).join('\n') : error?.stack ?? ''
|
|
35
|
+
logger.error(name, `\n${TSON.stringify(error?.data)}`, `\n${stack}\n`)
|
|
36
|
+
}
|
|
37
|
+
catch (_) {
|
|
38
|
+
logger.error(name, `\n${error?.toString()}`, `\n${error?.stack}\n`)
|
|
37
39
|
}
|
|
38
40
|
}
|
|
39
41
|
|
|
40
|
-
let result: MilkioResponseReject
|
|
42
|
+
let result: MilkioResponseReject
|
|
41
43
|
|
|
42
|
-
if (error?.$milkioReject === true) result = { success: false, code: error.code, reject: error.data, executeId }
|
|
43
|
-
else result = { success: false, code:
|
|
44
|
+
if (error?.$milkioReject === true) result = { success: false, code: error.code, reject: error.data, executeId }
|
|
45
|
+
else result = { success: false, code: 'INTERNAL_SERVER_ERROR', reject: undefined, executeId }
|
|
44
46
|
|
|
45
|
-
return result
|
|
47
|
+
return result
|
|
46
48
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { createId } from
|
|
1
|
+
import { createId } from '../utils/create-id'
|
|
2
2
|
|
|
3
|
-
export type ExecuteIdGenerator = (request?: Request) => string | Promise<string
|
|
3
|
+
export type ExecuteIdGenerator = (request?: Request) => string | Promise<string>
|
|
4
4
|
|
|
5
|
-
export
|
|
6
|
-
return createId
|
|
7
|
-
}
|
|
5
|
+
export function defineDefaultExecuteIdGenerator() {
|
|
6
|
+
return createId
|
|
7
|
+
}
|
package/execute/index.ts
CHANGED
|
@@ -1,64 +1,70 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { TSON } from
|
|
3
|
-
import { reject
|
|
4
|
-
import {
|
|
1
|
+
import type { IValidation } from 'typia'
|
|
2
|
+
import { TSON } from '@southern-aurora/tson'
|
|
3
|
+
import { reject } from '..'
|
|
4
|
+
import type { $context, $meta, Logger, Results, GeneratedInit } from '..'
|
|
5
|
+
import { headersToJSON } from '../utils/headers-to-json'
|
|
5
6
|
|
|
6
|
-
export
|
|
7
|
+
export function __initExecuter(generated: GeneratedInit, runtime: any) {
|
|
7
8
|
const __execute = async (
|
|
8
9
|
routeSchema: any,
|
|
9
10
|
options: {
|
|
10
|
-
createdExecuteId: string
|
|
11
|
-
createdLogger: Logger
|
|
12
|
-
path: string
|
|
13
|
-
headers: Record<string, string> | Headers
|
|
14
|
-
mixinContext: Record<any, any> | undefined
|
|
11
|
+
createdExecuteId: string
|
|
12
|
+
createdLogger: Logger
|
|
13
|
+
path: string
|
|
14
|
+
headers: Record<string, string> | Headers
|
|
15
|
+
mixinContext: Record<any, any> | undefined
|
|
15
16
|
} & (
|
|
16
17
|
| {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
params: Record<any, any>
|
|
19
|
+
paramsType: 'raw'
|
|
20
|
+
}
|
|
20
21
|
| {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
params: string
|
|
23
|
+
paramsType: 'string'
|
|
24
|
+
}
|
|
24
25
|
),
|
|
25
|
-
): Promise<{ executeId: string
|
|
26
|
-
const executeId: string = options.createdExecuteId
|
|
27
|
-
let headers: Headers
|
|
26
|
+
): Promise<{ executeId: string, headers: Headers, params: Record<any, unknown>, results: Results<any>, context: $context, meta: Readonly<$meta>, type: 'action' | 'stream', emptyResult: boolean, resultsTypeSafety: boolean }> => {
|
|
27
|
+
const executeId: string = options.createdExecuteId
|
|
28
|
+
let headers: Headers
|
|
28
29
|
if (!(options.headers instanceof Headers)) {
|
|
29
30
|
// @ts-ignore
|
|
30
31
|
headers = new Headers({
|
|
31
32
|
...options.headers,
|
|
32
|
-
})
|
|
33
|
-
}
|
|
34
|
-
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
headers = options.headers
|
|
35
37
|
}
|
|
36
|
-
if (!(
|
|
38
|
+
if (!('toJSON' in headers)) (headers as any).toJSON = () => headersToJSON(headers)
|
|
37
39
|
|
|
38
|
-
let params: Record<any, unknown
|
|
39
|
-
if (options.paramsType ===
|
|
40
|
-
params = options.params
|
|
41
|
-
if (typeof params ===
|
|
42
|
-
}
|
|
43
|
-
|
|
40
|
+
let params: Record<any, unknown>
|
|
41
|
+
if (options.paramsType === 'raw') {
|
|
42
|
+
params = options.params
|
|
43
|
+
if (typeof params === 'undefined') params = {}
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
if (options.params === '') {
|
|
47
|
+
params = {}
|
|
48
|
+
}
|
|
44
49
|
else {
|
|
45
50
|
try {
|
|
46
|
-
params = TSON.parse(options.params)
|
|
47
|
-
}
|
|
48
|
-
|
|
51
|
+
params = TSON.parse(options.params)
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
throw reject('PARAMS_TYPE_NOT_SUPPORTED', { expected: 'json' })
|
|
49
55
|
}
|
|
50
|
-
if (typeof params ===
|
|
56
|
+
if (typeof params === 'undefined') params = {}
|
|
51
57
|
}
|
|
52
58
|
}
|
|
53
|
-
if (typeof params !==
|
|
54
|
-
if (
|
|
55
|
-
if (!runtime.develop) throw reject(
|
|
56
|
-
delete params.$milkioGenerateParams
|
|
57
|
-
let paramsRand = routeSchema.randomParams()
|
|
58
|
-
if (paramsRand === undefined || paramsRand === null) paramsRand = {}
|
|
59
|
-
params = { ...paramsRand, ...params }
|
|
59
|
+
if (typeof params !== 'object' || Array.isArray(params)) throw reject('PARAMS_TYPE_NOT_SUPPORTED', { expected: 'json' })
|
|
60
|
+
if ('$milkioGenerateParams' in params && params.$milkioGenerateParams === 'enable') {
|
|
61
|
+
if (!runtime.develop) throw reject('NOT_DEVELOP_MODE', 'This feature must be in cookbook to use.')
|
|
62
|
+
delete params.$milkioGenerateParams
|
|
63
|
+
let paramsRand = routeSchema.randomParams()
|
|
64
|
+
if (paramsRand === undefined || paramsRand === null) paramsRand = {}
|
|
65
|
+
params = { ...paramsRand, ...params }
|
|
60
66
|
}
|
|
61
|
-
if (options.mixinContext?.http?.params?.string) options.mixinContext.http.params.parsed = params
|
|
67
|
+
if (options.mixinContext?.http?.params?.string) options.mixinContext.http.params.parsed = params // listen でパースしたパラメータを渡す
|
|
62
68
|
const context = {
|
|
63
69
|
...(options.mixinContext ? options.mixinContext : {}),
|
|
64
70
|
develop: runtime.develop,
|
|
@@ -67,52 +73,57 @@ export const __initExecuter = (generated: GeneratedInit, runtime: any) => {
|
|
|
67
73
|
executeId: options.createdExecuteId,
|
|
68
74
|
config: runtime.runtime.config,
|
|
69
75
|
call: (module: any, options: any) => __call(context, module, options),
|
|
70
|
-
} as unknown as $context
|
|
71
|
-
const results: Results<any> = { value: undefined }
|
|
76
|
+
} as unknown as $context
|
|
77
|
+
const results: Results<any> = { value: undefined }
|
|
72
78
|
|
|
73
79
|
if (runtime.develop) {
|
|
74
|
-
options.createdLogger.request(`headers - ${TSON.stringify(headers.toJSON())}`, `\nparams - ${TSON.stringify(params)}`)
|
|
80
|
+
options.createdLogger.request(`headers - ${TSON.stringify(headers.toJSON())}`, `\nparams - ${TSON.stringify(params)}`)
|
|
75
81
|
}
|
|
76
82
|
|
|
77
|
-
const module = await routeSchema.module()
|
|
78
|
-
|
|
83
|
+
const module = await routeSchema.module()
|
|
84
|
+
const meta = (module.default?.meta ? module.default?.meta : {}) as unknown as Readonly<$meta>
|
|
79
85
|
|
|
80
86
|
if (meta.typeSafety === undefined || meta.typeSafety === true) {
|
|
81
|
-
const validation = routeSchema.validateParams(params) as IValidation<any
|
|
82
|
-
if (!validation.success) throw reject(
|
|
87
|
+
const validation = routeSchema.validateParams(params) as IValidation<any>
|
|
88
|
+
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.` })
|
|
83
89
|
}
|
|
84
90
|
|
|
85
|
-
await runtime.emit(
|
|
91
|
+
await runtime.emit('milkio:executeBefore', { executeId: options.createdExecuteId, logger: options.createdLogger, path: options.path, meta, context })
|
|
86
92
|
|
|
87
|
-
results.value = await module.default.handler(context, params)
|
|
93
|
+
results.value = await module.default.handler(context, params)
|
|
88
94
|
|
|
89
|
-
let resultsTypeSafety = false
|
|
90
|
-
if (results?.value?.$milkioType ===
|
|
91
|
-
resultsTypeSafety = true
|
|
92
|
-
const validation = routeSchema.validateResults(results.value.value) as IValidation<any
|
|
93
|
-
if (!validation.success) throw reject(
|
|
94
|
-
results.value = results.value.value
|
|
95
|
+
let resultsTypeSafety = false
|
|
96
|
+
if (results?.value?.$milkioType === 'type-safety') {
|
|
97
|
+
resultsTypeSafety = true
|
|
98
|
+
const validation = routeSchema.validateResults(results.value.value) as IValidation<any>
|
|
99
|
+
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.` })
|
|
100
|
+
results.value = results.value.value
|
|
95
101
|
}
|
|
96
102
|
|
|
97
|
-
let emptyResult = false
|
|
98
|
-
if (results.value === undefined || results.value === null || results.value ===
|
|
99
|
-
emptyResult = true
|
|
100
|
-
results.value = {}
|
|
101
|
-
}
|
|
102
|
-
else if (
|
|
103
|
+
let emptyResult = false
|
|
104
|
+
if (results.value === undefined || results.value === null || results.value === '') {
|
|
105
|
+
emptyResult = true
|
|
106
|
+
results.value = {}
|
|
107
|
+
}
|
|
108
|
+
else if (Array.isArray(results.value)) {
|
|
109
|
+
throw reject('FAIL', 'The return type of the handler must be an object, which is currently an array.')
|
|
110
|
+
}
|
|
111
|
+
else if (typeof results.value !== 'object') {
|
|
112
|
+
throw reject('FAIL', 'The return type of the handler must be an object, which is currently a primitive type.')
|
|
113
|
+
}
|
|
103
114
|
|
|
104
|
-
await runtime.emit(
|
|
115
|
+
await runtime.emit('milkio:executeAfter', { executeId: options.createdExecuteId, logger: options.createdLogger, path: options.path, meta, context, results })
|
|
105
116
|
|
|
106
|
-
return { executeId, headers, params, results, context, meta, type: module.$milkioType, emptyResult, resultsTypeSafety }
|
|
107
|
-
}
|
|
117
|
+
return { executeId, headers, params, results, context, meta, type: module.$milkioType, emptyResult, resultsTypeSafety }
|
|
118
|
+
}
|
|
108
119
|
|
|
109
120
|
const __call = async (context: $context, module: { default: any }, params?: any): Promise<any> => {
|
|
110
|
-
const moduleAwaited = await module
|
|
111
|
-
return await moduleAwaited.default.handler(context, params)
|
|
112
|
-
}
|
|
121
|
+
const moduleAwaited = await module
|
|
122
|
+
return await moduleAwaited.default.handler(context, params)
|
|
123
|
+
}
|
|
113
124
|
|
|
114
125
|
return {
|
|
115
126
|
__call,
|
|
116
127
|
__execute,
|
|
117
|
-
}
|
|
118
|
-
}
|
|
128
|
+
}
|
|
129
|
+
}
|