@tsed/platform-response-filter 7.0.0-beta.2 → 7.0.0-beta.20

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": 100,
3
+ "branches": 100,
4
+ "functions": 100,
5
+ "lines": 100
6
+ }
package/jest.config.js ADDED
@@ -0,0 +1,9 @@
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: require("./coverage.json")
8
+ }
9
+ };
@@ -1 +1 @@
1
- {"version":3,"file":"responseFilter.js","sourceRoot":"","sources":["../../../src/decorators/responseFilter.ts"],"names":[],"mappings":";;;AACA,iCAA0C;AAC1C,iFAA6F;AAE7F;;;;GAIG;AACH,SAAgB,cAAc,CAAC,GAAG,YAAiC;IACjE,OAAO,CAAU,MAAS,EAAY,EAAE;QACtC,YAAY,CAAC,OAAO,CAAC,CAAC,WAAW,EAAE,EAAE;YACnC,IAAA,iDAAsB,EAAC,WAAW,EAAE,MAAa,CAAC,CAAC;QACrD,CAAC,CAAC,CAAC;QACH,IAAA,qBAAgB,EAAC;YACf,OAAO,EAAE,MAAM;YACf,QAAQ,EAAG,MAA+B;SAC3C,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC;AAVD,wCAUC","sourcesContent":["import {Type} from \"@tsed/core\";\nimport {registerProvider} from \"@tsed/di\";\nimport {registerResponseFilter, ResponseFilterKey} from \"../domain/ResponseFiltersContainer\";\n\n/**\n * Register a response filter service.\n * @param contentTypes\n * @decorator\n */\nexport function ResponseFilter(...contentTypes: ResponseFilterKey[]): ClassDecorator {\n return <T = any>(target: T): void | T => {\n contentTypes.forEach((contentType) => {\n registerResponseFilter(contentType, target as any);\n });\n registerProvider({\n provide: target,\n useClass: (target as unknown) as Type<any>\n });\n };\n}\n"]}
1
+ {"version":3,"file":"responseFilter.js","sourceRoot":"","sources":["../../../src/decorators/responseFilter.ts"],"names":[],"mappings":";;;AACA,iCAA0C;AAC1C,iFAA6F;AAE7F;;;;GAIG;AACH,SAAgB,cAAc,CAAC,GAAG,YAAiC;IACjE,OAAO,CAAU,MAAS,EAAY,EAAE;QACtC,YAAY,CAAC,OAAO,CAAC,CAAC,WAAW,EAAE,EAAE;YACnC,IAAA,iDAAsB,EAAC,WAAW,EAAE,MAAa,CAAC,CAAC;QACrD,CAAC,CAAC,CAAC;QACH,IAAA,qBAAgB,EAAC;YACf,OAAO,EAAE,MAAM;YACf,QAAQ,EAAE,MAA8B;SACzC,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC;AAVD,wCAUC","sourcesContent":["import {Type} from \"@tsed/core\";\nimport {registerProvider} from \"@tsed/di\";\nimport {registerResponseFilter, ResponseFilterKey} from \"../domain/ResponseFiltersContainer\";\n\n/**\n * Register a response filter service.\n * @param contentTypes\n * @decorator\n */\nexport function ResponseFilter(...contentTypes: ResponseFilterKey[]): ClassDecorator {\n return <T = any>(target: T): void | T => {\n contentTypes.forEach((contentType) => {\n registerResponseFilter(contentType, target as any);\n });\n registerProvider({\n provide: target,\n useClass: target as unknown as Type<any>\n });\n };\n}\n"]}
@@ -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;IAG1D,YAAY,MAA0B,EAAE,MAAuB,EAAE,GAAU;QACzE,KAAK,CAAC,mBAAmB,CAAC,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC;QAH/D,SAAI,GAAG,uBAAuB,CAAC;IAI/B,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"]}
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"]}
package/lib/cjs/index.js CHANGED
@@ -1,15 +1,14 @@
1
1
  "use strict";
2
+ /**
3
+ * @file Automatically generated by barrelsby.
4
+ */
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  const tslib_1 = require("tslib");
4
- // decorators
5
- (0, tslib_1.__exportStar)(require("./decorators/responseFilter"), exports);
6
- // domain
7
- (0, tslib_1.__exportStar)(require("./domain/ResponseFiltersContainer"), exports);
8
- (0, tslib_1.__exportStar)(require("./errors/TemplateRenderError"), exports);
9
- // interfaces
10
- (0, tslib_1.__exportStar)(require("./interfaces/ResponseFilterMethods"), exports);
11
- // services
12
- (0, tslib_1.__exportStar)(require("./services/PlatformResponseFilter"), exports);
13
- (0, tslib_1.__exportStar)(require("./utils/getContentType"), exports);
14
- (0, tslib_1.__exportStar)(require("./utils/renderView"), exports);
7
+ tslib_1.__exportStar(require("./decorators/responseFilter"), exports);
8
+ tslib_1.__exportStar(require("./domain/ResponseFiltersContainer"), exports);
9
+ tslib_1.__exportStar(require("./errors/TemplateRenderError"), exports);
10
+ tslib_1.__exportStar(require("./interfaces/ResponseFilterMethods"), exports);
11
+ tslib_1.__exportStar(require("./services/PlatformResponseFilter"), exports);
12
+ tslib_1.__exportStar(require("./utils/getContentType"), exports);
13
+ tslib_1.__exportStar(require("./utils/renderView"), exports);
15
14
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,aAAa;AACb,2EAA4C;AAE5C,SAAS;AACT,iFAAkD;AAClD,4EAA6C;AAE7C,aAAa;AACb,kFAAmD;AAEnD,WAAW;AACX,iFAAkD;AAElD,sEAAuC;AACvC,kEAAmC","sourcesContent":["// decorators\nexport * from \"./decorators/responseFilter\";\n\n// domain\nexport * from \"./domain/ResponseFiltersContainer\";\nexport * from \"./errors/TemplateRenderError\";\n\n// interfaces\nexport * from \"./interfaces/ResponseFilterMethods\";\n\n// services\nexport * from \"./services/PlatformResponseFilter\";\n\nexport * from \"./utils/getContentType\";\nexport * from \"./utils/renderView\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,sEAA4C;AAC5C,4EAAkD;AAClD,uEAA6C;AAC7C,6EAAmD;AACnD,4EAAkD;AAClD,iEAAuC;AACvC,6DAAmC","sourcesContent":["/**\n * @file Automatically generated by barrelsby.\n */\n\nexport * from \"./decorators/responseFilter\";\nexport * from \"./domain/ResponseFiltersContainer\";\nexport * from \"./errors/TemplateRenderError\";\nexport * from \"./interfaces/ResponseFilterMethods\";\nexport * from \"./services/PlatformResponseFilter\";\nexport * from \"./utils/getContentType\";\nexport * from \"./utils/renderView\";\n"]}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -4,17 +4,18 @@ exports.PlatformResponseFilter = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const core_1 = require("@tsed/core");
6
6
  const di_1 = require("@tsed/di");
7
+ const json_mapper_1 = require("@tsed/json-mapper");
7
8
  const ResponseFiltersContainer_1 = require("../domain/ResponseFiltersContainer");
8
9
  const getContentType_1 = require("../utils/getContentType");
9
- const json_mapper_1 = require("@tsed/json-mapper");
10
10
  const renderView_1 = require("../utils/renderView");
11
11
  /**
12
12
  * @platform
13
13
  */
14
14
  let PlatformResponseFilter = class PlatformResponseFilter {
15
- constructor() {
16
- this.types = new Map();
17
- }
15
+ types = new Map();
16
+ injector;
17
+ responseFilters;
18
+ additionalProperties;
18
19
  get contentTypes() {
19
20
  return [...this.types.keys()];
20
21
  }
@@ -42,13 +43,15 @@ let PlatformResponseFilter = class PlatformResponseFilter {
42
43
  */
43
44
  async transform(data, ctx) {
44
45
  const { response } = ctx;
45
- const bestContentType = this.getBestContentType(data, ctx);
46
- bestContentType && response.contentType(bestContentType);
47
- if (this.types.has(bestContentType)) {
48
- return this.types.get(bestContentType).transform(data, ctx);
49
- }
50
- if (this.types.has(getContentType_1.ANY_CONTENT_TYPE)) {
51
- return this.types.get(getContentType_1.ANY_CONTENT_TYPE).transform(data, ctx);
46
+ if (ctx.endpoint?.operation) {
47
+ const bestContentType = this.getBestContentType(data, ctx);
48
+ bestContentType && response.contentType(bestContentType);
49
+ if (this.types.has(bestContentType)) {
50
+ return this.types.get(bestContentType).transform(data, ctx);
51
+ }
52
+ if (this.types.has(getContentType_1.ANY_CONTENT_TYPE)) {
53
+ return this.types.get(getContentType_1.ANY_CONTENT_TYPE).transform(data, ctx);
54
+ }
52
55
  }
53
56
  return data;
54
57
  }
@@ -64,30 +67,39 @@ let PlatformResponseFilter = class PlatformResponseFilter {
64
67
  data = await (0, renderView_1.renderView)(data, ctx);
65
68
  }
66
69
  else if ((0, core_1.isSerializable)(data)) {
70
+ const responseOpts = endpoint.getResponseOptions(response.statusCode, {
71
+ includes: this.getIncludes(ctx)
72
+ });
67
73
  data = (0, json_mapper_1.serialize)(data, {
68
- ...endpoint.getResponseOptions(response.statusCode),
69
- additionalProperties: this.additionalProperties,
70
74
  useAlias: true,
75
+ additionalProperties: this.additionalProperties,
76
+ ...responseOpts,
71
77
  endpoint: true
72
78
  });
73
79
  }
74
80
  }
75
81
  return data;
76
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
+ }
77
89
  };
78
- (0, tslib_1.__decorate)([
90
+ tslib_1.__decorate([
79
91
  (0, di_1.Inject)(),
80
- (0, tslib_1.__metadata)("design:type", di_1.InjectorService)
92
+ tslib_1.__metadata("design:type", di_1.InjectorService)
81
93
  ], PlatformResponseFilter.prototype, "injector", void 0);
82
- (0, tslib_1.__decorate)([
94
+ tslib_1.__decorate([
83
95
  (0, di_1.Constant)("responseFilters", []),
84
- (0, tslib_1.__metadata)("design:type", Array)
96
+ tslib_1.__metadata("design:type", Array)
85
97
  ], PlatformResponseFilter.prototype, "responseFilters", void 0);
86
- (0, tslib_1.__decorate)([
98
+ tslib_1.__decorate([
87
99
  (0, di_1.Constant)("additionalProperties"),
88
- (0, tslib_1.__metadata)("design:type", Boolean)
100
+ tslib_1.__metadata("design:type", Boolean)
89
101
  ], PlatformResponseFilter.prototype, "additionalProperties", void 0);
90
- PlatformResponseFilter = (0, tslib_1.__decorate)([
102
+ PlatformResponseFilter = tslib_1.__decorate([
91
103
  (0, di_1.Injectable)()
92
104
  ], PlatformResponseFilter);
93
105
  exports.PlatformResponseFilter = PlatformResponseFilter;
@@ -1 +1 @@
1
- {"version":3,"file":"PlatformResponseFilter.js","sourceRoot":"","sources":["../../../src/services/PlatformResponseFilter.ts"],"names":[],"mappings":";;;;AAAA,qCAAgD;AAChD,iCAAoF;AACpF,iFAA+F;AAE/F,4DAAyE;AACzE,mDAA4C;AAC5C,oDAA+C;AAE/C;;GAEG;AAEH,IAAa,sBAAsB,GAAnC,MAAa,sBAAsB;IAAnC;QACE,UAAK,GAAkD,IAAI,GAAG,EAAE,CAAC;IAiFnE,CAAC;IAtEC,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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,IAAI,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,iCAAgB,CAAC,EAAE;YACpC,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,iCAAgB,CAAE,CAAC,SAAS,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;SAC/D;QAED,OAAO,IAAI,CAAC;IACd,CAAC;IACD;;;;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,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,GAAG,QAAQ,CAAC,kBAAkB,CAAC,QAAQ,CAAC,UAAU,CAAC;oBACnD,oBAAoB,EAAE,IAAI,CAAC,oBAAoB;oBAC/C,QAAQ,EAAE,IAAI;oBACd,QAAQ,EAAE,IAAI;iBACf,CAAC,CAAC;aACJ;SACF;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CACF,CAAA;AA9EC;IADC,IAAA,WAAM,GAAE;2CACC,oBAAe;wDAAC;AAG1B;IADC,IAAA,aAAQ,EAAC,iBAAiB,EAAE,EAAE,CAAC;;+DACyB;AAGzD;IADC,IAAA,aAAQ,EAAC,sBAAsB,CAAC;;oEACK;AAV3B,sBAAsB;IADlC,IAAA,eAAU,GAAE;GACA,sBAAsB,CAkFlC;AAlFY,wDAAsB","sourcesContent":["import {isSerializable, Type} from \"@tsed/core\";\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 {serialize} from \"@tsed/json-mapper\";\nimport {renderView} from \"../utils/renderView\";\n\n/**\n * @platform\n */\n@Injectable()\nexport class PlatformResponseFilter {\n types: Map<ResponseFilterKey, ResponseFilterMethods> = new Map();\n\n @Inject()\n injector: InjectorService;\n\n @Constant(\"responseFilters\", [])\n protected responseFilters: Type<ResponseFilterMethods>[];\n\n @Constant(\"additionalProperties\")\n private 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 * 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 ...endpoint.getResponseOptions(response.statusCode),\n additionalProperties: this.additionalProperties,\n useAlias: true,\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,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,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,EAAE,CAAC,MAAM,CAAC,eAAsB,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAA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AE,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC;iBAChC,CAAC,CAAC;gBAEH,IAAI,GAAG,IAAA,uBAAS,EAAC,IAAI,EAAE;oBACrB,QAAQ,EAAE,IAAI;oBACd,oBAAoB,EAAE,IAAI,CAAC,oBAAoB;oBAC/C,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,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,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(\"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\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,\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"]}
@@ -11,16 +11,12 @@ function getContentType(data, ctx) {
11
11
  const { operation } = endpoint;
12
12
  const contentType = response.getContentType() || operation.getContentTypeOf(response.statusCode) || "";
13
13
  if (contentType && contentType !== exports.ANY_CONTENT_TYPE) {
14
- if (contentType === "application/json") {
15
- if ((0, core_1.isObject)(data)) {
16
- return contentType;
17
- }
18
- }
19
- else {
20
- return contentType;
14
+ if (contentType === "application/json" && (0, core_1.isObject)(data)) {
15
+ return "application/json";
21
16
  }
17
+ return contentType;
22
18
  }
23
- if (typeof data === "string" && endpoint.view) {
19
+ if (endpoint.view) {
24
20
  return "text/html";
25
21
  }
26
22
  }
@@ -1 +1 @@
1
- {"version":3,"file":"getContentType.js","sourceRoot":"","sources":["../../../src/utils/getContentType.ts"],"names":[],"mappings":";;;AAAA,qCAAoC;AAEvB,QAAA,gBAAgB,GAAG,KAAK,CAAC;AACtC;;GAEG;AACH,SAAgB,cAAc,CAAC,IAAS,EAAE,GAAgB;IACxD,MAAM,EAAC,QAAQ,EAAE,QAAQ,EAAC,GAAG,GAAG,CAAC;IACjC,MAAM,EAAC,SAAS,EAAC,GAAG,QAAQ,CAAC;IAE7B,MAAM,WAAW,GAAG,QAAQ,CAAC,cAAc,EAAE,IAAI,SAAS,CAAC,gBAAgB,CAAC,QAAQ,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC;IAEvG,IAAI,WAAW,IAAI,WAAW,KAAK,wBAAgB,EAAE;QACnD,IAAI,WAAW,KAAK,kBAAkB,EAAE;YACtC,IAAI,IAAA,eAAQ,EAAC,IAAI,CAAC,EAAE;gBAClB,OAAO,WAAW,CAAC;aACpB;SACF;aAAM;YACL,OAAO,WAAW,CAAC;SACpB;KACF;IAED,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,QAAQ,CAAC,IAAI,EAAE;QAC7C,OAAO,WAAW,CAAC;KACpB;AACH,CAAC;AAnBD,wCAmBC","sourcesContent":["import {isObject} from \"@tsed/core\";\nimport type {BaseContext} from \"@tsed/di\";\nexport const ANY_CONTENT_TYPE = \"*/*\";\n/**\n * @ignore\n */\nexport function getContentType(data: any, ctx: BaseContext) {\n const {endpoint, response} = ctx;\n const {operation} = endpoint;\n\n const contentType = response.getContentType() || operation.getContentTypeOf(response.statusCode) || \"\";\n\n if (contentType && contentType !== ANY_CONTENT_TYPE) {\n if (contentType === \"application/json\") {\n if (isObject(data)) {\n return contentType;\n }\n } else {\n return contentType;\n }\n }\n\n if (typeof data === \"string\" && endpoint.view) {\n return \"text/html\";\n }\n}\n"]}
1
+ {"version":3,"file":"getContentType.js","sourceRoot":"","sources":["../../../src/utils/getContentType.ts"],"names":[],"mappings":";;;AAAA,qCAAoC;AAGvB,QAAA,gBAAgB,GAAG,KAAK,CAAC;AAEtC;;GAEG;AACH,SAAgB,cAAc,CAAC,IAAS,EAAE,GAAgB;IACxD,MAAM,EAAC,QAAQ,EAAE,QAAQ,EAAC,GAAG,GAAG,CAAC;IACjC,MAAM,EAAC,SAAS,EAAC,GAAG,QAAQ,CAAC;IAE7B,MAAM,WAAW,GAAG,QAAQ,CAAC,cAAc,EAAE,IAAI,SAAS,CAAC,gBAAgB,CAAC,QAAQ,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC;IAEvG,IAAI,WAAW,IAAI,WAAW,KAAK,wBAAgB,EAAE;QACnD,IAAI,WAAW,KAAK,kBAAkB,IAAI,IAAA,eAAQ,EAAC,IAAI,CAAC,EAAE;YACxD,OAAO,kBAAkB,CAAC;SAC3B;QAED,OAAO,WAAW,CAAC;KACpB;IAED,IAAI,QAAQ,CAAC,IAAI,EAAE;QACjB,OAAO,WAAW,CAAC;KACpB;AACH,CAAC;AAjBD,wCAiBC","sourcesContent":["import {isObject} from \"@tsed/core\";\nimport type {BaseContext} from \"@tsed/di\";\n\nexport const ANY_CONTENT_TYPE = \"*/*\";\n\n/**\n * @ignore\n */\nexport function getContentType(data: any, ctx: BaseContext) {\n const {endpoint, response} = ctx;\n const {operation} = endpoint;\n\n const contentType = response.getContentType() || operation.getContentTypeOf(response.statusCode) || \"\";\n\n if (contentType && contentType !== ANY_CONTENT_TYPE) {\n if (contentType === \"application/json\" && isObject(data)) {\n return \"application/json\";\n }\n\n return contentType;\n }\n\n if (endpoint.view) {\n return \"text/html\";\n }\n}\n"]}
@@ -1,5 +1,5 @@
1
1
  import { registerProvider } from "@tsed/di";
2
- import { registerResponseFilter } from "../domain/ResponseFiltersContainer";
2
+ import { registerResponseFilter } from "../domain/ResponseFiltersContainer.js";
3
3
  /**
4
4
  * Register a response filter service.
5
5
  * @param contentTypes
@@ -1 +1 @@
1
- {"version":3,"file":"responseFilter.js","sourceRoot":"","sources":["../../../src/decorators/responseFilter.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,gBAAgB,EAAC,MAAM,UAAU,CAAC;AAC1C,OAAO,EAAC,sBAAsB,EAAoB,MAAM,oCAAoC,CAAC;AAE7F;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAAC,GAAG,YAAiC;IACjE,OAAO,CAAU,MAAS,EAAY,EAAE;QACtC,YAAY,CAAC,OAAO,CAAC,CAAC,WAAW,EAAE,EAAE;YACnC,sBAAsB,CAAC,WAAW,EAAE,MAAa,CAAC,CAAC;QACrD,CAAC,CAAC,CAAC;QACH,gBAAgB,CAAC;YACf,OAAO,EAAE,MAAM;YACf,QAAQ,EAAG,MAA+B;SAC3C,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import {Type} from \"@tsed/core\";\nimport {registerProvider} from \"@tsed/di\";\nimport {registerResponseFilter, ResponseFilterKey} from \"../domain/ResponseFiltersContainer\";\n\n/**\n * Register a response filter service.\n * @param contentTypes\n * @decorator\n */\nexport function ResponseFilter(...contentTypes: ResponseFilterKey[]): ClassDecorator {\n return <T = any>(target: T): void | T => {\n contentTypes.forEach((contentType) => {\n registerResponseFilter(contentType, target as any);\n });\n registerProvider({\n provide: target,\n useClass: (target as unknown) as Type<any>\n });\n };\n}\n"]}
1
+ {"version":3,"file":"responseFilter.js","sourceRoot":"","sources":["../../../src/decorators/responseFilter.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,gBAAgB,EAAC,MAAM,UAAU,CAAC;AAC1C,OAAO,EAAC,sBAAsB,EAAoB,MAAM,oCAAoC,CAAC;AAE7F;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAAC,GAAG,YAAiC;IACjE,OAAO,CAAU,MAAS,EAAY,EAAE;QACtC,YAAY,CAAC,OAAO,CAAC,CAAC,WAAW,EAAE,EAAE;YACnC,sBAAsB,CAAC,WAAW,EAAE,MAAa,CAAC,CAAC;QACrD,CAAC,CAAC,CAAC;QACH,gBAAgB,CAAC;YACf,OAAO,EAAE,MAAM;YACf,QAAQ,EAAE,MAA8B;SACzC,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import {Type} from \"@tsed/core\";\nimport {registerProvider} from \"@tsed/di\";\nimport {registerResponseFilter, ResponseFilterKey} from \"../domain/ResponseFiltersContainer\";\n\n/**\n * Register a response filter service.\n * @param contentTypes\n * @decorator\n */\nexport function ResponseFilter(...contentTypes: ResponseFilterKey[]): ClassDecorator {\n return <T = any>(target: T): void | T => {\n contentTypes.forEach((contentType) => {\n registerResponseFilter(contentType, target as any);\n });\n registerProvider({\n provide: target,\n useClass: target as unknown as Type<any>\n });\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;IAG1D,YAAY,MAA0B,EAAE,MAAuB,EAAE,GAAU;QACzE,KAAK,CAAC,mBAAmB,CAAC,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC;QAH/D,SAAI,GAAG,uBAAuB,CAAC;IAI/B,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"]}
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"]}
package/lib/esm/index.js CHANGED
@@ -1,12 +1,11 @@
1
- // decorators
2
- export * from "./decorators/responseFilter";
3
- // domain
4
- export * from "./domain/ResponseFiltersContainer";
5
- export * from "./errors/TemplateRenderError";
6
- // interfaces
7
- export * from "./interfaces/ResponseFilterMethods";
8
- // services
9
- export * from "./services/PlatformResponseFilter";
10
- export * from "./utils/getContentType";
11
- export * from "./utils/renderView";
1
+ /**
2
+ * @file Automatically generated by barrelsby.
3
+ */
4
+ export * from "./decorators/responseFilter.js";
5
+ export * from "./domain/ResponseFiltersContainer.js";
6
+ export * from "./errors/TemplateRenderError.js";
7
+ export * from "./interfaces/ResponseFilterMethods.js";
8
+ export * from "./services/PlatformResponseFilter.js";
9
+ export * from "./utils/getContentType.js";
10
+ export * from "./utils/renderView.js";
12
11
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,aAAa;AACb,cAAc,6BAA6B,CAAC;AAE5C,SAAS;AACT,cAAc,mCAAmC,CAAC;AAClD,cAAc,8BAA8B,CAAC;AAE7C,aAAa;AACb,cAAc,oCAAoC,CAAC;AAEnD,WAAW;AACX,cAAc,mCAAmC,CAAC;AAElD,cAAc,wBAAwB,CAAC;AACvC,cAAc,oBAAoB,CAAC","sourcesContent":["// decorators\nexport * from \"./decorators/responseFilter\";\n\n// domain\nexport * from \"./domain/ResponseFiltersContainer\";\nexport * from \"./errors/TemplateRenderError\";\n\n// interfaces\nexport * from \"./interfaces/ResponseFilterMethods\";\n\n// services\nexport * from \"./services/PlatformResponseFilter\";\n\nexport * from \"./utils/getContentType\";\nexport * from \"./utils/renderView\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,6BAA6B,CAAC;AAC5C,cAAc,mCAAmC,CAAC;AAClD,cAAc,8BAA8B,CAAC;AAC7C,cAAc,oCAAoC,CAAC;AACnD,cAAc,mCAAmC,CAAC;AAClD,cAAc,wBAAwB,CAAC;AACvC,cAAc,oBAAoB,CAAC","sourcesContent":["/**\n * @file Automatically generated by barrelsby.\n */\n\nexport * from \"./decorators/responseFilter\";\nexport * from \"./domain/ResponseFiltersContainer\";\nexport * from \"./errors/TemplateRenderError\";\nexport * from \"./interfaces/ResponseFilterMethods\";\nexport * from \"./services/PlatformResponseFilter\";\nexport * from \"./utils/getContentType\";\nexport * from \"./utils/renderView\";\n"]}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -1,17 +1,18 @@
1
1
  import { __decorate, __metadata } from "tslib";
2
2
  import { isSerializable } from "@tsed/core";
3
3
  import { Constant, Inject, Injectable, InjectorService } from "@tsed/di";
4
- import { ResponseFiltersContainer } from "../domain/ResponseFiltersContainer";
5
- import { ANY_CONTENT_TYPE, getContentType } from "../utils/getContentType";
6
4
  import { serialize } from "@tsed/json-mapper";
7
- import { renderView } from "../utils/renderView";
5
+ import { ResponseFiltersContainer } from "../domain/ResponseFiltersContainer.js";
6
+ import { ANY_CONTENT_TYPE, getContentType } from "../utils/getContentType.js";
7
+ import { renderView } from "../utils/renderView.js";
8
8
  /**
9
9
  * @platform
10
10
  */
11
11
  let PlatformResponseFilter = class PlatformResponseFilter {
12
- constructor() {
13
- this.types = new Map();
14
- }
12
+ types = new Map();
13
+ injector;
14
+ responseFilters;
15
+ additionalProperties;
15
16
  get contentTypes() {
16
17
  return [...this.types.keys()];
17
18
  }
@@ -39,13 +40,15 @@ let PlatformResponseFilter = class PlatformResponseFilter {
39
40
  */
40
41
  async transform(data, ctx) {
41
42
  const { response } = ctx;
42
- const bestContentType = this.getBestContentType(data, ctx);
43
- bestContentType && response.contentType(bestContentType);
44
- if (this.types.has(bestContentType)) {
45
- return this.types.get(bestContentType).transform(data, ctx);
46
- }
47
- if (this.types.has(ANY_CONTENT_TYPE)) {
48
- return this.types.get(ANY_CONTENT_TYPE).transform(data, ctx);
43
+ if (ctx.endpoint?.operation) {
44
+ const bestContentType = this.getBestContentType(data, ctx);
45
+ bestContentType && response.contentType(bestContentType);
46
+ if (this.types.has(bestContentType)) {
47
+ return this.types.get(bestContentType).transform(data, ctx);
48
+ }
49
+ if (this.types.has(ANY_CONTENT_TYPE)) {
50
+ return this.types.get(ANY_CONTENT_TYPE).transform(data, ctx);
51
+ }
49
52
  }
50
53
  return data;
51
54
  }
@@ -61,16 +64,25 @@ let PlatformResponseFilter = class PlatformResponseFilter {
61
64
  data = await renderView(data, ctx);
62
65
  }
63
66
  else if (isSerializable(data)) {
67
+ const responseOpts = endpoint.getResponseOptions(response.statusCode, {
68
+ includes: this.getIncludes(ctx)
69
+ });
64
70
  data = serialize(data, {
65
- ...endpoint.getResponseOptions(response.statusCode),
66
- additionalProperties: this.additionalProperties,
67
71
  useAlias: true,
72
+ additionalProperties: this.additionalProperties,
73
+ ...responseOpts,
68
74
  endpoint: true
69
75
  });
70
76
  }
71
77
  }
72
78
  return data;
73
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
+ }
74
86
  };
75
87
  __decorate([
76
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,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,SAAS,EAAC,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAC,UAAU,EAAC,MAAM,qBAAqB,CAAC;AAE/C;;GAEG;AAEH,IAAa,sBAAsB,GAAnC,MAAa,sBAAsB;IAAnC;QACE,UAAK,GAAkD,IAAI,GAAG,EAAE,CAAC;IAiFnE,CAAC;IAtEC,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,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,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;QAE9C,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE;YAC7B,MAAM,eAAe,GAAG,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AU,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;aACpC;iBAAM,IAAI,cAAc,CAAC,IAAI,CAAC,EAAE;gBAC/B,IAAI,GAAG,SAAS,CAAC,IAAI,EAAE;oBACrB,GAAG,QAAQ,CAAC,kBAAkB,CAAC,QAAQ,CAAC,UAAU,CAAC;oBACnD,oBAAoB,EAAE,IAAI,CAAC,oBAAoB;oBAC/C,QAAQ,EAAE,IAAI;oBACd,QAAQ,EAAE,IAAI;iBACf,CAAC,CAAC;aACJ;SACF;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CACF,CAAA;AA9EC;IADC,MAAM,EAAE;8BACC,eAAe;wDAAC;AAG1B;IADC,QAAQ,CAAC,iBAAiB,EAAE,EAAE,CAAC;;+DACyB;AAGzD;IADC,QAAQ,CAAC,sBAAsB,CAAC;;oEACK;AAV3B,sBAAsB;IADlC,UAAU,EAAE;GACA,sBAAsB,CAkFlC;SAlFY,sBAAsB","sourcesContent":["import {isSerializable, Type} from \"@tsed/core\";\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 {serialize} from \"@tsed/json-mapper\";\nimport {renderView} from \"../utils/renderView\";\n\n/**\n * @platform\n */\n@Injectable()\nexport class PlatformResponseFilter {\n types: Map<ResponseFilterKey, ResponseFilterMethods> = new Map();\n\n @Inject()\n injector: InjectorService;\n\n @Constant(\"responseFilters\", [])\n protected responseFilters: Type<ResponseFilterMethods>[];\n\n @Constant(\"additionalProperties\")\n private 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 * 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 ...endpoint.getResponseOptions(response.statusCode),\n additionalProperties: this.additionalProperties,\n useAlias: true,\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,OAAO,EAAC,cAAc,EAAO,MAAM,YAAY,CAAC;AAChD,OAAO,EAAc,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,eAAe,EAAC,MAAM,UAAU,CAAC;AACpF,OAAO,EAAC,SAAS,EAAC,MAAM,mBAAmB,CAAC;AAC5C,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;AAE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;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,sBAAsB,CAAC;;oEACO;AAV7B,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(\"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\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,\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"]}
@@ -8,16 +8,12 @@ export function getContentType(data, ctx) {
8
8
  const { operation } = endpoint;
9
9
  const contentType = response.getContentType() || operation.getContentTypeOf(response.statusCode) || "";
10
10
  if (contentType && contentType !== ANY_CONTENT_TYPE) {
11
- if (contentType === "application/json") {
12
- if (isObject(data)) {
13
- return contentType;
14
- }
15
- }
16
- else {
17
- return contentType;
11
+ if (contentType === "application/json" && isObject(data)) {
12
+ return "application/json";
18
13
  }
14
+ return contentType;
19
15
  }
20
- if (typeof data === "string" && endpoint.view) {
16
+ if (endpoint.view) {
21
17
  return "text/html";
22
18
  }
23
19
  }
@@ -1 +1 @@
1
- {"version":3,"file":"getContentType.js","sourceRoot":"","sources":["../../../src/utils/getContentType.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,YAAY,CAAC;AAEpC,MAAM,CAAC,MAAM,gBAAgB,GAAG,KAAK,CAAC;AACtC;;GAEG;AACH,MAAM,UAAU,cAAc,CAAC,IAAS,EAAE,GAAgB;IACxD,MAAM,EAAC,QAAQ,EAAE,QAAQ,EAAC,GAAG,GAAG,CAAC;IACjC,MAAM,EAAC,SAAS,EAAC,GAAG,QAAQ,CAAC;IAE7B,MAAM,WAAW,GAAG,QAAQ,CAAC,cAAc,EAAE,IAAI,SAAS,CAAC,gBAAgB,CAAC,QAAQ,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC;IAEvG,IAAI,WAAW,IAAI,WAAW,KAAK,gBAAgB,EAAE;QACnD,IAAI,WAAW,KAAK,kBAAkB,EAAE;YACtC,IAAI,QAAQ,CAAC,IAAI,CAAC,EAAE;gBAClB,OAAO,WAAW,CAAC;aACpB;SACF;aAAM;YACL,OAAO,WAAW,CAAC;SACpB;KACF;IAED,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,QAAQ,CAAC,IAAI,EAAE;QAC7C,OAAO,WAAW,CAAC;KACpB;AACH,CAAC","sourcesContent":["import {isObject} from \"@tsed/core\";\nimport type {BaseContext} from \"@tsed/di\";\nexport const ANY_CONTENT_TYPE = \"*/*\";\n/**\n * @ignore\n */\nexport function getContentType(data: any, ctx: BaseContext) {\n const {endpoint, response} = ctx;\n const {operation} = endpoint;\n\n const contentType = response.getContentType() || operation.getContentTypeOf(response.statusCode) || \"\";\n\n if (contentType && contentType !== ANY_CONTENT_TYPE) {\n if (contentType === \"application/json\") {\n if (isObject(data)) {\n return contentType;\n }\n } else {\n return contentType;\n }\n }\n\n if (typeof data === \"string\" && endpoint.view) {\n return \"text/html\";\n }\n}\n"]}
1
+ {"version":3,"file":"getContentType.js","sourceRoot":"","sources":["../../../src/utils/getContentType.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,YAAY,CAAC;AAGpC,MAAM,CAAC,MAAM,gBAAgB,GAAG,KAAK,CAAC;AAEtC;;GAEG;AACH,MAAM,UAAU,cAAc,CAAC,IAAS,EAAE,GAAgB;IACxD,MAAM,EAAC,QAAQ,EAAE,QAAQ,EAAC,GAAG,GAAG,CAAC;IACjC,MAAM,EAAC,SAAS,EAAC,GAAG,QAAQ,CAAC;IAE7B,MAAM,WAAW,GAAG,QAAQ,CAAC,cAAc,EAAE,IAAI,SAAS,CAAC,gBAAgB,CAAC,QAAQ,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC;IAEvG,IAAI,WAAW,IAAI,WAAW,KAAK,gBAAgB,EAAE;QACnD,IAAI,WAAW,KAAK,kBAAkB,IAAI,QAAQ,CAAC,IAAI,CAAC,EAAE;YACxD,OAAO,kBAAkB,CAAC;SAC3B;QAED,OAAO,WAAW,CAAC;KACpB;IAED,IAAI,QAAQ,CAAC,IAAI,EAAE;QACjB,OAAO,WAAW,CAAC;KACpB;AACH,CAAC","sourcesContent":["import {isObject} from \"@tsed/core\";\nimport type {BaseContext} from \"@tsed/di\";\n\nexport const ANY_CONTENT_TYPE = \"*/*\";\n\n/**\n * @ignore\n */\nexport function getContentType(data: any, ctx: BaseContext) {\n const {endpoint, response} = ctx;\n const {operation} = endpoint;\n\n const contentType = response.getContentType() || operation.getContentTypeOf(response.statusCode) || \"\";\n\n if (contentType && contentType !== ANY_CONTENT_TYPE) {\n if (contentType === \"application/json\" && isObject(data)) {\n return \"application/json\";\n }\n\n return contentType;\n }\n\n if (endpoint.view) {\n return \"text/html\";\n }\n}\n"]}
@@ -1,4 +1,4 @@
1
- import { TemplateRenderError } from "../errors/TemplateRenderError";
1
+ import { TemplateRenderError } from "../errors/TemplateRenderError.js";
2
2
  export async function renderView(data, $ctx) {
3
3
  const { response, endpoint } = $ctx;
4
4
  try {
@@ -1,3 +1,6 @@
1
+ /**
2
+ * @file Automatically generated by barrelsby.
3
+ */
1
4
  export * from "./decorators/responseFilter";
2
5
  export * from "./domain/ResponseFiltersContainer";
3
6
  export * from "./errors/TemplateRenderError";
@@ -6,10 +6,10 @@ import { ResponseFilterMethods } from "../interfaces/ResponseFilterMethods";
6
6
  * @platform
7
7
  */
8
8
  export declare class PlatformResponseFilter {
9
- types: Map<ResponseFilterKey, ResponseFilterMethods>;
10
- injector: InjectorService;
9
+ protected types: Map<ResponseFilterKey, ResponseFilterMethods>;
10
+ protected injector: InjectorService;
11
11
  protected responseFilters: Type<ResponseFilterMethods>[];
12
- private additionalProperties;
12
+ protected additionalProperties: boolean;
13
13
  get contentTypes(): ResponseFilterKey[];
14
14
  $onInit(): void;
15
15
  getBestContentType(data: any, ctx: BaseContext): any;
@@ -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,10 +1,11 @@
1
1
  {
2
2
  "name": "@tsed/platform-response-filter",
3
- "version": "7.0.0-beta.2",
3
+ "version": "7.0.0-beta.20",
4
4
  "description": "Platform response filter module for Ts.ED Framework",
5
5
  "private": false,
6
6
  "source": "./src/index.ts",
7
7
  "main": "./lib/cjs/index.js",
8
+ "module": "./lib/esm/index.js",
8
9
  "typings": "./lib/types/index.d.ts",
9
10
  "exports": {
10
11
  "types": "./lib/types/index.d.ts",
@@ -13,34 +14,54 @@
13
14
  "default": "./lib/esm/index.js"
14
15
  },
15
16
  "scripts": {
16
- "build": "yarn run build:esm && yarn run build:cjs",
17
+ "build": "yarn barrels && yarn run build:esm && yarn run build:cjs",
17
18
  "build:cjs": "tsc --build tsconfig.compile.json",
18
- "build:esm": "tsc --build tsconfig.compile.esm.json"
19
+ "build:esm": "tsc --build tsconfig.compile.esm.json",
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"
19
22
  },
20
23
  "dependencies": {
21
- "tslib": "2.2.0"
24
+ "tslib": "2.4.0"
22
25
  },
23
26
  "devDependencies": {
24
- "@tsed/core": "7.0.0-beta.2",
25
- "@tsed/di": "7.0.0-beta.2",
26
- "@tsed/exceptions": "7.0.0-beta.2",
27
- "@tsed/json-mapper": "7.0.0-beta.2",
28
- "@tsed/platform-views": "7.0.0-beta.2",
29
- "@tsed/schema": "7.0.0-beta.2"
27
+ "@tsed/core": "7.0.0-beta.20",
28
+ "@tsed/di": "7.0.0-beta.20",
29
+ "@tsed/exceptions": "7.0.0-beta.20",
30
+ "@tsed/json-mapper": "7.0.0-beta.20",
31
+ "@tsed/schema": "7.0.0-beta.20"
30
32
  },
31
33
  "peerDependencies": {
32
- "@tsed/core": "^7.0.0-beta.2",
33
- "@tsed/di": "^7.0.0-beta.2",
34
- "@tsed/exceptions": "^7.0.0-beta.2",
35
- "@tsed/json-mapper": "^7.0.0-beta.2",
36
- "@tsed/platform-views": "^7.0.0-beta.2",
37
- "@tsed/schema": "^7.0.0-beta.2"
34
+ "@tsed/core": "^7.0.0-beta.20",
35
+ "@tsed/di": "^7.0.0-beta.20",
36
+ "@tsed/exceptions": "^7.0.0-beta.20",
37
+ "@tsed/json-mapper": "^7.0.0-beta.20",
38
+ "@tsed/schema": "^7.0.0-beta.20"
39
+ },
40
+ "peerDependenciesMeta": {
41
+ "@tsed/core": {
42
+ "optional": false
43
+ },
44
+ "@tsed/di": {
45
+ "optional": false
46
+ },
47
+ "@tsed/exceptions": {
48
+ "optional": false
49
+ },
50
+ "@tsed/json-mapper": {
51
+ "optional": false
52
+ },
53
+ "@tsed/schema": {
54
+ "optional": false
55
+ }
38
56
  },
39
57
  "repository": "https://github.com/tsedio/tsed",
40
58
  "bugs": {
41
59
  "url": "https://github.com/tsedio/tsed/issues"
42
60
  },
43
- "homepage": "https://github.com/tsedio/tsed/tree/alpha/packages/platform/platform-response-filter",
61
+ "homepage": "https://github.com/tsedio/tsed/tree/beta/packages/platform/platform-response-filter",
44
62
  "author": "Romain Lenzotti",
45
- "license": "MIT"
63
+ "license": "MIT",
64
+ "publishConfig": {
65
+ "tag": "beta"
66
+ }
46
67
  }
package/readme.md CHANGED
@@ -11,7 +11,8 @@
11
11
  [![npm version](https://badge.fury.io/js/%40tsed%2Fcommon.svg)](https://badge.fury.io/js/%40tsed%2Fcommon)
12
12
  [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
13
13
  [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
14
- [![backers](https://opencollective.com/tsed/tiers/badge.svg)](https://opencollective.com/tsed)
14
+ [![github](https://img.shields.io/static/v1?label=Github%20sponsor&message=%E2%9D%A4&logo=GitHub&color=%23fe8e86)](https://github.com/sponsors/romakita)
15
+ [![opencollective](https://img.shields.io/static/v1?label=OpenCollective%20sponsor&message=%E2%9D%A4&logo=OpenCollective&color=%23fe8e86)](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({ id: "id", test: "test" })
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({ id: "id", test: "test" })
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
  }
@@ -165,7 +167,7 @@ website. [[Become a sponsor](https://opencollective.com/tsed#sponsor)]
165
167
 
166
168
  The MIT License (MIT)
167
169
 
168
- Copyright (c) 2016 - 2018 Romain Lenzotti
170
+ Copyright (c) 2016 - 2022 Romain Lenzotti
169
171
 
170
172
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
171
173
  documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
@@ -8,16 +8,5 @@
8
8
  "declaration": true,
9
9
  "declarationDir": "./lib/types"
10
10
  },
11
- "exclude": [
12
- "node_modules",
13
- "test",
14
- "lib",
15
- "benchmark",
16
- "coverage",
17
- "spec",
18
- "**/*.benchmark.ts",
19
- "**/*.spec.ts",
20
- "keys",
21
- "jest.config.js"
22
- ]
11
+ "exclude": ["node_modules", "test", "lib", "benchmark", "coverage", "spec", "**/*.benchmark.ts", "**/*.spec.ts", "keys", "jest.config.js"]
23
12
  }