Skip to content

Commit

Permalink
more updates as requested on the PR
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy committed Aug 5, 2022
1 parent cb8601e commit ee99935
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 21 deletions.
4 changes: 2 additions & 2 deletions packages/cli/src/UserManagement/controllers/MeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ export class MeController {
/**
* Return the logged-in user.
*/
@Get()
@Get('/')
async getCurrentUser(req: AuthenticatedRequest): Promise<PublicUser> {
return sanitizeUser(req.user);
}

/**
* Update the logged-in user's settings, except password.
*/
@Patch()
@Patch('/')
async updateCurrentUser(req: MeRequest.Settings, res: Response): Promise<PublicUser> {
const { email } = req.body;
if (!email) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class OwnerController {
* Promote a shell into the owner of the n8n instance,
* and enable `isInstanceOwnerSetUp` setting.
*/
@Post()
@Post('/')
async promoteOwner(req: OwnerRequest.Post, res: Response) {
const { email, firstName, lastName, password } = req.body;
const { id: userId } = req.user;
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/UserManagement/controllers/UserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class UserController {
/**
* Send email invite(s) to one or multiple users and create user shell(s).
*/
@Post()
@Post('/')
async sendEmailInvites(req: UserRequest.Invite) {
if (config.getEnv('userManagement.emails.mode') === '') {
Logger.debug(
Expand Down Expand Up @@ -288,7 +288,7 @@ export class UserController {
return sanitizeUser(updatedUser);
}

@Get()
@Get('/')
async listUsers() {
const users = await Db.collections.User.find({ relations: ['globalRole'] });
return users.map((user) => sanitizeUser(user, ['personalizationAnswers']));
Expand Down
7 changes: 4 additions & 3 deletions packages/cli/src/UserManagement/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const Keys = {
BASE_PATH: Symbol('base_path'),
} as const;

type Method = 'get' | 'post' | 'patch' | 'delete';
type Method = 'get' | 'post' | 'patch' | 'put' | 'delete';

export const RestController =
(basePath: `/${string}` = '/'): ClassDecorator =>
Expand All @@ -26,7 +26,7 @@ export const RestController =

const RouteFactory =
(...methods: Method[]) =>
(path: `/${string}` = '/'): MethodDecorator =>
(path: `/${string}`): MethodDecorator =>
(target, handlerName) => {
const ControllerClass = target.constructor;
const routes: RouteMetadata[] = Reflect.getMetadata(Keys.ROUTES, ControllerClass) ?? [];
Expand All @@ -40,6 +40,7 @@ const RouteFactory =
export const Get = RouteFactory('get');
export const Post = RouteFactory('post');
export const Patch = RouteFactory('patch');
export const Put = RouteFactory('put');
export const Delete = RouteFactory('delete');

type RequestHandler = (req: Request, res: Response) => Promise<unknown>;
Expand All @@ -55,7 +56,7 @@ export const registerController = <T>(
): void => {
const instance = new ControllerClass() as unknown as Record<string, RequestHandler>;
const routes: RouteMetadata[] = Reflect.getMetadata(Keys.ROUTES, ControllerClass);
if (routes.length > 0) {
if (routes.length) {
const router = Router({ mergeParams: true });
const restEndpoint: string = config.getEnv('endpoints.rest');
const basePath: string = Reflect.getMetadata(Keys.BASE_PATH, ControllerClass);
Expand Down
22 changes: 9 additions & 13 deletions packages/cli/test/integration/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export async function initTestServer({
testServer.externalHooks = ExternalHooks();
}

const [routerEndpoints, functionEndpoints] = classifyEndpointGroups(endpointGroups);
const [routerEndpoints] = classifyEndpointGroups(endpointGroups);

if (routerEndpoints.length) {
const { apiRouters } = await loadPublicApiVersions(testServer.publicApiEndpoint);
Expand All @@ -116,13 +116,11 @@ export async function initTestServer({
}
}

if (functionEndpoints.length) {
registerController(testServer.app, MeController);
registerController(testServer.app, UserController);
registerController(testServer.app, AuthController);
registerController(testServer.app, OwnerController);
registerController(testServer.app, PasswordResetController);
}
registerController(testServer.app, MeController);
registerController(testServer.app, UserController);
registerController(testServer.app, AuthController);
registerController(testServer.app, OwnerController);
registerController(testServer.app, PasswordResetController);

return testServer.app;
}
Expand All @@ -137,20 +135,18 @@ export function initTestTelemetry() {
}

/**
* Classify endpoint groups into `routerEndpoints` (newest, using `express.Router`),
* and `functionEndpoints` (legacy, namespaced inside a function).
* Identify endpoint groups as `routerEndpoints` (newest, using `express.Router`)
*/
const classifyEndpointGroups = (endpointGroups: string[]) => {
const routerEndpoints: string[] = [];
const functionEndpoints: string[] = [];

const ROUTER_GROUP = ['credentials', 'nodes', 'workflows', 'publicApi'];

endpointGroups.forEach((group) =>
(ROUTER_GROUP.includes(group) ? routerEndpoints : functionEndpoints).push(group),
ROUTER_GROUP.includes(group) && routerEndpoints.push(group),
);

return [routerEndpoints, functionEndpoints];
return [routerEndpoints];
};

// ----------------------------------
Expand Down

0 comments on commit ee99935

Please sign in to comment.