zyket 1.0.15 → 1.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +134 -23
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,63 +1,174 @@
1
- # ZYKET - Easy Socket.io Framework
1
+ # Zyket
2
2
 
3
- Zyket is a framework inspired by Symfony that simplifies working with Socket.io by providing a structured approach.
3
+ Zyket is a Node.js framework designed to simplify the development of real-time applications with Socket.IO and Express. Inspired by the structured approach of frameworks like Symfony, Zyket provides a robust, service-oriented architecture for building scalable and maintainable server-side applications.
4
4
 
5
- # Getting Started
5
+ Upon initial boot, Zyket automatically scaffolds a default project structure, including handlers, routes, and configuration files, allowing you to get started immediately.
6
6
 
7
- To use zyket we need to install it into our project:
7
+ ## Getting Started
8
8
 
9
- ```javascript
9
+ To begin using Zyket, install it in your project:
10
+
11
+ ```bash
10
12
  npm i zyket
11
13
  ```
12
14
 
13
- Then we just need to import the Kernel and boot it:
15
+ Then, create an `index.js` file and boot the Zyket Kernel:
14
16
 
15
17
  ```javascript
18
+ // index.js
16
19
  const { Kernel } = require("zyket");
20
+
21
+ // Instantiate the kernel
17
22
  const kernel = new Kernel();
18
23
 
19
- kernel.boot().then(() => console.log(`Kernel Booted`));
24
+ // Boot the kernel to start all services
25
+ kernel.boot().then(() => {
26
+ console.log("Kernel booted successfully!");
27
+ });
20
28
  ```
21
29
 
22
- After boot is done for fist time, project folders/files will be created if doesnt exists.
30
+ When you run this for the first time, Zyket will create a default `.env` file and a `src` directory containing boilerplate for routes, handlers, guards, and middlewares.
31
+
32
+ ## Core Concepts
23
33
 
24
- ## Handlers
34
+ Zyket is built around a few key architectural concepts:
35
+
36
+ * **Socket.IO Handlers & Guards**: For managing real-time WebSocket events and their authorization.
37
+ * **Express Routes & Middlewares**: For handling traditional RESTful API endpoints.
38
+ * **Services**: Reusable components that are managed by a dependency injection container.
39
+ * **CLI**: A command-line tool to scaffold features from templates.
40
+
41
+ ### Socket.IO Development
42
+
43
+ #### Handlers
44
+
45
+ Handlers are classes that process incoming Socket.IO events. The name of the handler file (e.g., `message.js`) determines the event it listens to (`message`).
25
46
 
26
- Handlers are the way to interact with user messages on the socket:
27
47
  ```javascript
48
+ // src/handlers/message.js
28
49
  const { Handler } = require("zyket");
29
50
 
30
51
  module.exports = class MessageHandler extends Handler {
31
- middlewares = ["default"];
52
+ // Array of guard names to execute before the handler
53
+ guards = ["default"];
54
+
55
+ async handle({ container, socket, data, io }) {
56
+ const logger = container.get("logger");
57
+ logger.info(`Received message: ${JSON.stringify(data)}`);
58
+ socket.emit("response", { received: data });
59
+ }
60
+ };
61
+ ```
32
62
 
33
- async handle({ container, socket, data, io }) {
34
- container.get("logger").info("Message handler");
35
- }
63
+ #### Guards
64
+
65
+ Guards are used to protect Socket.IO handlers or the initial connection. They run before the handler's `handle` method and are ideal for authorization logic.
66
+
67
+ ```javascript
68
+ // src/guards/default.js
69
+ const { Guard } = require("zyket");
70
+
71
+ module.exports = class DefaultGuard extends Guard {
72
+ async handle({ container, socket, io }) {
73
+ container.get("logger").info(`Executing default guard for socket ${socket.id}`);
74
+
75
+ // Example: Check for an auth token. If invalid, disconnect the user.
76
+ // if (!socket.token) {
77
+ // socket.disconnect();
78
+ // }
79
+ }
36
80
  };
37
81
  ```
38
82
 
39
- - middlewares: An array of middleware names that should be executed before the handler.
83
+ ### Express API Development
40
84
 
41
- - handle(): The function that processes the event, receiving container, socket, and data.
85
+ #### Routes
86
+
87
+ Routes handle HTTP requests. The file path maps directly to the URL endpoint. For example, `src/routes/index.js` handles requests to `/`, and `src/routes/[test]/message.js` handles requests to `/:test/message`.
88
+
89
+ ```javascript
90
+ // src/routes/index.js
91
+ const { Route } = require("zyket");
92
+ const DefaultMiddleware = require("../middlewares/default");
93
+
94
+ module.exports = class DefaultRoute extends Route {
95
+ // Apply middlewares to specific HTTP methods
96
+ middlewares = {
97
+ get: [ new DefaultMiddleware() ],
98
+ post: [ new DefaultMiddleware() ]
99
+ }
100
+
101
+ async get({ container, request }) {
102
+ container.get("logger").info("Default route GET");
103
+ return { test: "Hello World GET!" };
104
+ }
105
+
106
+ async post({ container, request }) {
107
+ container.get("logger").info("Default route POST");
108
+ return { test: "Hello World POST!" };
109
+ }
110
+ };
111
+ ```
42
112
 
43
- ## Middlewares
113
+ #### Middlewares
44
114
 
45
- Middlewares allow you to process data before it reaches the handler. They can be used for validation, authentication, logging, etc.
115
+ Middlewares process the request before it reaches the route handler. They follow the standard Express middleware pattern.
46
116
 
47
117
  ```javascript
118
+ // src/middlewares/default.js
48
119
  const { Middleware } = require("zyket");
49
120
 
50
121
  module.exports = class DefaultMiddleware extends Middleware {
51
- async handle({ container, socket, io }) {
52
- container.get("logger").info("Default middleware");
53
- }
122
+ async handle({ container, request, response, next }) {
123
+ container.get("logger").info("Default Express middleware executing");
124
+ next(); // Pass control to the next middleware or route handler
125
+ }
54
126
  };
55
127
  ```
56
128
 
57
- - handle(): The function that processes the event, receiving container, socket, and data.
129
+ ## Services
58
130
 
131
+ Services are the cornerstone of Zyket's architecture, providing reusable functionality across your application. Zyket includes several default services and allows you to register your own.
59
132
 
60
- ## Services
133
+ ### Custom Services
134
+
135
+ You can create your own services by extending the `Service` class and registering it with the Kernel.
136
+
137
+ ```javascript
138
+ // src/services/MyCustomService.js
139
+ const { Service } = require("zyket");
140
+
141
+ module.exports = class MyCustomService extends Service {
142
+ constructor() {
143
+ super("my-custom-service");
144
+ }
145
+
146
+ async boot() {
147
+ console.log("MyCustomService has been booted!");
148
+ }
149
+
150
+ doSomething() {
151
+ return "Something was done.";
152
+ }
153
+ }
154
+ ```
155
+
156
+ Register the service in your main `index.js` file:
157
+
158
+ ```javascript
159
+ // index.js
160
+ const { Kernel } = require("zyket");
161
+ const MyCustomService = require("./src/services/MyCustomService");
162
+
163
+ const kernel = new Kernel({
164
+ services: [
165
+ // [name, class, [constructor_args]]
166
+ ["my-service", MyCustomService, []]
167
+ ]
168
+ });
169
+
170
+ kernel.boot();
171
+ ```
61
172
  Services are reusable components specified in the kernel configuration. Each service must include a boot() function that is executed when the kernel starts.
62
173
 
63
174
  ```javascript
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zyket",
3
- "version": "1.0.15",
3
+ "version": "1.0.16",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"