@tsed/platform-response-filter 6.88.1 → 6.89.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.
Files changed (60) hide show
  1. package/lib/{decorators → cjs/decorators}/responseFilter.d.ts +0 -0
  2. package/lib/cjs/decorators/responseFilter.js +23 -0
  3. package/lib/cjs/decorators/responseFilter.js.map +1 -0
  4. package/lib/{domain → cjs/domain}/ResponseFiltersContainer.d.ts +0 -0
  5. package/lib/cjs/domain/ResponseFiltersContainer.js +16 -0
  6. package/lib/cjs/domain/ResponseFiltersContainer.js.map +1 -0
  7. package/lib/{errors → cjs/errors}/TemplateRenderError.d.ts +0 -0
  8. package/lib/cjs/errors/TemplateRenderError.js +23 -0
  9. package/lib/cjs/errors/TemplateRenderError.js.map +1 -0
  10. package/lib/{index.d.ts → cjs/index.d.ts} +0 -0
  11. package/lib/cjs/index.js +16 -0
  12. package/lib/cjs/index.js.map +1 -0
  13. package/lib/{interfaces → cjs/interfaces}/ResponseFilterMethods.d.ts +0 -0
  14. package/lib/cjs/interfaces/ResponseFilterMethods.js +3 -0
  15. package/lib/cjs/interfaces/ResponseFilterMethods.js.map +1 -0
  16. package/lib/{services → cjs/services}/ConverterService.d.ts +0 -0
  17. package/lib/cjs/services/ConverterService.js +54 -0
  18. package/lib/cjs/services/ConverterService.js.map +1 -0
  19. package/lib/{services → cjs/services}/PlatformResponseFilter.d.ts +0 -0
  20. package/lib/cjs/services/PlatformResponseFilter.js +88 -0
  21. package/lib/cjs/services/PlatformResponseFilter.js.map +1 -0
  22. package/lib/{utils → cjs/utils}/getContentType.d.ts +0 -0
  23. package/lib/cjs/utils/getContentType.js +28 -0
  24. package/lib/cjs/utils/getContentType.js.map +1 -0
  25. package/lib/{utils → cjs/utils}/renderView.d.ts +0 -0
  26. package/lib/cjs/utils/renderView.js +17 -0
  27. package/lib/cjs/utils/renderView.js.map +1 -0
  28. package/lib/esm/decorators/responseFilter.js +19 -0
  29. package/lib/esm/decorators/responseFilter.js.map +1 -0
  30. package/lib/esm/domain/ResponseFiltersContainer.js +12 -0
  31. package/lib/esm/domain/ResponseFiltersContainer.js.map +1 -0
  32. package/lib/esm/errors/TemplateRenderError.js +19 -0
  33. package/lib/esm/errors/TemplateRenderError.js.map +1 -0
  34. package/lib/esm/index.js +13 -0
  35. package/lib/esm/index.js.map +1 -0
  36. package/lib/esm/interfaces/ResponseFilterMethods.js +2 -0
  37. package/lib/esm/interfaces/ResponseFilterMethods.js.map +1 -0
  38. package/lib/esm/services/ConverterService.js +51 -0
  39. package/lib/esm/services/ConverterService.js.map +1 -0
  40. package/lib/esm/services/PlatformResponseFilter.js +85 -0
  41. package/lib/esm/services/PlatformResponseFilter.js.map +1 -0
  42. package/lib/esm/utils/getContentType.js +24 -0
  43. package/lib/esm/utils/getContentType.js.map +1 -0
  44. package/lib/esm/utils/renderView.js +13 -0
  45. package/lib/esm/utils/renderView.js.map +1 -0
  46. package/lib/types/decorators/responseFilter.d.ts +7 -0
  47. package/lib/types/domain/ResponseFiltersContainer.d.ts +14 -0
  48. package/lib/types/errors/TemplateRenderError.d.ts +14 -0
  49. package/lib/types/index.d.ts +8 -0
  50. package/lib/types/interfaces/ResponseFilterMethods.d.ts +15 -0
  51. package/lib/types/services/ConverterService.d.ts +30 -0
  52. package/lib/types/services/PlatformResponseFilter.d.ts +27 -0
  53. package/lib/types/utils/getContentType.d.ts +6 -0
  54. package/lib/types/utils/renderView.d.ts +2 -0
  55. package/package.json +22 -18
  56. package/tsconfig.compile.esm.json +23 -0
  57. package/lib/index.js +0 -275
  58. package/lib/index.js.map +0 -1
  59. package/lib/index.modern.js +0 -269
  60. package/lib/index.modern.js.map +0 -1
@@ -0,0 +1,24 @@
1
+ import { isObject } from "@tsed/core";
2
+ export const ANY_CONTENT_TYPE = "*/*";
3
+ /**
4
+ * @ignore
5
+ */
6
+ export function getContentType(data, ctx) {
7
+ const { endpoint, response } = ctx;
8
+ const { operation } = endpoint;
9
+ const contentType = response.getContentType() || operation.getContentTypeOf(response.statusCode) || "";
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;
18
+ }
19
+ }
20
+ if (typeof data === "string" && endpoint.view) {
21
+ return "text/html";
22
+ }
23
+ }
24
+ //# sourceMappingURL=getContentType.js.map
@@ -0,0 +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"]}
@@ -0,0 +1,13 @@
1
+ import { TemplateRenderError } from "../errors/TemplateRenderError";
2
+ export async function renderView(data, $ctx) {
3
+ const { response, endpoint } = $ctx;
4
+ try {
5
+ const { data } = $ctx;
6
+ const { path, options } = endpoint.view;
7
+ return await response.render(path, { ...options, ...data, $ctx });
8
+ }
9
+ catch (err) {
10
+ throw new TemplateRenderError(endpoint.targetName, endpoint.propertyKey, err);
11
+ }
12
+ }
13
+ //# sourceMappingURL=renderView.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"renderView.js","sourceRoot":"","sources":["../../../src/utils/renderView.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,mBAAmB,EAAC,MAAM,+BAA+B,CAAC;AAElE,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,IAAS,EAAE,IAAiB;IAC3D,MAAM,EAAC,QAAQ,EAAE,QAAQ,EAAC,GAAG,IAAI,CAAC;IAClC,IAAI;QACF,MAAM,EAAC,IAAI,EAAC,GAAG,IAAI,CAAC;QACpB,MAAM,EAAC,IAAI,EAAE,OAAO,EAAC,GAAG,QAAQ,CAAC,IAAI,CAAC;QAEtC,OAAO,MAAM,QAAQ,CAAC,MAAM,CAAC,IAAI,EAAE,EAAC,GAAG,OAAO,EAAE,GAAG,IAAI,EAAE,IAAI,EAAC,CAAC,CAAC;KACjE;IAAC,OAAO,GAAG,EAAE;QACZ,MAAM,IAAI,mBAAmB,CAAC,QAAQ,CAAC,UAAU,EAAE,QAAQ,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC;KAC/E;AACH,CAAC","sourcesContent":["import {BaseContext} from \"@tsed/di\";\nimport {TemplateRenderError} from \"../errors/TemplateRenderError\";\n\nexport async function renderView(data: any, $ctx: BaseContext) {\n const {response, endpoint} = $ctx;\n try {\n const {data} = $ctx;\n const {path, options} = endpoint.view;\n\n return await response.render(path, {...options, ...data, $ctx});\n } catch (err) {\n throw new TemplateRenderError(endpoint.targetName, endpoint.propertyKey, err);\n }\n}\n"]}
@@ -0,0 +1,7 @@
1
+ import { ResponseFilterKey } from "../domain/ResponseFiltersContainer";
2
+ /**
3
+ * Register a response filter service.
4
+ * @param contentTypes
5
+ * @decorator
6
+ */
7
+ export declare function ResponseFilter(...contentTypes: ResponseFilterKey[]): ClassDecorator;
@@ -0,0 +1,14 @@
1
+ import { Type } from "@tsed/core";
2
+ import { ResponseFilterMethods } from "../interfaces/ResponseFilterMethods";
3
+ /**
4
+ * @ignore
5
+ */
6
+ export declare type ResponseFilterKey = "*/*" | "application/json" | "text/html" | "plain/text" | string;
7
+ /**
8
+ * @ignore
9
+ */
10
+ export declare const ResponseFiltersContainer: Map<string, Type<ResponseFilterMethods<unknown>>>;
11
+ /**
12
+ * @ignore
13
+ */
14
+ export declare function registerResponseFilter(type: ResponseFilterKey, token: Type<ResponseFilterMethods>): void;
@@ -0,0 +1,14 @@
1
+ import { Type } from "@tsed/core";
2
+ import { InternalServerError } from "@tsed/exceptions";
3
+ /**
4
+ * @private
5
+ */
6
+ export declare class TemplateRenderError extends InternalServerError {
7
+ name: string;
8
+ constructor(target: Type<any> | string, method: string | symbol, err: Error);
9
+ /**
10
+ *
11
+ * @returns {string}
12
+ */
13
+ static buildMessage(target: Type<any> | string, method: string | symbol, err: Error): string;
14
+ }
@@ -0,0 +1,8 @@
1
+ export * from "./decorators/responseFilter";
2
+ export * from "./domain/ResponseFiltersContainer";
3
+ export * from "./errors/TemplateRenderError";
4
+ export * from "./interfaces/ResponseFilterMethods";
5
+ export * from "./services/PlatformResponseFilter";
6
+ export * from "./services/ConverterService";
7
+ export * from "./utils/getContentType";
8
+ export * from "./utils/renderView";
@@ -0,0 +1,15 @@
1
+ import { Type } from "@tsed/core";
2
+ import type { BaseContext } from "@tsed/di";
3
+ declare global {
4
+ namespace TsED {
5
+ interface Configuration {
6
+ /**
7
+ * A list of response filters must be called before returning a response to the consumer. See more on [Response filters](/docs/response-filter.md).
8
+ */
9
+ responseFilters: Type<ResponseFilterMethods>[];
10
+ }
11
+ }
12
+ }
13
+ export interface ResponseFilterMethods<T = unknown> {
14
+ transform(data: T, ctx: BaseContext): any;
15
+ }
@@ -0,0 +1,30 @@
1
+ import { Configuration, InjectorService } from "@tsed/di";
2
+ import { JsonDeserializerOptions, JsonSerializerOptions } from "@tsed/json-mapper";
3
+ /**
4
+ * @deprecated Since 2021-10-03. Use serialize/deserialize functions from @tsed/json-mapper instead
5
+ */
6
+ export declare class ConverterService {
7
+ private injectorService;
8
+ private readonly _additionalProperties;
9
+ constructor(injectorService: InjectorService, configuration: Configuration);
10
+ /**
11
+ * Convert instance to plainObject.
12
+ *
13
+ * @param obj
14
+ * @param options
15
+ */
16
+ serialize(obj: any, options?: JsonSerializerOptions): any;
17
+ /**
18
+ * Convert a plainObject to targetType.
19
+ *
20
+ * ### Options
21
+ *
22
+ * - `ignoreCallback`: callback called for each object which will be deserialized. The callback can return a boolean to avoid the default converter behavior.
23
+ * - `checkRequiredValue`: Disable the required check condition.
24
+ *
25
+ * @param obj Object source that will be deserialized
26
+ * @param options Mapping options
27
+ * @returns {any}
28
+ */
29
+ deserialize(obj: any, options?: JsonDeserializerOptions): any;
30
+ }
@@ -0,0 +1,27 @@
1
+ import { Type } from "@tsed/core";
2
+ import { BaseContext, InjectorService } from "@tsed/di";
3
+ import { ResponseFilterKey } from "../domain/ResponseFiltersContainer";
4
+ import { ResponseFilterMethods } from "../interfaces/ResponseFilterMethods";
5
+ /**
6
+ * @platform
7
+ */
8
+ export declare class PlatformResponseFilter {
9
+ types: Map<ResponseFilterKey, ResponseFilterMethods>;
10
+ injector: InjectorService;
11
+ protected responseFilters: Type<ResponseFilterMethods>[];
12
+ get contentTypes(): ResponseFilterKey[];
13
+ $onInit(): void;
14
+ getBestContentType(data: any, ctx: BaseContext): any;
15
+ /**
16
+ * Call filters to transform data
17
+ * @param data
18
+ * @param ctx
19
+ */
20
+ transform(data: unknown, ctx: BaseContext): Promise<any>;
21
+ /**
22
+ * Serialize data before calling filters
23
+ * @param data
24
+ * @param ctx
25
+ */
26
+ serialize(data: unknown, ctx: BaseContext): Promise<unknown>;
27
+ }
@@ -0,0 +1,6 @@
1
+ import type { BaseContext } from "@tsed/di";
2
+ export declare const ANY_CONTENT_TYPE = "*/*";
3
+ /**
4
+ * @ignore
5
+ */
6
+ export declare function getContentType(data: any, ctx: BaseContext): any;
@@ -0,0 +1,2 @@
1
+ import { BaseContext } from "@tsed/di";
2
+ export declare function renderView(data: any, $ctx: BaseContext): Promise<any>;
package/package.json CHANGED
@@ -1,36 +1,40 @@
1
1
  {
2
2
  "name": "@tsed/platform-response-filter",
3
- "version": "6.88.1",
3
+ "version": "6.89.0",
4
4
  "description": "Platform response filter module for Ts.ED Framework",
5
5
  "private": false,
6
6
  "source": "./src/index.ts",
7
- "main": "./lib/index.js",
8
- "typings": "./lib/index.d.ts",
7
+ "main": "./lib/cjs/index.js",
8
+ "typings": "./lib/types/index.d.ts",
9
9
  "exports": {
10
- "require": "./lib/index.js",
11
- "default": "./lib/index.modern.js"
10
+ "types": "./lib/types/index.d.ts",
11
+ "import": "./lib/esm/index.js",
12
+ "require": "./lib/cjs/index.js",
13
+ "default": "./lib/esm/index.js"
12
14
  },
13
15
  "scripts": {
14
- "build": "microbundle --target node --no-compress --format modern,cjs --tsconfig ./tsconfig.compile.json"
16
+ "build": "yarn run build:esm && yarn run build:cjs",
17
+ "build:cjs": "tsc --build tsconfig.compile.json",
18
+ "build:esm": "tsc --build tsconfig.compile.esm.json"
15
19
  },
16
20
  "dependencies": {
17
21
  "tslib": "2.2.0"
18
22
  },
19
23
  "devDependencies": {
20
- "@tsed/core": "6.88.1",
21
- "@tsed/di": "6.88.1",
22
- "@tsed/exceptions": "6.88.1",
23
- "@tsed/json-mapper": "6.88.1",
24
- "@tsed/platform-views": "6.88.1",
25
- "@tsed/schema": "6.88.1"
24
+ "@tsed/core": "6.89.0",
25
+ "@tsed/di": "6.89.0",
26
+ "@tsed/exceptions": "6.89.0",
27
+ "@tsed/json-mapper": "6.89.0",
28
+ "@tsed/platform-views": "6.89.0",
29
+ "@tsed/schema": "6.89.0"
26
30
  },
27
31
  "peerDependencies": {
28
- "@tsed/core": "^6.88.1",
29
- "@tsed/di": "^6.88.1",
30
- "@tsed/exceptions": "^6.88.1",
31
- "@tsed/json-mapper": "^6.88.1",
32
- "@tsed/platform-views": "^6.88.1",
33
- "@tsed/schema": "^6.88.1"
32
+ "@tsed/core": "^6.89.0",
33
+ "@tsed/di": "^6.89.0",
34
+ "@tsed/exceptions": "^6.89.0",
35
+ "@tsed/json-mapper": "^6.89.0",
36
+ "@tsed/platform-views": "^6.89.0",
37
+ "@tsed/schema": "^6.89.0"
34
38
  },
35
39
  "repository": "https://github.com/tsedio/tsed",
36
40
  "bugs": {
@@ -0,0 +1,23 @@
1
+ {
2
+ "extends": "./tsconfig.compile.json",
3
+ "compilerOptions": {
4
+ "baseUrl": ".",
5
+ "module": "ES2020",
6
+ "rootDir": "src",
7
+ "outDir": "./lib/esm",
8
+ "declaration": true,
9
+ "declarationDir": "./lib/types"
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
+ ]
23
+ }
package/lib/index.js DELETED
@@ -1,275 +0,0 @@
1
- var di = require('@tsed/di');
2
- var core = require('@tsed/core');
3
- var exceptions = require('@tsed/exceptions');
4
- var tslib = require('tslib');
5
- var jsonMapper = require('@tsed/json-mapper');
6
-
7
- /**
8
- * @ignore
9
- */
10
- // tslint:disable-next-line:variable-name
11
- const ResponseFiltersContainer = new Map();
12
- /**
13
- * @ignore
14
- */
15
-
16
- function registerResponseFilter(type, token) {
17
- ResponseFiltersContainer.set(type, token);
18
- }
19
-
20
- /**
21
- * Register a response filter service.
22
- * @param contentTypes
23
- * @decorator
24
- */
25
-
26
- function ResponseFilter(...contentTypes) {
27
- return target => {
28
- contentTypes.forEach(contentType => {
29
- registerResponseFilter(contentType, target);
30
- });
31
- di.registerProvider({
32
- provide: target,
33
- useClass: target
34
- });
35
- };
36
- }
37
-
38
- /**
39
- * @private
40
- */
41
-
42
- class TemplateRenderError extends exceptions.InternalServerError {
43
- constructor(target, method, err) {
44
- super(TemplateRenderError.buildMessage(target, method, err));
45
- this.name = "TEMPLATE_RENDER_ERROR";
46
- }
47
- /**
48
- *
49
- * @returns {string}
50
- */
51
-
52
-
53
- static buildMessage(target, method, err) {
54
- return `Template rendering error: ${core.nameOf(target)}.${String(method)}()\n` + err;
55
- }
56
-
57
- }
58
-
59
- function _extends() {
60
- _extends = Object.assign || function (target) {
61
- for (var i = 1; i < arguments.length; i++) {
62
- var source = arguments[i];
63
-
64
- for (var key in source) {
65
- if (Object.prototype.hasOwnProperty.call(source, key)) {
66
- target[key] = source[key];
67
- }
68
- }
69
- }
70
-
71
- return target;
72
- };
73
-
74
- return _extends.apply(this, arguments);
75
- }
76
-
77
- const ANY_CONTENT_TYPE = "*/*";
78
- /**
79
- * @ignore
80
- */
81
-
82
- function getContentType(data, ctx) {
83
- const {
84
- endpoint,
85
- response
86
- } = ctx;
87
- const {
88
- operation
89
- } = endpoint;
90
- const contentType = response.getContentType() || operation.getContentTypeOf(response.statusCode) || "";
91
-
92
- if (contentType && contentType !== ANY_CONTENT_TYPE) {
93
- if (contentType === "application/json") {
94
- if (core.isObject(data)) {
95
- return contentType;
96
- }
97
- } else {
98
- return contentType;
99
- }
100
- }
101
-
102
- if (typeof data === "string" && endpoint.view) {
103
- return "text/html";
104
- }
105
- }
106
-
107
- /**
108
- * @deprecated Since 2021-10-03. Use serialize/deserialize functions from @tsed/json-mapper instead
109
- */
110
-
111
- exports.ConverterService = class ConverterService {
112
- constructor(injectorService, configuration) {
113
- this.injectorService = injectorService;
114
- this._additionalProperties = configuration.get("converter.additionalProperties");
115
- }
116
- /**
117
- * Convert instance to plainObject.
118
- *
119
- * @param obj
120
- * @param options
121
- */
122
-
123
-
124
- serialize(obj, options = {}) {
125
- return jsonMapper.serialize(obj, _extends({
126
- useAlias: true,
127
- additionalProperties: this._additionalProperties === "accept"
128
- }, options));
129
- }
130
- /**
131
- * Convert a plainObject to targetType.
132
- *
133
- * ### Options
134
- *
135
- * - `ignoreCallback`: callback called for each object which will be deserialized. The callback can return a boolean to avoid the default converter behavior.
136
- * - `checkRequiredValue`: Disable the required check condition.
137
- *
138
- * @param obj Object source that will be deserialized
139
- * @param options Mapping options
140
- * @returns {any}
141
- */
142
-
143
-
144
- deserialize(obj, options = {}) {
145
- return jsonMapper.deserialize(obj, _extends({
146
- useAlias: true,
147
- additionalProperties: this._additionalProperties === "accept"
148
- }, options));
149
- }
150
-
151
- };
152
- exports.ConverterService = tslib.__decorate([di.Injectable(), tslib.__param(1, di.Configuration()), tslib.__metadata("design:paramtypes", [di.InjectorService, Object])], exports.ConverterService);
153
-
154
- async function renderView(data, $ctx) {
155
- const {
156
- response,
157
- endpoint
158
- } = $ctx;
159
-
160
- try {
161
- const {
162
- data
163
- } = $ctx;
164
- const {
165
- path,
166
- options
167
- } = endpoint.view;
168
- return await response.render(path, _extends({}, options, data, {
169
- $ctx
170
- }));
171
- } catch (err) {
172
- throw new TemplateRenderError(endpoint.targetName, endpoint.propertyKey, err);
173
- }
174
- }
175
-
176
- /**
177
- * @platform
178
- */
179
-
180
- exports.PlatformResponseFilter = class PlatformResponseFilter {
181
- constructor() {
182
- this.types = new Map();
183
- }
184
-
185
- get contentTypes() {
186
- return [...this.types.keys()];
187
- }
188
-
189
- $onInit() {
190
- ResponseFiltersContainer.forEach((token, type) => {
191
- if (this.responseFilters.includes(token)) {
192
- this.types.set(type, this.injector.get(token));
193
- }
194
- });
195
- }
196
-
197
- getBestContentType(data, ctx) {
198
- const contentType = getContentType(data, ctx);
199
-
200
- if (ctx.request.get("Accept")) {
201
- const bestContentType = ctx.request.accepts([contentType].concat(this.contentTypes).filter(Boolean));
202
-
203
- if (bestContentType) {
204
- return [].concat(bestContentType).filter(type => type !== "*/*")[0];
205
- }
206
- }
207
-
208
- return contentType;
209
- }
210
- /**
211
- * Call filters to transform data
212
- * @param data
213
- * @param ctx
214
- */
215
-
216
-
217
- async transform(data, ctx) {
218
- const {
219
- response
220
- } = ctx;
221
- const bestContentType = this.getBestContentType(data, ctx);
222
- bestContentType && response.contentType(bestContentType);
223
-
224
- if (this.types.has(bestContentType)) {
225
- return this.types.get(bestContentType).transform(data, ctx);
226
- }
227
-
228
- if (this.types.has(ANY_CONTENT_TYPE)) {
229
- return this.types.get(ANY_CONTENT_TYPE).transform(data, ctx);
230
- }
231
-
232
- return data;
233
- }
234
- /**
235
- * Serialize data before calling filters
236
- * @param data
237
- * @param ctx
238
- */
239
-
240
-
241
- async serialize(data, ctx) {
242
- const {
243
- response,
244
- endpoint
245
- } = ctx;
246
-
247
- if (endpoint) {
248
- if (endpoint.view) {
249
- data = await renderView(data, ctx);
250
- } else if (core.isSerializable(data)) {
251
- data = this.injector.get(exports.ConverterService).serialize(data, _extends({}, endpoint.getResponseOptions(response.statusCode), {
252
- endpoint: true
253
- }));
254
- }
255
- }
256
-
257
- return data;
258
- }
259
-
260
- };
261
-
262
- tslib.__decorate([di.Inject(), tslib.__metadata("design:type", di.InjectorService)], exports.PlatformResponseFilter.prototype, "injector", void 0);
263
-
264
- tslib.__decorate([di.Constant("responseFilters", []), tslib.__metadata("design:type", Array)], exports.PlatformResponseFilter.prototype, "responseFilters", void 0);
265
-
266
- exports.PlatformResponseFilter = tslib.__decorate([di.Injectable()], exports.PlatformResponseFilter);
267
-
268
- exports.ANY_CONTENT_TYPE = ANY_CONTENT_TYPE;
269
- exports.ResponseFilter = ResponseFilter;
270
- exports.ResponseFiltersContainer = ResponseFiltersContainer;
271
- exports.TemplateRenderError = TemplateRenderError;
272
- exports.getContentType = getContentType;
273
- exports.registerResponseFilter = registerResponseFilter;
274
- exports.renderView = renderView;
275
- //# sourceMappingURL=index.js.map
package/lib/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sources":["../src/domain/ResponseFiltersContainer.ts","../src/decorators/responseFilter.ts","../src/errors/TemplateRenderError.ts","../src/utils/getContentType.ts","../src/services/ConverterService.ts","../src/utils/renderView.ts","../src/services/PlatformResponseFilter.ts"],"sourcesContent":["import {Type} from \"@tsed/core\";\nimport {ResponseFilterMethods} from \"../interfaces/ResponseFilterMethods\";\n/**\n * @ignore\n */\nexport type ResponseFilterKey = \"*/*\" | \"application/json\" | \"text/html\" | \"plain/text\" | string;\n/**\n * @ignore\n */\n// tslint:disable-next-line:variable-name\nexport const ResponseFiltersContainer = new Map<ResponseFilterKey, Type<ResponseFilterMethods>>();\n/**\n * @ignore\n */\nexport function registerResponseFilter(type: ResponseFilterKey, token: Type<ResponseFilterMethods>) {\n ResponseFiltersContainer.set(type, token);\n}\n","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","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","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","import {Configuration, Injectable, InjectorService} from \"@tsed/di\";\nimport {deserialize, JsonDeserializerOptions, JsonSerializerOptions, serialize} from \"@tsed/json-mapper\";\n\n/**\n * @deprecated Since 2021-10-03. Use serialize/deserialize functions from @tsed/json-mapper instead\n */\n@Injectable()\nexport class ConverterService {\n private readonly _additionalProperties: string;\n\n constructor(private injectorService: InjectorService, @Configuration() configuration: Configuration) {\n this._additionalProperties = configuration.get<string>(\"converter.additionalProperties\");\n }\n\n /**\n * Convert instance to plainObject.\n *\n * @param obj\n * @param options\n */\n serialize(obj: any, options: JsonSerializerOptions = {}): any {\n return serialize(obj, {\n useAlias: true,\n additionalProperties: this._additionalProperties === \"accept\",\n ...options\n });\n }\n\n /**\n * Convert a plainObject to targetType.\n *\n * ### Options\n *\n * - `ignoreCallback`: callback called for each object which will be deserialized. The callback can return a boolean to avoid the default converter behavior.\n * - `checkRequiredValue`: Disable the required check condition.\n *\n * @param obj Object source that will be deserialized\n * @param options Mapping options\n * @returns {any}\n */\n deserialize(obj: any, options: JsonDeserializerOptions = {}): any {\n return deserialize(obj, {\n useAlias: true,\n additionalProperties: this._additionalProperties === \"accept\",\n ...options\n });\n }\n}\n","import {BaseContext} from \"@tsed/di\";\nimport {TemplateRenderError} from \"../errors/TemplateRenderError\";\n\nexport async function renderView(data: any, $ctx: BaseContext) {\n const {response, endpoint} = $ctx;\n try {\n const {data} = $ctx;\n const {path, options} = endpoint.view;\n\n return await response.render(path, {...options, ...data, $ctx});\n } catch (err) {\n throw new TemplateRenderError(endpoint.targetName, endpoint.propertyKey, err);\n }\n}\n","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 {ConverterService} from \"./ConverterService\";\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 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 * 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 = this.injector.get<ConverterService>(ConverterService)!.serialize(data, {\n ...endpoint.getResponseOptions(response.statusCode),\n endpoint: true\n });\n }\n }\n\n return data;\n }\n}\n"],"names":["ResponseFiltersContainer","Map","registerResponseFilter","type","token","set","ResponseFilter","contentTypes","target","forEach","contentType","registerProvider","provide","useClass","TemplateRenderError","InternalServerError","constructor","method","err","buildMessage","nameOf","String","ANY_CONTENT_TYPE","getContentType","data","ctx","endpoint","response","operation","getContentTypeOf","statusCode","isObject","view","ConverterService","injectorService","configuration","_additionalProperties","get","serialize","obj","options","useAlias","additionalProperties","deserialize","Injectable","__param","Configuration","InjectorService","renderView","$ctx","path","render","targetName","propertyKey","PlatformResponseFilter","types","keys","$onInit","responseFilters","includes","injector","getBestContentType","request","bestContentType","accepts","concat","filter","Boolean","transform","has","isSerializable","getResponseOptions","__decorate","Inject","Constant"],"mappings":";;;;;;AAMA;;;AAGA;MACaA,wBAAwB,GAAG,IAAIC,GAAJ;AACxC;;;;SAGgBC,uBAAuBC,MAAyBC;AAC9DJ,EAAAA,wBAAwB,CAACK,GAAzB,CAA6BF,IAA7B,EAAmCC,KAAnC;AACD;;ACZD;;;;;;SAKgBE,eAAe,GAAGC;AAChC,SAAiBC,MAAV;AACLD,IAAAA,YAAY,CAACE,OAAb,CAAsBC,WAAD;AACnBR,MAAAA,sBAAsB,CAACQ,WAAD,EAAcF,MAAd,CAAtB;AACD,KAFD;AAGAG,IAAAA,mBAAgB,CAAC;AACfC,MAAAA,OAAO,EAAEJ,MADM;AAEfK,MAAAA,QAAQ,EAAGL;AAFI,KAAD,CAAhB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