@tsed/platform-http
Version:
A TypeScript Framework on top of Express
28 lines (27 loc) • 1.05 kB
JavaScript
import { ServerResponse } from "node:http";
import { isClass, Metadata, nameOf } from "@tsed/core";
import { ParamTypes, UseParam } from "@tsed/platform-params";
function getParamType(target, propertyKey, parameterIndex) {
const type = Metadata.getOwnParamTypes(target, propertyKey)[parameterIndex];
if (isClass(type)) {
if (nameOf(type) === "PlatformResponse") {
return { paramType: ParamTypes.PLATFORM_RESPONSE, dataPath: "$ctx.response" };
}
if (type === ServerResponse) {
return { paramType: ParamTypes.NODE_RESPONSE, dataPath: "$ctx.response.res" };
}
}
return { paramType: ParamTypes.RESPONSE, dataPath: "$ctx.response.response" };
}
export function Response() {
return Res();
}
export function Res() {
return (target, propertyKey, parameterIndex) => {
const { paramType, dataPath } = getParamType(target, propertyKey, parameterIndex);
UseParam({
paramType,
dataPath
})(target, propertyKey, parameterIndex);
};
}