@webpieces/http-filters 0.1.0
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 +19 -0
- package/package.json +26 -0
- package/src/Filter.d.ts +110 -0
- package/src/Filter.js +36 -0
- package/src/Filter.js.map +1 -0
- package/src/FilterChain.d.ts +30 -0
- package/src/FilterChain.js +57 -0
- package/src/FilterChain.js.map +1 -0
- package/src/filters/ContextFilter.d.ts +12 -0
- package/src/filters/ContextFilter.js +40 -0
- package/src/filters/ContextFilter.js.map +1 -0
- package/src/filters/JsonFilter.d.ts +73 -0
- package/src/filters/JsonFilter.js +167 -0
- package/src/filters/JsonFilter.js.map +1 -0
- package/src/index.d.ts +4 -0
- package/src/index.js +16 -0
- package/src/index.js.map +1 -0
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# @webpieces/http-filters
|
|
2
|
+
|
|
3
|
+
> Filter chain infrastructure for cross-cutting concerns
|
|
4
|
+
|
|
5
|
+
Part of the [WebPieces TypeScript](https://github.com/deanhiller/webpieces-ts) framework.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @webpieces/http-filters
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Documentation
|
|
14
|
+
|
|
15
|
+
See the main [WebPieces README](https://github.com/deanhiller/webpieces-ts#readme) for complete documentation and examples.
|
|
16
|
+
|
|
17
|
+
## License
|
|
18
|
+
|
|
19
|
+
Apache-2.0
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@webpieces/http-filters",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Filter chain infrastructure for cross-cutting concerns",
|
|
5
|
+
"type": "commonjs",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"types": "./src/index.d.ts",
|
|
8
|
+
"author": "Dean Hiller",
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/deanhiller/webpieces-ts.git",
|
|
13
|
+
"directory": "packages/http/http-filters"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"webpieces",
|
|
17
|
+
"filters",
|
|
18
|
+
"middleware"
|
|
19
|
+
],
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@webpieces/core-context": "0.1.0"
|
|
25
|
+
}
|
|
26
|
+
}
|
package/src/Filter.d.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Metadata about the method being invoked.
|
|
3
|
+
* Passed to filters and contains request information.
|
|
4
|
+
*/
|
|
5
|
+
export interface MethodMeta {
|
|
6
|
+
/**
|
|
7
|
+
* The HTTP method (GET, POST, etc.)
|
|
8
|
+
*/
|
|
9
|
+
httpMethod: string;
|
|
10
|
+
/**
|
|
11
|
+
* The request path
|
|
12
|
+
*/
|
|
13
|
+
path: string;
|
|
14
|
+
/**
|
|
15
|
+
* The method name being invoked on the controller
|
|
16
|
+
*/
|
|
17
|
+
methodName: string;
|
|
18
|
+
/**
|
|
19
|
+
* Parameters to pass to the controller method.
|
|
20
|
+
* Filters can modify this array (e.g., JsonFilter deserializes request body into params[0])
|
|
21
|
+
*/
|
|
22
|
+
params: any[];
|
|
23
|
+
/**
|
|
24
|
+
* The original request object (if applicable)
|
|
25
|
+
*/
|
|
26
|
+
request?: any;
|
|
27
|
+
/**
|
|
28
|
+
* The response object (if applicable)
|
|
29
|
+
*/
|
|
30
|
+
response?: any;
|
|
31
|
+
/**
|
|
32
|
+
* Additional metadata
|
|
33
|
+
*/
|
|
34
|
+
metadata?: Map<string, any>;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Action returned by filters and controllers.
|
|
38
|
+
* Can represent different types of responses.
|
|
39
|
+
*/
|
|
40
|
+
export interface Action {
|
|
41
|
+
type: 'json' | 'html' | 'redirect' | 'error';
|
|
42
|
+
data?: any;
|
|
43
|
+
statusCode?: number;
|
|
44
|
+
headers?: Record<string, string>;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Next filter class.
|
|
48
|
+
* This is a class instead of a function type to make it easier to trace
|
|
49
|
+
* who is calling what in the debugger/IDE.
|
|
50
|
+
*/
|
|
51
|
+
export declare abstract class NextFilter {
|
|
52
|
+
/**
|
|
53
|
+
* Execute the next filter in the chain.
|
|
54
|
+
* @returns Promise of the action
|
|
55
|
+
*/
|
|
56
|
+
abstract execute(): Promise<Action>;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Filter interface.
|
|
60
|
+
* Similar to Java WebPieces RouteFilter.
|
|
61
|
+
*
|
|
62
|
+
* Filters are executed in priority order (higher priority first)
|
|
63
|
+
* and can wrap the execution of subsequent filters and the controller.
|
|
64
|
+
*
|
|
65
|
+
* Example:
|
|
66
|
+
* ```typescript
|
|
67
|
+
* @injectable()
|
|
68
|
+
* export class LoggingFilter implements Filter {
|
|
69
|
+
* priority = 100;
|
|
70
|
+
*
|
|
71
|
+
* async filter(meta: MethodMeta, next: NextFilter): Promise<Action> {
|
|
72
|
+
* console.log(`Request: ${meta.httpMethod} ${meta.path}`);
|
|
73
|
+
* const action = await next.execute();
|
|
74
|
+
* console.log(`Response: ${action.statusCode}`);
|
|
75
|
+
* return action;
|
|
76
|
+
* }
|
|
77
|
+
* }
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export interface Filter {
|
|
81
|
+
/**
|
|
82
|
+
* Priority of this filter.
|
|
83
|
+
* Higher numbers execute first.
|
|
84
|
+
* Typical values:
|
|
85
|
+
* - 140: Context setup
|
|
86
|
+
* - 120: Request attributes
|
|
87
|
+
* - 90: Metrics
|
|
88
|
+
* - 80: Logging
|
|
89
|
+
* - 60: JSON serialization
|
|
90
|
+
* - 40: Transactions
|
|
91
|
+
* - 0: Controller
|
|
92
|
+
*/
|
|
93
|
+
priority: number;
|
|
94
|
+
/**
|
|
95
|
+
* Filter method that wraps the next filter/controller.
|
|
96
|
+
*
|
|
97
|
+
* @param meta - Metadata about the method being invoked
|
|
98
|
+
* @param next - NextFilter instance to invoke the next filter in the chain
|
|
99
|
+
* @returns Promise of the action to return
|
|
100
|
+
*/
|
|
101
|
+
filter(meta: MethodMeta, next: NextFilter): Promise<Action>;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Helper to create a JSON action response.
|
|
105
|
+
*/
|
|
106
|
+
export declare function jsonAction(data: any, statusCode?: number): Action;
|
|
107
|
+
/**
|
|
108
|
+
* Helper to create an error action response.
|
|
109
|
+
*/
|
|
110
|
+
export declare function errorAction(error: Error | string, statusCode?: number): Action;
|
package/src/Filter.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NextFilter = void 0;
|
|
4
|
+
exports.jsonAction = jsonAction;
|
|
5
|
+
exports.errorAction = errorAction;
|
|
6
|
+
/**
|
|
7
|
+
* Next filter class.
|
|
8
|
+
* This is a class instead of a function type to make it easier to trace
|
|
9
|
+
* who is calling what in the debugger/IDE.
|
|
10
|
+
*/
|
|
11
|
+
class NextFilter {
|
|
12
|
+
}
|
|
13
|
+
exports.NextFilter = NextFilter;
|
|
14
|
+
/**
|
|
15
|
+
* Helper to create a JSON action response.
|
|
16
|
+
*/
|
|
17
|
+
function jsonAction(data, statusCode = 200) {
|
|
18
|
+
return {
|
|
19
|
+
type: 'json',
|
|
20
|
+
data,
|
|
21
|
+
statusCode,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Helper to create an error action response.
|
|
26
|
+
*/
|
|
27
|
+
function errorAction(error, statusCode = 500) {
|
|
28
|
+
return {
|
|
29
|
+
type: 'error',
|
|
30
|
+
data: {
|
|
31
|
+
error: typeof error === 'string' ? error : error.message,
|
|
32
|
+
},
|
|
33
|
+
statusCode,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=Filter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Filter.js","sourceRoot":"","sources":["../../../../../packages/http/http-filters/src/Filter.ts"],"names":[],"mappings":";;;AAoHA,gCAMC;AAKD,kCAWC;AArFD;;;;GAIG;AACH,MAAsB,UAAU;CAM/B;AAND,gCAMC;AAiDD;;GAEG;AACH,SAAgB,UAAU,CAAC,IAAS,EAAE,aAAqB,GAAG;IAC5D,OAAO;QACL,IAAI,EAAE,MAAM;QACZ,IAAI;QACJ,UAAU;KACX,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,SAAgB,WAAW,CACzB,KAAqB,EACrB,aAAqB,GAAG;IAExB,OAAO;QACL,IAAI,EAAE,OAAO;QACb,IAAI,EAAE;YACJ,KAAK,EAAE,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO;SACzD;QACD,UAAU;KACX,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Filter, MethodMeta, Action } from './Filter';
|
|
2
|
+
/**
|
|
3
|
+
* FilterChain - Manages execution of filters in priority order.
|
|
4
|
+
* Similar to Java servlet filter chains.
|
|
5
|
+
*
|
|
6
|
+
* Filters are sorted by priority (highest first) and each filter
|
|
7
|
+
* calls next() to invoke the next filter in the chain.
|
|
8
|
+
*
|
|
9
|
+
* The final "filter" in the chain is the controller method itself.
|
|
10
|
+
*/
|
|
11
|
+
export declare class FilterChain {
|
|
12
|
+
private filters;
|
|
13
|
+
constructor(filters: Filter[]);
|
|
14
|
+
/**
|
|
15
|
+
* Execute the filter chain.
|
|
16
|
+
*
|
|
17
|
+
* @param meta - Method metadata
|
|
18
|
+
* @param finalHandler - The controller method to execute at the end
|
|
19
|
+
* @returns Promise of the action
|
|
20
|
+
*/
|
|
21
|
+
execute(meta: MethodMeta, finalHandler: () => Promise<Action>): Promise<Action>;
|
|
22
|
+
/**
|
|
23
|
+
* Get all filters in the chain (sorted by priority).
|
|
24
|
+
*/
|
|
25
|
+
getFilters(): Filter[];
|
|
26
|
+
/**
|
|
27
|
+
* Get the number of filters in the chain.
|
|
28
|
+
*/
|
|
29
|
+
size(): number;
|
|
30
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FilterChain = void 0;
|
|
4
|
+
const Filter_1 = require("./Filter");
|
|
5
|
+
/**
|
|
6
|
+
* FilterChain - Manages execution of filters in priority order.
|
|
7
|
+
* Similar to Java servlet filter chains.
|
|
8
|
+
*
|
|
9
|
+
* Filters are sorted by priority (highest first) and each filter
|
|
10
|
+
* calls next() to invoke the next filter in the chain.
|
|
11
|
+
*
|
|
12
|
+
* The final "filter" in the chain is the controller method itself.
|
|
13
|
+
*/
|
|
14
|
+
class FilterChain {
|
|
15
|
+
constructor(filters) {
|
|
16
|
+
// Sort filters by priority (highest first)
|
|
17
|
+
this.filters = [...filters].sort((a, b) => b.priority - a.priority);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Execute the filter chain.
|
|
21
|
+
*
|
|
22
|
+
* @param meta - Method metadata
|
|
23
|
+
* @param finalHandler - The controller method to execute at the end
|
|
24
|
+
* @returns Promise of the action
|
|
25
|
+
*/
|
|
26
|
+
async execute(meta, finalHandler) {
|
|
27
|
+
let index = 0;
|
|
28
|
+
const filters = this.filters;
|
|
29
|
+
const next = new class extends Filter_1.NextFilter {
|
|
30
|
+
async execute() {
|
|
31
|
+
if (index < filters.length) {
|
|
32
|
+
const filter = filters[index++];
|
|
33
|
+
return filter.filter(meta, next);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
// All filters have been executed, now execute the controller
|
|
37
|
+
return finalHandler();
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
return next.execute();
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Get all filters in the chain (sorted by priority).
|
|
45
|
+
*/
|
|
46
|
+
getFilters() {
|
|
47
|
+
return [...this.filters];
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Get the number of filters in the chain.
|
|
51
|
+
*/
|
|
52
|
+
size() {
|
|
53
|
+
return this.filters.length;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.FilterChain = FilterChain;
|
|
57
|
+
//# sourceMappingURL=FilterChain.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FilterChain.js","sourceRoot":"","sources":["../../../../../packages/http/http-filters/src/FilterChain.ts"],"names":[],"mappings":";;;AAAA,qCAAkE;AAElE;;;;;;;;GAQG;AACH,MAAa,WAAW;IAGtB,YAAY,OAAiB;QAC3B,2CAA2C;QAC3C,IAAI,CAAC,OAAO,GAAG,CAAC,GAAG,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,GAAG,CAAC,CAAC,QAAQ,CAAC,CAAC;IACtE,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,OAAO,CACX,IAAgB,EAChB,YAAmC;QAEnC,IAAI,KAAK,GAAG,CAAC,CAAC;QACd,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC;QAE7B,MAAM,IAAI,GAAe,IAAI,KAAM,SAAQ,mBAAU;YACnD,KAAK,CAAC,OAAO;gBACX,IAAI,KAAK,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;oBAC3B,MAAM,MAAM,GAAG,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC;oBAChC,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;gBACnC,CAAC;qBAAM,CAAC;oBACN,6DAA6D;oBAC7D,OAAO,YAAY,EAAE,CAAC;gBACxB,CAAC;YACH,CAAC;SACF,CAAC;QAEF,OAAO,IAAI,CAAC,OAAO,EAAE,CAAC;IACxB,CAAC;IAED;;OAEG;IACH,UAAU;QACR,OAAO,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC;IAC3B,CAAC;IAED;;OAEG;IACH,IAAI;QACF,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC;IAC7B,CAAC;CACF;AAlDD,kCAkDC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Filter, MethodMeta, Action, NextFilter } from '../Filter';
|
|
2
|
+
/**
|
|
3
|
+
* ContextFilter - Sets up AsyncLocalStorage context for each request.
|
|
4
|
+
* Priority: 140 (executes first)
|
|
5
|
+
*
|
|
6
|
+
* This filter ensures that all subsequent filters and the controller
|
|
7
|
+
* execute within a context that can store request-scoped data.
|
|
8
|
+
*/
|
|
9
|
+
export declare class ContextFilter implements Filter {
|
|
10
|
+
priority: number;
|
|
11
|
+
filter(meta: MethodMeta, next: NextFilter): Promise<Action>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ContextFilter = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const inversify_1 = require("inversify");
|
|
6
|
+
const core_context_1 = require("@webpieces/core-context");
|
|
7
|
+
/**
|
|
8
|
+
* ContextFilter - Sets up AsyncLocalStorage context for each request.
|
|
9
|
+
* Priority: 140 (executes first)
|
|
10
|
+
*
|
|
11
|
+
* This filter ensures that all subsequent filters and the controller
|
|
12
|
+
* execute within a context that can store request-scoped data.
|
|
13
|
+
*/
|
|
14
|
+
let ContextFilter = class ContextFilter {
|
|
15
|
+
constructor() {
|
|
16
|
+
this.priority = 140;
|
|
17
|
+
}
|
|
18
|
+
async filter(meta, next) {
|
|
19
|
+
// Run the rest of the filter chain within a new context
|
|
20
|
+
return core_context_1.Context.run(async () => {
|
|
21
|
+
// Store request metadata in context for other filters to access
|
|
22
|
+
core_context_1.Context.put('METHOD_META', meta);
|
|
23
|
+
core_context_1.Context.put('REQUEST_PATH', meta.path);
|
|
24
|
+
core_context_1.Context.put('HTTP_METHOD', meta.httpMethod);
|
|
25
|
+
try {
|
|
26
|
+
return await next.execute();
|
|
27
|
+
}
|
|
28
|
+
finally {
|
|
29
|
+
// Clean up context (AsyncLocalStorage handles this automatically,
|
|
30
|
+
// but we can explicitly clear if needed)
|
|
31
|
+
core_context_1.Context.clear();
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
exports.ContextFilter = ContextFilter;
|
|
37
|
+
exports.ContextFilter = ContextFilter = tslib_1.__decorate([
|
|
38
|
+
(0, inversify_1.injectable)()
|
|
39
|
+
], ContextFilter);
|
|
40
|
+
//# sourceMappingURL=ContextFilter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ContextFilter.js","sourceRoot":"","sources":["../../../../../../packages/http/http-filters/src/filters/ContextFilter.ts"],"names":[],"mappings":";;;;AAAA,yCAAuC;AACvC,0DAAkD;AAGlD;;;;;;GAMG;AAEI,IAAM,aAAa,GAAnB,MAAM,aAAa;IAAnB;QACL,aAAQ,GAAG,GAAG,CAAC;IAmBjB,CAAC;IAjBC,KAAK,CAAC,MAAM,CAAC,IAAgB,EAAE,IAAgB;QAC7C,wDAAwD;QACxD,OAAO,sBAAO,CAAC,GAAG,CAAC,KAAK,IAAI,EAAE;YAC5B,gEAAgE;YAChE,sBAAO,CAAC,GAAG,CAAC,aAAa,EAAE,IAAI,CAAC,CAAC;YACjC,sBAAO,CAAC,GAAG,CAAC,cAAc,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;YACvC,sBAAO,CAAC,GAAG,CAAC,aAAa,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;YAE5C,IAAI,CAAC;gBACH,OAAO,MAAM,IAAI,CAAC,OAAO,EAAE,CAAC;YAC9B,CAAC;oBAAS,CAAC;gBACT,kEAAkE;gBAClE,yCAAyC;gBACzC,sBAAO,CAAC,KAAK,EAAE,CAAC;YAClB,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF,CAAA;AApBY,sCAAa;wBAAb,aAAa;IADzB,IAAA,sBAAU,GAAE;GACA,aAAa,CAoBzB"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Filter, MethodMeta, Action, NextFilter } from '../Filter';
|
|
2
|
+
/**
|
|
3
|
+
* Configuration for JsonFilter.
|
|
4
|
+
*/
|
|
5
|
+
export interface JsonFilterConfig {
|
|
6
|
+
/**
|
|
7
|
+
* Whether to enable validation using class-validator.
|
|
8
|
+
* Default: true
|
|
9
|
+
*/
|
|
10
|
+
validationEnabled?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Whether to log requests and responses.
|
|
13
|
+
* Default: false
|
|
14
|
+
*/
|
|
15
|
+
loggingEnabled?: boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* JsonFilter - Handles JSON deserialization and serialization.
|
|
19
|
+
* Priority: 60
|
|
20
|
+
*
|
|
21
|
+
* Similar to Java WebPieces JacksonCatchAllFilter.
|
|
22
|
+
*
|
|
23
|
+
* Responsibilities:
|
|
24
|
+
* 1. Deserialize request body to DTO (if request has body)
|
|
25
|
+
* 2. Validate DTO using class-validator (if enabled)
|
|
26
|
+
* 3. Execute next filter/controller
|
|
27
|
+
* 4. Serialize response to JSON
|
|
28
|
+
* 5. Handle errors and translate to JSON error responses
|
|
29
|
+
*/
|
|
30
|
+
export declare class JsonFilter implements Filter {
|
|
31
|
+
private config;
|
|
32
|
+
priority: number;
|
|
33
|
+
constructor(config?: JsonFilterConfig);
|
|
34
|
+
filter(meta: MethodMeta, next: NextFilter): Promise<Action>;
|
|
35
|
+
/**
|
|
36
|
+
* Process the request: deserialize and validate.
|
|
37
|
+
*/
|
|
38
|
+
private processRequest;
|
|
39
|
+
/**
|
|
40
|
+
* Validate a DTO using class-validator.
|
|
41
|
+
*/
|
|
42
|
+
private validateDto;
|
|
43
|
+
/**
|
|
44
|
+
* Format validation errors into a readable format.
|
|
45
|
+
*/
|
|
46
|
+
private formatValidationErrors;
|
|
47
|
+
/**
|
|
48
|
+
* Handle errors and translate to JSON error responses.
|
|
49
|
+
*/
|
|
50
|
+
private handleError;
|
|
51
|
+
/**
|
|
52
|
+
* Log the incoming request.
|
|
53
|
+
*/
|
|
54
|
+
private logRequest;
|
|
55
|
+
/**
|
|
56
|
+
* Log the outgoing response.
|
|
57
|
+
*/
|
|
58
|
+
private logResponse;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Exception thrown when validation fails.
|
|
62
|
+
*/
|
|
63
|
+
export declare class ValidationException extends Error {
|
|
64
|
+
violations: string[];
|
|
65
|
+
constructor(violations: string[]);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* HTTP exception with status code.
|
|
69
|
+
*/
|
|
70
|
+
export declare class HttpException extends Error {
|
|
71
|
+
statusCode: number;
|
|
72
|
+
constructor(message: string, statusCode: number);
|
|
73
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpException = exports.ValidationException = exports.JsonFilter = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const inversify_1 = require("inversify");
|
|
6
|
+
const class_validator_1 = require("class-validator");
|
|
7
|
+
const Filter_1 = require("../Filter");
|
|
8
|
+
/**
|
|
9
|
+
* JsonFilter - Handles JSON deserialization and serialization.
|
|
10
|
+
* Priority: 60
|
|
11
|
+
*
|
|
12
|
+
* Similar to Java WebPieces JacksonCatchAllFilter.
|
|
13
|
+
*
|
|
14
|
+
* Responsibilities:
|
|
15
|
+
* 1. Deserialize request body to DTO (if request has body)
|
|
16
|
+
* 2. Validate DTO using class-validator (if enabled)
|
|
17
|
+
* 3. Execute next filter/controller
|
|
18
|
+
* 4. Serialize response to JSON
|
|
19
|
+
* 5. Handle errors and translate to JSON error responses
|
|
20
|
+
*/
|
|
21
|
+
let JsonFilter = class JsonFilter {
|
|
22
|
+
constructor(config = {}) {
|
|
23
|
+
this.config = config;
|
|
24
|
+
this.priority = 60;
|
|
25
|
+
this.config = {
|
|
26
|
+
validationEnabled: true,
|
|
27
|
+
loggingEnabled: false,
|
|
28
|
+
...config,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
async filter(meta, next) {
|
|
32
|
+
try {
|
|
33
|
+
// Deserialize and validate request if there's a body
|
|
34
|
+
await this.processRequest(meta);
|
|
35
|
+
if (this.config.loggingEnabled) {
|
|
36
|
+
this.logRequest(meta);
|
|
37
|
+
}
|
|
38
|
+
// Execute next filter/controller
|
|
39
|
+
const action = await next.execute();
|
|
40
|
+
if (this.config.loggingEnabled) {
|
|
41
|
+
this.logResponse(action);
|
|
42
|
+
}
|
|
43
|
+
// Ensure response is JSON
|
|
44
|
+
if (action.type !== 'json' && action.type !== 'error') {
|
|
45
|
+
return (0, Filter_1.jsonAction)(action.data);
|
|
46
|
+
}
|
|
47
|
+
return action;
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
// Translate error to JSON response
|
|
51
|
+
return this.handleError(error, meta);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Process the request: deserialize and validate.
|
|
56
|
+
*/
|
|
57
|
+
async processRequest(meta) {
|
|
58
|
+
// If there's request data and a parameter type, deserialize it
|
|
59
|
+
if (meta.request?.body && meta.params.length === 0) {
|
|
60
|
+
const body = meta.request.body;
|
|
61
|
+
// For now, we'll just pass the body as-is
|
|
62
|
+
// In a real implementation, we'd use the parameter type from decorators
|
|
63
|
+
// to properly deserialize and validate
|
|
64
|
+
// If we have type information, we can do proper transformation
|
|
65
|
+
// For this MVP, we'll store the body in params[0]
|
|
66
|
+
meta.params[0] = body;
|
|
67
|
+
// If validation is enabled and we have a class instance, validate it
|
|
68
|
+
if (this.config.validationEnabled && body.constructor !== Object) {
|
|
69
|
+
await this.validateDto(body);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Validate a DTO using class-validator.
|
|
75
|
+
*/
|
|
76
|
+
async validateDto(dto) {
|
|
77
|
+
const errors = await (0, class_validator_1.validate)(dto);
|
|
78
|
+
if (errors.length > 0) {
|
|
79
|
+
const messages = this.formatValidationErrors(errors);
|
|
80
|
+
throw new ValidationException(messages);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Format validation errors into a readable format.
|
|
85
|
+
*/
|
|
86
|
+
formatValidationErrors(errors) {
|
|
87
|
+
const messages = [];
|
|
88
|
+
for (const error of errors) {
|
|
89
|
+
if (error.constraints) {
|
|
90
|
+
const constraints = Object.values(error.constraints);
|
|
91
|
+
messages.push(...constraints);
|
|
92
|
+
}
|
|
93
|
+
if (error.children && error.children.length > 0) {
|
|
94
|
+
const childMessages = this.formatValidationErrors(error.children);
|
|
95
|
+
messages.push(...childMessages);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return messages;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Handle errors and translate to JSON error responses.
|
|
102
|
+
*/
|
|
103
|
+
handleError(error, meta) {
|
|
104
|
+
if (error instanceof ValidationException) {
|
|
105
|
+
return (0, Filter_1.errorAction)({
|
|
106
|
+
error: 'Validation failed',
|
|
107
|
+
violations: error.violations,
|
|
108
|
+
}, 400);
|
|
109
|
+
}
|
|
110
|
+
if (error instanceof HttpException) {
|
|
111
|
+
return (0, Filter_1.errorAction)({
|
|
112
|
+
error: error.message,
|
|
113
|
+
code: error.statusCode,
|
|
114
|
+
}, error.statusCode);
|
|
115
|
+
}
|
|
116
|
+
// Log unexpected errors
|
|
117
|
+
console.error('Unexpected error in filter chain:', error);
|
|
118
|
+
return (0, Filter_1.errorAction)('Internal server error', 500);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Log the incoming request.
|
|
122
|
+
*/
|
|
123
|
+
logRequest(meta) {
|
|
124
|
+
console.log(`[JsonFilter] ${meta.httpMethod} ${meta.path}`);
|
|
125
|
+
if (meta.params.length > 0) {
|
|
126
|
+
console.log('[JsonFilter] Request body:', JSON.stringify(meta.params[0], null, 2));
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Log the outgoing response.
|
|
131
|
+
*/
|
|
132
|
+
logResponse(action) {
|
|
133
|
+
console.log(`[JsonFilter] Response: ${action.statusCode}`);
|
|
134
|
+
if (action.data) {
|
|
135
|
+
console.log('[JsonFilter] Response body:', JSON.stringify(action.data, null, 2));
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
exports.JsonFilter = JsonFilter;
|
|
140
|
+
exports.JsonFilter = JsonFilter = tslib_1.__decorate([
|
|
141
|
+
(0, inversify_1.injectable)(),
|
|
142
|
+
tslib_1.__param(0, (0, inversify_1.unmanaged)()),
|
|
143
|
+
tslib_1.__metadata("design:paramtypes", [Object])
|
|
144
|
+
], JsonFilter);
|
|
145
|
+
/**
|
|
146
|
+
* Exception thrown when validation fails.
|
|
147
|
+
*/
|
|
148
|
+
class ValidationException extends Error {
|
|
149
|
+
constructor(violations) {
|
|
150
|
+
super('Validation failed');
|
|
151
|
+
this.violations = violations;
|
|
152
|
+
this.name = 'ValidationException';
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
exports.ValidationException = ValidationException;
|
|
156
|
+
/**
|
|
157
|
+
* HTTP exception with status code.
|
|
158
|
+
*/
|
|
159
|
+
class HttpException extends Error {
|
|
160
|
+
constructor(message, statusCode) {
|
|
161
|
+
super(message);
|
|
162
|
+
this.statusCode = statusCode;
|
|
163
|
+
this.name = 'HttpException';
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
exports.HttpException = HttpException;
|
|
167
|
+
//# sourceMappingURL=JsonFilter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JsonFilter.js","sourceRoot":"","sources":["../../../../../../packages/http/http-filters/src/filters/JsonFilter.ts"],"names":[],"mappings":";;;;AAAA,yCAAkD;AAElD,qDAA4D;AAC5D,sCAA4F;AAoB5F;;;;;;;;;;;;GAYG;AAEI,IAAM,UAAU,GAAhB,MAAM,UAAU;IAGrB,YAAyB,SAAmC,EAAE;QAA7B,WAAM,GAAN,MAAM,CAAuB;QAF9D,aAAQ,GAAG,EAAE,CAAC;QAGZ,IAAI,CAAC,MAAM,GAAG;YACZ,iBAAiB,EAAE,IAAI;YACvB,cAAc,EAAE,KAAK;YACrB,GAAG,MAAM;SACV,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,IAAgB,EAAE,IAAgB;QAC7C,IAAI,CAAC;YACH,qDAAqD;YACrD,MAAM,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;YAEhC,IAAI,IAAI,CAAC,MAAM,CAAC,cAAc,EAAE,CAAC;gBAC/B,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;YACxB,CAAC;YAED,iCAAiC;YACjC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,EAAE,CAAC;YAEpC,IAAI,IAAI,CAAC,MAAM,CAAC,cAAc,EAAE,CAAC;gBAC/B,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;YAC3B,CAAC;YAED,0BAA0B;YAC1B,IAAI,MAAM,CAAC,IAAI,KAAK,MAAM,IAAI,MAAM,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;gBACtD,OAAO,IAAA,mBAAU,EAAC,MAAM,CAAC,IAAI,CAAC,CAAC;YACjC,CAAC;YAED,OAAO,MAAM,C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E,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;QACrF,CAAC;IACH,CAAC;IAED;;OAEG;IACK,WAAW,CAAC,MAAc;QAChC,OAAO,CAAC,GAAG,CAAC,0BAA0B,MAAM,CAAC,UAAU,EAAE,CAAC,CAAC;QAC3D,IAAI,MAAM,CAAC,IAAI,EAAE,CAAC;YAChB,OAAO,CAAC,GAAG,CAAC,6BAA6B,EAAE,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;QACnF,CAAC;IACH,CAAC;CACF,CAAA;AAnJY,gCAAU;qBAAV,UAAU;IADtB,IAAA,sBAAU,GAAE;IAIE,mBAAA,IAAA,qBAAS,GAAE,CAAA;;GAHb,UAAU,CAmJtB;AAED;;GAEG;AACH,MAAa,mBAAoB,SAAQ,KAAK;IAC5C,YAAmB,UAAoB;QACrC,KAAK,CAAC,mBAAmB,CAAC,CAAC;QADV,eAAU,GAAV,UAAU,CAAU;QAErC,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;IACpC,CAAC;CACF;AALD,kDAKC;AAED;;GAEG;AACH,MAAa,aAAc,SAAQ,KAAK;IACtC,YAAY,OAAe,EAAS,UAAkB;QACpD,KAAK,CAAC,OAAO,CAAC,CAAC;QADmB,eAAU,GAAV,UAAU,CAAQ;QAEpD,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;IAC9B,CAAC;CACF;AALD,sCAKC"}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { Filter, MethodMeta, Action, NextFilter, jsonAction, errorAction, } from './Filter';
|
|
2
|
+
export { FilterChain } from './FilterChain';
|
|
3
|
+
export { ContextFilter } from './filters/ContextFilter';
|
|
4
|
+
export { JsonFilter, JsonFilterConfig, ValidationException, HttpException, } from './filters/JsonFilter';
|
package/src/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpException = exports.ValidationException = exports.JsonFilter = exports.ContextFilter = exports.FilterChain = exports.errorAction = exports.jsonAction = exports.NextFilter = void 0;
|
|
4
|
+
var Filter_1 = require("./Filter");
|
|
5
|
+
Object.defineProperty(exports, "NextFilter", { enumerable: true, get: function () { return Filter_1.NextFilter; } });
|
|
6
|
+
Object.defineProperty(exports, "jsonAction", { enumerable: true, get: function () { return Filter_1.jsonAction; } });
|
|
7
|
+
Object.defineProperty(exports, "errorAction", { enumerable: true, get: function () { return Filter_1.errorAction; } });
|
|
8
|
+
var FilterChain_1 = require("./FilterChain");
|
|
9
|
+
Object.defineProperty(exports, "FilterChain", { enumerable: true, get: function () { return FilterChain_1.FilterChain; } });
|
|
10
|
+
var ContextFilter_1 = require("./filters/ContextFilter");
|
|
11
|
+
Object.defineProperty(exports, "ContextFilter", { enumerable: true, get: function () { return ContextFilter_1.ContextFilter; } });
|
|
12
|
+
var JsonFilter_1 = require("./filters/JsonFilter");
|
|
13
|
+
Object.defineProperty(exports, "JsonFilter", { enumerable: true, get: function () { return JsonFilter_1.JsonFilter; } });
|
|
14
|
+
Object.defineProperty(exports, "ValidationException", { enumerable: true, get: function () { return JsonFilter_1.ValidationException; } });
|
|
15
|
+
Object.defineProperty(exports, "HttpException", { enumerable: true, get: function () { return JsonFilter_1.HttpException; } });
|
|
16
|
+
//# sourceMappingURL=index.js.map
|
package/src/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../packages/http/http-filters/src/index.ts"],"names":[],"mappings":";;;AAAA,mCAOkB;AAHhB,oGAAA,UAAU,OAAA;AACV,oGAAA,UAAU,OAAA;AACV,qGAAA,WAAW,OAAA;AAGb,6CAA4C;AAAnC,0GAAA,WAAW,OAAA;AAEpB,yDAAwD;AAA/C,8GAAA,aAAa,OAAA;AACtB,mDAK8B;AAJ5B,wGAAA,UAAU,OAAA;AAEV,iHAAA,mBAAmB,OAAA;AACnB,2GAAA,aAAa,OAAA"}
|