@tsed/platform-response-filter 6.125.3 → 6.126.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/coverage.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "statements": 97.8,
3
+ "branches": 92.59,
4
+ "functions": 95,
5
+ "lines": 98.82
6
+ }
package/jest.config.js CHANGED
@@ -4,11 +4,6 @@
4
4
  module.exports = {
5
5
  ...require("@tsed/jest-config")(__dirname, "platform-response-filter"),
6
6
  coverageThreshold: {
7
- global: {
8
- statements: 96.47,
9
- branches: 84,
10
- functions: 89.47,
11
- lines: 96.25
12
- }
7
+ global: require("./coverage.json")
13
8
  }
14
9
  };
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PlatformResponseFilter = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const core_1 = require("@tsed/core");
6
- const json_mapper_1 = require("@tsed/json-mapper");
7
6
  const di_1 = require("@tsed/di");
7
+ const json_mapper_1 = require("@tsed/json-mapper");
8
8
  const ResponseFiltersContainer_1 = require("../domain/ResponseFiltersContainer");
9
9
  const getContentType_1 = require("../utils/getContentType");
10
10
  const renderView_1 = require("../utils/renderView");
@@ -67,16 +67,25 @@ let PlatformResponseFilter = class PlatformResponseFilter {
67
67
  data = await (0, renderView_1.renderView)(data, ctx);
68
68
  }
69
69
  else if ((0, core_1.isSerializable)(data)) {
70
+ const responseOpts = endpoint.getResponseOptions(response.statusCode, {
71
+ includes: this.getIncludes(ctx)
72
+ });
70
73
  data = (0, json_mapper_1.serialize)(data, {
71
74
  useAlias: true,
72
75
  additionalProperties: this.additionalProperties === "accept",
73
- ...endpoint.getResponseOptions(response.statusCode),
76
+ ...responseOpts,
74
77
  endpoint: true
75
78
  });
76
79
  }
77
80
  }
78
81
  return data;
79
82
  }
83
+ getIncludes(ctx) {
84
+ if (ctx.request.query.includes) {
85
+ return [].concat(ctx.request.query.includes).flatMap((include) => include.split(","));
86
+ }
87
+ return undefined;
88
+ }
80
89
  };
81
90
  tslib_1.__decorate([
82
91
  (0, di_1.Inject)(),
@@ -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;IACvB,KAAK,GAAkD,IAAI,GAAG,EAAE,CAAC;IAGjE,QAAQ,CAAkB;IAG1B,eAAe,CAAgC;IAG/C,oBAAoB,CAAS;IAEvC,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,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE;YAC7B,MAAM,eAAe,GAAG,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,WAAW,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC;YAErG,IAAI,eAAe,EAAE;gBACnB,OAAO,EAA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AG,IAAA,uBAAS,EAAC,IAAI,EAAE;oBACrB,QAAQ,EAAE,IAAI;oBACd,oBAAoB,EAAE,IAAI,CAAC,oBAAoB,KAAK,QAAQ;oBAC5D,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;AAlFC;IADC,IAAA,WAAM,GAAE;sCACW,oBAAe;wDAAC;AAGpC;IADC,IAAA,aAAQ,EAAC,iBAAiB,EAAE,EAAE,CAAC;;+DACyB;AAGzD;IADC,IAAA,aAAQ,EAAC,gCAAgC,CAAC;;oEACJ;AAV5B,sBAAsB;IADlC,IAAA,eAAU,GAAE;GACA,sBAAsB,CAsFlC;AAtFY,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(\"converter.additionalProperties\")\n protected additionalProperties: string;\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\n if (ctx.endpoint?.operation) {\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\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 === \"accept\",\n ...endpoint.getResponseOptions(response.statusCode),\n endpoint: true\n });\n }\n }\n\n return data;\n }\n}\n"]}
1
+ {"version":3,"file":"PlatformResponseFilter.js","sourceRoot":"","sources":["../../../src/services/PlatformResponseFilter.ts"],"names":[],"mappings":";;;;AAAA,qCAAgD;AAChD,iCAAoF;AACpF,mDAA4C;AAC5C,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,CAAS;IAEvC,IAAI,YAAY;QACd,OAAO,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;IAChC,CAAC;IAED,OAAO;QACL,mDAAwB,CAAC,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C,OAAe,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;SAC/F;QAED,OAAO,SAAS,CAAC;IACnB,CAAC;CACF,CAAA;AA9FC;IADC,IAAA,WAAM,GAAE;sCACW,oBAAe;wDAAC;AAGpC;IADC,IAAA,aAAQ,EAAC,iBAAiB,EAAE,EAAE,CAAC;;+DACyB;AAGzD;IADC,IAAA,aAAQ,EAAC,gCAAgC,CAAC;;oEACJ;AAV5B,sBAAsB;IADlC,IAAA,eAAU,GAAE;GACA,sBAAsB,CAkGlC;AAlGY,wDAAsB","sourcesContent":["import {isSerializable, Type} from \"@tsed/core\";\nimport {BaseContext, Constant, Inject, Injectable, InjectorService} from \"@tsed/di\";\nimport {serialize} from \"@tsed/json-mapper\";\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(\"converter.additionalProperties\")\n protected additionalProperties: string;\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\n if (ctx.endpoint?.operation) {\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\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 const responseOpts = endpoint.getResponseOptions(response.statusCode, {\n includes: this.getIncludes(ctx)\n });\n\n data = serialize(data, {\n useAlias: true,\n additionalProperties: this.additionalProperties === \"accept\",\n ...responseOpts,\n endpoint: true\n });\n }\n }\n\n return data;\n }\n\n private getIncludes(ctx: BaseContext) {\n if (ctx.request.query.includes) {\n return [].concat(ctx.request.query.includes).flatMap((include: string) => include.split(\",\"));\n }\n\n return undefined;\n }\n}\n"]}
@@ -1,7 +1,7 @@
1
1
  import { __decorate, __metadata } from "tslib";
2
2
  import { isSerializable } from "@tsed/core";
3
- import { serialize } from "@tsed/json-mapper";
4
3
  import { Constant, Inject, Injectable, InjectorService } from "@tsed/di";
4
+ import { serialize } from "@tsed/json-mapper";
5
5
  import { ResponseFiltersContainer } from "../domain/ResponseFiltersContainer.js";
6
6
  import { ANY_CONTENT_TYPE, getContentType } from "../utils/getContentType.js";
7
7
  import { renderView } from "../utils/renderView.js";
@@ -64,16 +64,25 @@ let PlatformResponseFilter = class PlatformResponseFilter {
64
64
  data = await renderView(data, ctx);
65
65
  }
66
66
  else if (isSerializable(data)) {
67
+ const responseOpts = endpoint.getResponseOptions(response.statusCode, {
68
+ includes: this.getIncludes(ctx)
69
+ });
67
70
  data = serialize(data, {
68
71
  useAlias: true,
69
72
  additionalProperties: this.additionalProperties === "accept",
70
- ...endpoint.getResponseOptions(response.statusCode),
73
+ ...responseOpts,
71
74
  endpoint: true
72
75
  });
73
76
  }
74
77
  }
75
78
  return data;
76
79
  }
80
+ getIncludes(ctx) {
81
+ if (ctx.request.query.includes) {
82
+ return [].concat(ctx.request.query.includes).flatMap((include) => include.split(","));
83
+ }
84
+ return undefined;
85
+ }
77
86
  };
78
87
  __decorate([
79
88
  Inject(),
@@ -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;IACvB,KAAK,GAAkD,IAAI,GAAG,EAAE,CAAC;IAGjE,QAAQ,CAAkB;IAG1B,eAAe,CAAgC;IAG/C,oBAAoB,CAAS;IAEvC,IAAI,YAAY;QACd,OAAO,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;IAChC,CAAC;IAED,OAAO;QACL,wBAAwB,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,I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C,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,gBAAgB,CAAE,CAAC,SAAS,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;aAC/D;SACF;QAED,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,SAAS,CAAC,IAAa,EAAE,GAAgB;QAC7C,MAAM,EAAC,QAAQ,EAAE,QAAQ,EAAC,GAAG,GAAG,CAAC;QAEjC,IAAI,QAAQ,EAAE;YACZ,IAAI,QAAQ,CAAC,IAAI,EAAE;gBACjB,IAAI,GAAG,MAAM,UAAU,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;aACpC;iBAAM,IAAI,cAAc,CAAC,IAAI,CAAC,EAAE;gBAC/B,IAAI,GAAG,SAAS,CAAC,IAAI,EAAE;oBACrB,QAAQ,EAAE,IAAI;oBACd,oBAAoB,EAAE,IAAI,CAAC,oBAAoB,KAAK,QAAQ;oBAC5D,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;AAlFC;IADC,MAAM,EAAE;8BACW,eAAe;wDAAC;AAGpC;IADC,QAAQ,CAAC,iBAAiB,EAAE,EAAE,CAAC;;+DACyB;AAGzD;IADC,QAAQ,CAAC,gCAAgC,CAAC;;oEACJ;AAV5B,sBAAsB;IADlC,UAAU,EAAE;GACA,sBAAsB,CAsFlC;SAtFY,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(\"converter.additionalProperties\")\n protected additionalProperties: string;\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\n if (ctx.endpoint?.operation) {\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\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 === \"accept\",\n ...endpoint.getResponseOptions(response.statusCode),\n endpoint: true\n });\n }\n }\n\n return data;\n }\n}\n"]}
1
+ {"version":3,"file":"PlatformResponseFilter.js","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AAM,UAAU,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;aACpC;iBAAM,IAAI,cAAc,CAAC,IAAI,CAAC,EAAE;gBAC/B,MAAM,YAAY,GAAG,QAAQ,CAAC,kBAAkB,CAAC,QAAQ,CAAC,UAAU,EAAE;oBACpE,QAAQ,EAAE,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC;iBAChC,CAAC,CAAC;gBAEH,IAAI,GAAG,SAAS,CAAC,IAAI,EAAE;oBACrB,QAAQ,EAAE,IAAI;oBACd,oBAAoB,EAAE,IAAI,CAAC,oBAAoB,KAAK,QAAQ;oBAC5D,GAAG,YAAY;oBACf,QAAQ,EAAE,IAAI;iBACf,CAAC,CAAC;aACJ;SACF;QAED,OAAO,IAAI,CAAC;IACd,CAAC;IAEO,WAAW,CAAC,GAAgB;QAClC,IAAI,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,QAAQ,EAAE;YAC9B,OAAO,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,CAAC,OAAe,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;SAC/F;QAED,OAAO,SAAS,CAAC;IACnB,CAAC;CACF,CAAA;AA9FC;IADC,MAAM,EAAE;8BACW,eAAe;wDAAC;AAGpC;IADC,QAAQ,CAAC,iBAAiB,EAAE,EAAE,CAAC;;+DACyB;AAGzD;IADC,QAAQ,CAAC,gCAAgC,CAAC;;oEACJ;AAV5B,sBAAsB;IADlC,UAAU,EAAE;GACA,sBAAsB,CAkGlC;SAlGY,sBAAsB","sourcesContent":["import {isSerializable, Type} from \"@tsed/core\";\nimport {BaseContext, Constant, Inject, Injectable, InjectorService} from \"@tsed/di\";\nimport {serialize} from \"@tsed/json-mapper\";\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(\"converter.additionalProperties\")\n protected additionalProperties: string;\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\n if (ctx.endpoint?.operation) {\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\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 const responseOpts = endpoint.getResponseOptions(response.statusCode, {\n includes: this.getIncludes(ctx)\n });\n\n data = serialize(data, {\n useAlias: true,\n additionalProperties: this.additionalProperties === \"accept\",\n ...responseOpts,\n endpoint: true\n });\n }\n }\n\n return data;\n }\n\n private getIncludes(ctx: BaseContext) {\n if (ctx.request.query.includes) {\n return [].concat(ctx.request.query.includes).flatMap((include: string) => include.split(\",\"));\n }\n\n return undefined;\n }\n}\n"]}
@@ -25,4 +25,5 @@ export declare class PlatformResponseFilter {
25
25
  * @param ctx
26
26
  */
27
27
  serialize(data: unknown, ctx: BaseContext): Promise<unknown>;
28
+ private getIncludes;
28
29
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsed/platform-response-filter",
3
- "version": "6.125.3",
3
+ "version": "6.126.0",
4
4
  "description": "Platform response filter module for Ts.ED Framework",
5
5
  "private": false,
6
6
  "source": "./src/index.ts",
@@ -24,18 +24,18 @@
24
24
  "tslib": "2.4.0"
25
25
  },
26
26
  "devDependencies": {
27
- "@tsed/core": "6.125.3",
28
- "@tsed/di": "6.125.3",
29
- "@tsed/exceptions": "6.125.3",
30
- "@tsed/json-mapper": "6.125.3",
31
- "@tsed/schema": "6.125.3"
27
+ "@tsed/core": "6.126.0",
28
+ "@tsed/di": "6.126.0",
29
+ "@tsed/exceptions": "6.126.0",
30
+ "@tsed/json-mapper": "6.126.0",
31
+ "@tsed/schema": "6.126.0"
32
32
  },
33
33
  "peerDependencies": {
34
- "@tsed/core": "^6.125.3",
35
- "@tsed/di": "^6.125.3",
36
- "@tsed/exceptions": "^6.125.3",
37
- "@tsed/json-mapper": "^6.125.3",
38
- "@tsed/schema": "^6.125.3"
34
+ "@tsed/core": "^6.126.0",
35
+ "@tsed/di": "^6.126.0",
36
+ "@tsed/exceptions": "^6.126.0",
37
+ "@tsed/json-mapper": "^6.126.0",
38
+ "@tsed/schema": "^6.126.0"
39
39
  },
40
40
  "peerDependenciesMeta": {
41
41
  "@tsed/core": {