@tsed/platform-http
Version:
A TypeScript Framework on top of Express
34 lines (33 loc) • 1.3 kB
JavaScript
import { IncomingMessage } from "node:http";
import { isClass, Metadata, nameOf } from "@tsed/core";
import { mapParamsOptions, ParamTypes, UseParam } from "@tsed/platform-params";
function getParamType(target, propertyKey, parameterIndex) {
const type = Metadata.getOwnParamTypes(target, propertyKey)[parameterIndex];
if (isClass(type)) {
if (nameOf(type) === "PlatformRequest") {
return { paramType: ParamTypes.PLATFORM_REQUEST, dataPath: "$ctx.request" };
}
if (type === IncomingMessage) {
return { paramType: ParamTypes.NODE_REQUEST, dataPath: "$ctx.request.req" };
}
}
return { paramType: ParamTypes.REQUEST, dataPath: "$ctx.request.request" };
}
export function Request(...args) {
// @ts-ignore
return Req(...args);
}
export function Req(...args) {
const { expression, useType, useMapper = false, useValidation = false } = mapParamsOptions(args);
return (target, propertyKey, parameterIndex) => {
const { paramType, dataPath } = getParamType(target, propertyKey, parameterIndex);
UseParam({
paramType,
dataPath,
expression,
useType,
useMapper,
useValidation
})(target, propertyKey, parameterIndex);
};
}