diff --git a/package.json b/package.json index 462ce1b7..8bf450df 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "powership", - "version": "3.1.6", + "version": "3.1.7", "private": true, "scripts": { "pack": "run-s pack:*", diff --git a/packages/accounts/package.json b/packages/accounts/package.json index 7b794438..67a864bf 100644 --- a/packages/accounts/package.json +++ b/packages/accounts/package.json @@ -1,6 +1,6 @@ { "name": "@powership/accounts", - "version": "3.1.6", + "version": "3.1.7", "description": "Powership accounts", "#type": "module", "main": "./out/index.cjs", diff --git a/packages/babel-plugins/package.json b/packages/babel-plugins/package.json index 718bab0c..3db560f4 100644 --- a/packages/babel-plugins/package.json +++ b/packages/babel-plugins/package.json @@ -1,6 +1,6 @@ { "name": "@powership/babel-plugins", - "version": "3.1.6", + "version": "3.1.7", "main": "out", "sideEffects": false, "typings": "out", diff --git a/packages/boilerplate/package.json b/packages/boilerplate/package.json index 3dfd4793..86ca4945 100644 --- a/packages/boilerplate/package.json +++ b/packages/boilerplate/package.json @@ -1,6 +1,6 @@ { "name": "@powership/boilerplate", - "version": "3.1.6", + "version": "3.1.7", "author": "antoniopresto ", "sideEffects": false, "#type": "module", diff --git a/packages/deepstate/package.json b/packages/deepstate/package.json index f3d5b885..0dc5cdfa 100644 --- a/packages/deepstate/package.json +++ b/packages/deepstate/package.json @@ -1,6 +1,6 @@ { "name": "@powership/deepstate", - "version": "3.1.6", + "version": "3.1.7", "main": "out/index.js", "module": "out/module/index.mjs", "sideEffects": false, diff --git a/packages/entity/package.json b/packages/entity/package.json index 64267bd0..c306dbe1 100644 --- a/packages/entity/package.json +++ b/packages/entity/package.json @@ -1,6 +1,6 @@ { "name": "@powership/entity", - "version": "3.1.6", + "version": "3.1.7", "#type": "module", "main": "./out/index.cjs", "module": "./out/module/index.mjs", diff --git a/packages/helpers/package.json b/packages/helpers/package.json index dcc43962..b656aa40 100644 --- a/packages/helpers/package.json +++ b/packages/helpers/package.json @@ -1,6 +1,6 @@ { "name": "@powership/helpers", - "version": "3.1.6", + "version": "3.1.7", "#type": "module", "main": "./out/index.cjs", "module": "./out/module/index.mjs", diff --git a/packages/logstorm/package.json b/packages/logstorm/package.json index 0b69f4e8..32facaf9 100644 --- a/packages/logstorm/package.json +++ b/packages/logstorm/package.json @@ -1,6 +1,6 @@ { "name": "logstorm", - "version": "3.1.6", + "version": "3.1.7", "typings": "out", "author": "antoniopresto ", "#type": "module", diff --git a/packages/mongo/package.json b/packages/mongo/package.json index 0823fdaa..3810be7c 100644 --- a/packages/mongo/package.json +++ b/packages/mongo/package.json @@ -1,6 +1,6 @@ { "name": "@powership/mongo", - "version": "3.1.6", + "version": "3.1.7", "#type": "module", "main": "./out/index.cjs", "module": "./out/module/index.mjs", diff --git a/packages/plugin-engine/README.md b/packages/plugin-engine/README.md index 2193fcb3..5ccb19ab 100644 --- a/packages/plugin-engine/README.md +++ b/packages/plugin-engine/README.md @@ -1,125 +1,54 @@ # PluginEngine -PluginEngine is designed to facilitate the extensibility of -applications by providing a simple and minimalist pattern -to manage events and middleware. It's crucial for creating -loosely coupled systems which are essential for maintaining -a scalable and maintainable codebase. - -By leveraging a middleware system with an event-driven -architecture, different parts of an application can -communicate with each other in a decoupled way. This enables -easier feature additions and modifications without causing a -ripple effect of changes throughout the codebase. - -### Defining Events - -Define the events that your application will exec and the data associated with those events. +PluginEngine is a TypeScript library designed for extensible application architecture. +It employs a Publish-Subscribe pattern with middleware support, allowing for a decoupled, +event-driven architecture. ```typescript -type MyEvents = { - userRegistered: { username: string; email: string }; - userLoggedIn: { username: string }; -}; -``` +import { PluginEngine, Plugin } from 'plugin-engine'; -### Creating a Plugin +// Initialize PluginEngine +const engine = new PluginEngine<{ httpRequest: { request: string } }>(); -Create a plugin to handle an event. A plugin can have `enter`, `exit`, and `error` handlers. - -```typescript -const userNotificationPlugin = { - name: 'UserNotificationPlugin', +// Define a logging plugin with 'enter' hook +const logPlugin: Plugin<{ request: string }> = { + name: 'Log', enter: (data, context) => { - // Send a welcome email on user registration - if (data.username && data.email) { - sendWelcomeEmail(data.email); - } + console.log(`Request: ${data.request}`); + // 'context.abortWith' can be used to short-circuit the event chain }, }; -``` - -### Registering a Plugin - -Register the plugin to handle a specific event. - -```typescript -const engine = new PluginEngine(); - -engine.on('userRegistered', userNotificationPlugin); -``` - -### Executing Events - -Exec an event when a particular action occurs in your application. - -```typescript -engine.exec('userRegistered', { - username: 'johndoe', - email: 'john.doe@example.com', -}); -``` -#### Express.js Example +// Register the plugin to an 'httpRequest' event +engine.on('httpRequest', logPlugin); -```typescript -import express from 'express'; -import { PluginEngine } from 'plugin-engine'; - -const app = express(); -const engine = new PluginEngine(); - -app.use((req, res, next) => { - engine - .exec('requestReceived', { req, res }) - .then(({ req, res }) => { - next(); - }) - .catch((error) => { - res.status(500).send(error.message); - }); -}); - -// Register a plugin -engine.on('requestReceived', authenticationPlugin); - -app.listen(3000, () => { - console.log('Server is running on port 3000'); -}); -``` - -## FAQ - -### How do I handle errors? - -Use the `abortWith` method provided in the context argument to the event handler to terminate the processing of subsequent handlers for a particular event and immediately return the current state of the event data. This provides a mechanism to short-circuit the event processing chain when a certain condition is met. - -```typescript -const errorHandlingPlugin = { - name: 'ErrorHandlingPlugin', - enter: (data, context) => { - if (someErrorCondition) { - context.abortWith({ - error: 'An error occurred', - }); - } +// Define a modification plugin with 'enter' hook +const modifyPlugin: Plugin<{ request: string }> = { + name: 'Modify', + enter: (data) => { + data.request = `Modified: ${data.request}`; + return data; }, }; -engine.on('someEvent', errorHandlingPlugin); -``` +// Register the modification plugin +engine.on('httpRequest', modifyPlugin); -### Can I use PluginEngine on the client-side? +// Execute the 'httpRequest' event +engine.exec('httpRequest', { request: 'GET /home' }).then((result) => { + console.log(`Final Request: ${result.request}`); +}); +// Output: "Request: GET /home", "Request: Modified: GET /home", "Final Request: Modified: GET /home" +``` -Yes, PluginEngine can be used on the client-side. The usage is the same as shown in the basic examples. The `exec`, `on`, and `abortWith` methods, along with the plugin structure, remain consistent whether you are working on the client-side or server-side. +### Key Concepts: -### How to unregister a plugin? +- **enter and exit hooks:** Plugins can have `enter` and `exit` hooks that run when an event is executed. -When you register a plugin using the `on` method, it returns a function that you can call to unregister the plugin. +- **abortWith:** Within the `context` argument in the plugin, you can use `abortWith` to +immediately stop the processing of subsequent plugins and return the current state of the event data. -```typescript -const unregister = engine.on('someEvent', somePlugin); +- **Asynchronous Execution:** The `exec` method returns a promise, allowing for asynchronous event handling. -// Later... -unregister(); -``` +- **Error Handling:** Plugins can define an `error` method to handle exceptions gracefully. +PluginEngine? diff --git a/packages/plugin-engine/package.json b/packages/plugin-engine/package.json index d245e842..aac15252 100644 --- a/packages/plugin-engine/package.json +++ b/packages/plugin-engine/package.json @@ -1,6 +1,6 @@ { "name": "plugin-engine", - "version": "3.1.6", + "version": "3.1.7", "#type": "module", "main": "./out/index.cjs", "module": "./out/module/index.mjs", diff --git a/packages/powership/package.json b/packages/powership/package.json index 5c9e242d..2b2f4224 100644 --- a/packages/powership/package.json +++ b/packages/powership/package.json @@ -1,6 +1,6 @@ { "name": "powership", - "version": "3.1.6", + "version": "3.1.7", "author": "antoniopresto ", "#type": "module", "main": "./out/index.cjs", diff --git a/packages/runmate/package.json b/packages/runmate/package.json index e8783589..f02e0156 100644 --- a/packages/runmate/package.json +++ b/packages/runmate/package.json @@ -1,6 +1,6 @@ { "name": "runmate", - "version": "3.1.6", + "version": "3.1.7", "typings": "out", "author": "antoniopresto ", "license": "MIT", diff --git a/packages/schema/package.json b/packages/schema/package.json index 11f15b4a..ef65c1f2 100644 --- a/packages/schema/package.json +++ b/packages/schema/package.json @@ -1,6 +1,6 @@ { "name": "@powership/schema", - "version": "3.1.6", + "version": "3.1.7", "#type": "module", "main": "./out/index.cjs", "module": "./out/module/index.mjs", diff --git a/packages/server/package.json b/packages/server/package.json index 5f1bd770..29da4a17 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -1,6 +1,6 @@ { "name": "@powership/server", - "version": "3.1.6", + "version": "3.1.7", "#type": "module", "main": "./out/index.cjs", "module": "./out/module/index.mjs", diff --git a/packages/transporter/package.json b/packages/transporter/package.json index 0138aa7b..a64d7a92 100644 --- a/packages/transporter/package.json +++ b/packages/transporter/package.json @@ -1,6 +1,6 @@ { "name": "@powership/transporter", - "version": "3.1.6", + "version": "3.1.7", "#type": "module", "main": "./out/index.cjs", "module": "./out/module/index.mjs", diff --git a/packages/utils/package.json b/packages/utils/package.json index f8c8b2ff..bac95c14 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -1,6 +1,6 @@ { "name": "@powership/utils", - "version": "3.1.6", + "version": "3.1.7", "typings": "out", "author": "antoniopresto ", "license": "MIT",