@athenna/http 1.2.3 → 1.2.6

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.2.3",
3
+ "version": "1.2.6",
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>",
@@ -159,10 +159,12 @@
159
159
  },
160
160
  "dependencies": {
161
161
  "@athenna/config": "1.0.8",
162
- "@athenna/ioc": "1.1.2",
163
- "@athenna/logger": "1.1.6",
162
+ "@athenna/ioc": "1.1.3",
163
+ "@athenna/logger": "1.1.7",
164
164
  "@secjs/utils": "1.8.3",
165
165
  "fastify": "3.27.4",
166
+ "fastify-cors": "6.0.3",
167
+ "fastify-rate-limit": "5.8.0",
166
168
  "reflect-metadata": "0.1.13",
167
169
  "tscpaths": "0.0.9"
168
170
  }
@@ -9,5 +9,5 @@
9
9
  */
10
10
  Object.defineProperty(exports, "__esModule", { value: true });
11
11
  exports.Route = void 0;
12
- const Handler_1 = require("../Utils/Handler");
13
- exports.Route = new Proxy({}, new Handler_1.Handler('Athenna/Core/HttpRoute'));
12
+ const ioc_1 = require("@athenna/ioc");
13
+ exports.Route = ioc_1.Facade.createFor('Athenna/Core/HttpRoute');
@@ -9,5 +9,5 @@
9
9
  */
10
10
  Object.defineProperty(exports, "__esModule", { value: true });
11
11
  exports.Server = void 0;
12
- const Handler_1 = require("../Utils/Handler");
13
- exports.Server = new Proxy({}, new Handler_1.Handler('Athenna/Core/HttpServer'));
12
+ const ioc_1 = require("@athenna/ioc");
13
+ exports.Server = ioc_1.Facade.createFor('Athenna/Core/HttpServer');
@@ -34,7 +34,7 @@ class HttpErrorHandler {
34
34
  delete body.stack;
35
35
  }
36
36
  if (isDebugMode) {
37
- new logger_1.Logger().error(`Error: ${JSON.stringify(body, null, 2)}`, {
37
+ logger_1.Log.error(`Error: ${JSON.stringify(body, null, 2)}`, {
38
38
  formatterConfig: {
39
39
  context: HttpErrorHandler.name,
40
40
  },
package/src/Http.d.ts CHANGED
@@ -7,7 +7,9 @@
7
7
  * file that was distributed with this source code.
8
8
  */
9
9
  import LightMyRequest from 'light-my-request';
10
- import { FastifyInstance, InjectOptions, PrintRoutesOptions } from 'fastify';
10
+ import { FastifyCorsOptions } from 'fastify-cors';
11
+ import { RateLimitPluginOptions } from 'fastify-rate-limit';
12
+ import { FastifyInstance, FastifyPluginCallback, FastifyPluginOptions, FastifyRegisterOptions, InjectOptions, PrintRoutesOptions } from 'fastify';
11
13
  import { HttpMethodTypes } from './Contracts/HttpMethodTypes';
12
14
  import { HandlerContract } from './Contracts/Context/HandlerContract';
13
15
  import { MiddlewareTypesContract } from './Contracts/MiddlewareTypesContract';
@@ -34,6 +36,20 @@ export declare class Http {
34
36
  * @return Http
35
37
  */
36
38
  setErrorHandler(handler: ErrorHandlerContract): void;
39
+ /**
40
+ * Register a new fastify plugin.
41
+ *
42
+ * @return FastifyInstance
43
+ */
44
+ register(plugin: FastifyPluginCallback<FastifyPluginOptions>, opts?: FastifyRegisterOptions<FastifyPluginOptions>): void;
45
+ /**
46
+ * Register the cors plugin to fastify server.
47
+ */
48
+ registerCors(opts?: FastifyCorsOptions): void;
49
+ /**
50
+ * Register the rateLimit plugin to fastify server.
51
+ */
52
+ registerRateLimit(opts?: RateLimitPluginOptions): void;
37
53
  /**
38
54
  * Get the fastify server instance.
39
55
  *
package/src/Http.js CHANGED
@@ -12,6 +12,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  };
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
14
  exports.Http = void 0;
15
+ const fastify_cors_1 = __importDefault(require("fastify-cors"));
16
+ const fastify_rate_limit_1 = __importDefault(require("fastify-rate-limit"));
15
17
  const fastify_1 = __importDefault(require("fastify"));
16
18
  const FastifyHandler_1 = require("./Handlers/FastifyHandler");
17
19
  class Http {
@@ -32,6 +34,26 @@ class Http {
32
34
  const fastifyErrorHandler = FastifyHandler_1.FastifyHandler.createErrorHandler(handler);
33
35
  this.server.setErrorHandler(fastifyErrorHandler);
34
36
  }
37
+ /**
38
+ * Register a new fastify plugin.
39
+ *
40
+ * @return FastifyInstance
41
+ */
42
+ register(plugin, opts) {
43
+ this.server.register(plugin, opts);
44
+ }
45
+ /**
46
+ * Register the cors plugin to fastify server.
47
+ */
48
+ registerCors(opts) {
49
+ this.register(fastify_cors_1.default, opts);
50
+ }
51
+ /**
52
+ * Register the rateLimit plugin to fastify server.
53
+ */
54
+ registerRateLimit(opts) {
55
+ this.register(fastify_rate_limit_1.default, opts);
56
+ }
35
57
  /**
36
58
  * Get the fastify server instance.
37
59
  *
@@ -16,17 +16,33 @@ export declare abstract class HttpKernel {
16
16
  */
17
17
  protected abstract namedMiddlewares: Record<string, Promise<any> | MiddlewareContractClass>;
18
18
  /**
19
- * Returns an instance of any class that extends HttpKernel.
20
- * Also configure the error handler, detect environment and
21
- * configure log intercept middleware for requests.
19
+ * Register all global and named middlewares to the server.
22
20
  *
23
- * @return HttpKernel
21
+ * @return void
24
22
  */
25
- constructor();
23
+ registerMiddlewares(): Promise<void>;
26
24
  /**
27
- * Register all global and named middlewares to the server.
25
+ * Register cors plugin
28
26
  *
29
27
  * @return void
30
28
  */
31
- registerMiddlewares(): Promise<void>;
29
+ registerCors(): Promise<void>;
30
+ /**
31
+ * Register rate limit plugin
32
+ *
33
+ * @return void
34
+ */
35
+ registerRateLimit(): Promise<void>;
36
+ /**
37
+ * Register the default error handler
38
+ *
39
+ * @return void
40
+ */
41
+ registerErrorHandler(): Promise<void>;
42
+ /**
43
+ * Register log terminate middleware
44
+ *
45
+ * @return void
46
+ */
47
+ registerLogMiddleware(): Promise<void>;
32
48
  }
@@ -1,35 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.HttpKernel = void 0;
4
- const config_1 = require("@athenna/config");
5
4
  const logger_1 = require("@athenna/logger");
5
+ const config_1 = require("@athenna/config");
6
6
  const utils_1 = require("@secjs/utils");
7
7
  const HttpErrorHandler_1 = require("../Handlers/HttpErrorHandler");
8
+ const Server_1 = require("../Facades/Server");
8
9
  class HttpKernel {
9
- /**
10
- * Returns an instance of any class that extends HttpKernel.
11
- * Also configure the error handler, detect environment and
12
- * configure log intercept middleware for requests.
13
- *
14
- * @return HttpKernel
15
- */
16
- constructor() {
17
- const httpServer = ioc.safeUse('Athenna/Core/HttpServer');
18
- httpServer.setErrorHandler(HttpErrorHandler_1.HttpErrorHandler.handler);
19
- if (config_1.Config.get('http.log')) {
20
- httpServer.use(async (ctx) => {
21
- await new logger_1.Logger().channel('request').log(ctx);
22
- return ctx.next();
23
- }, 'terminate');
24
- }
25
- }
26
10
  /**
27
11
  * Register all global and named middlewares to the server.
28
12
  *
29
13
  * @return void
30
14
  */
31
15
  async registerMiddlewares() {
32
- const httpServer = ioc.safeUse('Athenna/Core/HttpServer');
33
16
  /**
34
17
  * Binding the named middlewares inside the container and
35
18
  * creating a simple alias to use it inside Route.
@@ -52,15 +35,62 @@ class HttpKernel {
52
35
  }
53
36
  Middleware = ioc.safeUse(`App/Middlewares/${Middleware.name}`);
54
37
  if (Middleware.handle) {
55
- httpServer.use(Middleware.handle, 'handle');
38
+ Server_1.Server.use(Middleware.handle, 'handle');
56
39
  }
57
40
  if (Middleware.intercept) {
58
- httpServer.use(Middleware.intercept, 'intercept');
41
+ Server_1.Server.use(Middleware.intercept, 'intercept');
59
42
  }
60
43
  if (Middleware.terminate) {
61
- httpServer.use(Middleware.terminate, 'terminate');
44
+ Server_1.Server.use(Middleware.terminate, 'terminate');
62
45
  }
63
46
  }
64
47
  }
48
+ /**
49
+ * Register cors plugin
50
+ *
51
+ * @return void
52
+ */
53
+ async registerCors() {
54
+ if (config_1.Config.get('http.noCors')) {
55
+ return;
56
+ }
57
+ Server_1.Server.registerCors(config_1.Config.get('http.cors'));
58
+ }
59
+ /**
60
+ * Register rate limit plugin
61
+ *
62
+ * @return void
63
+ */
64
+ async registerRateLimit() {
65
+ if (config_1.Config.get('http.noRateLimit')) {
66
+ return;
67
+ }
68
+ Server_1.Server.registerRateLimit(config_1.Config.get('http.rateLimit'));
69
+ }
70
+ /**
71
+ * Register the default error handler
72
+ *
73
+ * @return void
74
+ */
75
+ async registerErrorHandler() {
76
+ if (config_1.Config.get('http.noErrorHandler')) {
77
+ return;
78
+ }
79
+ Server_1.Server.setErrorHandler(HttpErrorHandler_1.HttpErrorHandler.handler);
80
+ }
81
+ /**
82
+ * Register log terminate middleware
83
+ *
84
+ * @return void
85
+ */
86
+ async registerLogMiddleware() {
87
+ if (!config_1.Config.get('http.logRequests')) {
88
+ return;
89
+ }
90
+ Server_1.Server.use(async (ctx) => {
91
+ await logger_1.Log.channel('request').log(ctx);
92
+ return ctx.next();
93
+ }, 'terminate');
94
+ }
65
95
  }
66
96
  exports.HttpKernel = HttpKernel;
@@ -24,6 +24,7 @@ export declare class Router {
24
24
  route(url: string, methods: HttpMethodTypes[], handler: HandlerContract | string): Route;
25
25
  group(callback: () => void): RouteGroup;
26
26
  resource(resource: string, controller: any): RouteResource;
27
+ redirect(url: string, redirectTo: string, status?: number): Route;
27
28
  get(url: string, handler: HandlerContract | string): Route;
28
29
  head(url: string, handler: HandlerContract | string): Route;
29
30
  post(url: string, handler: HandlerContract | string): Route;
@@ -68,6 +68,9 @@ class Router {
68
68
  }
69
69
  return resourceInstance;
70
70
  }
71
+ redirect(url, redirectTo, status = 302) {
72
+ return this.any(url, ({ response }) => response.redirectTo(redirectTo, status));
73
+ }
71
74
  get(url, handler) {
72
75
  return this.route(url, ['GET', 'HEAD'], handler);
73
76
  }
@@ -1,13 +0,0 @@
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
- export declare class Handler {
10
- private readonly alias;
11
- constructor(alias: any);
12
- get(_object: any, key: string): any;
13
- }
@@ -1,24 +0,0 @@
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.Handler = void 0;
12
- class Handler {
13
- constructor(alias) {
14
- this.alias = alias;
15
- }
16
- get(_object, key) {
17
- const provider = ioc.use(this.alias);
18
- if (!provider) {
19
- return () => { };
20
- }
21
- return provider[key];
22
- }
23
- }
24
- exports.Handler = Handler;