diesel-core 0.0.1 → 0.0.3

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "diesel-core",
3
- "version": "0.0.1",
4
- "main": "index.js",
3
+ "version": "0.0.3",
4
+ "main": "src/server.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
7
7
  },
package/src/router.js CHANGED
@@ -1,53 +1,43 @@
1
1
  import diesel from "./server";
2
2
 
3
- class Router extends diesel{
4
- constructor(){
5
- super()
6
- }
7
- #addRoute(method,path,handlers){
8
-
3
+ class Router extends diesel {
4
+ constructor() {
5
+ super();
6
+ }
7
+ #addRoute(method, path, handlers) {
8
+ if (!this.trie.root.subMiddlewares.has(path)) {
9
+ this.trie.root.subMiddlewares.set(path,[])
10
+ }
9
11
  const middlewareHandlers = handlers.slice(0, -1);
10
12
 
11
- if (!this.middlewares.has(path)) {
12
- this.middlewares.set(path,[])
13
- }
14
- if (path === '/') {
15
- middlewareHandlers.forEach(midlleware => {
16
- if(!this.globalMiddlewares.includes(midlleware)){
17
- this.globalMiddlewares.push(midlleware)
18
- }
19
- })
20
- } else {
21
- if (!this.middlewares.get(path).includes(...middlewareHandlers)) {
22
- this.middlewares.get(path).push(...middlewareHandlers);
23
- }
24
- }
25
-
26
- const handler = handlers[handlers.length-1]
27
- this.trie.insert(path,{handler,method})
28
-
13
+ if (!this.trie.root.subMiddlewares.get(path).includes(...middlewareHandlers)) {
14
+ this.trie.root.subMiddlewares.get(path).push(...middlewareHandlers)
29
15
  }
30
- get(path,...handlers){
31
- return this.#addRoute("GET",path,handlers)
32
- }
33
16
 
34
- post(path,...handlers){
35
- return this.#addRoute("POST",path,handlers)
36
- }
17
+ const handler = handlers[handlers.length - 1];
18
+ this.trie.insert(path, { handler, method });
19
+ }
20
+ get(path, ...handlers) {
21
+ return this.#addRoute("GET", path, handlers);
22
+ }
37
23
 
38
- put(path,...handlers){
39
- return this.#addRoute("PUT",path,handlers)
40
- }
24
+ post(path, ...handlers) {
25
+ return this.#addRoute("POST", path, handlers);
26
+ }
41
27
 
42
- patch(path,...handlers){
43
- if (handlers.length>0) {
44
- return this.#addRoute("PATCH",path,handlers)
28
+ put(path, ...handlers) {
29
+ return this.#addRoute("PUT", path, handlers);
30
+ }
31
+
32
+ patch(path, ...handlers) {
33
+ if (handlers.length > 0) {
34
+ return this.#addRoute("PATCH", path, handlers);
45
35
  }
46
- }
36
+ }
47
37
 
48
- delete(path,...handlers){
49
- return this.#addRoute("DELETE",path,handlers)
50
- }
38
+ delete(path, ...handlers) {
39
+ return this.#addRoute("DELETE", path, handlers);
40
+ }
51
41
  }
52
42
 
53
- export default Router
43
+ export default Router;
package/src/server.js CHANGED
@@ -44,6 +44,7 @@ class diesel {
44
44
 
45
45
  register(pathPrefix,handlerInstance){
46
46
  const routeEntries = Object.entries(handlerInstance.trie.root.children);
47
+ // console.log(handlerInstance.trie.root);
47
48
  handlerInstance.trie.root.subMiddlewares.forEach((middleware,path)=>{
48
49
  if (!this.middlewares.has(pathPrefix+path)) {
49
50
  this.middlewares.set(pathPrefix+path, []);