Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | 1x 2x 1x 5x 5x 1x 4x 4x 1x 7x 7x 1x 6x 1x 4x 4x 1x 1x 1x 3x 3x 3x 1x 1x 1x 3x 1x 2x 2x 2x 1x 2x 2x 2x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 3x 1x 1x 4x 4x 4x 4x 4x 4x 4x 1x 4x 1x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x | import {
CLASS_PIPELINE_KEY,
CONTROLLERS_KEY,
METHOD_PIPELINES_KEY,
ROUTES_KEY,
} from "./constants";
import {
ControllerMetadata,
ControllerOptions,
GuardLike,
HttpMethod,
MiddlewareLike,
PipeLike,
PipelineMetadata,
RouteMetadata,
RouteOptions,
} from "../../../types";
type MethodPipelineEntry = {
handler: PropertyKey;
pipeline: PipelineMetadata;
};
const emptyPipeline = (): PipelineMetadata => ({
middlewares: [],
guards: [],
pipes: [],
});
const normalizePath = (value?: string): string => {
const source = (value || "").trim();
if (!source || source === "/") {
return "";
}
const withLeadingSlash = source.startsWith("/") ? source : `/${source}`;
return withLeadingSlash.endsWith("/")
? withLeadingSlash.slice(0, -1)
: withLeadingSlash;
};
const getClassPipeline = (target: any): PipelineMetadata => {
const meta = Reflect.getMetadata(CLASS_PIPELINE_KEY, target) as
| PipelineMetadata
| undefined;
return meta || emptyPipeline();
};
const setClassPipeline = (target: any, pipeline: PipelineMetadata) => {
Reflect.defineMetadata(CLASS_PIPELINE_KEY, pipeline, target);
};
const getMethodPipelines = (target: any): MethodPipelineEntry[] => {
const entries = Reflect.getMetadata(METHOD_PIPELINES_KEY, target) as
| MethodPipelineEntry[]
| undefined;
return entries || [];
};
const setMethodPipelines = (target: any, entries: MethodPipelineEntry[]) => {
Reflect.defineMetadata(METHOD_PIPELINES_KEY, entries, target);
};
const ensureMethodPipeline = (target: any, handler: PropertyKey): PipelineMetadata => {
const entries = getMethodPipelines(target);
let entry = entries.find((candidate) => candidate.handler === handler);
if (!entry) {
entry = { handler, pipeline: emptyPipeline() };
entries.push(entry);
setMethodPipelines(target, entries);
}
return entry.pipeline;
};
const appendClassMiddlewares = (target: any, middlewares: MiddlewareLike[]) => {
const pipeline = getClassPipeline(target);
pipeline.middlewares.push(...middlewares);
setClassPipeline(target, pipeline);
};
const appendClassGuards = (target: any, guards: GuardLike[]) => {
const pipeline = getClassPipeline(target);
pipeline.guards.push(...guards);
setClassPipeline(target, pipeline);
};
const appendClassPipes = (target: any, pipes: PipeLike[]) => {
const pipeline = getClassPipeline(target);
pipeline.pipes.push(...pipes);
setClassPipeline(target, pipeline);
};
const appendMethodMiddlewares = (
target: any,
handler: PropertyKey,
middlewares: MiddlewareLike[]
) => {
const pipeline = ensureMethodPipeline(target.constructor, handler);
pipeline.middlewares.push(...middlewares);
};
const appendMethodGuards = (target: any, handler: PropertyKey, guards: GuardLike[]) => {
const pipeline = ensureMethodPipeline(target.constructor, handler);
pipeline.guards.push(...guards);
};
const appendMethodPipes = (target: any, handler: PropertyKey, pipes: PipeLike[]) => {
const pipeline = ensureMethodPipeline(target.constructor, handler);
pipeline.pipes.push(...pipes);
};
const routeOptionsFrom = (pathOrOptions: string | RouteOptions | undefined): RouteOptions => {
if (typeof pathOrOptions === "string") {
return { path: pathOrOptions };
}
return pathOrOptions || {};
};
const buildRouteDecorator = (method: HttpMethod) => {
return (pathOrOptions: string | RouteOptions = ""): MethodDecorator => {
const options = routeOptionsFrom(pathOrOptions);
return (target, propertyKey) => {
const routes: Omit<RouteMetadata, keyof PipelineMetadata>[] =
Reflect.getMetadata(ROUTES_KEY, target.constructor) || [];
routes.push({
method,
path: normalizePath(options.path),
handler: propertyKey,
});
Reflect.defineMetadata(ROUTES_KEY, routes, target.constructor);
if (options.middlewares?.length) {
appendMethodMiddlewares(target, propertyKey, options.middlewares);
}
if (options.guards?.length) {
appendMethodGuards(target, propertyKey, options.guards);
}
if (options.pipes?.length) {
appendMethodPipes(target, propertyKey, options.pipes);
}
};
};
};
export function Controller(pathOrOptions: string | ControllerOptions = ""): ClassDecorator {
const options: ControllerOptions =
typeof pathOrOptions === "string" ? { path: pathOrOptions } : pathOrOptions;
return (target) => {
Reflect.defineMetadata(
CONTROLLERS_KEY,
{
path: normalizePath(options.path),
},
target
);
Eif (options.middlewares?.length) {
appendClassMiddlewares(target, options.middlewares);
}
Eif (options.guards?.length) {
appendClassGuards(target, options.guards);
}
Eif (options.pipes?.length) {
appendClassPipes(target, options.pipes);
}
};
}
export const Get = buildRouteDecorator("GET");
export const Post = buildRouteDecorator("POST");
export const Patch = buildRouteDecorator("PATCH");
export const Delete = buildRouteDecorator("DELETE");
export function UseMiddlewares(...middlewares: MiddlewareLike[]): MethodDecorator & ClassDecorator {
return (target: any, propertyKey?: string | symbol) => {
Eif (!propertyKey) {
appendClassMiddlewares(target, middlewares);
return;
}
appendMethodMiddlewares(target, propertyKey, middlewares);
};
}
export function UseGuards(...guards: GuardLike[]): MethodDecorator & ClassDecorator {
return (target: any, propertyKey?: string | symbol) => {
Eif (!propertyKey) {
appendClassGuards(target, guards);
return;
}
appendMethodGuards(target, propertyKey, guards);
};
}
export function UsePipes(...pipes: PipeLike[]): MethodDecorator & ClassDecorator {
return (target: any, propertyKey?: string | symbol) => {
Eif (!propertyKey) {
appendClassPipes(target, pipes);
return;
}
appendMethodPipes(target, propertyKey, pipes);
};
}
export function getControllerMetadata(target: any): ControllerMetadata | undefined {
const controllerMeta: { path: string } | undefined = Reflect.getMetadata(CONTROLLERS_KEY, target);
Iif (!controllerMeta) {
return undefined;
}
const pipeline = getClassPipeline(target);
return {
path: controllerMeta.path,
middlewares: [...pipeline.middlewares],
guards: [...pipeline.guards],
pipes: [...pipeline.pipes],
};
}
export function getRouteMetadata(target: any): RouteMetadata[] {
const routes: Omit<RouteMetadata, keyof PipelineMetadata>[] =
Reflect.getMetadata(ROUTES_KEY, target) || [];
const methodPipelines = getMethodPipelines(target);
return routes.map((route) => {
const methodPipeline = methodPipelines.find((item) => item.handler === route.handler)?.pipeline;
return {
...route,
middlewares: [...(methodPipeline?.middlewares || [])],
guards: [...(methodPipeline?.guards || [])],
pipes: [...(methodPipeline?.pipes || [])],
};
});
}
|