@tsed/platform-response-filter
Advanced tools
Comparing version 8.2.0 to 8.3.0
/** | ||
* @file Automatically generated by @tsed/barrels. | ||
*/ | ||
export * from "./constants/ANY_CONTENT_TYPE.js"; | ||
export * from "./decorators/responseFilter.js"; | ||
@@ -8,5 +9,6 @@ export * from "./domain/ResponseFiltersContainer.js"; | ||
export * from "./interfaces/ResponseFilterMethods.js"; | ||
export * from "./services/PlatformContentTypeResolver.js"; | ||
export * from "./services/PlatformContentTypesContainer.js"; | ||
export * from "./services/PlatformResponseFilter.js"; | ||
export * from "./utils/getContentType.js"; | ||
export * from "./utils/renderView.js"; | ||
//# sourceMappingURL=index.js.map |
import { isSerializable } from "@tsed/core"; | ||
import { constant, inject, injectable } from "@tsed/di"; | ||
import { serialize } from "@tsed/json-mapper"; | ||
import { ResponseFiltersContainer } from "../domain/ResponseFiltersContainer.js"; | ||
import { ANY_CONTENT_TYPE, getContentType } from "../utils/getContentType.js"; | ||
import { renderView } from "../utils/renderView.js"; | ||
import { PLATFORM_CONTENT_TYPE_RESOLVER } from "./PlatformContentTypeResolver.js"; | ||
import { PLATFORM_CONTENT_TYPES_CONTAINER } from "./PlatformContentTypesContainer.js"; | ||
/** | ||
@@ -12,24 +12,6 @@ * @platform | ||
constructor() { | ||
this.types = new Map(); | ||
this.responseFilters = constant("responseFilters", []); | ||
this.additionalProperties = constant("additionalProperties"); | ||
ResponseFiltersContainer.forEach((token, type) => { | ||
if (this.responseFilters.includes(token)) { | ||
this.types.set(type, token); | ||
} | ||
}); | ||
this.container = inject(PLATFORM_CONTENT_TYPES_CONTAINER); | ||
this.contentTypeResolver = inject(PLATFORM_CONTENT_TYPE_RESOLVER); | ||
} | ||
get contentTypes() { | ||
return [...this.types.keys()]; | ||
} | ||
getBestContentType(data, ctx) { | ||
const contentType = getContentType(data, ctx); | ||
if (ctx.request.get("Accept")) { | ||
const bestContentType = ctx.request.accepts([contentType].concat(this.contentTypes).filter(Boolean)); | ||
if (bestContentType) { | ||
return [].concat(bestContentType).filter((type) => type !== "*/*")[0]; | ||
} | ||
} | ||
return contentType; | ||
} | ||
/** | ||
@@ -43,5 +25,5 @@ * Call filters to transform data | ||
if (ctx.endpoint?.operation) { | ||
const bestContentType = this.getBestContentType(data, ctx); | ||
const bestContentType = this.contentTypeResolver(data, ctx); | ||
bestContentType && response.contentType(bestContentType); | ||
const resolved = this.resolve(bestContentType); | ||
const resolved = this.container.resolve(bestContentType); | ||
if (resolved) { | ||
@@ -78,8 +60,2 @@ return resolved.transform(data, ctx); | ||
} | ||
resolve(bestContentType) { | ||
const token = this.types.get(bestContentType) || this.types.get(ANY_CONTENT_TYPE); | ||
if (token) { | ||
return inject(token); | ||
} | ||
} | ||
getIncludes(ctx) { | ||
@@ -86,0 +62,0 @@ if (ctx.request.query.includes) { |
/** | ||
* @file Automatically generated by @tsed/barrels. | ||
*/ | ||
export * from "./constants/ANY_CONTENT_TYPE.js"; | ||
export * from "./decorators/responseFilter.js"; | ||
@@ -8,4 +9,5 @@ export * from "./domain/ResponseFiltersContainer.js"; | ||
export * from "./interfaces/ResponseFilterMethods.js"; | ||
export * from "./services/PlatformContentTypeResolver.js"; | ||
export * from "./services/PlatformContentTypesContainer.js"; | ||
export * from "./services/PlatformResponseFilter.js"; | ||
export * from "./utils/getContentType.js"; | ||
export * from "./utils/renderView.js"; |
@@ -1,5 +0,2 @@ | ||
import { Type } from "@tsed/core"; | ||
import { BaseContext, TokenProvider } from "@tsed/di"; | ||
import { ResponseFilterKey } from "../domain/ResponseFiltersContainer.js"; | ||
import { ResponseFilterMethods } from "../interfaces/ResponseFilterMethods.js"; | ||
import { BaseContext } from "@tsed/di"; | ||
/** | ||
@@ -9,8 +6,8 @@ * @platform | ||
export declare class PlatformResponseFilter { | ||
protected types: Map<ResponseFilterKey, TokenProvider>; | ||
protected responseFilters: Type<ResponseFilterMethods<unknown>>[]; | ||
protected additionalProperties: boolean | undefined; | ||
constructor(); | ||
get contentTypes(): ResponseFilterKey[]; | ||
getBestContentType(data: any, ctx: BaseContext): any; | ||
protected container: { | ||
contentTypes: string[]; | ||
resolve(bestContentType: string): import("../index.js").ResponseFilterMethods<unknown> | undefined; | ||
}; | ||
protected contentTypeResolver: (data: any, ctx: BaseContext) => any; | ||
/** | ||
@@ -28,4 +25,3 @@ * Call filters to transform data | ||
serialize(data: unknown, ctx: BaseContext): Promise<unknown>; | ||
private resolve; | ||
private getIncludes; | ||
} |
@@ -5,3 +5,3 @@ { | ||
"type": "module", | ||
"version": "8.2.0", | ||
"version": "8.3.0", | ||
"source": "./src/index.ts", | ||
@@ -30,9 +30,9 @@ "main": "./lib/esm/index.js", | ||
"devDependencies": { | ||
"@tsed/barrels": "8.2.0", | ||
"@tsed/core": "8.2.0", | ||
"@tsed/di": "8.2.0", | ||
"@tsed/exceptions": "8.2.0", | ||
"@tsed/json-mapper": "8.2.0", | ||
"@tsed/schema": "8.2.0", | ||
"@tsed/typescript": "8.2.0", | ||
"@tsed/barrels": "8.3.0", | ||
"@tsed/core": "8.3.0", | ||
"@tsed/di": "8.3.0", | ||
"@tsed/exceptions": "8.3.0", | ||
"@tsed/json-mapper": "8.3.0", | ||
"@tsed/schema": "8.3.0", | ||
"@tsed/typescript": "8.3.0", | ||
"eslint": "9.12.0", | ||
@@ -43,7 +43,7 @@ "typescript": "5.4.5", | ||
"peerDependencies": { | ||
"@tsed/core": "8.2.0", | ||
"@tsed/di": "8.2.0", | ||
"@tsed/exceptions": "8.2.0", | ||
"@tsed/json-mapper": "8.2.0", | ||
"@tsed/schema": "8.2.0" | ||
"@tsed/core": "8.3.0", | ||
"@tsed/di": "8.3.0", | ||
"@tsed/exceptions": "8.3.0", | ||
"@tsed/json-mapper": "8.3.0", | ||
"@tsed/schema": "8.3.0" | ||
}, | ||
@@ -50,0 +50,0 @@ "peerDependenciesMeta": { |
<p style="text-align: center" align="center"> | ||
<a href="https://tsed.io" target="_blank"><img src="https://tsed.io/tsed-og.png" width="200" alt="Ts.ED logo"/></a> | ||
<a href="https://tsed.io" target="_blank"><img src="https://tsed.devtsed-og.png" width="200" alt="Ts.ED logo"/></a> | ||
</p> | ||
@@ -4,0 +4,0 @@ |
/** | ||
* @file Automatically generated by @tsed/barrels. | ||
*/ | ||
export * from "./constants/ANY_CONTENT_TYPE.js"; | ||
export * from "./decorators/responseFilter.js"; | ||
@@ -8,4 +9,5 @@ export * from "./domain/ResponseFiltersContainer.js"; | ||
export * from "./interfaces/ResponseFilterMethods.js"; | ||
export * from "./services/PlatformContentTypeResolver.js"; | ||
export * from "./services/PlatformContentTypesContainer.js"; | ||
export * from "./services/PlatformResponseFilter.js"; | ||
export * from "./utils/getContentType.js"; | ||
export * from "./utils/renderView.js"; |
@@ -1,9 +0,8 @@ | ||
import {isSerializable, Type} from "@tsed/core"; | ||
import {BaseContext, constant, inject, injectable, TokenProvider} from "@tsed/di"; | ||
import {isSerializable} from "@tsed/core"; | ||
import {BaseContext, constant, inject, injectable} from "@tsed/di"; | ||
import {serialize} from "@tsed/json-mapper"; | ||
import {ResponseFilterKey, ResponseFiltersContainer} from "../domain/ResponseFiltersContainer.js"; | ||
import {ResponseFilterMethods} from "../interfaces/ResponseFilterMethods.js"; | ||
import {ANY_CONTENT_TYPE, getContentType} from "../utils/getContentType.js"; | ||
import {renderView} from "../utils/renderView.js"; | ||
import {PLATFORM_CONTENT_TYPE_RESOLVER} from "./PlatformContentTypeResolver.js"; | ||
import {PLATFORM_CONTENT_TYPES_CONTAINER} from "./PlatformContentTypesContainer.js"; | ||
@@ -14,32 +13,6 @@ /** | ||
export class PlatformResponseFilter { | ||
protected types: Map<ResponseFilterKey, TokenProvider> = new Map(); | ||
protected responseFilters = constant<Type<ResponseFilterMethods>[]>("responseFilters", []); | ||
protected additionalProperties = constant<boolean>("additionalProperties"); | ||
protected container = inject<PLATFORM_CONTENT_TYPES_CONTAINER>(PLATFORM_CONTENT_TYPES_CONTAINER); | ||
protected contentTypeResolver = inject<PLATFORM_CONTENT_TYPE_RESOLVER>(PLATFORM_CONTENT_TYPE_RESOLVER); | ||
constructor() { | ||
ResponseFiltersContainer.forEach((token, type) => { | ||
if (this.responseFilters.includes(token)) { | ||
this.types.set(type, token); | ||
} | ||
}); | ||
} | ||
get contentTypes(): ResponseFilterKey[] { | ||
return [...this.types.keys()]; | ||
} | ||
getBestContentType(data: any, ctx: BaseContext) { | ||
const contentType = getContentType(data, ctx); | ||
if (ctx.request.get("Accept")) { | ||
const bestContentType = ctx.request.accepts([contentType].concat(this.contentTypes).filter(Boolean)); | ||
if (bestContentType) { | ||
return [].concat(bestContentType as any).filter((type) => type !== "*/*")[0]; | ||
} | ||
} | ||
return contentType; | ||
} | ||
/** | ||
@@ -54,7 +27,7 @@ * Call filters to transform data | ||
if (ctx.endpoint?.operation) { | ||
const bestContentType = this.getBestContentType(data, ctx); | ||
const bestContentType = this.contentTypeResolver(data, ctx); | ||
bestContentType && response.contentType(bestContentType); | ||
const resolved = this.resolve(bestContentType); | ||
const resolved = this.container.resolve(bestContentType); | ||
@@ -97,10 +70,2 @@ if (resolved) { | ||
private resolve(bestContentType: string) { | ||
const token = this.types.get(bestContentType) || this.types.get(ANY_CONTENT_TYPE); | ||
if (token) { | ||
return inject<ResponseFilterMethods>(token); | ||
} | ||
} | ||
private getIncludes(ctx: BaseContext) { | ||
@@ -107,0 +72,0 @@ if (ctx.request.query.includes) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
51658
47
946