tspace-spear 1.2.1-beta.1 → 1.2.2
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/README.md +41 -11
- package/dist/lib/core/decorators/context.d.ts +91 -0
- package/dist/lib/core/decorators/context.js +86 -0
- package/dist/lib/core/decorators/context.js.map +1 -1
- package/dist/lib/core/decorators/controller.d.ts +34 -0
- package/dist/lib/core/decorators/controller.js +33 -0
- package/dist/lib/core/decorators/controller.js.map +1 -1
- package/dist/lib/core/decorators/headers.d.ts +31 -0
- package/dist/lib/core/decorators/headers.js +29 -0
- package/dist/lib/core/decorators/headers.js.map +1 -1
- package/dist/lib/core/decorators/index.d.ts +9 -0
- package/dist/lib/core/decorators/methods.d.ts +82 -0
- package/dist/lib/core/decorators/methods.js +78 -1
- package/dist/lib/core/decorators/methods.js.map +1 -1
- package/dist/lib/core/decorators/middleware.d.ts +39 -0
- package/dist/lib/core/decorators/middleware.js +37 -0
- package/dist/lib/core/decorators/middleware.js.map +1 -1
- package/dist/lib/core/decorators/statusCode.d.ts +26 -0
- package/dist/lib/core/decorators/statusCode.js +24 -0
- package/dist/lib/core/decorators/statusCode.js.map +1 -1
- package/dist/lib/core/decorators/swagger.d.ts +36 -0
- package/dist/lib/core/decorators/swagger.js +35 -1
- package/dist/lib/core/decorators/swagger.js.map +1 -1
- package/dist/lib/core/server/index.d.ts +292 -0
- package/dist/lib/core/server/index.js +363 -359
- package/dist/lib/core/server/index.js.map +1 -1
- package/dist/lib/core/server/parser-factory.d.ts +23 -0
- package/dist/lib/core/server/parser-factory.js.map +1 -1
- package/dist/lib/core/server/router.d.ts +99 -0
- package/dist/lib/core/server/router.js +34 -0
- package/dist/lib/core/server/router.js.map +1 -1
- package/dist/lib/core/types/index.d.ts +228 -0
- package/dist/lib/index.d.ts +11 -0
- package/dist/tests/benchmark.test.d.ts +1 -0
- package/dist/tests/benchmark.test.js +2 -11
- package/dist/tests/benchmark.test.js.map +1 -1
- package/package.json +12 -10
- package/dist/lib/core/server/radix-router.js +0 -65
- package/dist/lib/core/server/radix-router.js.map +0 -1
package/README.md
CHANGED
|
@@ -111,6 +111,8 @@ const app = new Spear()
|
|
|
111
111
|
|
|
112
112
|
### Response
|
|
113
113
|
```js
|
|
114
|
+
import { Spear } from "tspace-spear";
|
|
115
|
+
|
|
114
116
|
const app = new Spear()
|
|
115
117
|
.get('/' , () => {
|
|
116
118
|
return {
|
|
@@ -119,14 +121,29 @@ const app = new Spear()
|
|
|
119
121
|
})
|
|
120
122
|
.response((results, statusCode) => {
|
|
121
123
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
124
|
+
if (typeof results === 'string') return results
|
|
125
|
+
|
|
126
|
+
if (Array.isArray(results)) {
|
|
127
|
+
return {
|
|
128
|
+
success: statusCode < 400,
|
|
129
|
+
data: results,
|
|
130
|
+
statusCode
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (typeof results === 'object' && results !== null) {
|
|
135
|
+
return {
|
|
136
|
+
success: statusCode < 400,
|
|
137
|
+
...results,
|
|
138
|
+
statusCode
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return {
|
|
143
|
+
success: statusCode < 400,
|
|
144
|
+
data: results,
|
|
145
|
+
statusCode
|
|
146
|
+
}
|
|
130
147
|
})
|
|
131
148
|
.listen(3000 , () => console.log(`Server is now listening http://localhost:3000`))
|
|
132
149
|
// http://localhost:3000 => { success: true , message : 'Hello World' , statusCode: 200 }
|
|
@@ -135,15 +152,28 @@ const app = new Spear()
|
|
|
135
152
|
|
|
136
153
|
### Catch
|
|
137
154
|
```js
|
|
155
|
+
import { Spear } from "tspace-spear";
|
|
156
|
+
import { z } from "zod";
|
|
138
157
|
const app = new Spear()
|
|
139
158
|
.get('/' , () => {
|
|
140
159
|
throw new Error('Catching failed')
|
|
141
160
|
})
|
|
142
|
-
.catch((err
|
|
161
|
+
.catch((err, { res } : T.Context) => {
|
|
162
|
+
|
|
163
|
+
if(err instanceof z.ZodError) {
|
|
164
|
+
return res
|
|
165
|
+
.status(422)
|
|
166
|
+
.json({
|
|
167
|
+
success : false,
|
|
168
|
+
message: "Validation failed",
|
|
169
|
+
issues : err?.issues,
|
|
170
|
+
statusCode : 422
|
|
171
|
+
});
|
|
172
|
+
}
|
|
143
173
|
|
|
144
174
|
return res
|
|
145
|
-
|
|
146
|
-
|
|
175
|
+
.status(500)
|
|
176
|
+
.json({
|
|
147
177
|
success : false,
|
|
148
178
|
message : err?.message,
|
|
149
179
|
statusCode : 500
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extract specific fields from `ctx.body`.
|
|
3
|
+
*
|
|
4
|
+
* This decorator filters the request body and keeps only the
|
|
5
|
+
* specified keys. The filtered result replaces `ctx.body`
|
|
6
|
+
* before the controller method is executed.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* @Body('email', 'password')
|
|
11
|
+
* async login(ctx: T.Context) {
|
|
12
|
+
* // ctx.body = { email: "...", password: "..." }
|
|
13
|
+
* }
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @param {...string} bodyParms - Body field names to extract.
|
|
17
|
+
* @returns {MethodDecorator}
|
|
18
|
+
*/
|
|
19
|
+
export declare const Body: (...bodyParms: string[]) => Function;
|
|
20
|
+
/**
|
|
21
|
+
* Extract specific uploaded files from `ctx.files`.
|
|
22
|
+
*
|
|
23
|
+
* Filters uploaded files and keeps only the specified
|
|
24
|
+
* file field names before executing the controller method.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* \@Files('avatar', 'resume')
|
|
29
|
+
* async upload(ctx: T.Context) {
|
|
30
|
+
* // ctx.files = { avatar: File, resume: File }
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @param {...string} filesParms - File field names to extract.
|
|
35
|
+
* @returns {MethodDecorator}
|
|
36
|
+
*/
|
|
37
|
+
export declare const Files: (...filesParms: string[]) => Function;
|
|
38
|
+
/**
|
|
39
|
+
* Extract specific route parameters from `ctx.params`.
|
|
40
|
+
*
|
|
41
|
+
* Filters route parameters and keeps only the specified
|
|
42
|
+
* parameter names.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* \@Params('id')
|
|
47
|
+
* async getUser(ctx: T.Context) {
|
|
48
|
+
* // ctx.params = { id: "123" }
|
|
49
|
+
* }
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @param {...string} paramsData - Route parameter names to extract.
|
|
53
|
+
* @returns {MethodDecorator}
|
|
54
|
+
*/
|
|
55
|
+
export declare const Params: (...paramsData: string[]) => Function;
|
|
56
|
+
/**
|
|
57
|
+
* Extract specific query parameters from `ctx.query`.
|
|
58
|
+
*
|
|
59
|
+
* Filters query parameters and keeps only the specified
|
|
60
|
+
* query keys.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* \@Query('page', 'limit')
|
|
65
|
+
* async listUsers(ctx: T.Context) {
|
|
66
|
+
* // ctx.query = { page: "1", limit: "10" }
|
|
67
|
+
* }
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* @param {...string} queryParms - Query parameter names to extract.
|
|
71
|
+
* @returns {MethodDecorator}
|
|
72
|
+
*/
|
|
73
|
+
export declare const Query: (...queryParms: string[]) => Function;
|
|
74
|
+
/**
|
|
75
|
+
* Extract specific cookies from `ctx.cookies`.
|
|
76
|
+
*
|
|
77
|
+
* Filters cookies and keeps only the specified cookie keys
|
|
78
|
+
* before executing the controller method.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* \@Cookies('token')
|
|
83
|
+
* async profile(ctx: T.Context) {
|
|
84
|
+
* // ctx.cookies = { token: "..." }
|
|
85
|
+
* }
|
|
86
|
+
* ```
|
|
87
|
+
*
|
|
88
|
+
* @param {...string} cookiesParms - Cookie names to extract.
|
|
89
|
+
* @returns {MethodDecorator}
|
|
90
|
+
*/
|
|
91
|
+
export declare const Cookies: (...cookiesParms: string[]) => Function;
|
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Cookies = exports.Query = exports.Params = exports.Files = exports.Body = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Extract specific fields from `ctx.body`.
|
|
6
|
+
*
|
|
7
|
+
* This decorator filters the request body and keeps only the
|
|
8
|
+
* specified keys. The filtered result replaces `ctx.body`
|
|
9
|
+
* before the controller method is executed.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* @Body('email', 'password')
|
|
14
|
+
* async login(ctx: T.Context) {
|
|
15
|
+
* // ctx.body = { email: "...", password: "..." }
|
|
16
|
+
* }
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @param {...string} bodyParms - Body field names to extract.
|
|
20
|
+
* @returns {MethodDecorator}
|
|
21
|
+
*/
|
|
4
22
|
const Body = (...bodyParms) => {
|
|
5
23
|
return function (target, key, descriptor) {
|
|
6
24
|
const originalMethod = descriptor.value;
|
|
@@ -14,6 +32,23 @@ const Body = (...bodyParms) => {
|
|
|
14
32
|
};
|
|
15
33
|
};
|
|
16
34
|
exports.Body = Body;
|
|
35
|
+
/**
|
|
36
|
+
* Extract specific uploaded files from `ctx.files`.
|
|
37
|
+
*
|
|
38
|
+
* Filters uploaded files and keeps only the specified
|
|
39
|
+
* file field names before executing the controller method.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* \@Files('avatar', 'resume')
|
|
44
|
+
* async upload(ctx: T.Context) {
|
|
45
|
+
* // ctx.files = { avatar: File, resume: File }
|
|
46
|
+
* }
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
* @param {...string} filesParms - File field names to extract.
|
|
50
|
+
* @returns {MethodDecorator}
|
|
51
|
+
*/
|
|
17
52
|
const Files = (...filesParms) => {
|
|
18
53
|
return function (target, key, descriptor) {
|
|
19
54
|
const originalMethod = descriptor.value;
|
|
@@ -27,6 +62,23 @@ const Files = (...filesParms) => {
|
|
|
27
62
|
};
|
|
28
63
|
};
|
|
29
64
|
exports.Files = Files;
|
|
65
|
+
/**
|
|
66
|
+
* Extract specific route parameters from `ctx.params`.
|
|
67
|
+
*
|
|
68
|
+
* Filters route parameters and keeps only the specified
|
|
69
|
+
* parameter names.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* \@Params('id')
|
|
74
|
+
* async getUser(ctx: T.Context) {
|
|
75
|
+
* // ctx.params = { id: "123" }
|
|
76
|
+
* }
|
|
77
|
+
* ```
|
|
78
|
+
*
|
|
79
|
+
* @param {...string} paramsData - Route parameter names to extract.
|
|
80
|
+
* @returns {MethodDecorator}
|
|
81
|
+
*/
|
|
30
82
|
const Params = (...paramsData) => {
|
|
31
83
|
return function (target, key, descriptor) {
|
|
32
84
|
const originalMethod = descriptor.value;
|
|
@@ -40,6 +92,23 @@ const Params = (...paramsData) => {
|
|
|
40
92
|
};
|
|
41
93
|
};
|
|
42
94
|
exports.Params = Params;
|
|
95
|
+
/**
|
|
96
|
+
* Extract specific query parameters from `ctx.query`.
|
|
97
|
+
*
|
|
98
|
+
* Filters query parameters and keeps only the specified
|
|
99
|
+
* query keys.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```ts
|
|
103
|
+
* \@Query('page', 'limit')
|
|
104
|
+
* async listUsers(ctx: T.Context) {
|
|
105
|
+
* // ctx.query = { page: "1", limit: "10" }
|
|
106
|
+
* }
|
|
107
|
+
* ```
|
|
108
|
+
*
|
|
109
|
+
* @param {...string} queryParms - Query parameter names to extract.
|
|
110
|
+
* @returns {MethodDecorator}
|
|
111
|
+
*/
|
|
43
112
|
const Query = (...queryParms) => {
|
|
44
113
|
return function (target, key, descriptor) {
|
|
45
114
|
const originalMethod = descriptor.value;
|
|
@@ -53,6 +122,23 @@ const Query = (...queryParms) => {
|
|
|
53
122
|
};
|
|
54
123
|
};
|
|
55
124
|
exports.Query = Query;
|
|
125
|
+
/**
|
|
126
|
+
* Extract specific cookies from `ctx.cookies`.
|
|
127
|
+
*
|
|
128
|
+
* Filters cookies and keeps only the specified cookie keys
|
|
129
|
+
* before executing the controller method.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```ts
|
|
133
|
+
* \@Cookies('token')
|
|
134
|
+
* async profile(ctx: T.Context) {
|
|
135
|
+
* // ctx.cookies = { token: "..." }
|
|
136
|
+
* }
|
|
137
|
+
* ```
|
|
138
|
+
*
|
|
139
|
+
* @param {...string} cookiesParms - Cookie names to extract.
|
|
140
|
+
* @returns {MethodDecorator}
|
|
141
|
+
*/
|
|
56
142
|
const Cookies = (...cookiesParms) => {
|
|
57
143
|
return function (target, key, descriptor) {
|
|
58
144
|
const originalMethod = descriptor.value;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../../../src/lib/core/decorators/context.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../../../src/lib/core/decorators/context.ts"],"names":[],"mappings":";;;AAEA;;;;;;;;;;;;;;;;;GAiBG;AACI,MAAM,IAAI,GAAG,CAAC,GAAG,SAAmB,EAAY,EAAE;IACrD,OAAO,UAAU,MAAW,EAAE,GAAW,EAAE,UAA8B;QACrE,MAAM,cAAc,GAAG,UAAU,CAAC,KAAK,CAAC;QAExC,UAAU,CAAC,KAAK,GAAG,KAAK,WAAW,GAAc,EAAE,IAAoB;YACnE,MAAM,CAAC,GAAG,GAAG,E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YAAY,CAAC,MAAM,CAC/B,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC,EAAE,GAAG,GAAG,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,EAChE,EAAE,CACL,CAAC;YAEF,GAAG,CAAC,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC;YAEzD,OAAO,MAAM,cAAc,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC;QACtD,CAAC,CAAC;QAEF,OAAO,UAAU,CAAC;IACtB,CAAC,CAAC;AACN,CAAC,CAAC;AAlBW,QAAA,OAAO,WAkBlB"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Declares a class as a controller and assigns a base route path.
|
|
3
|
+
*
|
|
4
|
+
* The provided path will be used as the **base URL prefix**
|
|
5
|
+
* for all routes defined inside the controller. The path must
|
|
6
|
+
* start with `/`.
|
|
7
|
+
*
|
|
8
|
+
* This decorator stores the controller path using
|
|
9
|
+
* `Reflect.defineMetadata` under the key `"controllers"`.
|
|
10
|
+
* The framework can later read this metadata to register
|
|
11
|
+
* routes automatically.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* \@Controller('/users')
|
|
16
|
+
* class UserController {
|
|
17
|
+
*
|
|
18
|
+
* async list(ctx: T.Context) {
|
|
19
|
+
* return [{ id: 1, name: "John" }];
|
|
20
|
+
* }
|
|
21
|
+
*
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* If a route handler defines `/profile`, the final route becomes:
|
|
26
|
+
*
|
|
27
|
+
* ```
|
|
28
|
+
* /users/profile
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @param {`/${string}`} path - Base route path for the controller.
|
|
32
|
+
* @returns {ClassDecorator}
|
|
33
|
+
*/
|
|
34
|
+
export declare const Controller: (path: `/${string}`) => ClassDecorator;
|
|
@@ -1,6 +1,39 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Controller = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Declares a class as a controller and assigns a base route path.
|
|
6
|
+
*
|
|
7
|
+
* The provided path will be used as the **base URL prefix**
|
|
8
|
+
* for all routes defined inside the controller. The path must
|
|
9
|
+
* start with `/`.
|
|
10
|
+
*
|
|
11
|
+
* This decorator stores the controller path using
|
|
12
|
+
* `Reflect.defineMetadata` under the key `"controllers"`.
|
|
13
|
+
* The framework can later read this metadata to register
|
|
14
|
+
* routes automatically.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* \@Controller('/users')
|
|
19
|
+
* class UserController {
|
|
20
|
+
*
|
|
21
|
+
* async list(ctx: T.Context) {
|
|
22
|
+
* return [{ id: 1, name: "John" }];
|
|
23
|
+
* }
|
|
24
|
+
*
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* If a route handler defines `/profile`, the final route becomes:
|
|
29
|
+
*
|
|
30
|
+
* ```
|
|
31
|
+
* /users/profile
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @param {`/${string}`} path - Base route path for the controller.
|
|
35
|
+
* @returns {ClassDecorator}
|
|
36
|
+
*/
|
|
4
37
|
const Controller = (path) => {
|
|
5
38
|
return (target) => {
|
|
6
39
|
return Reflect.defineMetadata("controllers", path, target);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"controller.js","sourceRoot":"","sources":["../../../../src/lib/core/decorators/controller.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"controller.js","sourceRoot":"","sources":["../../../../src/lib/core/decorators/controller.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACI,MAAM,UAAU,GAAG,CAAC,IAAkB,EAAkB,EAAE;IAC/D,OAAO,CAAC,MAAM,EAAE,EAAE;QAChB,OAAO,OAAO,CAAC,cAAc,CAAC,aAAa,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;IAC7D,CAAC,CAAC;AACJ,CAAC,CAAA;AAJY,QAAA,UAAU,cAItB"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { OutgoingHttpHeaders } from 'http';
|
|
2
|
+
/**
|
|
3
|
+
* Sets the HTTP response headers and status code before the controller method executes.
|
|
4
|
+
*
|
|
5
|
+
* This decorator calls `res.writeHead()` on the underlying Node.js response object,
|
|
6
|
+
* allowing you to define the response **status code** and **headers** in advance.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* class UserController {
|
|
11
|
+
*
|
|
12
|
+
* \@WriteHeader(200, { "Content-Type": "application/json" })
|
|
13
|
+
* async profile(ctx: T.Context) {
|
|
14
|
+
* return { id: 1, name: "John" };
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* Example response:
|
|
21
|
+
*
|
|
22
|
+
* ```
|
|
23
|
+
* HTTP/1.1 200 OK
|
|
24
|
+
* Content-Type: application/json
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @param {number} statusCode - HTTP status code to send with the response.
|
|
28
|
+
* @param {OutgoingHttpHeaders} contentType - Response headers to set.
|
|
29
|
+
* @returns {Function}
|
|
30
|
+
*/
|
|
31
|
+
export declare const WriteHeader: (statusCode: number, contentType: OutgoingHttpHeaders) => Function;
|
|
@@ -1,6 +1,35 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.WriteHeader = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Sets the HTTP response headers and status code before the controller method executes.
|
|
6
|
+
*
|
|
7
|
+
* This decorator calls `res.writeHead()` on the underlying Node.js response object,
|
|
8
|
+
* allowing you to define the response **status code** and **headers** in advance.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* class UserController {
|
|
13
|
+
*
|
|
14
|
+
* \@WriteHeader(200, { "Content-Type": "application/json" })
|
|
15
|
+
* async profile(ctx: T.Context) {
|
|
16
|
+
* return { id: 1, name: "John" };
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* }
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* Example response:
|
|
23
|
+
*
|
|
24
|
+
* ```
|
|
25
|
+
* HTTP/1.1 200 OK
|
|
26
|
+
* Content-Type: application/json
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @param {number} statusCode - HTTP status code to send with the response.
|
|
30
|
+
* @param {OutgoingHttpHeaders} contentType - Response headers to set.
|
|
31
|
+
* @returns {Function}
|
|
32
|
+
*/
|
|
4
33
|
const WriteHeader = (statusCode, contentType) => {
|
|
5
34
|
return (target, key, descriptor) => {
|
|
6
35
|
const originalMethod = descriptor.value;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"headers.js","sourceRoot":"","sources":["../../../../src/lib/core/decorators/headers.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"headers.js","sourceRoot":"","sources":["../../../../src/lib/core/decorators/headers.ts"],"names":[],"mappings":";;;AAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACI,MAAM,WAAW,GAAG,CAAC,UAAkB,EAAE,WAAgC,EAAY,EAAE;IAC5F,OAAO,CAAC,MAAW,EAAE,GAAW,EAAE,UAA8B,EAAE,EAAE;QAClE,MAAM,cAAc,GAAG,UAAU,CAAC,KAAK,CAAC;QAExC,UAAU,CAAC,KAAK,GAAG,KAAK,WAAW,GAAc,EAAE,IAAoB;YACrE,GAAG,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,UAAU,EAAE,WAAW,CAAC,CAAC,CAAC;YAChD,OAAO,MAAM,cAAc,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC;QACpD,CAAC,CAAC;QAEF,OAAO,UAAU,CAAC;IACpB,CAAC,CAAC;AACJ,CAAC,CAAC;AAXW,QAAA,WAAW,eAWtB"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
export * from './methods';
|
|
3
|
+
export * from './headers';
|
|
4
|
+
export * from './middleware';
|
|
5
|
+
export * from './controller';
|
|
6
|
+
export * from './headers';
|
|
7
|
+
export * from './statusCode';
|
|
8
|
+
export * from './context';
|
|
9
|
+
export * from './swagger';
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Maps a controller method to an HTTP GET route.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* \@Get('/users')
|
|
7
|
+
* async list(ctx: T.Context) {
|
|
8
|
+
* return [{ id: 1, name: "John" }];
|
|
9
|
+
* }
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export declare const Get: (path: `/${string}`) => (target: any, propertyKey: any) => void;
|
|
13
|
+
/**
|
|
14
|
+
* Maps a controller method to an HTTP POST route.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* \@Post('/users')
|
|
19
|
+
* async create(ctx: T.Context) {
|
|
20
|
+
* return { success: true };
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare const Post: (path: `/${string}`) => (target: any, propertyKey: any) => void;
|
|
25
|
+
/**
|
|
26
|
+
* Maps a controller method to an HTTP PUT route.
|
|
27
|
+
*
|
|
28
|
+
* Used for replacing a resource.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* \@Put('/users/:id')
|
|
33
|
+
* async update(ctx: T.Context) {}
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare const Put: (path: `/${string}`) => (target: any, propertyKey: any) => void;
|
|
37
|
+
/**
|
|
38
|
+
* Maps a controller method to an HTTP PATCH route.
|
|
39
|
+
*
|
|
40
|
+
* Used for partially updating a resource.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* \@Patch('/users/:id')
|
|
45
|
+
* async patch(ctx: T.Context) {}
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export declare const Patch: (path: `/${string}`) => (target: any, propertyKey: any) => void;
|
|
49
|
+
/**
|
|
50
|
+
* Maps a controller method to an HTTP DELETE route.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* \@Delete('/users/:id')
|
|
55
|
+
* async remove(ctx: T.Context) {}
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export declare const Delete: (path: `/${string}`) => (target: any, propertyKey: any) => void;
|
|
59
|
+
/**
|
|
60
|
+
* Maps a controller method to an HTTP HEAD route.
|
|
61
|
+
*
|
|
62
|
+
* HEAD responses contain only headers and no response body.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```ts
|
|
66
|
+
* \@Head('/health')
|
|
67
|
+
* async health(ctx: T.Context) {}
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export declare const Head: (path: `/${string}`) => (target: any, propertyKey: any) => void;
|
|
71
|
+
/**
|
|
72
|
+
* Maps a controller method to an HTTP OPTIONS route.
|
|
73
|
+
*
|
|
74
|
+
* Typically used for CORS preflight requests.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* \@Options('/users')
|
|
79
|
+
* async options(ctx: T.Context) {}
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export declare const Options: (path: `/${string}`) => (target: any, propertyKey: any) => void;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Delete = exports.Patch = exports.Put = exports.Post = exports.Get = void 0;
|
|
3
|
+
exports.Options = exports.Head = exports.Delete = exports.Patch = exports.Put = exports.Post = exports.Get = void 0;
|
|
4
4
|
const methodDecorator = (method) => {
|
|
5
5
|
return (path) => {
|
|
6
6
|
return (target, propertyKey) => {
|
|
@@ -17,9 +17,86 @@ const methodDecorator = (method) => {
|
|
|
17
17
|
};
|
|
18
18
|
};
|
|
19
19
|
};
|
|
20
|
+
/**
|
|
21
|
+
* Maps a controller method to an HTTP GET route.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* \@Get('/users')
|
|
26
|
+
* async list(ctx: T.Context) {
|
|
27
|
+
* return [{ id: 1, name: "John" }];
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
20
31
|
exports.Get = methodDecorator('get');
|
|
32
|
+
/**
|
|
33
|
+
* Maps a controller method to an HTTP POST route.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* \@Post('/users')
|
|
38
|
+
* async create(ctx: T.Context) {
|
|
39
|
+
* return { success: true };
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
21
43
|
exports.Post = methodDecorator('post');
|
|
44
|
+
/**
|
|
45
|
+
* Maps a controller method to an HTTP PUT route.
|
|
46
|
+
*
|
|
47
|
+
* Used for replacing a resource.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* \@Put('/users/:id')
|
|
52
|
+
* async update(ctx: T.Context) {}
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
22
55
|
exports.Put = methodDecorator('put');
|
|
56
|
+
/**
|
|
57
|
+
* Maps a controller method to an HTTP PATCH route.
|
|
58
|
+
*
|
|
59
|
+
* Used for partially updating a resource.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* \@Patch('/users/:id')
|
|
64
|
+
* async patch(ctx: T.Context) {}
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
23
67
|
exports.Patch = methodDecorator('patch');
|
|
68
|
+
/**
|
|
69
|
+
* Maps a controller method to an HTTP DELETE route.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* \@Delete('/users/:id')
|
|
74
|
+
* async remove(ctx: T.Context) {}
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
24
77
|
exports.Delete = methodDecorator('delete');
|
|
78
|
+
/**
|
|
79
|
+
* Maps a controller method to an HTTP HEAD route.
|
|
80
|
+
*
|
|
81
|
+
* HEAD responses contain only headers and no response body.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* \@Head('/health')
|
|
86
|
+
* async health(ctx: T.Context) {}
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
exports.Head = methodDecorator('head');
|
|
90
|
+
/**
|
|
91
|
+
* Maps a controller method to an HTTP OPTIONS route.
|
|
92
|
+
*
|
|
93
|
+
* Typically used for CORS preflight requests.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* \@Options('/users')
|
|
98
|
+
* async options(ctx: T.Context) {}
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
exports.Options = methodDecorator('options');
|
|
25
102
|
//# sourceMappingURL=methods.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"methods.js","sourceRoot":"","sources":["../../../../src/lib/core/decorators/methods.ts"],"names":[],"mappings":";;;AAEA,MAAM,eAAe,GAAG,CAAC,MAAgB,EAAE,EAAE;IAC3C,OAAO,CAAC,IAAkB,EAAE,EAAE;QAC5B,OAAO,CAAC,MAAU,EAAE,WAAe,EAAE,EAAE;YACrC,MAAM,UAAU,GAAG,MAAM,CAAC,WAAW,CAAC;YAEtC,MAAM,OAAO,GAAe,OAAO,CAAC,WAAW,CAAC,SAAS,EAAE,UAAU,CAAC;gBACpE,CAAC,CAAC,OAAO,CAAC,WAAW,CAAC,SAAS,EAAE,UAAU,CAAC;gBAC5C,CAAC,CAAC,EAAE,CAAC;YAEP,OAAO,CAAC,IAAI,CAAC;gBACX,MAAM;gBACN,IAAI;gBACJ,OAAO,EAAE,WAAW;aACrB,CAAC,CAAC;YAEH,OAAO,CAAC,cAAc,CAAC,SAAS,EAAE,OAAO,EAAE,UAAU,CAAC,CAAC;QACzD,CAAC,CAAA;IACH,CAAC,CAAA;AACH,CAAC,CAAA;
|
|
1
|
+
{"version":3,"file":"methods.js","sourceRoot":"","sources":["../../../../src/lib/core/decorators/methods.ts"],"names":[],"mappings":";;;AAEA,MAAM,eAAe,GAAG,CAAC,MAAgB,EAAE,EAAE;IAC3C,OAAO,CAAC,IAAkB,EAAE,EAAE;QAC5B,OAAO,CAAC,MAAU,EAAE,WAAe,EAAE,EAAE;YACrC,MAAM,UAAU,GAAG,MAAM,CAAC,WAAW,CAAC;YAEtC,MAAM,OAAO,GAAe,OAAO,CAAC,WAAW,CAAC,SAAS,EAAE,UAAU,CAAC;gBACpE,CAAC,CAAC,OAAO,CAAC,WAAW,CAAC,SAAS,EAAE,UAAU,CAAC;gBAC5C,CAAC,CAAC,EAAE,CAAC;YAEP,OAAO,CAAC,IAAI,CAAC;gBACX,MAAM;gBACN,IAAI;gBACJ,OAAO,EAAE,WAAW;aACrB,CAAC,CAAC;YAEH,OAAO,CAAC,cAAc,CAAC,SAAS,EAAE,OAAO,EAAE,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