-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add internal plugin of requestParser
- Loading branch information
Showing
7 changed files
with
111 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters