<commit_before>import chalk from 'chalk';
import { IEndpoint, IParam } from '@materia/interfaces';

import { App } from '../app';
import { MateriaError } from '../error';
import { Controller } from './controller';
import { Entity } from '../entities/entity';
import { Query } from '../entities/query';

// Not used yet
export enum Method {
	GET,
	POST,
	PUT,
	DELETE,
	PATCH
}
/*
	data format:
	{
		name: string,
		desc: string,
		method: string (GET|POST|PUT|DELETE|PATCH)
		url: string
		base: string
		params: array
		data: array
		action: string (QUERY|JS|SQL)
		file: path (if action == CODE)
		query: {
			entity: string
			id: string (queryId)
		}
	}
*/

export class Endpoint {
	name: string;
	method: string;
	url: string;
	fromAddon?: IEndpoint['fromAddon'];

	entity: Entity;
	params: Array<IParam>;
	data: Array<IParam>;
	permissions: Array<string>;

	controller: string;
	action: string;

	query: Query;
	queryStr: string;

	static controllers: {[name: string]: Controller} = {};

	static resetControllers(): void {
		Endpoint.controllers = {};
	}

	constructor(private app: App, endpointConfig: IEndpoint) {
		this.method = (endpointConfig.method && endpointConfig.method.toLowerCase()) || 'get';

		// this.name = endpointConfig.name
		// this.desc = endpointConfig.desc
		this.url = endpointConfig.url;
		this.fromAddon = endpointConfig.fromAddon;

		this.params = [];
		this.data = [];
		this.permissions = endpointConfig.permissions || [];

		/*if (typeof endpointConfig.query == 'function') {
			this.params = endpointConfig.params || []
			this.data = endpointConfig.data || []
			this.query = endpointConfig.query
		}*/

		if (endpointConfig.controller) {
			this.controller = endpointConfig.controller;
			this.action = endpointConfig.action;

			const basePath = this.fromAddon ? this.fromAddon.path : this.app.path;

			const ctrl = this._getController();
			ctrl.load(basePath, this.controller);
			if ( ! ctrl.ctrlClass.prototype[this.action]) {
				throw new MateriaError(`cannot find method ${this.action} in controller ${this.controller}.ctrl.js`);
			}
			this._buildParams(endpointConfig.params);
		} else {
			let entity_name;
			if (typeof endpointConfig.query.entity == 'string') {
				entity_name = endpointConfig.query.entity;
			}

			this.entity = this.app.entities.get(entity_name);

			if ( ! this.entity) {
				throw new MateriaError('Could not find entity ' + entity_name);
			}

			this.query = this.entity.getQuery(endpointConfig.query.id);

			if ( ! this.query ) {
				throw new MateriaError('Could not find query "' + endpointConfig.query.id + '" of entity ' + this.entity.name);<commit_msg>refac(api/endpoints): align error msg style for missing method<commit_after>import chalk from 'chalk';
import { IEndpoint, IParam } from '@materia/interfaces';

import { App } from '../app';
import { MateriaError } from '../error';
import { Controller } from './controller';
import { Entity } from '../entities/entity';
import { Query } from '../entities/query';

// Not used yet
export enum Method {
	GET,
	POST,
	PUT,
	DELETE,
	PATCH
}
/*
	data format:
	{
		name: string,
		desc: string,
		method: string (GET|POST|PUT|DELETE|PATCH)
		url: string
		base: string
		params: array
		data: array
		action: string (QUERY|JS|SQL)
		file: path (if action == CODE)
		query: {
			entity: string
			id: string (queryId)
		}
	}
*/

export class Endpoint {
	name: string;
	method: string;
	url: string;
	fromAddon?: IEndpoint['fromAddon'];

	entity: Entity;
	params: Array<IParam>;
	data: Array<IParam>;
	permissions: Array<string>;

	controller: string;
	action: string;

	query: Query;
	queryStr: string;

	static controllers: {[name: string]: Controller} = {};

	static resetControllers(): void {
		Endpoint.controllers = {};
	}

	constructor(private app: App, endpointConfig: IEndpoint) {
		this.method = (endpointConfig.method && endpointConfig.method.toLowerCase()) || 'get';

		// this.name = endpointConfig.name
		// this.desc = endpointConfig.desc
		this.url = endpointConfig.url;
		this.fromAddon = endpointConfig.fromAddon;

		this.params = [];
		this.data = [];
		this.permissions = endpointConfig.permissions || [];

		/*if (typeof endpointConfig.query == 'function') {
			this.params = endpointConfig.params || []
			this.data = endpointConfig.data || []
			this.query = endpointConfig.query
		}*/

		if (endpointConfig.controller) {
			this.controller = endpointConfig.controller;
			this.action = endpointConfig.action;

			const basePath = this.fromAddon ? this.fromAddon.path : this.app.path;

			const ctrl = this._getController();
			ctrl.load(basePath, this.controller);
			if ( ! ctrl.ctrlClass.prototype[this.action]) {
				throw new MateriaError(`Could not find method ${this.action} in controller ${this.controller}.ctrl.js`);
			}
			this._buildParams(endpointConfig.params);
		} else {
			let entity_name;
			if (typeof endpointConfig.query.entity == 'string') {
				entity_name = endpointConfig.query.entity;
			}

			this.entity = this.app.entities.get(entity_name);

			if ( ! this.entity) {
				throw new MateriaError('Could not find entity ' + entity_name);
			}

			this.query = this.entity.getQuery(endpointConfig.query.id);

			if ( ! this.query ) {
				throw new MateriaError('Could not find query "' + endpointConfig.query.id + '" of entity ' + this.entity.name);
{'fraction_non_alphanumeric': 0.14189189189189189, 'fraction_numerical': 0.0, 'mean_word_length': 3.7836624775583485, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '4085612', 'n_tokens_mistral': 1863, 'n_tokens_neox': 1681, 'n_words': 407}