@tsed/platform-response-filter 7.0.0-beta.11 → 7.0.0-beta.14
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/jest.config.js +14 -0
- package/lib/cjs/errors/TemplateRenderError.js +1 -1
- package/lib/cjs/errors/TemplateRenderError.js.map +1 -1
- package/lib/cjs/services/PlatformResponseFilter.js +4 -3
- package/lib/cjs/services/PlatformResponseFilter.js.map +1 -1
- package/lib/esm/errors/TemplateRenderError.js +1 -1
- package/lib/esm/errors/TemplateRenderError.js.map +1 -1
- package/lib/esm/services/PlatformResponseFilter.js +4 -3
- package/lib/esm/services/PlatformResponseFilter.js.map +1 -1
- package/package.json +18 -14
- package/readme.md +17 -15
package/jest.config.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// For a detailed explanation regarding each configuration property, visit:
|
|
2
|
+
// https://jestjs.io/docs/en/configuration.html
|
|
3
|
+
|
|
4
|
+
module.exports = {
|
|
5
|
+
...require("@tsed/jest-config")(__dirname, "platform-response-filter"),
|
|
6
|
+
coverageThreshold: {
|
|
7
|
+
global: {
|
|
8
|
+
statements: 96.47,
|
|
9
|
+
branches: 84,
|
|
10
|
+
functions: 89.47,
|
|
11
|
+
lines: 96.25
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
};
|
|
@@ -7,9 +7,9 @@ const exceptions_1 = require("@tsed/exceptions");
|
|
|
7
7
|
* @private
|
|
8
8
|
*/
|
|
9
9
|
class TemplateRenderError extends exceptions_1.InternalServerError {
|
|
10
|
+
name = "TEMPLATE_RENDER_ERROR";
|
|
10
11
|
constructor(target, method, err) {
|
|
11
12
|
super(TemplateRenderError.buildMessage(target, method, err));
|
|
12
|
-
this.name = "TEMPLATE_RENDER_ERROR";
|
|
13
13
|
}
|
|
14
14
|
/**
|
|
15
15
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TemplateRenderError.js","sourceRoot":"","sources":["../../../src/errors/TemplateRenderError.ts"],"names":[],"mappings":";;;AAAA,qCAAwC;AACxC,iDAAqD;AAErD;;GAEG;AACH,MAAa,mBAAoB,SAAQ,gCAAmB;
|
|
1
|
+
{"version":3,"file":"TemplateRenderError.js","sourceRoot":"","sources":["../../../src/errors/TemplateRenderError.ts"],"names":[],"mappings":";;;AAAA,qCAAwC;AACxC,iDAAqD;AAErD;;GAEG;AACH,MAAa,mBAAoB,SAAQ,gCAAmB;IAC1D,IAAI,GAAG,uBAAuB,CAAC;IAE/B,YAAY,MAA0B,EAAE,MAAuB,EAAE,GAAU;QACzE,KAAK,CAAC,mBAAmB,CAAC,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC;IAC/D,CAAC;IAED;;;OAGG;IACH,MAAM,CAAC,YAAY,CAAC,MAA0B,EAAE,MAAuB,EAAE,GAAU;QACjF,OAAO,6BAA6B,IAAA,aAAM,EAAC,MAAM,CAAC,IAAI,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,GAAG,CAAC;IACnF,CAAC;CACF;AAdD,kDAcC","sourcesContent":["import {nameOf, Type} from \"@tsed/core\";\nimport {InternalServerError} from \"@tsed/exceptions\";\n\n/**\n * @private\n */\nexport class TemplateRenderError extends InternalServerError {\n name = \"TEMPLATE_RENDER_ERROR\";\n\n constructor(target: Type<any> | string, method: string | symbol, err: Error) {\n super(TemplateRenderError.buildMessage(target, method, err));\n }\n\n /**\n *\n * @returns {string}\n */\n static buildMessage(target: Type<any> | string, method: string | symbol, err: Error) {\n return `Template rendering error: ${nameOf(target)}.${String(method)}()\\n` + err;\n }\n}\n"]}
|
|
@@ -12,9 +12,10 @@ const renderView_1 = require("../utils/renderView");
|
|
|
12
12
|
* @platform
|
|
13
13
|
*/
|
|
14
14
|
let PlatformResponseFilter = class PlatformResponseFilter {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
types = new Map();
|
|
16
|
+
injector;
|
|
17
|
+
responseFilters;
|
|
18
|
+
additionalProperties;
|
|
18
19
|
get contentTypes() {
|
|
19
20
|
return [...this.types.keys()];
|
|
20
21
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformResponseFilter.js","sourceRoot":"","sources":["../../../src/services/PlatformResponseFilter.ts"],"names":[],"mappings":";;;;AAAA,qCAAgD;AAChD,mDAA4C;AAC5C,iCAAoF;AACpF,iFAA+F;AAE/F,4DAAyE;AACzE,oDAA+C;AAE/C;;GAEG;AAEH,IAAa,sBAAsB,GAAnC,MAAa,sBAAsB;
|
|
1
|
+
{"version":3,"file":"PlatformResponseFilter.js","sourceRoot":"","sources":["../../../src/services/PlatformResponseFilter.ts"],"names":[],"mappings":";;;;AAAA,qCAAgD;AAChD,mDAA4C;AAC5C,iCAAoF;AACpF,iFAA+F;AAE/F,4DAAyE;AACzE,oDAA+C;AAE/C;;GAEG;AAEH,IAAa,sBAAsB,GAAnC,MAAa,sBAAsB;IACvB,KAAK,GAAkD,IAAI,GAAG,EAAE,CAAC;IAGjE,QAAQ,CAAkB;IAG1B,eAAe,CAAgC;IAG/C,oBAAoB,CAAU;IAExC,IAAI,YAAY;QACd,OAAO,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;IAChC,CAAC;IAED,OAAO;QACL,mDAAwB,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE;YAC/C,IAAI,IAAI,CAAC,eAAe,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE;gBACxC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAE,CAAC,CAAC;aACjD;QACH,CAAC,CAAC,CAAC;IACL,CAAC;IAED,kBAAkB,CAAC,IAAS,EAAE,GAAgB;QAC5C,MAAM,WAAW,GAAG,IAAA,+BAAc,EAAC,IAAI,EAAE,GAAG,CAAC,CAAC;QAE9C,I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,IAAI,QAAQ,EAAE;YACZ,IAAI,QAAQ,CAAC,IAAI,EAAE;gBACjB,IAAI,GAAG,MAAM,IAAA,uBAAU,EAAC,IAAI,EAAE,GAAG,CAAC,CAAC;aACpC;iBAAM,IAAI,IAAA,qBAAc,EAAC,IAAI,CAAC,EAAE;gBAC/B,IAAI,GAAG,IAAA,uBAAS,EAAC,IAAI,EAAE;oBACrB,QAAQ,EAAE,IAAI;oBACd,oBAAoB,EAAE,IAAI,CAAC,oBAAoB;oBAC/C,GAAG,QAAQ,CAAC,kBAAkB,CAAC,QAAQ,CAAC,UAAU,CAAC;oBACnD,QAAQ,EAAE,IAAI;iBACf,CAAC,CAAC;aACJ;SACF;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CACF,CAAA;AA/EC;IADC,IAAA,WAAM,GAAE;sCACW,oBAAe;wDAAC;AAGpC;IADC,IAAA,aAAQ,EAAC,iBAAiB,EAAE,EAAE,CAAC;;+DACyB;AAGzD;IADC,IAAA,aAAQ,EAAC,sBAAsB,CAAC;;oEACO;AAV7B,sBAAsB;IADlC,IAAA,eAAU,GAAE;GACA,sBAAsB,CAmFlC;AAnFY,wDAAsB","sourcesContent":["import {isSerializable, Type} from \"@tsed/core\";\nimport {serialize} from \"@tsed/json-mapper\";\nimport {BaseContext, Constant, Inject, Injectable, InjectorService} from \"@tsed/di\";\nimport {ResponseFilterKey, ResponseFiltersContainer} from \"../domain/ResponseFiltersContainer\";\nimport {ResponseFilterMethods} from \"../interfaces/ResponseFilterMethods\";\nimport {ANY_CONTENT_TYPE, getContentType} from \"../utils/getContentType\";\nimport {renderView} from \"../utils/renderView\";\n\n/**\n * @platform\n */\n@Injectable()\nexport class PlatformResponseFilter {\n protected types: Map<ResponseFilterKey, ResponseFilterMethods> = new Map();\n\n @Inject()\n protected injector: InjectorService;\n\n @Constant(\"responseFilters\", [])\n protected responseFilters: Type<ResponseFilterMethods>[];\n\n @Constant(\"additionalProperties\")\n protected additionalProperties: boolean;\n\n get contentTypes(): ResponseFilterKey[] {\n return [...this.types.keys()];\n }\n\n $onInit() {\n ResponseFiltersContainer.forEach((token, type) => {\n if (this.responseFilters.includes(token)) {\n this.types.set(type, this.injector.get(token)!);\n }\n });\n }\n\n getBestContentType(data: any, ctx: BaseContext) {\n const contentType = getContentType(data, ctx);\n\n if (ctx.request.get(\"Accept\")) {\n const bestContentType = ctx.request.accepts([contentType].concat(this.contentTypes).filter(Boolean));\n\n if (bestContentType) {\n return [].concat(bestContentType as any).filter((type) => type !== \"*/*\")[0];\n }\n }\n\n return contentType;\n }\n\n /**\n * Call filters to transform data\n * @param data\n * @param ctx\n */\n async transform(data: unknown, ctx: BaseContext) {\n const {response} = ctx;\n const bestContentType = this.getBestContentType(data, ctx);\n\n bestContentType && response.contentType(bestContentType);\n\n if (this.types.has(bestContentType)) {\n return this.types.get(bestContentType)!.transform(data, ctx);\n }\n\n if (this.types.has(ANY_CONTENT_TYPE)) {\n return this.types.get(ANY_CONTENT_TYPE)!.transform(data, ctx);\n }\n\n return data;\n }\n\n /**\n * Serialize data before calling filters\n * @param data\n * @param ctx\n */\n async serialize(data: unknown, ctx: BaseContext) {\n const {response, endpoint} = ctx;\n\n if (endpoint) {\n if (endpoint.view) {\n data = await renderView(data, ctx);\n } else if (isSerializable(data)) {\n data = serialize(data, {\n useAlias: true,\n additionalProperties: this.additionalProperties,\n ...endpoint.getResponseOptions(response.statusCode),\n endpoint: true\n });\n }\n }\n\n return data;\n }\n}\n"]}
|
|
@@ -4,9 +4,9 @@ import { InternalServerError } from "@tsed/exceptions";
|
|
|
4
4
|
* @private
|
|
5
5
|
*/
|
|
6
6
|
export class TemplateRenderError extends InternalServerError {
|
|
7
|
+
name = "TEMPLATE_RENDER_ERROR";
|
|
7
8
|
constructor(target, method, err) {
|
|
8
9
|
super(TemplateRenderError.buildMessage(target, method, err));
|
|
9
|
-
this.name = "TEMPLATE_RENDER_ERROR";
|
|
10
10
|
}
|
|
11
11
|
/**
|
|
12
12
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TemplateRenderError.js","sourceRoot":"","sources":["../../../src/errors/TemplateRenderError.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAO,MAAM,YAAY,CAAC;AACxC,OAAO,EAAC,mBAAmB,EAAC,MAAM,kBAAkB,CAAC;AAErD;;GAEG;AACH,MAAM,OAAO,mBAAoB,SAAQ,mBAAmB;
|
|
1
|
+
{"version":3,"file":"TemplateRenderError.js","sourceRoot":"","sources":["../../../src/errors/TemplateRenderError.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAO,MAAM,YAAY,CAAC;AACxC,OAAO,EAAC,mBAAmB,EAAC,MAAM,kBAAkB,CAAC;AAErD;;GAEG;AACH,MAAM,OAAO,mBAAoB,SAAQ,mBAAmB;IAC1D,IAAI,GAAG,uBAAuB,CAAC;IAE/B,YAAY,MAA0B,EAAE,MAAuB,EAAE,GAAU;QACzE,KAAK,CAAC,mBAAmB,CAAC,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC;IAC/D,CAAC;IAED;;;OAGG;IACH,MAAM,CAAC,YAAY,CAAC,MAA0B,EAAE,MAAuB,EAAE,GAAU;QACjF,OAAO,6BAA6B,MAAM,CAAC,MAAM,CAAC,IAAI,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,GAAG,CAAC;IACnF,CAAC;CACF","sourcesContent":["import {nameOf, Type} from \"@tsed/core\";\nimport {InternalServerError} from \"@tsed/exceptions\";\n\n/**\n * @private\n */\nexport class TemplateRenderError extends InternalServerError {\n name = \"TEMPLATE_RENDER_ERROR\";\n\n constructor(target: Type<any> | string, method: string | symbol, err: Error) {\n super(TemplateRenderError.buildMessage(target, method, err));\n }\n\n /**\n *\n * @returns {string}\n */\n static buildMessage(target: Type<any> | string, method: string | symbol, err: Error) {\n return `Template rendering error: ${nameOf(target)}.${String(method)}()\\n` + err;\n }\n}\n"]}
|
|
@@ -9,9 +9,10 @@ import { renderView } from "../utils/renderView.js";
|
|
|
9
9
|
* @platform
|
|
10
10
|
*/
|
|
11
11
|
let PlatformResponseFilter = class PlatformResponseFilter {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
types = new Map();
|
|
13
|
+
injector;
|
|
14
|
+
responseFilters;
|
|
15
|
+
additionalProperties;
|
|
15
16
|
get contentTypes() {
|
|
16
17
|
return [...this.types.keys()];
|
|
17
18
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformResponseFilter.js","sourceRoot":"","sources":["../../../src/services/PlatformResponseFilter.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,cAAc,EAAO,MAAM,YAAY,CAAC;AAChD,OAAO,EAAC,SAAS,EAAC,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAc,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,eAAe,EAAC,MAAM,UAAU,CAAC;AACpF,OAAO,EAAoB,wBAAwB,EAAC,MAAM,oCAAoC,CAAC;AAE/F,OAAO,EAAC,gBAAgB,EAAE,cAAc,EAAC,MAAM,yBAAyB,CAAC;AACzE,OAAO,EAAC,UAAU,EAAC,MAAM,qBAAqB,CAAC;AAE/C;;GAEG;AAEH,IAAa,sBAAsB,GAAnC,MAAa,sBAAsB;
|
|
1
|
+
{"version":3,"file":"PlatformResponseFilter.js","sourceRoot":"","sources":["../../../src/services/PlatformResponseFilter.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,cAAc,EAAO,MAAM,YAAY,CAAC;AAChD,OAAO,EAAC,SAAS,EAAC,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAc,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,eAAe,EAAC,MAAM,UAAU,CAAC;AACpF,OAAO,EAAoB,wBAAwB,EAAC,MAAM,oCAAoC,CAAC;AAE/F,OAAO,EAAC,gBAAgB,EAAE,cAAc,EAAC,MAAM,yBAAyB,CAAC;AACzE,OAAO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AAQ,CAAC,iBAAiB,EAAE,EAAE,CAAC;;+DACyB;AAGzD;IADC,QAAQ,CAAC,sBAAsB,CAAC;;oEACO;AAV7B,sBAAsB;IADlC,UAAU,EAAE;GACA,sBAAsB,CAmFlC;SAnFY,sBAAsB","sourcesContent":["import {isSerializable, Type} from \"@tsed/core\";\nimport {serialize} from \"@tsed/json-mapper\";\nimport {BaseContext, Constant, Inject, Injectable, InjectorService} from \"@tsed/di\";\nimport {ResponseFilterKey, ResponseFiltersContainer} from \"../domain/ResponseFiltersContainer\";\nimport {ResponseFilterMethods} from \"../interfaces/ResponseFilterMethods\";\nimport {ANY_CONTENT_TYPE, getContentType} from \"../utils/getContentType\";\nimport {renderView} from \"../utils/renderView\";\n\n/**\n * @platform\n */\n@Injectable()\nexport class PlatformResponseFilter {\n protected types: Map<ResponseFilterKey, ResponseFilterMethods> = new Map();\n\n @Inject()\n protected injector: InjectorService;\n\n @Constant(\"responseFilters\", [])\n protected responseFilters: Type<ResponseFilterMethods>[];\n\n @Constant(\"additionalProperties\")\n protected additionalProperties: boolean;\n\n get contentTypes(): ResponseFilterKey[] {\n return [...this.types.keys()];\n }\n\n $onInit() {\n ResponseFiltersContainer.forEach((token, type) => {\n if (this.responseFilters.includes(token)) {\n this.types.set(type, this.injector.get(token)!);\n }\n });\n }\n\n getBestContentType(data: any, ctx: BaseContext) {\n const contentType = getContentType(data, ctx);\n\n if (ctx.request.get(\"Accept\")) {\n const bestContentType = ctx.request.accepts([contentType].concat(this.contentTypes).filter(Boolean));\n\n if (bestContentType) {\n return [].concat(bestContentType as any).filter((type) => type !== \"*/*\")[0];\n }\n }\n\n return contentType;\n }\n\n /**\n * Call filters to transform data\n * @param data\n * @param ctx\n */\n async transform(data: unknown, ctx: BaseContext) {\n const {response} = ctx;\n const bestContentType = this.getBestContentType(data, ctx);\n\n bestContentType && response.contentType(bestContentType);\n\n if (this.types.has(bestContentType)) {\n return this.types.get(bestContentType)!.transform(data, ctx);\n }\n\n if (this.types.has(ANY_CONTENT_TYPE)) {\n return this.types.get(ANY_CONTENT_TYPE)!.transform(data, ctx);\n }\n\n return data;\n }\n\n /**\n * Serialize data before calling filters\n * @param data\n * @param ctx\n */\n async serialize(data: unknown, ctx: BaseContext) {\n const {response, endpoint} = ctx;\n\n if (endpoint) {\n if (endpoint.view) {\n data = await renderView(data, ctx);\n } else if (isSerializable(data)) {\n data = serialize(data, {\n useAlias: true,\n additionalProperties: this.additionalProperties,\n ...endpoint.getResponseOptions(response.statusCode),\n endpoint: true\n });\n }\n }\n\n return data;\n }\n}\n"]}
|
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.14",
|
|
4
4
|
"description": "Platform response filter module for Ts.ED Framework",
|
|
5
5
|
"private": false,
|
|
6
6
|
"source": "./src/index.ts",
|
|
@@ -17,24 +17,25 @@
|
|
|
17
17
|
"build": "yarn barrels && yarn run build:esm && yarn run build:cjs",
|
|
18
18
|
"build:cjs": "tsc --build tsconfig.compile.json",
|
|
19
19
|
"build:esm": "tsc --build tsconfig.compile.esm.json",
|
|
20
|
-
"barrels": "yarn barrelsby --delete -d ./src -e \"\\.spec\\.ts\" -e \"__mock__\" -e \".benchmark.ts\""
|
|
20
|
+
"barrels": "yarn barrelsby --delete -d ./src -e \"\\.spec\\.ts\" -e \"__mock__\" -e \".benchmark.ts\"",
|
|
21
|
+
"test": "cross-env NODE_ENV=test yarn jest --max-workers=2"
|
|
21
22
|
},
|
|
22
23
|
"dependencies": {
|
|
23
|
-
"tslib": "2.
|
|
24
|
+
"tslib": "2.4.0"
|
|
24
25
|
},
|
|
25
26
|
"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.
|
|
27
|
+
"@tsed/core": "7.0.0-beta.14",
|
|
28
|
+
"@tsed/di": "7.0.0-beta.14",
|
|
29
|
+
"@tsed/exceptions": "7.0.0-beta.14",
|
|
30
|
+
"@tsed/json-mapper": "7.0.0-beta.14",
|
|
31
|
+
"@tsed/schema": "7.0.0-beta.14"
|
|
31
32
|
},
|
|
32
33
|
"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.
|
|
34
|
+
"@tsed/core": "^7.0.0-beta.14",
|
|
35
|
+
"@tsed/di": "^7.0.0-beta.14",
|
|
36
|
+
"@tsed/exceptions": "^7.0.0-beta.14",
|
|
37
|
+
"@tsed/json-mapper": "^7.0.0-beta.14",
|
|
38
|
+
"@tsed/schema": "^7.0.0-beta.14"
|
|
38
39
|
},
|
|
39
40
|
"peerDependenciesMeta": {
|
|
40
41
|
"@tsed/core": {
|
|
@@ -59,5 +60,8 @@
|
|
|
59
60
|
},
|
|
60
61
|
"homepage": "https://github.com/tsedio/tsed/tree/beta/packages/platform/platform-response-filter",
|
|
61
62
|
"author": "Romain Lenzotti",
|
|
62
|
-
"license": "MIT"
|
|
63
|
+
"license": "MIT",
|
|
64
|
+
"publishConfig": {
|
|
65
|
+
"tag": "beta"
|
|
66
|
+
}
|
|
63
67
|
}
|
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
|
}
|