@athenna/http 1.3.3 → 1.3.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.
Files changed (95) hide show
  1. package/LICENSE.md +3 -15
  2. package/README.md +8 -5
  3. package/package.json +95 -106
  4. package/src/Context/Request.js +168 -137
  5. package/src/Context/Response.js +102 -94
  6. package/src/Exceptions/UndefinedMethodException.js +30 -0
  7. package/src/Facades/Route.js +7 -5
  8. package/src/Facades/Server.js +7 -5
  9. package/src/Handlers/FastifyHandler.js +134 -115
  10. package/src/Handlers/HttpExceptionHandler.js +72 -68
  11. package/src/Kernels/HttpKernel.js +142 -124
  12. package/src/Providers/ControllerProvider.js +30 -39
  13. package/src/Providers/HttpRouteProvider.js +15 -16
  14. package/src/Providers/HttpServerProvider.js +13 -15
  15. package/src/Providers/MiddlewareProvider.js +30 -39
  16. package/src/Router/Route.js +192 -99
  17. package/src/Router/RouteGroup.js +66 -40
  18. package/src/Router/RouteResource.js +156 -74
  19. package/src/Router/Router.js +284 -112
  20. package/src/Utils/isMiddlewareContract.js +8 -6
  21. package/src/Utils/removeSlashes.js +21 -0
  22. package/src/index.d.ts +658 -0
  23. package/src/index.js +284 -0
  24. package/index.d.ts +0 -29
  25. package/index.js +0 -41
  26. package/src/Context/Request.d.ts +0 -103
  27. package/src/Context/Response.d.ts +0 -77
  28. package/src/Contracts/Context/ContextContract.d.ts +0 -17
  29. package/src/Contracts/Context/ContextContract.js +0 -10
  30. package/src/Contracts/Context/Error/ErrorContextContract.d.ts +0 -18
  31. package/src/Contracts/Context/Error/ErrorContextContract.js +0 -10
  32. package/src/Contracts/Context/Error/ErrorHandlerContract.d.ts +0 -12
  33. package/src/Contracts/Context/Error/ErrorHandlerContract.js +0 -10
  34. package/src/Contracts/Context/HandlerContract.d.ts +0 -12
  35. package/src/Contracts/Context/HandlerContract.js +0 -10
  36. package/src/Contracts/Context/Middlewares/Handle/HandleContextContract.d.ts +0 -19
  37. package/src/Contracts/Context/Middlewares/Handle/HandleContextContract.js +0 -10
  38. package/src/Contracts/Context/Middlewares/Handle/HandleHandlerContract.d.ts +0 -12
  39. package/src/Contracts/Context/Middlewares/Handle/HandleHandlerContract.js +0 -10
  40. package/src/Contracts/Context/Middlewares/Intercept/InterceptContextContract.d.ts +0 -19
  41. package/src/Contracts/Context/Middlewares/Intercept/InterceptContextContract.js +0 -10
  42. package/src/Contracts/Context/Middlewares/Intercept/InterceptHandlerContract.d.ts +0 -12
  43. package/src/Contracts/Context/Middlewares/Intercept/InterceptHandlerContract.js +0 -10
  44. package/src/Contracts/Context/Middlewares/Terminate/TerminateContextContract.d.ts +0 -23
  45. package/src/Contracts/Context/Middlewares/Terminate/TerminateContextContract.js +0 -10
  46. package/src/Contracts/Context/Middlewares/Terminate/TerminateHandlerContract.d.ts +0 -12
  47. package/src/Contracts/Context/Middlewares/Terminate/TerminateHandlerContract.js +0 -10
  48. package/src/Contracts/Context/NextContract.d.ts +0 -11
  49. package/src/Contracts/Context/NextContract.js +0 -10
  50. package/src/Contracts/Context/RequestContract.d.ts +0 -25
  51. package/src/Contracts/Context/RequestContract.js +0 -10
  52. package/src/Contracts/Context/ResponseContract.d.ts +0 -20
  53. package/src/Contracts/Context/ResponseContract.js +0 -2
  54. package/src/Contracts/FastifyErrorHandlerContract.d.ts +0 -12
  55. package/src/Contracts/FastifyErrorHandlerContract.js +0 -10
  56. package/src/Contracts/FastifyHandlerContract.d.ts +0 -12
  57. package/src/Contracts/FastifyHandlerContract.js +0 -10
  58. package/src/Contracts/HttpMethodTypes.d.ts +0 -9
  59. package/src/Contracts/HttpMethodTypes.js +0 -10
  60. package/src/Contracts/MiddlewareContract.d.ts +0 -16
  61. package/src/Contracts/MiddlewareContract.js +0 -10
  62. package/src/Contracts/MiddlewareTypes.d.ts +0 -9
  63. package/src/Contracts/MiddlewareTypes.js +0 -10
  64. package/src/Contracts/MiddlewareTypesContract.d.ts +0 -16
  65. package/src/Contracts/MiddlewareTypesContract.js +0 -10
  66. package/src/Contracts/RouteContract.d.ts +0 -16
  67. package/src/Contracts/RouteContract.js +0 -10
  68. package/src/Exceptions/BadRequestException.d.ts +0 -12
  69. package/src/Exceptions/BadRequestException.js +0 -18
  70. package/src/Exceptions/CannotDefineGroupException.d.ts +0 -12
  71. package/src/Exceptions/CannotDefineGroupException.js +0 -19
  72. package/src/Exceptions/MiddlewareNotFoundException.d.ts +0 -12
  73. package/src/Exceptions/MiddlewareNotFoundException.js +0 -19
  74. package/src/Exceptions/UndefinedControllerMethodException.d.ts +0 -12
  75. package/src/Exceptions/UndefinedControllerMethodException.js +0 -19
  76. package/src/Facades/Route.d.ts +0 -10
  77. package/src/Facades/Server.d.ts +0 -10
  78. package/src/Handlers/FastifyHandler.d.ts +0 -26
  79. package/src/Handlers/HttpExceptionHandler.d.ts +0 -27
  80. package/src/Http.d.ts +0 -167
  81. package/src/Http.js +0 -223
  82. package/src/Kernels/HttpKernel.d.ts +0 -54
  83. package/src/Providers/ControllerProvider.d.ts +0 -17
  84. package/src/Providers/HttpRouteProvider.d.ts +0 -17
  85. package/src/Providers/HttpServerProvider.d.ts +0 -17
  86. package/src/Providers/MiddlewareProvider.d.ts +0 -17
  87. package/src/Router/Route.d.ts +0 -32
  88. package/src/Router/RouteGroup.d.ts +0 -24
  89. package/src/Router/RouteResource.d.ts +0 -29
  90. package/src/Router/Router.d.ts +0 -39
  91. package/src/Utils/getAppFiles.d.ts +0 -10
  92. package/src/Utils/getAppFiles.js +0 -22
  93. package/src/Utils/isMiddlewareContract.d.ts +0 -10
  94. package/src/Utils/removeSlash.d.ts +0 -9
  95. package/src/Utils/removeSlash.js +0 -23
package/LICENSE.md CHANGED
@@ -2,20 +2,8 @@ MIT License
2
2
 
3
3
  Copyright (c) 2022 The Athenna Framework
4
4
 
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
11
6
 
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14
8
 
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,9 +1,9 @@
1
- # Http 📶
1
+ # Http 📡
2
2
 
3
- > The Athenna Http server. Built on top of fastify
3
+ > The Athenna Http server. Built on top of fastify.
4
4
 
5
5
  [![GitHub followers](https://img.shields.io/github/followers/athennaio.svg?style=social&label=Follow&maxAge=2592000)](https://github.com/athennaio?tab=followers)
6
- [![GitHub stars](https://img.shields.io/github/stars/athennaio/http.svg?style=social&label=Star&maxAge=2592000)](https://github.com/athennaio/http/stargazers/)
6
+ [![GitHub stars](https://img.shields.io/github/stars/AthennaIO/Http.svg?style=social&label=Star&maxAge=2592000)](https://github.com/AthennaIO/Http/stargazers/)
7
7
 
8
8
  <p>
9
9
  <a href="https://www.buymeacoffee.com/athenna" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>
@@ -23,11 +23,14 @@
23
23
 
24
24
  ## Links
25
25
 
26
- > For project documentation [click here](https://athenna.io). If something is not clear in the documentation please open an issue in the [documentation repository](https://github.com/athennaio/docs)
26
+ > For project documentation [click here](https://athenna.io). If something is not clear in the documentation please open
27
+ > an issue in the [documentation repository](https://github.com/athennaio/docs)
27
28
 
28
29
  ## Contributing
29
30
 
30
- > If you want to contribute to this project, first read the [CONTRIBUTING.MD](https://github.com/AthennaIO/Http/blob/develop/CONTRIBUTING.md) file. It will be a pleasure to receive your help.
31
+ > If you want to contribute to this project, first read
32
+ > the [CONTRIBUTING.MD](https://github.com/AthennaIO/Http/blob/develop/CONTRIBUTING.md) file. It will be a pleasure to
33
+ > receive your help.
31
34
 
32
35
  ---
33
36
 
package/package.json CHANGED
@@ -1,54 +1,100 @@
1
1
  {
2
2
  "name": "@athenna/http",
3
- "version": "1.3.3",
4
- "description": "The Athenna Http server. Built on top of fastify",
3
+ "version": "1.3.6",
4
+ "description": "The Athenna Http server. Built on top of fastify.",
5
5
  "license": "MIT",
6
6
  "author": "João Lenon <lenon@athenna.io>",
7
- "repository": "https://github.com/AthennaIO/Http.git",
8
7
  "bugs": "https://github.com/AthennaIO/Http/issues",
8
+ "repository": "https://github.com/AthennaIO/Http.git",
9
9
  "homepage": "https://github.com/AthennaIO/Http#readme",
10
- "scripts": {
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 --runInBand",
13
- "test:debug": "cross-env DEBUG=api:* && npm run test",
14
- "lint:fix": "eslint \"{src,tests}/**/*.ts\" --fix"
15
- },
16
10
  "keywords": [
17
- "nodejs",
18
11
  "fastify",
19
12
  "router",
13
+ "cors",
14
+ "ctx",
15
+ "exception-handling",
16
+ "rate-limiting",
20
17
  "middlewares",
21
18
  "http-server",
22
19
  "athenna",
23
- "typescript"
20
+ "esm"
21
+ ],
22
+ "scripts": {
23
+ "lint:fix": "eslint \"{src,tests}/**/*.js\" --fix",
24
+ "test": "npm run --silent lint:fix && node ./tests/index.js",
25
+ "test:debug": "cross-env DEBUG=api:* && npm run --silent test",
26
+ "test:coverage": "c8 npm run --silent test",
27
+ "gen:types": "tsc src/*.js --declaration --allowJs --emitDeclarationOnly --out src/index.js"
28
+ },
29
+ "files": [
30
+ "src/*.js",
31
+ "src/*.d.ts",
32
+ "src/**/*.js",
33
+ "src/**/*.d.ts"
24
34
  ],
35
+ "type": "module",
36
+ "main": "./src/index.js",
37
+ "exports": {
38
+ ".": "./src/index.js",
39
+ "./facades/Route": "./src/Facades/Route.js",
40
+ "./facades/Server": "./src/Facades/Server.js",
41
+ "./providers/ControllerProvider": "./src/Providers/ControllerProvider.js",
42
+ "./providers/HttpRouteProvider": "./src/Providers/HttpRouteProvider.js",
43
+ "./providers/HttpServerProvider": "./src/Providers/HttpServerProvider.js",
44
+ "./providers/MiddlewareProvider": "./src/Providers/MiddlewareProvider.js"
45
+ },
46
+ "types": "./src/index.d.ts",
47
+ "imports": {
48
+ "#src/*": "./src/*.js",
49
+ "#tests/*": "./tests/*.js"
50
+ },
51
+ "dependencies": {
52
+ "@athenna/ioc": "1.1.8",
53
+ "@athenna/logger": "1.2.3",
54
+ "@secjs/utils": "1.9.3",
55
+ "fastify": "3.27.4",
56
+ "fastify-cors": "6.0.3",
57
+ "fastify-rate-limit": "5.8.0"
58
+ },
25
59
  "devDependencies": {
26
- "@types/jest": "27.0.1",
27
- "@types/node": "14.17.0",
28
- "@typescript-eslint/eslint-plugin": "4.31.0",
29
- "@typescript-eslint/parser": "4.31.0",
60
+ "@japa/assert": "1.3.4",
61
+ "@japa/run-failed-tests": "1.0.7",
62
+ "@japa/runner": "2.0.7",
63
+ "@japa/spec-reporter": "1.1.12",
64
+ "c8": "7.11.2",
30
65
  "commitizen": "4.2.4",
31
66
  "cross-env": "7.0.3",
32
67
  "cz-conventional-changelog": "3.3.0",
33
- "eslint": "7.32.0",
34
- "eslint-config-prettier": "8.3.0",
35
- "eslint-config-standard": "16.0.3",
36
- "eslint-import-resolver-typescript": "2.4.0",
37
- "eslint-plugin-import": "2.24.2",
38
- "eslint-plugin-node": "11.1.0",
68
+ "eslint": "8.14.0",
69
+ "eslint-config-prettier": "8.5.0",
70
+ "eslint-config-standard": "17.0.0",
71
+ "eslint-plugin-import": "2.26.0",
72
+ "eslint-plugin-n": "15.2.0",
39
73
  "eslint-plugin-prettier": "4.0.0",
40
- "eslint-plugin-promise": "5.1.0",
41
- "eslint-plugin-standard": "5.0.0",
74
+ "eslint-plugin-promise": "6.0.0",
42
75
  "husky": "3.0.9",
43
- "jest": "27.1.0",
44
- "lint-staged": "9.4.3",
45
- "prettier": "2.0.5",
46
- "supertest": "^6.2.2",
47
- "ts-jest": "27.0.5",
48
- "ts-loader": "9.2.3",
49
- "ts-node": "10.0.0",
50
- "tsconfig-paths": "3.9.0",
51
- "typescript": "4.3.5"
76
+ "lint-staged": "12.4.1",
77
+ "minimist": "1.2.6",
78
+ "prettier": "2.6.2",
79
+ "rimraf": "3.0.2",
80
+ "typescript": "4.6.4"
81
+ },
82
+ "c8": {
83
+ "all": true,
84
+ "include": [
85
+ "src/**/*.js"
86
+ ],
87
+ "reporter": [
88
+ "text-summary",
89
+ "html"
90
+ ],
91
+ "report-dir": "./tests/Coverage",
92
+ "check-coverage": true
93
+ },
94
+ "husky": {
95
+ "hooks": {
96
+ "prepare-commit-msg": "lint-staged && exec < /dev/tty && git cz --hook || true"
97
+ }
52
98
  },
53
99
  "lint-staged": {
54
100
  "*.js": [
@@ -60,30 +106,11 @@
60
106
  "git add"
61
107
  ]
62
108
  },
63
- "husky": {
64
- "hooks": {
65
- "pre-commit": "lint-staged",
66
- "prepare-commit-msg": "exec < /dev/tty && git cz --hook || true"
109
+ "config": {
110
+ "commitizen": {
111
+ "path": "./node_modules/cz-conventional-changelog"
67
112
  }
68
113
  },
69
- "jest": {
70
- "modulePaths": [
71
- "<rootDir>"
72
- ],
73
- "moduleFileExtensions": [
74
- "js",
75
- "json",
76
- "ts"
77
- ],
78
- "rootDir": ".",
79
- "testRegex": "Test.ts$",
80
- "transform": {
81
- "^.+\\.(t|j)s$": "ts-jest"
82
- },
83
- "coverageDirectory": "./coverage",
84
- "preset": "ts-jest",
85
- "testEnvironment": "node"
86
- },
87
114
  "prettier": {
88
115
  "singleQuote": true,
89
116
  "trailingComma": "all",
@@ -102,70 +129,32 @@
102
129
  },
103
130
  "eslintConfig": {
104
131
  "env": {
105
- "es6": true,
132
+ "es2021": true,
106
133
  "node": true
107
134
  },
135
+ "globals": {
136
+ "ioc": true
137
+ },
138
+ "plugins": [
139
+ "prettier"
140
+ ],
108
141
  "extends": [
109
142
  "standard",
110
- "plugin:@typescript-eslint/recommended",
143
+ "eslint:recommended",
111
144
  "plugin:prettier/recommended"
112
145
  ],
113
- "globals": {
114
- "Atomics": "readonly",
115
- "SharedArrayBuffer": "readonly"
116
- },
117
- "parser": "@typescript-eslint/parser",
146
+ "ignorePatterns": [
147
+ "**/*.d.ts"
148
+ ],
118
149
  "parserOptions": {
119
- "ecmaVersion": 2018,
150
+ "ecmaVersion": "latest",
120
151
  "sourceType": "module"
121
152
  },
122
- "plugins": [
123
- "@typescript-eslint",
124
- "prettier"
125
- ],
126
153
  "rules": {
127
- "dot-notation": "off",
128
154
  "camelcase": "off",
129
- "no-undef": "off",
130
- "@typescript-eslint/no-empty-function": "off",
131
- "@typescript-eslint/no-var-requires": "off",
132
- "no-useless-constructor": "off",
133
- "@typescript-eslint/no-useless-constructor": "off",
134
- "@typescript-eslint/camelcase": "off",
135
- "@typescript-eslint/explicit-module-boundary-types": "off",
136
- "@typescript-eslint/interface-name-prefix": "off",
137
- "@typescript-eslint/no-explicit-any": "off",
155
+ "dot-notation": "off",
138
156
  "prettier/prettier": "error",
139
- "@typescript-eslint/no-unused-vars": [
140
- "error",
141
- {
142
- "argsIgnorePattern": "_"
143
- }
144
- ]
157
+ "no-useless-constructor": "off"
145
158
  }
146
- },
147
- "files": [
148
- "src/*.js",
149
- "src/*.d.ts",
150
- "src/**/*.js",
151
- "src/**/*.d.ts",
152
- "*.js",
153
- "*.d.ts"
154
- ],
155
- "config": {
156
- "commitizen": {
157
- "path": "./node_modules/cz-conventional-changelog"
158
- }
159
- },
160
- "dependencies": {
161
- "@athenna/config": "1.0.8",
162
- "@athenna/ioc": "1.1.3",
163
- "@athenna/logger": "1.1.8",
164
- "@secjs/utils": "1.8.3",
165
- "fastify": "3.27.4",
166
- "fastify-cors": "6.0.3",
167
- "fastify-rate-limit": "5.8.0",
168
- "reflect-metadata": "0.1.13",
169
- "tscpaths": "0.0.9"
170
159
  }
171
160
  }
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  /**
3
2
  * @athenna/http
4
3
  *
@@ -7,141 +6,173 @@
7
6
  * For the full copyright and license information, please view the LICENSE
8
7
  * file that was distributed with this source code.
9
8
  */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.Request = void 0;
12
- const utils_1 = require("@secjs/utils");
13
- const config_1 = require("@athenna/config");
14
- const removeSlash_1 = require("../Utils/removeSlash");
15
- class Request {
16
- /**
17
- * Create a new instance of Request.
18
- *
19
- * @return Request
20
- */
21
- constructor(request) {
22
- this.request = request;
23
- }
24
- /**
25
- * Get the request ip.
26
- *
27
- * @return string
28
- */
29
- get ip() {
30
- return this.request.ip;
31
- }
32
- /**
33
- * Get the request method.
34
- *
35
- * @return string
36
- */
37
- get method() {
38
- return this.request.method;
39
- }
40
- /**
41
- * Get the host url from request.
42
- *
43
- * @return string
44
- */
45
- get hostUrl() {
46
- const url = this.request.url;
47
- const port = config_1.Config.get('http.port', 1335);
48
- let host = config_1.Config.get('http.domain', `http://localhost:${port}`);
49
- if (!utils_1.Is.Ip(host) && !host.includes('localhost')) {
50
- host = host.replace(`:${port}`, '');
51
- }
52
- return removeSlash_1.removeSlash(`${host}${url}`);
53
- }
54
- /**
55
- * Get the base request url.
56
- *
57
- * @return string
58
- */
59
- get baseUrl() {
60
- return this.request.url.split('?')[0];
61
- }
62
- /**
63
- * Get the original request url.
64
- *
65
- * @return string
66
- */
67
- get originalUrl() {
68
- return this.request.url;
69
- }
70
- /**
71
- * Get all body from request.
72
- *
73
- * @return any
74
- */
75
- get body() {
76
- return this.request.body;
77
- }
78
- /**
79
- * Get all params from request.
80
- *
81
- * @return any
82
- */
83
- get params() {
84
- return this.request.params;
85
- }
86
- /**
87
- * Get all queries from request.
88
- *
89
- * @return any
90
- */
91
- get queries() {
92
- return this.request.query;
93
- }
94
- /**
95
- * Get all headers from request.
96
- *
97
- * @return any
98
- */
99
- get headers() {
100
- return this.request.headers;
101
- }
102
- /**
103
- * Get a value from the request params or the default value.
104
- *
105
- * @return any
106
- */
107
- param(param, defaultValue) {
108
- const params = this.request.params;
109
- return params[param] || defaultValue;
110
- }
111
- /**
112
- * Get a value from the request query param or the default value.
113
- *
114
- * @return any
115
- */
116
- query(query, defaultValue) {
117
- const queries = this.request.query;
118
- return queries[query] || defaultValue;
119
- }
120
- /**
121
- * Get a value from the request header or the default value.
122
- *
123
- * @return any
124
- */
125
- header(header, defaultValue) {
126
- const headers = this.request.headers;
127
- return headers[header] || defaultValue;
128
- }
129
- /**
130
- * Get a value from the request body or the default value.
131
- *
132
- * @return any
133
- */
134
- payload(payload, defaultValue) {
135
- const body = this.request.body;
136
- return body[payload] || defaultValue;
137
- }
138
- /**
139
- * Get the default fastify request object.
140
- *
141
- * @return FastifyRequest
142
- */
143
- getFastifyRequest() {
144
- return this.request;
9
+
10
+ import { Config, Is } from '@secjs/utils'
11
+
12
+ export class Request {
13
+ /**
14
+ * Fastify request object.
15
+ *
16
+ * @type {import('fastify').FastifyRequest}
17
+ */
18
+ #request
19
+
20
+ /**
21
+ * Create a new instance of Request.
22
+ *
23
+ * @param {import('fastify').FastifyRequest} request
24
+ * @return {Request}
25
+ */
26
+ constructor(request) {
27
+ this.#request = request
28
+ }
29
+
30
+ /**
31
+ * Get the request ip.
32
+ *
33
+ * @return {string}
34
+ */
35
+ get ip() {
36
+ return this.#request.ip
37
+ }
38
+
39
+ /**
40
+ * Get the request method.
41
+ *
42
+ * @return {string}
43
+ */
44
+ get method() {
45
+ return this.#request.method
46
+ }
47
+
48
+ /**
49
+ * Get the host url from request.
50
+ *
51
+ * @return {string}
52
+ */
53
+ get hostUrl() {
54
+ const port = Config.get('http.port', 1335)
55
+ let host = Config.get('http.domain', `http://localhost:${port}`)
56
+
57
+ if (!Is.Ip(host) && !host.includes('localhost')) {
58
+ host = host.replace(`:${port}`, '')
145
59
  }
60
+
61
+ return host.concat(this.originalUrl)
62
+ }
63
+
64
+ /**
65
+ * Get the base request url.
66
+ *
67
+ * @return {string}
68
+ */
69
+ get baseUrl() {
70
+ return this.#request.url.split('?')[0]
71
+ }
72
+
73
+ /**
74
+ * Get the original request url.
75
+ *
76
+ * @return {string}
77
+ */
78
+ get originalUrl() {
79
+ return this.#request.url
80
+ }
81
+
82
+ /**
83
+ * Get all body from request.
84
+ *
85
+ * @return {any}
86
+ */
87
+ get body() {
88
+ return this.#request.body
89
+ }
90
+
91
+ /**
92
+ * Get all params from request.
93
+ *
94
+ * @return {any}
95
+ */
96
+ get params() {
97
+ return this.#request.params
98
+ }
99
+
100
+ /**
101
+ * Get all queries from request.
102
+ *
103
+ * @return {any}
104
+ */
105
+ get queries() {
106
+ return this.#request.query
107
+ }
108
+
109
+ /**
110
+ * Get all headers from request.
111
+ *
112
+ * @return {any}
113
+ */
114
+ get headers() {
115
+ return this.#request.headers
116
+ }
117
+
118
+ /**
119
+ * Get a value from the request params or the default value.
120
+ *
121
+ * @param {string} param
122
+ * @param {string} [defaultValue]
123
+ * @return {any}
124
+ */
125
+ param(param, defaultValue) {
126
+ const params = this.#request.params
127
+
128
+ return params[param] || defaultValue
129
+ }
130
+
131
+ /**
132
+ * Get a value from the request query param or the default value.
133
+ *
134
+ * @param {string} query
135
+ * @param {string} [defaultValue]
136
+ * @return {any}
137
+ */
138
+ query(query, defaultValue) {
139
+ const queries = this.#request.query
140
+
141
+ return queries[query] || defaultValue
142
+ }
143
+
144
+ /**
145
+ * Get a value from the request header or the default value.
146
+ *
147
+ * @param {string} header
148
+ * @param {string} [defaultValue]
149
+ * @return {any}
150
+ */
151
+ header(header, defaultValue) {
152
+ const headers = this.#request.headers
153
+
154
+ return headers[header] || defaultValue
155
+ }
156
+
157
+ /**
158
+ * Get a value from the request body or the default value.
159
+ *
160
+ * @param {string} payload
161
+ * @param {string} [defaultValue]
162
+ * @return {any}
163
+ */
164
+ payload(payload, defaultValue) {
165
+ const body = this.#request.body
166
+
167
+ return body[payload] || defaultValue
168
+ }
169
+
170
+ /**
171
+ * Get the default fastify request object.
172
+ *
173
+ * @return {import('fastify').FastifyRequest}
174
+ */
175
+ getFastifyRequest() {
176
+ return this.#request
177
+ }
146
178
  }
147
- exports.Request = Request;