@tsed/platform-response-filter
Advanced tools
Comparing version 8.4.6 to 8.5.0-beta.1
@@ -1,2 +0,2 @@ | ||
import { isObject } from "@tsed/core"; | ||
import { isObject, isStream } from "@tsed/core"; | ||
import { inject, injectable } from "@tsed/di"; | ||
@@ -6,13 +6,12 @@ import { ANY_CONTENT_TYPE } from "../constants/ANY_CONTENT_TYPE.js"; | ||
/** | ||
* Determine the content type of the response based on the data and the context. | ||
* @param {unknown} data | ||
* @param {BaseContext} ctx | ||
* @ignore | ||
*/ | ||
export function getContentType(data, ctx) { | ||
const { endpoint, response } = ctx; | ||
const { operation } = endpoint; | ||
const contentType = response.getContentType() || operation.getContentTypeOf(response.statusCode) || ""; | ||
const { endpoint, endpoint: { operation }, response } = ctx; | ||
const contentType = response.getContentType() || operation.getContentTypeOf(response.statusCode); | ||
if (contentType && contentType !== ANY_CONTENT_TYPE) { | ||
if (contentType === "application/json" && isObject(data)) { | ||
return "application/json"; | ||
} | ||
return contentType; | ||
return contentType === "application/json" && isObject(data) ? "application/json" : contentType; | ||
} | ||
@@ -22,2 +21,3 @@ if (endpoint.view) { | ||
} | ||
return isObject(data) && !isStream(data) && !Buffer.isBuffer(data) ? "application/json" : contentType; | ||
} | ||
@@ -24,0 +24,0 @@ /** |
import { type BaseContext } from "@tsed/di"; | ||
/** | ||
* Determine the content type of the response based on the data and the context. | ||
* @param {unknown} data | ||
* @param {BaseContext} ctx | ||
* @ignore | ||
*/ | ||
export declare function getContentType(data: any, ctx: BaseContext): any; | ||
export declare function getContentType(data: unknown, ctx: BaseContext): any; | ||
/** | ||
@@ -7,0 +10,0 @@ * @ignore |
@@ -5,3 +5,3 @@ { | ||
"type": "module", | ||
"version": "8.4.6", | ||
"version": "8.5.0-beta.1", | ||
"source": "./src/index.ts", | ||
@@ -30,9 +30,9 @@ "main": "./lib/esm/index.js", | ||
"devDependencies": { | ||
"@tsed/barrels": "8.4.6", | ||
"@tsed/core": "8.4.6", | ||
"@tsed/di": "8.4.6", | ||
"@tsed/exceptions": "8.4.6", | ||
"@tsed/json-mapper": "8.4.6", | ||
"@tsed/schema": "8.4.6", | ||
"@tsed/typescript": "8.4.6", | ||
"@tsed/barrels": "8.5.0-beta.1", | ||
"@tsed/core": "8.5.0-beta.1", | ||
"@tsed/di": "8.5.0-beta.1", | ||
"@tsed/exceptions": "8.5.0-beta.1", | ||
"@tsed/json-mapper": "8.5.0-beta.1", | ||
"@tsed/schema": "8.5.0-beta.1", | ||
"@tsed/typescript": "8.5.0-beta.1", | ||
"eslint": "9.12.0", | ||
@@ -43,7 +43,7 @@ "typescript": "5.4.5", | ||
"peerDependencies": { | ||
"@tsed/core": ">=8.4.6", | ||
"@tsed/di": ">=8.4.6", | ||
"@tsed/exceptions": ">=8.4.6", | ||
"@tsed/json-mapper": ">=8.4.6", | ||
"@tsed/schema": ">=8.4.6" | ||
"@tsed/core": ">=8.5.0-beta.1", | ||
"@tsed/di": ">=8.5.0-beta.1", | ||
"@tsed/exceptions": ">=8.5.0-beta.1", | ||
"@tsed/json-mapper": ">=8.5.0-beta.1", | ||
"@tsed/schema": ">=8.5.0-beta.1" | ||
}, | ||
@@ -73,3 +73,6 @@ "peerDependenciesMeta": { | ||
"author": "Romain Lenzotti", | ||
"license": "MIT" | ||
"license": "MIT", | ||
"publishConfig": { | ||
"tag": "beta" | ||
} | ||
} |
import {PlatformTest} from "@tsed/platform-http/testing"; | ||
import {EndpointMetadata, Get, Returns, View} from "@tsed/schema"; | ||
import {createReadStream} from "fs"; | ||
@@ -33,3 +34,3 @@ import {PLATFORM_CONTENT_TYPE_RESOLVER} from "./PlatformContentTypeResolver.js"; | ||
it("should return the content type (undefined)", async () => { | ||
it("should return the content type (undefined | Buffer)", async () => { | ||
class TestController { | ||
@@ -44,6 +45,36 @@ @Get("/") | ||
const result = await contentTypeResolver(data, ctx); | ||
const result = await contentTypeResolver(Buffer.from("data"), ctx); | ||
expect(result).toEqual(undefined); | ||
}); | ||
it("should return the content type (undefined | Stream)", async () => { | ||
class TestController { | ||
@Get("/") | ||
get() {} | ||
} | ||
const stream = createReadStream(`${import.meta.dirname}/__mock__/response.txt`); | ||
const {contentTypeResolver, ctx, data} = await getTestFixture(); | ||
ctx.endpoint = EndpointMetadata.get(TestController, "get"); | ||
const result = await contentTypeResolver(stream, ctx); | ||
expect(result).toEqual(undefined); | ||
}); | ||
it("should return the content type (object - empty resolve content type - application/json)", async () => { | ||
class TestController { | ||
@Get("/") | ||
get() {} | ||
} | ||
const {contentTypeResolver, ctx, data} = await getTestFixture(); | ||
ctx.endpoint = EndpointMetadata.get(TestController, "get"); | ||
const result = await contentTypeResolver(data, ctx); | ||
expect(result).toEqual("application/json"); | ||
}); | ||
it("should return the content type (object - application/json)", async () => { | ||
@@ -50,0 +81,0 @@ class TestController { |
@@ -1,2 +0,2 @@ | ||
import {isObject} from "@tsed/core"; | ||
import {isObject, isStream} from "@tsed/core"; | ||
import {type BaseContext, inject, injectable} from "@tsed/di"; | ||
@@ -8,16 +8,18 @@ | ||
/** | ||
* Determine the content type of the response based on the data and the context. | ||
* @param {unknown} data | ||
* @param {BaseContext} ctx | ||
* @ignore | ||
*/ | ||
export function getContentType(data: any, ctx: BaseContext) { | ||
const {endpoint, response} = ctx; | ||
const {operation} = endpoint; | ||
export function getContentType(data: unknown, ctx: BaseContext) { | ||
const { | ||
endpoint, | ||
endpoint: {operation}, | ||
response | ||
} = ctx; | ||
const contentType = response.getContentType() || operation.getContentTypeOf(response.statusCode) || ""; | ||
const contentType = response.getContentType() || operation.getContentTypeOf(response.statusCode); | ||
if (contentType && contentType !== ANY_CONTENT_TYPE) { | ||
if (contentType === "application/json" && isObject(data)) { | ||
return "application/json"; | ||
} | ||
return contentType; | ||
return contentType === "application/json" && isObject(data) ? "application/json" : contentType; | ||
} | ||
@@ -28,2 +30,4 @@ | ||
} | ||
return isObject(data) && !isStream(data) && !Buffer.isBuffer(data) ? "application/json" : contentType; | ||
} | ||
@@ -30,0 +34,0 @@ |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
54403
990
1