Skip to content

Commit

Permalink
apply PR suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy committed Aug 4, 2022
1 parent e5470d7 commit bdf64a8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 32 deletions.
1 change: 0 additions & 1 deletion packages/cli/src/ResponseHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ export function send<T, R extends Request, S extends Response>(
processFunction: (req: R, res: S) => Promise<T>,
raw = false,
) {
// eslint-disable-next-line consistent-return
return async (req: R, res: S) => {
try {
const data = await processFunction(req, res);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import type { Request, Response } from 'express';
import { In } from 'typeorm';
import validator from 'validator';
import type { IDataObject } from 'n8n-workflow';
import { LoggerProxy as Logger } from 'n8n-workflow';
import { Db, InternalHooksManager, ResponseHelper } from '../..';
import { issueCookie, resolveJwt } from '../auth/jwt';
Expand Down Expand Up @@ -185,7 +184,7 @@ export class AuthController {
* Authless endpoint.
*/
@Post('/logout')
logout(req: Request, res: Response): IDataObject {
logout(req: Request, res: Response) {
res.clearCookie(AUTH_COOKIE_NAME);
return { loggedOut: true };
}
Expand Down
49 changes: 20 additions & 29 deletions packages/cli/src/UserManagement/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import { posix } from 'path';
import { ResponseHelper } from '..';
import config from '../../config';

interface RequestHandler<ReqBody, ResBody> extends Function {
async(req: Request<unknown, ResBody, ReqBody>, res: Response<ResBody>): Promise<ReqBody>;
}

export interface IRouter {
export interface RouteMetadata {
method: Method;
path: string;
handlerName: string | symbol;
Expand All @@ -21,12 +17,7 @@ const Keys = {
BASE_PATH: Symbol('base_path'),
} as const;

export enum Method {
GET = 'get',
POST = 'post',
PATCH = 'patch',
DELETE = 'delete',
}
type Method = 'get' | 'post' | 'patch' | 'delete';

export const RestController =
(basePath = '/'): ClassDecorator =>
Expand All @@ -39,37 +30,37 @@ const RouteFactory =
(path = '/'): MethodDecorator =>
(target, handlerName) => {
const ControllerClass = target.constructor;
const routers: IRouter[] = Reflect.hasMetadata(Keys.ROUTES, ControllerClass)
? (Reflect.getMetadata(Keys.ROUTES, ControllerClass) as IRouter[])
: [];
const routes: RouteMetadata[] = Reflect.getMetadata(Keys.ROUTES, ControllerClass) ?? [];
methods.forEach((method) => {
routers.push({ method, path, handlerName });
routes.push({ method, path, handlerName });
});

Reflect.defineMetadata(Keys.ROUTES, routers, ControllerClass);
Reflect.defineMetadata(Keys.ROUTES, routes, ControllerClass);
};

export const Get = RouteFactory(Method.GET);
export const Post = RouteFactory(Method.POST);
export const Patch = RouteFactory(Method.PATCH);
export const Delete = RouteFactory(Method.DELETE);
export const Get = RouteFactory('get');
export const Post = RouteFactory('post');
export const Patch = RouteFactory('patch');
export const Delete = RouteFactory('delete');

export interface Controller<T> extends Function {
new (...args: unknown[]): T;
interface RequestHandler<ReqBody = unknown, ResBody = unknown> extends Function {
async(req: Request<unknown, ResBody, ReqBody>, res: Response<ResBody>): Promise<ReqBody>;
}

interface Controller extends Function {
new (...args: unknown[]): Record<string, RequestHandler>;
}

export const registerController = <T>(
export const registerController = (
app: Application,
ControllerClass: Controller<T>,
ControllerClass: Controller,
...middlewares: Middleware[]
): void => {
const instance = new ControllerClass() as unknown as {
[handleName: string]: RequestHandler<unknown, unknown>;
};
const routes: IRouter[] = Reflect.getMetadata(Keys.ROUTES, ControllerClass);
const instance = new ControllerClass();
const routes: RouteMetadata[] = Reflect.getMetadata(Keys.ROUTES, ControllerClass);
if (routes.length > 0) {
const router = Router({ mergeParams: true });
const restEndpoint: string = config.get('endpoints.rest');
const restEndpoint: string = config.getEnv('endpoints.rest');
const basePath: string = Reflect.getMetadata(Keys.BASE_PATH, ControllerClass);
const prefix = `/${posix.join(restEndpoint, basePath)}`;

Expand Down

0 comments on commit bdf64a8

Please sign in to comment.