@tsed/platform-response-filter 7.0.0-beta.11 → 7.0.0-beta.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +15 -12
- package/readme.md +17 -15
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/platform-response-filter",
|
|
3
|
-
"version": "7.0.0-beta.
|
|
3
|
+
"version": "7.0.0-beta.12",
|
|
4
4
|
"description": "Platform response filter module for Ts.ED Framework",
|
|
5
5
|
"private": false,
|
|
6
6
|
"source": "./src/index.ts",
|
|
@@ -23,18 +23,18 @@
|
|
|
23
23
|
"tslib": "2.3.1"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@tsed/core": "7.0.0-beta.
|
|
27
|
-
"@tsed/di": "7.0.0-beta.
|
|
28
|
-
"@tsed/exceptions": "7.0.0-beta.
|
|
29
|
-
"@tsed/json-mapper": "7.0.0-beta.
|
|
30
|
-
"@tsed/schema": "7.0.0-beta.
|
|
26
|
+
"@tsed/core": "7.0.0-beta.12",
|
|
27
|
+
"@tsed/di": "7.0.0-beta.12",
|
|
28
|
+
"@tsed/exceptions": "7.0.0-beta.12",
|
|
29
|
+
"@tsed/json-mapper": "7.0.0-beta.12",
|
|
30
|
+
"@tsed/schema": "7.0.0-beta.12"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"@tsed/core": "^7.0.0-beta.
|
|
34
|
-
"@tsed/di": "^7.0.0-beta.
|
|
35
|
-
"@tsed/exceptions": "^7.0.0-beta.
|
|
36
|
-
"@tsed/json-mapper": "^7.0.0-beta.
|
|
37
|
-
"@tsed/schema": "^7.0.0-beta.
|
|
33
|
+
"@tsed/core": "^7.0.0-beta.12",
|
|
34
|
+
"@tsed/di": "^7.0.0-beta.12",
|
|
35
|
+
"@tsed/exceptions": "^7.0.0-beta.12",
|
|
36
|
+
"@tsed/json-mapper": "^7.0.0-beta.12",
|
|
37
|
+
"@tsed/schema": "^7.0.0-beta.12"
|
|
38
38
|
},
|
|
39
39
|
"peerDependenciesMeta": {
|
|
40
40
|
"@tsed/core": {
|
|
@@ -59,5 +59,8 @@
|
|
|
59
59
|
},
|
|
60
60
|
"homepage": "https://github.com/tsedio/tsed/tree/beta/packages/platform/platform-response-filter",
|
|
61
61
|
"author": "Romain Lenzotti",
|
|
62
|
-
"license": "MIT"
|
|
62
|
+
"license": "MIT",
|
|
63
|
+
"publishConfig": {
|
|
64
|
+
"tag": "beta"
|
|
65
|
+
}
|
|
63
66
|
}
|
package/readme.md
CHANGED
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
[](https://badge.fury.io/js/%40tsed%2Fcommon)
|
|
12
12
|
[](https://github.com/semantic-release/semantic-release)
|
|
13
13
|
[](https://github.com/prettier/prettier)
|
|
14
|
-
[](https://github.com/sponsors/romakita)
|
|
15
|
+
[](https://opencollective.com/tsed)
|
|
15
16
|
|
|
16
17
|
</div>
|
|
17
18
|
|
|
@@ -36,12 +37,13 @@ Transform data returned by a method to a formatted response based on the stored
|
|
|
36
37
|
# Installation
|
|
37
38
|
|
|
38
39
|
```bash
|
|
39
|
-
npm install --save @tsed/di @tsed/platform-views @tsed/schema @tsed/json-schema @tsed/platform-response-filter
|
|
40
|
+
npm install --save @tsed/di @tsed/platform-views @tsed/schema @tsed/json-schema @tsed/platform-response-filter
|
|
40
41
|
```
|
|
41
42
|
|
|
42
43
|
## Usage
|
|
43
44
|
|
|
44
45
|
Define a class that return data:
|
|
46
|
+
|
|
45
47
|
```typescript
|
|
46
48
|
import {Injectable} from "@tsed/di";
|
|
47
49
|
import {Returns} from "@tsed/schema";
|
|
@@ -51,13 +53,13 @@ import {MyModel} from "../models/MyModel";
|
|
|
51
53
|
class MyService {
|
|
52
54
|
@Returns(200, MyModel)
|
|
53
55
|
async getData() {
|
|
54
|
-
return new MyModel({
|
|
56
|
+
return new MyModel({id: "id", test: "test"});
|
|
55
57
|
}
|
|
56
58
|
|
|
57
59
|
@Returns(200, MyModel)
|
|
58
60
|
@View("myview.ejs")
|
|
59
61
|
async getDataView() {
|
|
60
|
-
return new MyModel({
|
|
62
|
+
return new MyModel({id: "id", test: "test"});
|
|
61
63
|
}
|
|
62
64
|
}
|
|
63
65
|
```
|
|
@@ -80,7 +82,7 @@ Then call the service in you module:
|
|
|
80
82
|
```typescript
|
|
81
83
|
import {Module, DIContext} from "@tsed/di";
|
|
82
84
|
import {ResponseFilter} from "@tsed/platform-response-filter";
|
|
83
|
-
import "./filters/WrapperResponseFilter"
|
|
85
|
+
import "./filters/WrapperResponseFilter";
|
|
84
86
|
|
|
85
87
|
@Module()
|
|
86
88
|
class MyModule {
|
|
@@ -95,36 +97,36 @@ class MyModule {
|
|
|
95
97
|
id: uuid.v4(),
|
|
96
98
|
injector: this.injector,
|
|
97
99
|
logger: this.injector.logger
|
|
98
|
-
})
|
|
100
|
+
});
|
|
99
101
|
|
|
100
102
|
// must implement these methods
|
|
101
103
|
context.request = {
|
|
102
104
|
accepts(...args: any[]) {
|
|
103
|
-
return req.accepts(...args)
|
|
105
|
+
return req.accepts(...args);
|
|
104
106
|
},
|
|
105
107
|
get(key: string) {
|
|
106
|
-
return req.get(key)
|
|
108
|
+
return req.get(key);
|
|
107
109
|
}
|
|
108
|
-
}
|
|
110
|
+
};
|
|
109
111
|
context.response = {
|
|
110
112
|
contentType(contentType: string) {
|
|
111
|
-
res.contentType(contentType)
|
|
113
|
+
res.contentType(contentType);
|
|
112
114
|
}
|
|
113
|
-
}
|
|
115
|
+
};
|
|
114
116
|
|
|
115
117
|
const service = this.injector.get<MyService>(MyService);
|
|
116
|
-
let data = await service.getData()
|
|
118
|
+
let data = await service.getData();
|
|
117
119
|
|
|
118
120
|
// serialize data (map Model to Plain object)
|
|
119
121
|
data = await this.responseFilter.serialize(data, context);
|
|
120
122
|
|
|
121
123
|
// call filter based on the right content type
|
|
122
|
-
data = await this.responseFilter.transform(data, context)
|
|
124
|
+
data = await this.responseFilter.transform(data, context);
|
|
123
125
|
|
|
124
126
|
if (isObject(data)) {
|
|
125
|
-
res.json(data)
|
|
127
|
+
res.json(data);
|
|
126
128
|
} else {
|
|
127
|
-
res.send(data)
|
|
129
|
+
res.send(data);
|
|
128
130
|
}
|
|
129
131
|
}
|
|
130
132
|
}
|