@tsed/platform-response-filter 8.8.0 → 8.8.1

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.
@@ -0,0 +1,7 @@
1
+ export var ContentTypes;
2
+ (function (ContentTypes) {
3
+ ContentTypes["ANY"] = "*/*";
4
+ ContentTypes["JSON"] = "application/json";
5
+ ContentTypes["HTML"] = "text/html";
6
+ })(ContentTypes || (ContentTypes = {}));
7
+ //# sourceMappingURL=ContentTypes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ContentTypes.js","sourceRoot":"","sources":["../../../src/constants/ContentTypes.ts"],"names":[],"mappings":"AAAA,MAAM,CAAN,IAAY,YAIX;AAJD,WAAY,YAAY;IACtB,2BAAW,CAAA;IACX,yCAAyB,CAAA;IACzB,kCAAkB,CAAA;AACpB,CAAC,EAJW,YAAY,KAAZ,YAAY,QAIvB"}
package/lib/esm/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @file Automatically generated by @tsed/barrels.
3
3
  */
4
- export * from "./constants/ANY_CONTENT_TYPE.js";
4
+ export * from "./constants/ContentTypes.js";
5
5
  export * from "./decorators/responseFilter.js";
6
6
  export * from "./domain/ResponseFiltersContainer.js";
7
7
  export * from "./errors/TemplateRenderError.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,cAAc,iCAAiC,CAAC;AAChD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,sCAAsC,CAAC;AACrD,cAAc,iCAAiC,CAAC;AAChD,cAAc,uCAAuC,CAAC;AACtD,cAAc,2CAA2C,CAAC;AAC1D,cAAc,6CAA6C,CAAC;AAC5D,cAAc,sCAAsC,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,cAAc,6BAA6B,CAAC;AAC5C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,sCAAsC,CAAC;AACrD,cAAc,iCAAiC,CAAC;AAChD,cAAc,uCAAuC,CAAC;AACtD,cAAc,2CAA2C,CAAC;AAC1D,cAAc,6CAA6C,CAAC;AAC5D,cAAc,sCAAsC,CAAC"}
@@ -1,7 +1,10 @@
1
- import { isObject, isStream } from "@tsed/core";
1
+ import { isBoolean, isNumber, isObject, isStream } from "@tsed/core";
2
2
  import { inject, injectable } from "@tsed/di";
3
- import { ANY_CONTENT_TYPE } from "../constants/ANY_CONTENT_TYPE.js";
3
+ import { ContentTypes } from "../constants/ContentTypes.js";
4
4
  import { PLATFORM_CONTENT_TYPES_CONTAINER } from "./PlatformContentTypesContainer.js";
5
+ const isJsonData = (data) => {
6
+ return (isObject(data) || isNumber(data) || isBoolean(data)) && !isStream(data) && !Buffer.isBuffer(data);
7
+ };
5
8
  /**
6
9
  * Determine the content type of the response based on the data and the context.
7
10
  * @param {unknown} data
@@ -9,15 +12,12 @@ import { PLATFORM_CONTENT_TYPES_CONTAINER } from "./PlatformContentTypesContaine
9
12
  * @ignore
10
13
  */
11
14
  export function getContentType(data, ctx) {
12
- const { endpoint, endpoint: { operation }, response } = ctx;
13
- const contentType = response.getContentType() || operation.getContentTypeOf(response.statusCode);
14
- if (contentType && contentType !== ANY_CONTENT_TYPE) {
15
- return contentType === "application/json" && isObject(data) ? "application/json" : contentType;
15
+ const { endpoint, response } = ctx;
16
+ const contentType = response.getContentType() || endpoint.operation.getContentTypeOf(response.statusCode);
17
+ if (contentType && contentType !== ContentTypes.ANY) {
18
+ return contentType;
16
19
  }
17
- if (endpoint.view) {
18
- return "text/html";
19
- }
20
- return isObject(data) && !isStream(data) && !Buffer.isBuffer(data) ? "application/json" : contentType;
20
+ return endpoint.view ? ContentTypes.HTML : isJsonData(data) ? ContentTypes.JSON : contentType;
21
21
  }
22
22
  /**
23
23
  * @ignore
@@ -28,7 +28,7 @@ function resolver(data, ctx) {
28
28
  const { contentTypes } = inject(PLATFORM_CONTENT_TYPES_CONTAINER);
29
29
  const bestContentType = ctx.request.accepts([contentType].concat(contentTypes).filter(Boolean));
30
30
  if (bestContentType) {
31
- return [].concat(bestContentType).filter((type) => type !== "*/*")[0];
31
+ return [].concat(bestContentType).filter((type) => type !== ContentTypes.ANY)[0];
32
32
  }
33
33
  }
34
34
  return contentType;
@@ -1 +1 @@
1
- {"version":3,"file":"PlatformContentTypeResolver.js","sourceRoot":"","sources":["../../../src/services/PlatformContentTypeResolver.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAE,QAAQ,EAAC,MAAM,YAAY,CAAC;AAC9C,OAAO,EAAmB,MAAM,EAAE,UAAU,EAAC,MAAM,UAAU,CAAC;AAE9D,OAAO,EAAC,gBAAgB,EAAC,MAAM,kCAAkC,CAAC;AAClE,OAAO,EAAC,gCAAgC,EAAC,MAAM,oCAAoC,CAAC;AAEpF;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAC,IAAa,EAAE,GAAgB;IAC5D,MAAM,EACJ,QAAQ,EACR,QAAQ,EAAE,EAAC,SAAS,EAAC,EACrB,QAAQ,EACT,GAAG,GAAG,CAAC;IAER,MAAM,WAAW,GAAG,QAAQ,CAAC,cAAc,EAAE,IAAI,SAAS,CAAC,gBAAgB,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;IAEjG,IAAI,WAAW,IAAI,WAAW,KAAK,gBAAgB,EAAE,CAAC;QACpD,OAAO,WAAW,KAAK,kBAAkB,IAAI,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,kBAAkB,CAAC,CAAC,CAAC,WAAW,CAAC;IACjG,CAAC;IAED,IAAI,QAAQ,CAAC,IAAI,EAAE,CAAC;QAClB,OAAO,WAAW,CAAC;IACrB,CAAC;IAED,OAAO,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,C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QAEhG,IAAI,eAAe,EAAE,CAAC;YACpB,OAAO,EAAE,CAAC,MAAM,CAAC,eAAsB,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,KAAK,YAAY,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;QAC1F,CAAC;IACH,CAAC;IAED,OAAO,WAAW,CAAC;AACrB,CAAC;AAMD,MAAM,CAAC,MAAM,8BAA8B,GAAG,UAAU,CAAC,MAAM,CAAC,GAAG,CAAC,gCAAgC,CAAC,CAAC;KACnG,OAAO,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC;KACvB,KAAK,EAAE,CAAC"}
@@ -1,5 +1,5 @@
1
1
  import { constant, inject, injectable } from "@tsed/di";
2
- import { ANY_CONTENT_TYPE } from "../constants/ANY_CONTENT_TYPE.js";
2
+ import { ContentTypes } from "../constants/ContentTypes.js";
3
3
  import { ResponseFiltersContainer } from "../domain/ResponseFiltersContainer.js";
4
4
  function factory() {
5
5
  const responseFilters = constant("responseFilters", []);
@@ -12,7 +12,7 @@ function factory() {
12
12
  return {
13
13
  contentTypes: [...containers.keys()],
14
14
  resolve(bestContentType) {
15
- const token = containers.get(bestContentType) || containers.get(ANY_CONTENT_TYPE);
15
+ const token = containers.get(bestContentType) || containers.get(ContentTypes.ANY);
16
16
  if (token) {
17
17
  return inject(token);
18
18
  }
@@ -1 +1 @@
1
- {"version":3,"file":"PlatformContentTypesContainer.js","sourceRoot":"","sources":["../../../src/services/PlatformContentTypesContainer.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAqB,MAAM,UAAU,CAAC;AAE1E,OAAO,EAAC,gBAAgB,EAAC,MAAM,kCAAkC,CAAC;AAClE,OAAO,EAAoB,wBAAwB,EAAC,MAAM,uCAAuC,CAAC;AAGlG,SAAS,OAAO;IACd,MAAM,eAAe,GAAG,QAAQ,CAAgC,iBAAiB,EAAE,EAAE,CAAC,CAAC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E,EAAE;QAC/C,IAAI,eAAe,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;YACpC,UAAU,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QAC9B,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO;QACL,YAAY,EAAE,CAAC,GAAG,UAAU,CAAC,IAAI,EAAE,CAAC;QACpC,OAAO,CAAC,eAAuB;YAC7B,MAAM,KAAK,GAAG,UAAU,CAAC,GAAG,CAAC,eAAe,CAAC,IAAI,UAAU,CAAC,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC;YAElF,IAAI,KAAK,EAAE,CAAC;gBACV,OAAO,MAAM,CAAwB,KAAK,CAAC,CAAC;YAC9C,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC;AAGD,MAAM,CAAC,MAAM,gCAAgC,GAAG,UAAU,CAAC,MAAM,CAAC,GAAG,CAAC,kCAAkC,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,CAAC"}
@@ -0,0 +1,5 @@
1
+ export declare enum ContentTypes {
2
+ ANY = "*/*",
3
+ JSON = "application/json",
4
+ HTML = "text/html"
5
+ }
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @file Automatically generated by @tsed/barrels.
3
3
  */
4
- export * from "./constants/ANY_CONTENT_TYPE.js";
4
+ export * from "./constants/ContentTypes.js";
5
5
  export * from "./decorators/responseFilter.js";
6
6
  export * from "./domain/ResponseFiltersContainer.js";
7
7
  export * from "./errors/TemplateRenderError.js";
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@tsed/platform-response-filter",
3
3
  "description": "Platform response filter module for Ts.ED Framework",
4
4
  "type": "module",
5
- "version": "8.8.0",
5
+ "version": "8.8.1",
6
6
  "source": "./src/index.ts",
7
7
  "main": "./lib/esm/index.js",
8
8
  "module": "./lib/esm/index.js",
@@ -26,23 +26,23 @@
26
26
  "tslib": "2.7.0"
27
27
  },
28
28
  "devDependencies": {
29
- "@tsed/barrels": "8.8.0",
30
- "@tsed/core": "8.8.0",
31
- "@tsed/di": "8.8.0",
32
- "@tsed/exceptions": "8.8.0",
33
- "@tsed/json-mapper": "8.8.0",
34
- "@tsed/schema": "8.8.0",
35
- "@tsed/typescript": "8.8.0",
29
+ "@tsed/barrels": "8.8.1",
30
+ "@tsed/core": "8.8.1",
31
+ "@tsed/di": "8.8.1",
32
+ "@tsed/exceptions": "8.8.1",
33
+ "@tsed/json-mapper": "8.8.1",
34
+ "@tsed/schema": "8.8.1",
35
+ "@tsed/typescript": "8.8.1",
36
36
  "eslint": "9.12.0",
37
37
  "typescript": "5.4.5",
38
- "vitest": "3.0.9"
38
+ "vitest": "3.1.1"
39
39
  },
40
40
  "peerDependencies": {
41
- "@tsed/core": ">=8.8.0",
42
- "@tsed/di": ">=8.8.0",
43
- "@tsed/exceptions": ">=8.8.0",
44
- "@tsed/json-mapper": ">=8.8.0",
45
- "@tsed/schema": ">=8.8.0"
41
+ "@tsed/core": ">=8.8.1",
42
+ "@tsed/di": ">=8.8.1",
43
+ "@tsed/exceptions": ">=8.8.1",
44
+ "@tsed/json-mapper": ">=8.8.1",
45
+ "@tsed/schema": ">=8.8.1"
46
46
  },
47
47
  "peerDependenciesMeta": {
48
48
  "@tsed/core": {
@@ -0,0 +1,5 @@
1
+ export enum ContentTypes {
2
+ ANY = "*/*",
3
+ JSON = "application/json",
4
+ HTML = "text/html"
5
+ }
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @file Automatically generated by @tsed/barrels.
3
3
  */
4
- export * from "./constants/ANY_CONTENT_TYPE.js";
4
+ export * from "./constants/ContentTypes.js";
5
5
  export * from "./decorators/responseFilter.js";
6
6
  export * from "./domain/ResponseFiltersContainer.js";
7
7
  export * from "./errors/TemplateRenderError.js";
@@ -2,10 +2,11 @@ import {PlatformTest} from "@tsed/platform-http/testing";
2
2
  import {EndpointMetadata, Get, Returns, View} from "@tsed/schema";
3
3
  import {createReadStream} from "fs";
4
4
 
5
+ import {ContentTypes} from "../constants/ContentTypes.js";
5
6
  import {PLATFORM_CONTENT_TYPE_RESOLVER} from "./PlatformContentTypeResolver.js";
6
7
  import {PLATFORM_CONTENT_TYPES_CONTAINER} from "./PlatformContentTypesContainer.js";
7
8
 
8
- async function getTestFixture(contentTypes = ["application/json", "text/html"]) {
9
+ async function getTestFixture(contentTypes = [ContentTypes.JSON, ContentTypes.HTML]) {
9
10
  const contentTypeResolver = await PlatformTest.invoke<PLATFORM_CONTENT_TYPE_RESOLVER>(PLATFORM_CONTENT_TYPE_RESOLVER, [
10
11
  {
11
12
  token: PLATFORM_CONTENT_TYPES_CONTAINER,
@@ -78,7 +79,7 @@ describe("PlatformContentTypeResolver", () => {
78
79
  it("should return the content type (object - application/json)", async () => {
79
80
  class TestController {
80
81
  @Get("/")
81
- @(Returns(200).ContentType("application/json"))
82
+ @(Returns(200).ContentType(ContentTypes.JSON))
82
83
  get() {}
83
84
  }
84
85
 
@@ -87,11 +88,29 @@ describe("PlatformContentTypeResolver", () => {
87
88
  ctx.endpoint = EndpointMetadata.get(TestController, "get");
88
89
  ctx.response.getRes().statusCode = 200;
89
90
 
90
- vi.spyOn(ctx.response, "getContentType").mockReturnValue("application/json");
91
+ vi.spyOn(ctx.response, "getContentType").mockReturnValue(ContentTypes.JSON);
91
92
 
92
93
  const result = contentTypeResolver(data, ctx);
93
94
 
94
- expect(result).toEqual("application/json");
95
+ expect(result).toEqual(ContentTypes.JSON);
96
+ });
97
+ it("should return the content type (object - custom content-type)", async () => {
98
+ class TestController {
99
+ @Get("/")
100
+ @(Returns(200).ContentType("application/vnd.custom+json"))
101
+ get() {}
102
+ }
103
+
104
+ const {contentTypeResolver, ctx, data} = await getTestFixture();
105
+
106
+ ctx.endpoint = EndpointMetadata.get(TestController, "get");
107
+ ctx.response.getRes().statusCode = 200;
108
+
109
+ vi.spyOn(ctx.response, "getContentType").mockReturnValue("application/vnd.custom+json");
110
+
111
+ const result = contentTypeResolver(data, ctx);
112
+
113
+ expect(result).toEqual("application/vnd.custom+json");
95
114
  });
96
115
  it("should return the content type (string - application/json)", async () => {
97
116
  class TestController {
@@ -110,10 +129,27 @@ describe("PlatformContentTypeResolver", () => {
110
129
 
111
130
  expect(result).toEqual("application/json");
112
131
  });
132
+ it("should return the content type (boolean - application/json)", async () => {
133
+ class TestController {
134
+ @Get("/")
135
+ @Returns(200)
136
+ get() {}
137
+ }
138
+
139
+ const {contentTypeResolver, ctx, data} = await getTestFixture();
140
+
141
+ ctx.endpoint = EndpointMetadata.get(TestController, "get");
142
+ ctx.response.getRes().statusCode = 200;
143
+ vi.spyOn(ctx.response, "getContentType").mockReturnValue("application/json");
144
+
145
+ const result = contentTypeResolver(true, ctx);
146
+
147
+ expect(result).toEqual("application/json");
148
+ });
113
149
  it("should return the content type (string - text/html)", async () => {
114
150
  class TestController {
115
151
  @Get("/")
116
- @(Returns(200).ContentType("text/html"))
152
+ @(Returns(200).ContentType(ContentTypes.HTML))
117
153
  get() {}
118
154
  }
119
155
 
@@ -121,11 +157,11 @@ describe("PlatformContentTypeResolver", () => {
121
157
 
122
158
  ctx.endpoint = EndpointMetadata.get(TestController, "get");
123
159
  ctx.response.getRes().statusCode = 200;
124
- vi.spyOn(ctx.response, "getContentType").mockReturnValue("text/html");
160
+ vi.spyOn(ctx.response, "getContentType").mockReturnValue(ContentTypes.HTML);
125
161
 
126
162
  const result = contentTypeResolver(data, ctx);
127
163
 
128
- expect(result).toEqual("text/html");
164
+ expect(result).toEqual(ContentTypes.HTML);
129
165
  });
130
166
  it("should return the content type (string - view)", async () => {
131
167
  class TestController {
@@ -143,6 +179,6 @@ describe("PlatformContentTypeResolver", () => {
143
179
 
144
180
  const result = contentTypeResolver(data, ctx);
145
181
 
146
- expect(result).toEqual("text/html");
182
+ expect(result).toEqual(ContentTypes.HTML);
147
183
  });
148
184
  });
@@ -1,9 +1,13 @@
1
- import {isObject, isStream} from "@tsed/core";
1
+ import {isBoolean, isNumber, isObject, isStream} from "@tsed/core";
2
2
  import {type BaseContext, inject, injectable} from "@tsed/di";
3
3
 
4
- import {ANY_CONTENT_TYPE} from "../constants/ANY_CONTENT_TYPE.js";
4
+ import {ContentTypes} from "../constants/ContentTypes.js";
5
5
  import {PLATFORM_CONTENT_TYPES_CONTAINER} from "./PlatformContentTypesContainer.js";
6
6
 
7
+ const isJsonData = (data: unknown) => {
8
+ return (isObject(data) || isNumber(data) || isBoolean(data)) && !isStream(data) && !Buffer.isBuffer(data);
9
+ };
10
+
7
11
  /**
8
12
  * Determine the content type of the response based on the data and the context.
9
13
  * @param {unknown} data
@@ -11,23 +15,14 @@ import {PLATFORM_CONTENT_TYPES_CONTAINER} from "./PlatformContentTypesContainer.
11
15
  * @ignore
12
16
  */
13
17
  export function getContentType(data: unknown, ctx: BaseContext) {
14
- const {
15
- endpoint,
16
- endpoint: {operation},
17
- response
18
- } = ctx;
19
-
20
- const contentType = response.getContentType() || operation.getContentTypeOf(response.statusCode);
21
-
22
- if (contentType && contentType !== ANY_CONTENT_TYPE) {
23
- return contentType === "application/json" && isObject(data) ? "application/json" : contentType;
24
- }
18
+ const {endpoint, response} = ctx;
19
+ const contentType = response.getContentType() || endpoint.operation.getContentTypeOf(response.statusCode);
25
20
 
26
- if (endpoint.view) {
27
- return "text/html";
21
+ if (contentType && contentType !== ContentTypes.ANY) {
22
+ return contentType;
28
23
  }
29
24
 
30
- return isObject(data) && !isStream(data) && !Buffer.isBuffer(data) ? "application/json" : contentType;
25
+ return endpoint.view ? ContentTypes.HTML : isJsonData(data) ? ContentTypes.JSON : contentType;
31
26
  }
32
27
 
33
28
  /**
@@ -42,7 +37,7 @@ function resolver(data: any, ctx: BaseContext) {
42
37
  const bestContentType = ctx.request.accepts([contentType].concat(contentTypes).filter(Boolean));
43
38
 
44
39
  if (bestContentType) {
45
- return [].concat(bestContentType as any).filter((type) => type !== "*/*")[0];
40
+ return [].concat(bestContentType as any).filter((type) => type !== ContentTypes.ANY)[0];
46
41
  }
47
42
  }
48
43
 
@@ -1,7 +1,7 @@
1
1
  import type {Type} from "@tsed/core";
2
2
  import {constant, inject, injectable, type TokenProvider} from "@tsed/di";
3
3
 
4
- import {ANY_CONTENT_TYPE} from "../constants/ANY_CONTENT_TYPE.js";
4
+ import {ContentTypes} from "../constants/ContentTypes.js";
5
5
  import {ResponseFilterKey, ResponseFiltersContainer} from "../domain/ResponseFiltersContainer.js";
6
6
  import type {ResponseFilterMethods} from "../interfaces/ResponseFilterMethods.js";
7
7
 
@@ -18,7 +18,7 @@ function factory() {
18
18
  return {
19
19
  contentTypes: [...containers.keys()],
20
20
  resolve(bestContentType: string) {
21
- const token = containers.get(bestContentType) || containers.get(ANY_CONTENT_TYPE);
21
+ const token = containers.get(bestContentType) || containers.get(ContentTypes.ANY);
22
22
 
23
23
  if (token) {
24
24
  return inject<ResponseFilterMethods>(token);
@@ -1,2 +0,0 @@
1
- export const ANY_CONTENT_TYPE = "*/*";
2
- //# sourceMappingURL=ANY_CONTENT_TYPE.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ANY_CONTENT_TYPE.js","sourceRoot":"","sources":["../../../src/constants/ANY_CONTENT_TYPE.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,gBAAgB,GAAG,KAAK,CAAC"}
@@ -1 +0,0 @@
1
- export declare const ANY_CONTENT_TYPE = "*/*";
@@ -1 +0,0 @@
1
- export const ANY_CONTENT_TYPE = "*/*";