-
-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(platform-response-filter): add PLATFORM_CONTENT_TYPES_CONTAI…
…NER and PLATFORM_CONTENT_TYPE_RESOLVER
- Loading branch information
Showing
7 changed files
with
143 additions
and
112 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
packages/platform/platform-response-filter/src/constants/ANY_CONTENT_TYPE.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const ANY_CONTENT_TYPE = "*/*"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
/** | ||
* @file Automatically generated by @tsed/barrels. | ||
*/ | ||
export * from "./constants/ANY_CONTENT_TYPE.js"; | ||
export * from "./decorators/responseFilter.js"; | ||
export * from "./domain/ResponseFiltersContainer.js"; | ||
export * from "./errors/TemplateRenderError.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"; |
88 changes: 46 additions & 42 deletions
88
...e-filter/src/utils/getContentType.spec.ts → ...vices/PlatformContentTypeResolver.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
packages/platform/platform-response-filter/src/services/PlatformContentTypeResolver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import {isObject} from "@tsed/core"; | ||
import {type BaseContext, inject, injectable} from "@tsed/di"; | ||
|
||
import {ANY_CONTENT_TYPE} from "../constants/ANY_CONTENT_TYPE.js"; | ||
import {PLATFORM_CONTENT_TYPES_CONTAINER} from "./PlatformContentTypesContainer.js"; | ||
|
||
/** | ||
* @ignore | ||
*/ | ||
export function getContentType(data: any, ctx: BaseContext) { | ||
const {endpoint, response} = ctx; | ||
const {operation} = endpoint; | ||
|
||
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; | ||
} | ||
|
||
if (endpoint.view) { | ||
return "text/html"; | ||
} | ||
} | ||
|
||
/** | ||
* @ignore | ||
*/ | ||
function resolver(data: any, ctx: BaseContext) { | ||
const contentType = getContentType(data, ctx); | ||
|
||
if (ctx.request.get("Accept")) { | ||
const {contentTypes} = inject<PLATFORM_CONTENT_TYPES_CONTAINER>(PLATFORM_CONTENT_TYPES_CONTAINER); | ||
|
||
const bestContentType = ctx.request.accepts([contentType].concat(contentTypes).filter(Boolean)); | ||
|
||
if (bestContentType) { | ||
return [].concat(bestContentType as any).filter((type) => type !== "*/*")[0]; | ||
} | ||
} | ||
|
||
return contentType; | ||
} | ||
|
||
/** | ||
* @ignore | ||
*/ | ||
export type PLATFORM_CONTENT_TYPE_RESOLVER = typeof resolver; | ||
export const PLATFORM_CONTENT_TYPE_RESOLVER = injectable(Symbol.for("PLATFORM_CONTENT_TYPE_RESOLVER")) | ||
.factory(() => resolver) | ||
.token(); |
31 changes: 31 additions & 0 deletions
31
packages/platform/platform-response-filter/src/services/PlatformContentTypesContainer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type {Type} from "@tsed/core"; | ||
import {constant, inject, injectable, type TokenProvider} from "@tsed/di"; | ||
|
||
import {ANY_CONTENT_TYPE} from "../constants/ANY_CONTENT_TYPE.js"; | ||
import {ResponseFilterKey, ResponseFiltersContainer} from "../domain/ResponseFiltersContainer.js"; | ||
import type {ResponseFilterMethods} from "../interfaces/ResponseFilterMethods.js"; | ||
|
||
function factory() { | ||
const responseFilters = constant<Type<ResponseFilterMethods>[]>("responseFilters", []); | ||
const containers: Map<ResponseFilterKey, TokenProvider> = new Map(); | ||
|
||
ResponseFiltersContainer.forEach((token, type) => { | ||
if (responseFilters.includes(token)) { | ||
containers.set(type, token); | ||
} | ||
}); | ||
|
||
return { | ||
contentTypes: [...containers.keys()], | ||
resolve(bestContentType: string) { | ||
const token = containers.get(bestContentType) || containers.get(ANY_CONTENT_TYPE); | ||
|
||
if (token) { | ||
return inject<ResponseFilterMethods>(token); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
export type PLATFORM_CONTENT_TYPES_CONTAINER = ReturnType<typeof factory>; | ||
export const PLATFORM_CONTENT_TYPES_CONTAINER = injectable(Symbol.for("PLATFORM_CONTENT_TYPES_CONTAINER")).factory(factory).token(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 0 additions & 26 deletions
26
packages/platform/platform-response-filter/src/utils/getContentType.ts
This file was deleted.
Oops, something went wrong.