@athenna/http 1.0.6 → 1.0.7

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@athenna/http",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "description": "The Athenna Http server. Built on top of fastify",
5
5
  "license": "MIT",
6
6
  "author": "João Lenon <lenon@athenna.io>",
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @athenna/http
3
+ *
4
+ * (c) João Lenon <lenon@athenna.io>
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ import { Exception } from '@secjs/utils';
10
+ export declare class UndefinedControllerMethodException extends Exception {
11
+ constructor(method: string, controller: string);
12
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ /**
3
+ * @athenna/http
4
+ *
5
+ * (c) João Lenon <lenon@athenna.io>
6
+ *
7
+ * For the full copyright and license information, please view the LICENSE
8
+ * file that was distributed with this source code.
9
+ */
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ exports.UndefinedControllerMethodException = void 0;
12
+ const utils_1 = require("@secjs/utils");
13
+ class UndefinedControllerMethodException extends utils_1.Exception {
14
+ constructor(method, controller) {
15
+ const content = `The method ${method} is not defined inside your controller ${controller}`;
16
+ super(content, 500, 'UNDEFINED_METHOD_ERROR', `Remember defining the method ${method} inside your controller ${controller}`);
17
+ }
18
+ }
19
+ exports.UndefinedControllerMethodException = UndefinedControllerMethodException;
@@ -11,32 +11,22 @@ import { MiddlewareTypes } from '../Contracts/MiddlewareTypes';
11
11
  import { HttpMethodTypes } from '../Contracts/HttpMethodTypes';
12
12
  import { MiddlewareContract } from '../Contracts/MiddlewareContract';
13
13
  import { HandlerContract } from '../Contracts/Context/HandlerContract';
14
- import { MiddlewareTypesContract } from '../Contracts/MiddlewareTypesContract';
15
14
  import { InterceptHandlerContract } from '../Contracts/Context/Middlewares/Intercept/InterceptHandlerContract';
16
15
  import { TerminateHandlerContract } from '../Contracts/Context/Middlewares/Terminate/TerminateHandlerContract';
17
16
  export declare class Route {
17
+ name: string;
18
+ deleted: boolean;
18
19
  private readonly url;
19
20
  private readonly handler;
20
21
  private readonly methods;
21
- name: string;
22
- deleted: boolean;
23
22
  private routeMiddlewares;
24
23
  private routeNamespace;
25
24
  private prefixes;
26
25
  constructor(url: string, methods: HttpMethodTypes[], handler: HandlerContract | string);
27
- private getUrl;
28
26
  prefix(prefix: any): this;
29
27
  as(name: string, prepend?: boolean): this;
30
28
  namespace(namespace: string, overwrite?: boolean): this;
31
29
  middleware(middleware: HandlerContract | MiddlewareContract | InterceptHandlerContract | TerminateHandlerContract | string, type?: MiddlewareTypes, prepend?: boolean): this;
32
- toJSON(): {
33
- name: string;
34
- url: string;
35
- handler: HandlerContract;
36
- methods: HttpMethodTypes[];
37
- middlewares: MiddlewareTypesContract;
38
- meta: {
39
- namespace: string;
40
- };
41
- };
30
+ toJSON(): any;
31
+ private getUrl;
42
32
  }
@@ -14,6 +14,7 @@ const utils_1 = require("@secjs/utils");
14
14
  const removeSlash_1 = require("../Utils/removeSlash");
15
15
  const isMiddlewareContract_1 = require("../Utils/isMiddlewareContract");
16
16
  const MiddlewareNotFoundException_1 = require("../Exceptions/MiddlewareNotFoundException");
17
+ const UndefinedControllerMethodException_1 = require("../Exceptions/UndefinedControllerMethodException");
17
18
  class Route {
18
19
  constructor(url, methods, handler) {
19
20
  this.url = url;
@@ -22,21 +23,8 @@ class Route {
22
23
  this.prefixes = [];
23
24
  this.deleted = false;
24
25
  this.routeMiddlewares = { handlers: [], terminators: [], interceptors: [] };
25
- if (utils_1.Is.String(handler)) {
26
- const [controller, method] = handler.split('.');
27
- handler = ioc.use(`App/Controllers/${controller}`)[method];
28
- }
29
26
  this.handler = handler;
30
27
  }
31
- getUrl() {
32
- const url = removeSlash_1.removeSlash(this.url);
33
- const prefix = this.prefixes
34
- .slice()
35
- .reverse()
36
- .map(p => removeSlash_1.removeSlash(p))
37
- .join('');
38
- return prefix ? `${prefix}${url === '/' ? '' : url}` : url;
39
- }
40
28
  prefix(prefix) {
41
29
  this.prefixes.push(prefix);
42
30
  return this;
@@ -63,37 +51,63 @@ class Route {
63
51
  if (!mid) {
64
52
  throw new MiddlewareNotFoundException_1.MiddlewareNotFoundException(middleware);
65
53
  }
66
- if (mid.handle)
67
- this.routeMiddlewares.handlers[insertionType](mid.handle);
68
- if (mid.intercept)
69
- this.routeMiddlewares.interceptors[insertionType](mid.intercept);
70
- if (mid.terminate)
71
- this.routeMiddlewares.terminators[insertionType](mid.terminate);
54
+ if (mid.handle) {
55
+ this.routeMiddlewares.handlers[insertionType](mid.handle.bind(mid));
56
+ }
57
+ if (mid.intercept) {
58
+ this.routeMiddlewares.interceptors[insertionType](mid.intercept.bind(mid));
59
+ }
60
+ if (mid.terminate) {
61
+ this.routeMiddlewares.terminators[insertionType](mid.terminate.bind(mid));
62
+ }
72
63
  return this;
73
64
  }
74
65
  if (isMiddlewareContract_1.isMiddlewareContract(middleware)) {
75
- if (middleware.handle)
76
- this.routeMiddlewares.handlers[insertionType](middleware.handle);
77
- if (middleware.intercept)
78
- this.routeMiddlewares.interceptors[insertionType](middleware.intercept);
79
- if (middleware.terminate)
80
- this.routeMiddlewares.terminators[insertionType](middleware.terminate);
66
+ if (middleware.handle) {
67
+ this.routeMiddlewares.handlers[insertionType](middleware.handle.bind(middleware));
68
+ }
69
+ if (middleware.intercept) {
70
+ this.routeMiddlewares.interceptors[insertionType](middleware.intercept.bind(middleware));
71
+ }
72
+ if (middleware.terminate) {
73
+ this.routeMiddlewares.terminators[insertionType](middleware.terminate.bind(middleware));
74
+ }
81
75
  return this;
82
76
  }
83
77
  this.routeMiddlewares[dictionary[type]][insertionType](middleware);
84
78
  return this;
85
79
  }
86
80
  toJSON() {
87
- return {
81
+ const json = {
88
82
  name: this.name,
89
83
  url: this.getUrl(),
90
- handler: this.handler,
91
84
  methods: this.methods,
92
85
  middlewares: this.routeMiddlewares,
93
86
  meta: {
94
87
  namespace: this.routeNamespace,
95
88
  },
96
89
  };
90
+ if (utils_1.Is.String(this.handler)) {
91
+ const [controller, method] = this.handler.split('.');
92
+ const dependency = ioc.use(`App/Controllers/${controller}`);
93
+ if (!dependency[method]) {
94
+ throw new UndefinedControllerMethodException_1.UndefinedControllerMethodException(method, controller);
95
+ }
96
+ json.handler = dependency[method].bind(dependency);
97
+ }
98
+ else {
99
+ json.handler = this.handler;
100
+ }
101
+ return json;
102
+ }
103
+ getUrl() {
104
+ const url = removeSlash_1.removeSlash(this.url);
105
+ const prefix = this.prefixes
106
+ .slice()
107
+ .reverse()
108
+ .map(p => removeSlash_1.removeSlash(p))
109
+ .join('');
110
+ return prefix ? `${prefix}${url === '/' ? '' : url}` : url;
97
111
  }
98
112
  }
99
113
  exports.Route = Route;
@@ -13,17 +13,17 @@ import { InterceptHandlerContract } from '../Contracts/Context/Middlewares/Inter
13
13
  import { TerminateHandlerContract } from '../Contracts/Context/Middlewares/Terminate/TerminateHandlerContract';
14
14
  import { MiddlewareContract } from '../Contracts/MiddlewareContract';
15
15
  export declare class RouteResource {
16
+ routes: Route[];
16
17
  private resource;
17
18
  private readonly controller;
18
19
  private resourceName;
19
- routes: Route[];
20
20
  constructor(resource: string, controller: any);
21
- private makeRoute;
22
- private buildRoutes;
23
- private filter;
24
21
  middleware(middleware: HandlerContract | MiddlewareContract | InterceptHandlerContract | TerminateHandlerContract | string, type?: MiddlewareTypes): this;
25
22
  only(names: string[]): this;
26
23
  except(names: string[]): this;
27
24
  namespace(namespace: string): this;
28
25
  as(name: string): this;
26
+ private makeRoute;
27
+ private buildRoutes;
28
+ private filter;
29
29
  }
@@ -22,6 +22,32 @@ class RouteResource {
22
22
  .join('.');
23
23
  this.buildRoutes();
24
24
  }
25
+ middleware(middleware, type = 'handle') {
26
+ this.routes.forEach(route => route.middleware(middleware, type));
27
+ return this;
28
+ }
29
+ only(names) {
30
+ this.filter(names, true).forEach(route => (route.deleted = true));
31
+ return this;
32
+ }
33
+ except(names) {
34
+ this.filter(names, false).forEach(route => (route.deleted = true));
35
+ return this;
36
+ }
37
+ namespace(namespace) {
38
+ this.routes.forEach(route => {
39
+ route.namespace(namespace);
40
+ });
41
+ return this;
42
+ }
43
+ as(name) {
44
+ name = utils_1.String.toSnakeCase(name);
45
+ this.routes.forEach(route => {
46
+ route.as(route.name.replace(this.resourceName, name), false);
47
+ });
48
+ this.resourceName = name;
49
+ return this;
50
+ }
25
51
  makeRoute(url, methods, action) {
26
52
  let handler = '';
27
53
  if (utils_1.Is.String(this.controller)) {
@@ -54,31 +80,5 @@ class RouteResource {
54
80
  return inverse ? !match : match;
55
81
  });
56
82
  }
57
- middleware(middleware, type = 'handle') {
58
- this.routes.forEach(route => route.middleware(middleware, type));
59
- return this;
60
- }
61
- only(names) {
62
- this.filter(names, true).forEach(route => (route.deleted = true));
63
- return this;
64
- }
65
- except(names) {
66
- this.filter(names, false).forEach(route => (route.deleted = true));
67
- return this;
68
- }
69
- namespace(namespace) {
70
- this.routes.forEach(route => {
71
- route.namespace(namespace);
72
- });
73
- return this;
74
- }
75
- as(name) {
76
- name = utils_1.String.toSnakeCase(name);
77
- this.routes.forEach(route => {
78
- route.as(route.name.replace(this.resourceName, name), false);
79
- });
80
- this.resourceName = name;
81
- return this;
82
- }
83
83
  }
84
84
  exports.RouteResource = RouteResource;