-
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:
Controller
for supporting plugin pre/post
- Refactor every controller - Extend `Controller` 1. do pre-process 2. do main process 3. do post-process
- Loading branch information
1 parent
7e57425
commit 6248724
Showing
18 changed files
with
879 additions
and
498 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const { pluginGroup, LocalPlugin } = require("../plugins/plugin.class"); | ||
|
||
class Controller { | ||
|
||
/** | ||
* | ||
* @param {import('http').IncomingMessage} req | ||
* @param {import('http').ServerResponse} res | ||
*/ | ||
constructor(req, res) { | ||
this.request = req; | ||
this.response = res; | ||
} | ||
|
||
async preProcess() { | ||
let currentRouterPlugin = pluginGroup.findLocalPlugin(this.request.originalUrl, this.request.method) || new LocalPlugin(); | ||
|
||
try { | ||
|
||
for(let preFn of currentRouterPlugin.preFns) { | ||
await preFn(this.request, this.response); | ||
} | ||
|
||
} catch(e) { | ||
throw new Error(`pre process error in path "${this.request.originalUrl}", ${e.message}`); | ||
} | ||
|
||
} | ||
|
||
async mainProcess() {} | ||
|
||
async postProcess() { | ||
let currentRouterPlugin = pluginGroup.findLocalPlugin(this.request.url, this.request.method) || new LocalPlugin(); | ||
|
||
try { | ||
|
||
for(let postFn of currentRouterPlugin.postFns) { | ||
await postFn(this.request, this.response); | ||
} | ||
|
||
} catch(e) { | ||
throw new Error(`post process error in path "${this.request.originalUrl}", ${e.message}`); | ||
} | ||
} | ||
} | ||
|
||
module.exports.Controller = Controller; |
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
Oops, something went wrong.