Skip to content

Commit

Permalink
feat(core): add bodyparser
Browse files Browse the repository at this point in the history
  • Loading branch information
kimwangchong committed Aug 9, 2023
1 parent 10507cf commit 8f638af
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 10 deletions.
4 changes: 3 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@
"@kever/router": "workspace:*",
"@kever/shared": "workspace:*",
"@types/koa": "^2.13.8",
"@types/koa-bodyparser": "^4.3.10",
"chalk": "^4.0.0",
"glob": "^10.3.3",
"koa": "^2.13.0"
"koa": "^2.13.0",
"koa-bodyparser": "^4.4.1"
},
"gitHead": "9e62d20cc11b371c2b5ba9b1234d3a5b0648ba90"
}
5 changes: 5 additions & 0 deletions packages/core/src/application.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type * as Koa from 'koa'
import type { Middleware } from 'koa'
import bodyparser from 'koa-bodyparser'
import chalk from 'chalk'
import type { ControllerMeta } from '@kever/shared'
import { fillLine, getAppVersion, getProjectName, setMetadataStore, META_LOGGER } from '@kever/shared'
Expand All @@ -10,6 +11,8 @@ import { initEvent } from './handler'
import { defaultLogger, type Logger } from './logger'
import { getMiddlewaresNum, getInjectableNum } from '@kever/ioc'

type BodyparserOptions = Parameters<typeof bodyparser>[0]

/**
* app env
*/
Expand Down Expand Up @@ -67,6 +70,7 @@ export interface AppOptions {
* custom logger, default is console
*/
logger?: Logger
body?: BodyparserOptions
}

const DEFAULT_OPTION: Required<AppOptions> = {
Expand All @@ -78,6 +82,7 @@ const DEFAULT_OPTION: Required<AppOptions> = {
env: Env.DEV,
tsconfig: 'tsconfig.json',
logger: defaultLogger,
body: {}
}

type Callback = (app: App) => void
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/koaRuntime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Koa, { Middleware } from 'koa'
import bodyparser from 'koa-bodyparser'
import { parseRouter } from '@kever/router'
import { getGlobalMiddleware } from '@kever/ioc'
import { type ControllerMeta} from '@kever/shared'
Expand All @@ -12,6 +13,7 @@ export const koaRuntime = (opts: Required<AppOptions>, controllerMetas: Set<Cont

const globalMiddlewares = getGlobalMiddleware();
const middlewares = [
bodyparser(opts.body),
...koaMiddleware,
...globalMiddlewares,
router.routes(),
Expand Down
94 changes: 85 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8f638af

Please sign in to comment.