@tsed/platform-response-filter 7.0.0-beta.1 → 7.0.0-beta.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/decorators/responseFilter.js.map +1 -1
- package/lib/cjs/index.js +10 -11
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/package.json +3 -0
- package/lib/cjs/services/PlatformResponseFilter.js +10 -10
- package/lib/cjs/services/PlatformResponseFilter.js.map +1 -1
- package/lib/esm/decorators/responseFilter.js +1 -1
- package/lib/esm/decorators/responseFilter.js.map +1 -1
- package/lib/esm/index.js +10 -11
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/package.json +3 -0
- package/lib/esm/services/PlatformResponseFilter.js +6 -6
- package/lib/esm/services/PlatformResponseFilter.js.map +1 -1
- package/lib/esm/utils/renderView.js +1 -1
- package/lib/types/index.d.ts +3 -0
- package/lib/types/services/PlatformResponseFilter.d.ts +3 -3
- package/package.json +38 -18
- package/readme.md +17 -15
- package/tsconfig.compile.esm.json +1 -12
|
@@ -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,
|
|
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"]}
|
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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
package/lib/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;
|
|
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"]}
|
|
@@ -3,10 +3,10 @@ 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");
|
|
6
7
|
const di_1 = require("@tsed/di");
|
|
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
|
|
@@ -65,9 +65,9 @@ let PlatformResponseFilter = class PlatformResponseFilter {
|
|
|
65
65
|
}
|
|
66
66
|
else if ((0, core_1.isSerializable)(data)) {
|
|
67
67
|
data = (0, json_mapper_1.serialize)(data, {
|
|
68
|
-
...endpoint.getResponseOptions(response.statusCode),
|
|
69
|
-
additionalProperties: this.additionalProperties,
|
|
70
68
|
useAlias: true,
|
|
69
|
+
additionalProperties: this.additionalProperties,
|
|
70
|
+
...endpoint.getResponseOptions(response.statusCode),
|
|
71
71
|
endpoint: true
|
|
72
72
|
});
|
|
73
73
|
}
|
|
@@ -75,19 +75,19 @@ let PlatformResponseFilter = class PlatformResponseFilter {
|
|
|
75
75
|
return data;
|
|
76
76
|
}
|
|
77
77
|
};
|
|
78
|
-
|
|
78
|
+
tslib_1.__decorate([
|
|
79
79
|
(0, di_1.Inject)(),
|
|
80
|
-
|
|
80
|
+
tslib_1.__metadata("design:type", di_1.InjectorService)
|
|
81
81
|
], PlatformResponseFilter.prototype, "injector", void 0);
|
|
82
|
-
|
|
82
|
+
tslib_1.__decorate([
|
|
83
83
|
(0, di_1.Constant)("responseFilters", []),
|
|
84
|
-
|
|
84
|
+
tslib_1.__metadata("design:type", Array)
|
|
85
85
|
], PlatformResponseFilter.prototype, "responseFilters", void 0);
|
|
86
|
-
|
|
86
|
+
tslib_1.__decorate([
|
|
87
87
|
(0, di_1.Constant)("additionalProperties"),
|
|
88
|
-
|
|
88
|
+
tslib_1.__metadata("design:type", Boolean)
|
|
89
89
|
], PlatformResponseFilter.prototype, "additionalProperties", void 0);
|
|
90
|
-
PlatformResponseFilter =
|
|
90
|
+
PlatformResponseFilter = tslib_1.__decorate([
|
|
91
91
|
(0, di_1.Injectable)()
|
|
92
92
|
], PlatformResponseFilter);
|
|
93
93
|
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,
|
|
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;IAAnC;QACY,UAAK,GAAkD,IAAI,GAAG,EAAE,CAAC;IAkF7E,CAAC;IAvEC,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,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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"]}
|
|
@@ -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,
|
|
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"]}
|
package/lib/esm/index.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export * from "./
|
|
5
|
-
export * from "./
|
|
6
|
-
|
|
7
|
-
export * from "./interfaces/ResponseFilterMethods";
|
|
8
|
-
|
|
9
|
-
export * from "./
|
|
10
|
-
export * from "./utils/
|
|
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
|
package/lib/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA
|
|
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"]}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { __decorate, __metadata } from "tslib";
|
|
2
2
|
import { isSerializable } from "@tsed/core";
|
|
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
3
|
import { serialize } from "@tsed/json-mapper";
|
|
7
|
-
import {
|
|
4
|
+
import { Constant, Inject, Injectable, InjectorService } from "@tsed/di";
|
|
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
|
*/
|
|
@@ -62,9 +62,9 @@ let PlatformResponseFilter = class PlatformResponseFilter {
|
|
|
62
62
|
}
|
|
63
63
|
else if (isSerializable(data)) {
|
|
64
64
|
data = serialize(data, {
|
|
65
|
-
...endpoint.getResponseOptions(response.statusCode),
|
|
66
|
-
additionalProperties: this.additionalProperties,
|
|
67
65
|
useAlias: true,
|
|
66
|
+
additionalProperties: this.additionalProperties,
|
|
67
|
+
...endpoint.getResponseOptions(response.statusCode),
|
|
68
68
|
endpoint: true
|
|
69
69
|
});
|
|
70
70
|
}
|
|
@@ -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,
|
|
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,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AAO,IAAI,CAAC;IACd,CAAC;CACF,CAAA;AA/EC;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,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/lib/types/index.d.ts
CHANGED
|
@@ -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
|
-
|
|
12
|
+
protected additionalProperties: boolean;
|
|
13
13
|
get contentTypes(): ResponseFilterKey[];
|
|
14
14
|
$onInit(): void;
|
|
15
15
|
getBestContentType(data: any, ctx: BaseContext): any;
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/platform-response-filter",
|
|
3
|
-
"version": "7.0.0-beta.
|
|
3
|
+
"version": "7.0.0-beta.12",
|
|
4
4
|
"description": "Platform response filter module for Ts.ED Framework",
|
|
5
5
|
"private": false,
|
|
6
6
|
"source": "./src/index.ts",
|
|
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,53 @@
|
|
|
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\""
|
|
19
21
|
},
|
|
20
22
|
"dependencies": {
|
|
21
|
-
"tslib": "2.
|
|
23
|
+
"tslib": "2.3.1"
|
|
22
24
|
},
|
|
23
25
|
"devDependencies": {
|
|
24
|
-
"@tsed/core": "7.0.0-beta.
|
|
25
|
-
"@tsed/di": "7.0.0-beta.
|
|
26
|
-
"@tsed/exceptions": "7.0.0-beta.
|
|
27
|
-
"@tsed/json-mapper": "7.0.0-beta.
|
|
28
|
-
"@tsed/
|
|
29
|
-
"@tsed/schema": "7.0.0-beta.1"
|
|
26
|
+
"@tsed/core": "7.0.0-beta.12",
|
|
27
|
+
"@tsed/di": "7.0.0-beta.12",
|
|
28
|
+
"@tsed/exceptions": "7.0.0-beta.12",
|
|
29
|
+
"@tsed/json-mapper": "7.0.0-beta.12",
|
|
30
|
+
"@tsed/schema": "7.0.0-beta.12"
|
|
30
31
|
},
|
|
31
32
|
"peerDependencies": {
|
|
32
|
-
"@tsed/core": "^7.0.0-beta.
|
|
33
|
-
"@tsed/di": "^7.0.0-beta.
|
|
34
|
-
"@tsed/exceptions": "^7.0.0-beta.
|
|
35
|
-
"@tsed/json-mapper": "^7.0.0-beta.
|
|
36
|
-
"@tsed/
|
|
37
|
-
|
|
33
|
+
"@tsed/core": "^7.0.0-beta.12",
|
|
34
|
+
"@tsed/di": "^7.0.0-beta.12",
|
|
35
|
+
"@tsed/exceptions": "^7.0.0-beta.12",
|
|
36
|
+
"@tsed/json-mapper": "^7.0.0-beta.12",
|
|
37
|
+
"@tsed/schema": "^7.0.0-beta.12"
|
|
38
|
+
},
|
|
39
|
+
"peerDependenciesMeta": {
|
|
40
|
+
"@tsed/core": {
|
|
41
|
+
"optional": false
|
|
42
|
+
},
|
|
43
|
+
"@tsed/di": {
|
|
44
|
+
"optional": false
|
|
45
|
+
},
|
|
46
|
+
"@tsed/exceptions": {
|
|
47
|
+
"optional": false
|
|
48
|
+
},
|
|
49
|
+
"@tsed/json-mapper": {
|
|
50
|
+
"optional": false
|
|
51
|
+
},
|
|
52
|
+
"@tsed/schema": {
|
|
53
|
+
"optional": false
|
|
54
|
+
}
|
|
38
55
|
},
|
|
39
56
|
"repository": "https://github.com/tsedio/tsed",
|
|
40
57
|
"bugs": {
|
|
41
58
|
"url": "https://github.com/tsedio/tsed/issues"
|
|
42
59
|
},
|
|
43
|
-
"homepage": "https://github.com/tsedio/tsed/tree/
|
|
60
|
+
"homepage": "https://github.com/tsedio/tsed/tree/beta/packages/platform/platform-response-filter",
|
|
44
61
|
"author": "Romain Lenzotti",
|
|
45
|
-
"license": "MIT"
|
|
62
|
+
"license": "MIT",
|
|
63
|
+
"publishConfig": {
|
|
64
|
+
"tag": "beta"
|
|
65
|
+
}
|
|
46
66
|
}
|
package/readme.md
CHANGED
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
[](https://badge.fury.io/js/%40tsed%2Fcommon)
|
|
12
12
|
[](https://github.com/semantic-release/semantic-release)
|
|
13
13
|
[](https://github.com/prettier/prettier)
|
|
14
|
-
[](https://github.com/sponsors/romakita)
|
|
15
|
+
[](https://opencollective.com/tsed)
|
|
15
16
|
|
|
16
17
|
</div>
|
|
17
18
|
|
|
@@ -36,12 +37,13 @@ Transform data returned by a method to a formatted response based on the stored
|
|
|
36
37
|
# Installation
|
|
37
38
|
|
|
38
39
|
```bash
|
|
39
|
-
npm install --save @tsed/di @tsed/platform-views @tsed/schema @tsed/json-schema @tsed/platform-response-filter
|
|
40
|
+
npm install --save @tsed/di @tsed/platform-views @tsed/schema @tsed/json-schema @tsed/platform-response-filter
|
|
40
41
|
```
|
|
41
42
|
|
|
42
43
|
## Usage
|
|
43
44
|
|
|
44
45
|
Define a class that return data:
|
|
46
|
+
|
|
45
47
|
```typescript
|
|
46
48
|
import {Injectable} from "@tsed/di";
|
|
47
49
|
import {Returns} from "@tsed/schema";
|
|
@@ -51,13 +53,13 @@ import {MyModel} from "../models/MyModel";
|
|
|
51
53
|
class MyService {
|
|
52
54
|
@Returns(200, MyModel)
|
|
53
55
|
async getData() {
|
|
54
|
-
return new MyModel({
|
|
56
|
+
return new MyModel({id: "id", test: "test"});
|
|
55
57
|
}
|
|
56
58
|
|
|
57
59
|
@Returns(200, MyModel)
|
|
58
60
|
@View("myview.ejs")
|
|
59
61
|
async getDataView() {
|
|
60
|
-
return new MyModel({
|
|
62
|
+
return new MyModel({id: "id", test: "test"});
|
|
61
63
|
}
|
|
62
64
|
}
|
|
63
65
|
```
|
|
@@ -80,7 +82,7 @@ Then call the service in you module:
|
|
|
80
82
|
```typescript
|
|
81
83
|
import {Module, DIContext} from "@tsed/di";
|
|
82
84
|
import {ResponseFilter} from "@tsed/platform-response-filter";
|
|
83
|
-
import "./filters/WrapperResponseFilter"
|
|
85
|
+
import "./filters/WrapperResponseFilter";
|
|
84
86
|
|
|
85
87
|
@Module()
|
|
86
88
|
class MyModule {
|
|
@@ -95,36 +97,36 @@ class MyModule {
|
|
|
95
97
|
id: uuid.v4(),
|
|
96
98
|
injector: this.injector,
|
|
97
99
|
logger: this.injector.logger
|
|
98
|
-
})
|
|
100
|
+
});
|
|
99
101
|
|
|
100
102
|
// must implement these methods
|
|
101
103
|
context.request = {
|
|
102
104
|
accepts(...args: any[]) {
|
|
103
|
-
return req.accepts(...args)
|
|
105
|
+
return req.accepts(...args);
|
|
104
106
|
},
|
|
105
107
|
get(key: string) {
|
|
106
|
-
return req.get(key)
|
|
108
|
+
return req.get(key);
|
|
107
109
|
}
|
|
108
|
-
}
|
|
110
|
+
};
|
|
109
111
|
context.response = {
|
|
110
112
|
contentType(contentType: string) {
|
|
111
|
-
res.contentType(contentType)
|
|
113
|
+
res.contentType(contentType);
|
|
112
114
|
}
|
|
113
|
-
}
|
|
115
|
+
};
|
|
114
116
|
|
|
115
117
|
const service = this.injector.get<MyService>(MyService);
|
|
116
|
-
let data = await service.getData()
|
|
118
|
+
let data = await service.getData();
|
|
117
119
|
|
|
118
120
|
// serialize data (map Model to Plain object)
|
|
119
121
|
data = await this.responseFilter.serialize(data, context);
|
|
120
122
|
|
|
121
123
|
// call filter based on the right content type
|
|
122
|
-
data = await this.responseFilter.transform(data, context)
|
|
124
|
+
data = await this.responseFilter.transform(data, context);
|
|
123
125
|
|
|
124
126
|
if (isObject(data)) {
|
|
125
|
-
res.json(data)
|
|
127
|
+
res.json(data);
|
|
126
128
|
} else {
|
|
127
|
-
res.send(data)
|
|
129
|
+
res.send(data);
|
|
128
130
|
}
|
|
129
131
|
}
|
|
130
132
|
}
|
|
@@ -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
|
}
|