Skip to content

Commit

Permalink
feat: add internal plugin of requestParser
Browse files Browse the repository at this point in the history
  • Loading branch information
leftstick committed Nov 25, 2019
1 parent 10aa95f commit 74bc171
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ Plugin system in `express-api-bootstrap` is a way to help developer hack in appl

Some lifecycles can be used for plugin development.

It's worth mentioning that core features such as: `server`, `cors`, `source watcher`, `api register` are written via Plugin concept.
It's worth mentioning that core features such as: `server`, `cors`, `requestParser`, `source watcher`, `api register` are written via Plugin concept.
2 changes: 1 addition & 1 deletion docs/zh/plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ sidebarDepth: 3

`express-api-bootstrap` 的插件系统提供了一个让开发轻松“介入”应用个别生命周期的方法。

值得一提的是,系统内置的服务,例如: `server`, `cors`, `source watcher`, `api register` 都是基于插件系统开发的“内置”插件
值得一提的是,系统内置的服务,例如: `server`, `cors`,`requestParser`, `source watcher`, `api register` 都是基于插件系统开发的“内置”插件
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "express-api-bootstrap",
"version": "3.0.2",
"version": "3.0.3",
"description": "express-api-bootstrap makes it easy to create stand-alone, production-grade express based API servers that you can \"just run\"",
"main": "./libs/index.js",
"typings": "./libs/src/index.d.ts",
Expand Down Expand Up @@ -57,12 +57,15 @@
"@babel/preset-env": "^7.7.1",
"@babel/preset-typescript": "^7.7.2",
"@babel/register": "^7.7.0",
"@types/cookie-parser": "^1.4.2",
"@types/express": "^4.17.2",
"@types/jest": "^24.0.23",
"@zerollup/ts-transform-paths": "^1.7.7",
"babel-plugin-parameter-decorator": "^1.0.12",
"body-parser": "^1.19.0",
"chokidar": "^3.3.0",
"commander": "^4.0.1",
"cookie-parser": "^1.4.4",
"del": "^5.1.0",
"express": "^4.17.1",
"glob": "^7.1.6",
Expand Down
3 changes: 2 additions & 1 deletion src/core/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { isArray, isEmpty } from '@/src/core/helper/object'

import cors from '@/src/plugins/cors'
import api from '@/src/plugins/api'
import requestParser from '@/src/plugins/requestParser'
import server from '@/src/plugins/server'
import watcher from '@/src/plugins/watcher'

Expand All @@ -21,7 +22,7 @@ interface IPluginDef {
options: any
}

const internalPlugins = [cors(), watcher(), api(), server()]
const internalPlugins = [cors(), watcher(), requestParser(), api(), server()]

export function getExternalPlugins() {
const factories = getExternalPluginFactories()
Expand Down
49 changes: 49 additions & 0 deletions src/plugins/requestParser/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import express from 'express'
import { isEmpty, isNotEmpty } from '@/src/core/helper/object'
import { PluginOrderEnum, IPlugin } from '@/src/core/plugin/pluginType'
import { IPluginType, IParsers } from '@/src/plugins/requestParser/type'
import bodyParser from 'body-parser'
import cookieParser from 'cookie-parser'

export default () => {
return <IPlugin>{
order: PluginOrderEnum.BEFORE_API_INIT,
configHandler(config: IPluginType): IPluginType {
const opts = {
config(app: express.Express, parsers: IParsers) {
app.use(parsers.bodyParser.json())
app.use(parsers.cookieParser())
}
}

if (isEmpty(config.requestParser)) {
return {
requestParser: opts
}
}

if (config.requestParser == false) {
return {
requestParser: false
}
}

if (isNotEmpty(config.requestParser.config)) {
opts.config = config.requestParser.config
}

return {
requestParser: opts
}
},
pluginHandler(app: express.Express, config: IPluginType) {
if (config.requestParser === false) {
return
}
config.requestParser.config(app, {
bodyParser,
cookieParser
})
}
}
}
32 changes: 32 additions & 0 deletions src/plugins/requestParser/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import express from 'express'
import bodyParser from 'body-parser'
import cookieParser from 'cookie-parser'

export interface IPluginType {
/**
* requestParser config.
*
* Enable by default
*/
requestParser: IRequestParserConfig | false
}

interface IRequestParserConfig {
/**
*
*
*
* @param parsers
*/
config(app: express.Express, parsers: IParsers): void
}

export interface IParsers {
bodyParser: {
json: (options?: bodyParser.OptionsJson) => any
raw: (options?: bodyParser.Options) => any
text: (options?: bodyParser.OptionsText) => any
urlencoded: (options?: bodyParser.OptionsUrlencoded) => any
}
cookieParser: (secret?: string | string[], options?: cookieParser.CookieParseOptions) => express.RequestHandler
}
24 changes: 22 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,13 @@
dependencies:
"@types/node" "*"

"@types/cookie-parser@^1.4.2":
version "1.4.2"
resolved "https://registry.yarnpkg.com/@types/cookie-parser/-/cookie-parser-1.4.2.tgz#e4d5c5ffda82b80672a88a4281aaceefb1bd9df5"
integrity sha512-uwcY8m6SDQqciHsqcKDGbo10GdasYsPCYkH3hVegj9qAah6pX5HivOnOuI3WYmyQMnOATV39zv/Ybs0bC/6iVg==
dependencies:
"@types/express" "*"

"@types/estree@*":
version "0.0.39"
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f"
Expand All @@ -1241,7 +1248,7 @@
"@types/node" "*"
"@types/range-parser" "*"

"@types/express@^4.17.2":
"@types/express@*", "@types/express@^4.17.2":
version "4.17.2"
resolved "https://registry.yarnpkg.com/@types/express/-/express-4.17.2.tgz#a0fb7a23d8855bac31bc01d5a58cadd9b2173e6c"
integrity sha512-5mHFNyavtLoJmnusB8OKJ5bshSzw+qkMIBAobLrIM48HJvunFva9mOa6aBwh64lBFyNwBbs0xiEFuj4eU/NjCA==
Expand Down Expand Up @@ -2315,7 +2322,7 @@ bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.1.1, bn.js@^4.4.0:
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.11.8.tgz#2cde09eb5ee341f484746bb0309b3253b1b1442f"
integrity sha512-ItfYfPLkWHUjckQCk8xC+LwxgK8NYcXywGigJgSwOP8Y2iyWT4f2vsZnoOXTTbo+o5yXmIUJ4gn5538SO5S3gA==

body-parser@1.19.0:
body-parser@1.19.0, body-parser@^1.19.0:
version "1.19.0"
resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.19.0.tgz#96b2709e57c9c4e09a6fd66a8fd979844f69f08a"
integrity sha512-dhEPs72UPbDnAQJ9ZKMNTP6ptJaionhP5cBb541nXPlW60Jepo9RV/a4fX4XWW9CuFNK22krhrj1+rgzifNCsw==
Expand Down Expand Up @@ -3077,11 +3084,24 @@ convert-source-map@^1.4.0, convert-source-map@^1.7.0:
dependencies:
safe-buffer "~5.1.1"

cookie-parser@^1.4.4:
version "1.4.4"
resolved "https://registry.yarnpkg.com/cookie-parser/-/cookie-parser-1.4.4.tgz#e6363de4ea98c3def9697b93421c09f30cf5d188"
integrity sha512-lo13tqF3JEtFO7FyA49CqbhaFkskRJ0u/UAiINgrIXeRCY41c88/zxtrECl8AKH3B0hj9q10+h3Kt8I7KlW4tw==
dependencies:
cookie "0.3.1"
cookie-signature "1.0.6"

cookie-signature@1.0.6:
version "1.0.6"
resolved "https://registry.yarnpkg.com/cookie-signature/-/cookie-signature-1.0.6.tgz#e303a882b342cc3ee8ca513a79999734dab3ae2c"
integrity sha1-4wOogrNCzD7oylE6eZmXNNqzriw=

cookie@0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.3.1.tgz#e7e0a1f9ef43b4c8ba925c5c5a96e806d16873bb"
integrity sha1-5+Ch+e9DtMi6klxcWpboBtFoc7s=

cookie@0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.0.tgz#beb437e7022b3b6d49019d088665303ebe9c14ba"
Expand Down

0 comments on commit 74bc171

Please sign in to comment.