<commit_before>import { Request, Response } from "express";

import { Plugins } from "./plugins";

import * as I from "./interfaces";

export class Route {
	pluginCallbacks: {[key: string]: Function};
	targetMethod: I.Method;
	befores: Array<I.Middleware>;
	plugins: Plugins|void;

	constructor(
		private server: any,
		public target: any,
		public method: I.RouteConfig
	) {
		this.targetMethod = target[method.methodName];
		this.targetMethod.path = method.path || `/${this.targetMethod.name}`;
		this.targetMethod.alias = this.targetMethod.alias || this.targetMethod.name;
		this.plugins = server.plugins;
		this.pluginCallbacks = {};

		console.log(`Loading route: ${method.action.toUpperCase()} ${this.target.path}${this.targetMethod.path}`);

		this.befores = this.addBeforeMiddlewares();

		this.befores.push(this.mainMiddleware.bind(this));
	}

	private addBeforeMiddlewares(): Array<I.Middleware> {
		let befores: Array<I.Middleware> = [];

		if (this.targetMethod && this.targetMethod.before) befores = befores.concat(this.targetMethod.before);
		if (this.target.beforeAll) befores = this.target.beforeAll.concat(befores);

		return befores;
	}

	private addAfterMiddlewares(result: any): any {
		if (!this.targetMethod.after) return result;

		return this.targetMethod.after.reduce((prev: Function, next: Function) => next(prev), result);
	}

	private mainMiddleware(request: Request, response: Response): void {
		if (this.plugins && this.plugins.runMiddlewares(request, response, this)) return;

		let parameters: Array<any> = this.getOrder(request, response);

		console.log(`${this.method.action.toUpperCase()} ${this.target.path}${this.targetMethod.path}`);

		new Promise((resolve) => {
			resolve(this.targetMethod.call(this.target, ...parameters));
		})
		.then(this.addAfterMiddlewares.bind(this))
		.then((methodResult: any) => {
			if (methodResult.error && methodResult.status) return response.status(methodResult.status).send({ error: methodResult.error });
			response.status(this.targetMethod.status || 200).send(methodResult);
		})
		.catch((error: any) => {
			console.error(error);
			response.status(error.status || 500).send(error ? error.error : error);
		});
	}<commit_msg>fix error in methodResult<commit_after>import { Request, Response } from "express";

import { Plugins } from "./plugins";

import * as I from "./interfaces";

export class Route {
	pluginCallbacks: {[key: string]: Function};
	targetMethod: I.Method;
	befores: Array<I.Middleware>;
	plugins: Plugins|void;

	constructor(
		private server: any,
		public target: any,
		public method: I.RouteConfig
	) {
		this.targetMethod = target[method.methodName];
		this.targetMethod.path = method.path || `/${this.targetMethod.name}`;
		this.targetMethod.alias = this.targetMethod.alias || this.targetMethod.name;
		this.plugins = server.plugins;
		this.pluginCallbacks = {};

		console.log(`Loading route: ${method.action.toUpperCase()} ${this.target.path}${this.targetMethod.path}`);

		this.befores = this.addBeforeMiddlewares();

		this.befores.push(this.mainMiddleware.bind(this));
	}

	private addBeforeMiddlewares(): Array<I.Middleware> {
		let befores: Array<I.Middleware> = [];

		if (this.targetMethod && this.targetMethod.before) befores = befores.concat(this.targetMethod.before);
		if (this.target.beforeAll) befores = this.target.beforeAll.concat(befores);

		return befores;
	}

	private addAfterMiddlewares(result: any): any {
		if (!this.targetMethod.after) return result;

		return this.targetMethod.after.reduce((prev: Function, next: Function) => next(prev), result);
	}

	private mainMiddleware(request: Request, response: Response): void {
		if (this.plugins && this.plugins.runMiddlewares(request, response, this)) return;

		let parameters: Array<any> = this.getOrder(request, response);

		console.log(`${this.method.action.toUpperCase()} ${this.target.path}${this.targetMethod.path}`);

		new Promise((resolve) => {
			resolve(this.targetMethod.call(this.target, ...parameters));
		})
		.then(this.addAfterMiddlewares.bind(this))
		.then((methodResult: any) => {
			if (methodResult && methodResult.error && methodResult.status) return response.status(methodResult.status).send({ error: methodResult.error });
			response.status(this.targetMethod.status || 200).send(methodResult);
		})
		.catch((error: any) => {
			console.error(error);
			response.status(error.status || 500).send(error ? error.error : error);
		});
	}
{'fraction_non_alphanumeric': 0.15713642503376857, 'fraction_numerical': 0.0027014858171994596, 'mean_word_length': 5.020325203252033, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '563454', 'n_tokens_mistral': 1506, 'n_tokens_neox': 1394, 'n_words': 262}