@athenna/http 1.0.7 → 1.0.8

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.7",
3
+ "version": "1.0.8",
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>",
@@ -9,8 +9,8 @@
9
9
  "homepage": "https://github.com/AthennaIO/Http#readme",
10
10
  "scripts": {
11
11
  "build": "tsc --project tsconfig.json && tscpaths -p tsconfig.json -s . -o .",
12
- "test": "npm run lint:fix && cross-env NODE_TS=true cross-env NODE_ENV=testing jest --verbose",
13
- "test:debug": "npm run lint:fix && cross-env NODE_TS=true cross-env NODE_ENV=testing cross-env DEBUG=api:* jest --verbose",
12
+ "test": "npm run lint:fix && cross-env NODE_TS=true cross-env NODE_ENV=testing jest --verbose --runInBand",
13
+ "test:debug": "cross-env DEBUG=api:* && npm run test",
14
14
  "lint:fix": "eslint \"{src,tests}/**/*.ts\" --fix"
15
15
  },
16
16
  "keywords": [
@@ -7,13 +7,11 @@
7
7
  * file that was distributed with this source code.
8
8
  */
9
9
  import { RequestContract } from '../../RequestContract';
10
- import { NextInterceptContract } from './NextInterceptContract';
11
10
  export interface InterceptContextContract {
12
11
  request: RequestContract;
13
12
  params: Record<string, string>;
14
13
  queries: Record<string, string>;
15
- body: Record<string, any>;
14
+ body: any;
16
15
  status: number;
17
16
  data: Record<string, any>;
18
- next: NextInterceptContract;
19
17
  }
@@ -11,6 +11,6 @@ import { InterceptContextContract } from './Context/Middlewares/Intercept/Interc
11
11
  import { TerminateContextContract } from './Context/Middlewares/Terminate/TerminateContextContract';
12
12
  export interface MiddlewareContract {
13
13
  handle?: (ctx: HandleContextContract) => void | Promise<void>;
14
- intercept?: (ctx: InterceptContextContract) => void | Promise<void>;
14
+ intercept?: (ctx: InterceptContextContract) => any | Promise<any>;
15
15
  terminate?: (ctx: TerminateContextContract) => void | Promise<void>;
16
16
  }
package/src/Http.d.ts CHANGED
@@ -6,7 +6,8 @@
6
6
  * For the full copyright and license information, please view the LICENSE
7
7
  * file that was distributed with this source code.
8
8
  */
9
- import { FastifyInstance, PrintRoutesOptions } from 'fastify';
9
+ import LightMyRequest from 'light-my-request';
10
+ import { FastifyInstance, InjectOptions, PrintRoutesOptions } from 'fastify';
10
11
  import { HttpMethodTypes } from './Contracts/HttpMethodTypes';
11
12
  import { HandlerContract } from './Contracts/Context/HandlerContract';
12
13
  import { MiddlewareTypesContract } from './Contracts/MiddlewareTypesContract';
@@ -21,6 +22,8 @@ export declare class Http {
21
22
  getServer(): FastifyInstance;
22
23
  getRoutes(options?: PrintRoutesOptions): string;
23
24
  use(handler: HandleHandlerContract | InterceptHandlerContract | TerminateHandlerContract, type?: 'handle' | 'intercept' | 'terminate'): void;
25
+ request(): LightMyRequest.Chain;
26
+ request(options: InjectOptions | string): Promise<LightMyRequest.Response>;
24
27
  listen(port?: string | number, host?: string): Promise<string>;
25
28
  close(cb?: any): Promise<void>;
26
29
  route(url: string, methods: HttpMethodTypes[], handler: HandlerContract, middlewares?: MiddlewareTypesContract): void;
package/src/Http.js CHANGED
@@ -38,11 +38,18 @@ class Http {
38
38
  break;
39
39
  case 'terminate':
40
40
  hookName = 'onResponse';
41
- handlerType = 'createDoneHandler';
41
+ handlerType = 'createResponseHandler';
42
42
  break;
43
43
  }
44
44
  this.server.addHook(hookName, FastifyHandler_1.FastifyHandler[handlerType](handler));
45
45
  }
46
+ request(options) {
47
+ const server = this.getServer();
48
+ if (!options) {
49
+ return server.inject();
50
+ }
51
+ return server.inject(options);
52
+ }
46
53
  async listen(port, host) {
47
54
  return this.server.listen(port || 1335, host || 'localhost');
48
55
  }
@@ -60,7 +67,7 @@ class Http {
60
67
  method: methods,
61
68
  handler: FastifyHandler_1.FastifyHandler.createRequestHandler(handler),
62
69
  preHandler: handlers.map(m => FastifyHandler_1.FastifyHandler.createDoneHandler(m)),
63
- onResponse: terminators.map(m => FastifyHandler_1.FastifyHandler.createDoneHandler(m)),
70
+ onResponse: terminators.map(m => FastifyHandler_1.FastifyHandler.createResponseHandler(m)),
64
71
  onSend: interceptors.map(m => FastifyHandler_1.FastifyHandler.createOnSendHandler(m)),
65
72
  });
66
73
  }
@@ -16,7 +16,6 @@ import { HandlerContract } from '../Contracts/Context/HandlerContract';
16
16
  export declare class Router {
17
17
  routes: (Route | RouteResource | RouteGroup)[];
18
18
  private readonly openedGroups;
19
- private getRecentGroup;
20
19
  private readonly http;
21
20
  private controllerInstance;
22
21
  constructor(http: Http);
@@ -35,4 +34,5 @@ export declare class Router {
35
34
  any(url: string, handler: HandlerContract | string): Route;
36
35
  register(): void;
37
36
  toRoutesJSON(routes?: any): any;
37
+ private getRecentGroup;
38
38
  }
@@ -20,9 +20,6 @@ class Router {
20
20
  this.routes = [];
21
21
  this.openedGroups = [];
22
22
  }
23
- getRecentGroup() {
24
- return this.openedGroups[this.openedGroups.length - 1];
25
- }
26
23
  listRoutes() {
27
24
  return this.toRoutesJSON(this.routes);
28
25
  }
@@ -118,5 +115,8 @@ class Router {
118
115
  return list;
119
116
  }, []);
120
117
  }
118
+ getRecentGroup() {
119
+ return this.openedGroups[this.openedGroups.length - 1];
120
+ }
121
121
  }
122
122
  exports.Router = Router;
@@ -11,14 +11,16 @@ import { HandlerContract } from '../Contracts/Context/HandlerContract';
11
11
  import { ErrorHandlerContract } from '../Contracts/Context/Error/ErrorHandlerContract';
12
12
  import { HandleHandlerContract } from '../Contracts/Context/Middlewares/Handle/HandleHandlerContract';
13
13
  import { InterceptHandlerContract } from '../Contracts/Context/Middlewares/Intercept/InterceptHandlerContract';
14
+ import { TerminateHandlerContract } from '../Contracts/Context/Middlewares/Terminate/TerminateHandlerContract';
14
15
  declare module 'fastify' {
15
16
  interface FastifyRequest {
16
17
  data: Record<string, any>;
17
18
  }
18
19
  }
19
20
  export declare class FastifyHandler {
20
- static createOnSendHandler(handler: InterceptHandlerContract): (req: any, _res: any, payload: any, done: any) => any;
21
+ static createOnSendHandler(handler: InterceptHandlerContract): (req: any, _res: any, payload: any) => Promise<any>;
21
22
  static createDoneHandler(handler: HandleHandlerContract): (req: any, res: any, done: any) => any;
23
+ static createResponseHandler(handler: TerminateHandlerContract): (req: any, res: any, done: any) => any;
22
24
  static createErrorHandler(handler: ErrorHandlerContract): (error: any, req: FastifyRequest, res: FastifyReply) => any;
23
25
  static createRequestHandler(handler: HandlerContract): (req: FastifyRequest, res: FastifyReply) => Promise<any>;
24
26
  }
@@ -15,7 +15,7 @@ const Request_1 = require("../Context/Request");
15
15
  const Response_1 = require("../Context/Response");
16
16
  class FastifyHandler {
17
17
  static createOnSendHandler(handler) {
18
- return (req, _res, payload, done) => {
18
+ return async (req, _res, payload) => {
19
19
  const request = new Request_1.Request(req);
20
20
  if (!req.data)
21
21
  req.data = {};
@@ -27,19 +27,17 @@ class FastifyHandler {
27
27
  if (utils_1.Is.Json(payload)) {
28
28
  body = JSON.parse(body);
29
29
  }
30
- return handler({
30
+ body = await handler({
31
31
  request,
32
32
  body,
33
33
  status: _res.statusCode,
34
34
  params: req.params,
35
35
  queries: req.query,
36
36
  data: req.data,
37
- next: (body, error = null) => {
38
- if (utils_1.Is.Object(body))
39
- body = JSON.stringify(body);
40
- done(error, body);
41
- },
42
37
  });
38
+ if (utils_1.Is.Object(body))
39
+ body = JSON.stringify(body);
40
+ return body;
43
41
  };
44
42
  }
45
43
  static createDoneHandler(handler) {
@@ -62,6 +60,26 @@ class FastifyHandler {
62
60
  });
63
61
  };
64
62
  }
63
+ static createResponseHandler(handler) {
64
+ return (req, res, done) => {
65
+ const request = new Request_1.Request(req);
66
+ const response = new Response_1.Response(res);
67
+ if (!req.data)
68
+ req.data = {};
69
+ if (!req.query)
70
+ req.query = {};
71
+ if (!req.params)
72
+ req.params = {};
73
+ return handler({
74
+ request,
75
+ response,
76
+ params: req.params,
77
+ queries: req.query,
78
+ data: req.data,
79
+ next: done,
80
+ });
81
+ };
82
+ }
65
83
  static createErrorHandler(handler) {
66
84
  return (error, req, res) => {
67
85
  const request = new Request_1.Request(req);
@@ -1,11 +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 interface NextInterceptContract {
10
- (body: Record<string, any>, error?: any): Promise<void> | void;
11
- }
@@ -1,10 +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 });