@athenna/http 1.3.2 → 1.3.5
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/LICENSE.md +3 -15
- package/README.md +8 -5
- package/package.json +95 -106
- package/src/Context/Request.js +168 -137
- package/src/Context/Response.js +102 -94
- package/src/Exceptions/UndefinedMethodException.js +30 -0
- package/src/Facades/Route.js +7 -5
- package/src/Facades/Server.js +7 -5
- package/src/Handlers/FastifyHandler.js +134 -115
- package/src/Handlers/HttpExceptionHandler.js +71 -68
- package/src/Kernels/HttpKernel.js +142 -124
- package/src/Providers/ControllerProvider.js +30 -39
- package/src/Providers/HttpRouteProvider.js +15 -16
- package/src/Providers/HttpServerProvider.js +13 -15
- package/src/Providers/MiddlewareProvider.js +30 -39
- package/src/Router/Route.js +192 -99
- package/src/Router/RouteGroup.js +66 -40
- package/src/Router/RouteResource.js +156 -74
- package/src/Router/Router.js +284 -112
- package/src/Utils/isMiddlewareContract.js +8 -6
- package/src/Utils/removeSlashes.js +21 -0
- package/src/index.d.ts +658 -0
- package/src/index.js +284 -0
- package/index.d.ts +0 -29
- package/index.js +0 -41
- package/src/Context/Request.d.ts +0 -103
- package/src/Context/Response.d.ts +0 -77
- package/src/Contracts/Context/ContextContract.d.ts +0 -17
- package/src/Contracts/Context/ContextContract.js +0 -10
- package/src/Contracts/Context/Error/ErrorContextContract.d.ts +0 -18
- package/src/Contracts/Context/Error/ErrorContextContract.js +0 -10
- package/src/Contracts/Context/Error/ErrorHandlerContract.d.ts +0 -12
- package/src/Contracts/Context/Error/ErrorHandlerContract.js +0 -10
- package/src/Contracts/Context/HandlerContract.d.ts +0 -12
- package/src/Contracts/Context/HandlerContract.js +0 -10
- package/src/Contracts/Context/Middlewares/Handle/HandleContextContract.d.ts +0 -19
- package/src/Contracts/Context/Middlewares/Handle/HandleContextContract.js +0 -10
- package/src/Contracts/Context/Middlewares/Handle/HandleHandlerContract.d.ts +0 -12
- package/src/Contracts/Context/Middlewares/Handle/HandleHandlerContract.js +0 -10
- package/src/Contracts/Context/Middlewares/Intercept/InterceptContextContract.d.ts +0 -19
- package/src/Contracts/Context/Middlewares/Intercept/InterceptContextContract.js +0 -10
- package/src/Contracts/Context/Middlewares/Intercept/InterceptHandlerContract.d.ts +0 -12
- package/src/Contracts/Context/Middlewares/Intercept/InterceptHandlerContract.js +0 -10
- package/src/Contracts/Context/Middlewares/Terminate/TerminateContextContract.d.ts +0 -23
- package/src/Contracts/Context/Middlewares/Terminate/TerminateContextContract.js +0 -10
- package/src/Contracts/Context/Middlewares/Terminate/TerminateHandlerContract.d.ts +0 -12
- package/src/Contracts/Context/Middlewares/Terminate/TerminateHandlerContract.js +0 -10
- package/src/Contracts/Context/NextContract.d.ts +0 -11
- package/src/Contracts/Context/NextContract.js +0 -10
- package/src/Contracts/Context/RequestContract.d.ts +0 -25
- package/src/Contracts/Context/RequestContract.js +0 -10
- package/src/Contracts/Context/ResponseContract.d.ts +0 -20
- package/src/Contracts/Context/ResponseContract.js +0 -2
- package/src/Contracts/FastifyErrorHandlerContract.d.ts +0 -12
- package/src/Contracts/FastifyErrorHandlerContract.js +0 -10
- package/src/Contracts/FastifyHandlerContract.d.ts +0 -12
- package/src/Contracts/FastifyHandlerContract.js +0 -10
- package/src/Contracts/HttpMethodTypes.d.ts +0 -9
- package/src/Contracts/HttpMethodTypes.js +0 -10
- package/src/Contracts/MiddlewareContract.d.ts +0 -16
- package/src/Contracts/MiddlewareContract.js +0 -10
- package/src/Contracts/MiddlewareTypes.d.ts +0 -9
- package/src/Contracts/MiddlewareTypes.js +0 -10
- package/src/Contracts/MiddlewareTypesContract.d.ts +0 -16
- package/src/Contracts/MiddlewareTypesContract.js +0 -10
- package/src/Contracts/RouteContract.d.ts +0 -16
- package/src/Contracts/RouteContract.js +0 -10
- package/src/Exceptions/BadRequestException.d.ts +0 -12
- package/src/Exceptions/BadRequestException.js +0 -18
- package/src/Exceptions/CannotDefineGroupException.d.ts +0 -12
- package/src/Exceptions/CannotDefineGroupException.js +0 -19
- package/src/Exceptions/MiddlewareNotFoundException.d.ts +0 -12
- package/src/Exceptions/MiddlewareNotFoundException.js +0 -19
- package/src/Exceptions/UndefinedControllerMethodException.d.ts +0 -12
- package/src/Exceptions/UndefinedControllerMethodException.js +0 -19
- package/src/Facades/Route.d.ts +0 -10
- package/src/Facades/Server.d.ts +0 -10
- package/src/Handlers/FastifyHandler.d.ts +0 -26
- package/src/Handlers/HttpExceptionHandler.d.ts +0 -27
- package/src/Http.d.ts +0 -167
- package/src/Http.js +0 -223
- package/src/Kernels/HttpKernel.d.ts +0 -54
- package/src/Providers/ControllerProvider.d.ts +0 -17
- package/src/Providers/HttpRouteProvider.d.ts +0 -17
- package/src/Providers/HttpServerProvider.d.ts +0 -17
- package/src/Providers/MiddlewareProvider.d.ts +0 -17
- package/src/Router/Route.d.ts +0 -32
- package/src/Router/RouteGroup.d.ts +0 -24
- package/src/Router/RouteResource.d.ts +0 -29
- package/src/Router/Router.d.ts +0 -39
- package/src/Utils/getAppFiles.d.ts +0 -10
- package/src/Utils/getAppFiles.js +0 -22
- package/src/Utils/isMiddlewareContract.d.ts +0 -10
- package/src/Utils/removeSlash.d.ts +0 -9
- 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
|
[](https://github.com/athennaio?tab=followers)
|
|
6
|
-
[](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
|
|
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
|
|
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.
|
|
4
|
-
"description": "The Athenna Http server. Built on top of fastify",
|
|
3
|
+
"version": "1.3.5",
|
|
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
|
-
"
|
|
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
|
-
"@
|
|
27
|
-
"@
|
|
28
|
-
"@
|
|
29
|
-
"@
|
|
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": "
|
|
34
|
-
"eslint-config-prettier": "8.
|
|
35
|
-
"eslint-config-standard": "
|
|
36
|
-
"eslint-import
|
|
37
|
-
"eslint-plugin-
|
|
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": "
|
|
41
|
-
"eslint-plugin-standard": "5.0.0",
|
|
74
|
+
"eslint-plugin-promise": "6.0.0",
|
|
42
75
|
"husky": "3.0.9",
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"prettier": "2.
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
"
|
|
51
|
-
"
|
|
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
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"
|
|
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
|
-
"
|
|
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
|
-
"
|
|
143
|
+
"eslint:recommended",
|
|
111
144
|
"plugin:prettier/recommended"
|
|
112
145
|
],
|
|
113
|
-
"
|
|
114
|
-
"
|
|
115
|
-
|
|
116
|
-
},
|
|
117
|
-
"parser": "@typescript-eslint/parser",
|
|
146
|
+
"ignorePatterns": [
|
|
147
|
+
"**/*.d.ts"
|
|
148
|
+
],
|
|
118
149
|
"parserOptions": {
|
|
119
|
-
"ecmaVersion":
|
|
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
|
-
"
|
|
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
|
-
"
|
|
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
|
}
|
package/src/Context/Request.js
CHANGED
|
@@ -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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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;
|