@tsed/platform-response-filter 8.5.2 → 8.6.0-beta.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.
- package/lib/esm/services/PlatformContentTypeResolver.js +8 -8
- package/lib/esm/services/PlatformContentTypeResolver.js.map +1 -1
- package/lib/types/services/PlatformContentTypeResolver.d.ts +4 -1
- package/package.json +17 -14
- package/src/services/PlatformContentTypeResolver.spec.ts +33 -2
- package/src/services/PlatformContentTypeResolver.ts +14 -10
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import { isObject } from "@tsed/core";
|
|
1
|
+
import { isObject, isStream } from "@tsed/core";
|
|
2
2
|
import { inject, injectable } from "@tsed/di";
|
|
3
3
|
import { ANY_CONTENT_TYPE } from "../constants/ANY_CONTENT_TYPE.js";
|
|
4
4
|
import { PLATFORM_CONTENT_TYPES_CONTAINER } from "./PlatformContentTypesContainer.js";
|
|
5
5
|
/**
|
|
6
|
+
* Determine the content type of the response based on the data and the context.
|
|
7
|
+
* @param {unknown} data
|
|
8
|
+
* @param {BaseContext} ctx
|
|
6
9
|
* @ignore
|
|
7
10
|
*/
|
|
8
11
|
export function getContentType(data, ctx) {
|
|
9
|
-
const { endpoint, response } = ctx;
|
|
10
|
-
const
|
|
11
|
-
const contentType = response.getContentType() || operation.getContentTypeOf(response.statusCode) || "";
|
|
12
|
+
const { endpoint, endpoint: { operation }, response } = ctx;
|
|
13
|
+
const contentType = response.getContentType() || operation.getContentTypeOf(response.statusCode);
|
|
12
14
|
if (contentType && contentType !== ANY_CONTENT_TYPE) {
|
|
13
|
-
|
|
14
|
-
return "application/json";
|
|
15
|
-
}
|
|
16
|
-
return contentType;
|
|
15
|
+
return contentType === "application/json" && isObject(data) ? "application/json" : contentType;
|
|
17
16
|
}
|
|
18
17
|
if (endpoint.view) {
|
|
19
18
|
return "text/html";
|
|
20
19
|
}
|
|
20
|
+
return isObject(data) && !isStream(data) && !Buffer.isBuffer(data) ? "application/json" : contentType;
|
|
21
21
|
}
|
|
22
22
|
/**
|
|
23
23
|
* @ignore
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformContentTypeResolver.js","sourceRoot":"","sources":["../../../src/services/PlatformContentTypeResolver.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,YAAY,CAAC;
|
|
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,CAAC,kBAAkB,CAAC,CAAC,CAAC,WAAW,CAAC;AACxG,CAAC;AAED;;GAEG;AACH,SAAS,QAAQ,CAAC,IAAS,EAAE,GAAgB;IAC3C,MAAM,WAAW,GAAG,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IAE9C,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC9B,MAAM,EAAC,YAAY,EAAC,GAAG,MAAM,CAAmC,gCAAgC,CAAC,CAAC;QAElG,MAAM,eAAe,GAAG,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,WAAW,CAAC,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC;QAEhG,IAAI,eAAe,EAAE,CAAC;YACpB,OAAO,EAAE,CAAC,MAAM,CAAC,eAAsB,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;QAC/E,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,8 +1,11 @@
|
|
|
1
1
|
import { type BaseContext } from "@tsed/di";
|
|
2
2
|
/**
|
|
3
|
+
* Determine the content type of the response based on the data and the context.
|
|
4
|
+
* @param {unknown} data
|
|
5
|
+
* @param {BaseContext} ctx
|
|
3
6
|
* @ignore
|
|
4
7
|
*/
|
|
5
|
-
export declare function getContentType(data:
|
|
8
|
+
export declare function getContentType(data: unknown, ctx: BaseContext): any;
|
|
6
9
|
/**
|
|
7
10
|
* @ignore
|
|
8
11
|
*/
|
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.
|
|
5
|
+
"version": "8.6.0-beta.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.
|
|
30
|
-
"@tsed/core": "8.
|
|
31
|
-
"@tsed/di": "8.
|
|
32
|
-
"@tsed/exceptions": "8.
|
|
33
|
-
"@tsed/json-mapper": "8.
|
|
34
|
-
"@tsed/schema": "8.
|
|
35
|
-
"@tsed/typescript": "8.
|
|
29
|
+
"@tsed/barrels": "8.6.0-beta.1",
|
|
30
|
+
"@tsed/core": "8.6.0-beta.1",
|
|
31
|
+
"@tsed/di": "8.6.0-beta.1",
|
|
32
|
+
"@tsed/exceptions": "8.6.0-beta.1",
|
|
33
|
+
"@tsed/json-mapper": "8.6.0-beta.1",
|
|
34
|
+
"@tsed/schema": "8.6.0-beta.1",
|
|
35
|
+
"@tsed/typescript": "8.6.0-beta.1",
|
|
36
36
|
"eslint": "9.12.0",
|
|
37
37
|
"typescript": "5.4.5",
|
|
38
38
|
"vitest": "2.1.2"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
|
-
"@tsed/core": ">=8.
|
|
42
|
-
"@tsed/di": ">=8.
|
|
43
|
-
"@tsed/exceptions": ">=8.
|
|
44
|
-
"@tsed/json-mapper": ">=8.
|
|
45
|
-
"@tsed/schema": ">=8.
|
|
41
|
+
"@tsed/core": ">=8.6.0-beta.1",
|
|
42
|
+
"@tsed/di": ">=8.6.0-beta.1",
|
|
43
|
+
"@tsed/exceptions": ">=8.6.0-beta.1",
|
|
44
|
+
"@tsed/json-mapper": ">=8.6.0-beta.1",
|
|
45
|
+
"@tsed/schema": ">=8.6.0-beta.1"
|
|
46
46
|
},
|
|
47
47
|
"peerDependenciesMeta": {
|
|
48
48
|
"@tsed/core": {
|
|
@@ -67,5 +67,8 @@
|
|
|
67
67
|
},
|
|
68
68
|
"homepage": "https://github.com/tsedio/tsed/tree/production/packages/platform/platform-response-filter",
|
|
69
69
|
"author": "Romain Lenzotti",
|
|
70
|
-
"license": "MIT"
|
|
70
|
+
"license": "MIT",
|
|
71
|
+
"publishConfig": {
|
|
72
|
+
"tag": "beta"
|
|
73
|
+
}
|
|
71
74
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {PlatformTest} from "@tsed/platform-http/testing";
|
|
2
2
|
import {EndpointMetadata, Get, Returns, View} from "@tsed/schema";
|
|
3
|
+
import {createReadStream} from "fs";
|
|
3
4
|
|
|
4
5
|
import {PLATFORM_CONTENT_TYPE_RESOLVER} from "./PlatformContentTypeResolver.js";
|
|
5
6
|
import {PLATFORM_CONTENT_TYPES_CONTAINER} from "./PlatformContentTypesContainer.js";
|
|
@@ -30,7 +31,7 @@ describe("PlatformContentTypeResolver", () => {
|
|
|
30
31
|
beforeEach(() => PlatformTest.create());
|
|
31
32
|
afterEach(() => PlatformTest.reset());
|
|
32
33
|
|
|
33
|
-
it("should return the content type (undefined)", async () => {
|
|
34
|
+
it("should return the content type (undefined | Buffer)", async () => {
|
|
34
35
|
class TestController {
|
|
35
36
|
@Get("/")
|
|
36
37
|
get() {}
|
|
@@ -40,10 +41,40 @@ describe("PlatformContentTypeResolver", () => {
|
|
|
40
41
|
|
|
41
42
|
ctx.endpoint = EndpointMetadata.get(TestController, "get");
|
|
42
43
|
|
|
43
|
-
const result = await contentTypeResolver(data, ctx);
|
|
44
|
+
const result = await contentTypeResolver(Buffer.from("data"), ctx);
|
|
45
|
+
|
|
46
|
+
expect(result).toEqual(undefined);
|
|
47
|
+
});
|
|
48
|
+
it("should return the content type (undefined | Stream)", async () => {
|
|
49
|
+
class TestController {
|
|
50
|
+
@Get("/")
|
|
51
|
+
get() {}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const stream = createReadStream(`${import.meta.dirname}/__mock__/response.txt`);
|
|
55
|
+
|
|
56
|
+
const {contentTypeResolver, ctx, data} = await getTestFixture();
|
|
57
|
+
|
|
58
|
+
ctx.endpoint = EndpointMetadata.get(TestController, "get");
|
|
59
|
+
|
|
60
|
+
const result = await contentTypeResolver(stream, ctx);
|
|
44
61
|
|
|
45
62
|
expect(result).toEqual(undefined);
|
|
46
63
|
});
|
|
64
|
+
it("should return the content type (object - empty resolve content type - application/json)", async () => {
|
|
65
|
+
class TestController {
|
|
66
|
+
@Get("/")
|
|
67
|
+
get() {}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const {contentTypeResolver, ctx, data} = await getTestFixture();
|
|
71
|
+
|
|
72
|
+
ctx.endpoint = EndpointMetadata.get(TestController, "get");
|
|
73
|
+
|
|
74
|
+
const result = await contentTypeResolver(data, ctx);
|
|
75
|
+
|
|
76
|
+
expect(result).toEqual("application/json");
|
|
77
|
+
});
|
|
47
78
|
it("should return the content type (object - application/json)", async () => {
|
|
48
79
|
class TestController {
|
|
49
80
|
@Get("/")
|
|
@@ -1,29 +1,33 @@
|
|
|
1
|
-
import {isObject} from "@tsed/core";
|
|
1
|
+
import {isObject, isStream} from "@tsed/core";
|
|
2
2
|
import {type BaseContext, inject, injectable} from "@tsed/di";
|
|
3
3
|
|
|
4
4
|
import {ANY_CONTENT_TYPE} from "../constants/ANY_CONTENT_TYPE.js";
|
|
5
5
|
import {PLATFORM_CONTENT_TYPES_CONTAINER} from "./PlatformContentTypesContainer.js";
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
|
+
* Determine the content type of the response based on the data and the context.
|
|
9
|
+
* @param {unknown} data
|
|
10
|
+
* @param {BaseContext} ctx
|
|
8
11
|
* @ignore
|
|
9
12
|
*/
|
|
10
|
-
export function getContentType(data:
|
|
11
|
-
const {
|
|
12
|
-
|
|
13
|
+
export function getContentType(data: unknown, ctx: BaseContext) {
|
|
14
|
+
const {
|
|
15
|
+
endpoint,
|
|
16
|
+
endpoint: {operation},
|
|
17
|
+
response
|
|
18
|
+
} = ctx;
|
|
13
19
|
|
|
14
|
-
const contentType = response.getContentType() || operation.getContentTypeOf(response.statusCode)
|
|
20
|
+
const contentType = response.getContentType() || operation.getContentTypeOf(response.statusCode);
|
|
15
21
|
|
|
16
22
|
if (contentType && contentType !== ANY_CONTENT_TYPE) {
|
|
17
|
-
|
|
18
|
-
return "application/json";
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
return contentType;
|
|
23
|
+
return contentType === "application/json" && isObject(data) ? "application/json" : contentType;
|
|
22
24
|
}
|
|
23
25
|
|
|
24
26
|
if (endpoint.view) {
|
|
25
27
|
return "text/html";
|
|
26
28
|
}
|
|
29
|
+
|
|
30
|
+
return isObject(data) && !isStream(data) && !Buffer.isBuffer(data) ? "application/json" : contentType;
|
|
27
31
|
}
|
|
28
32
|
|
|
29
33
|
/**
|