Home Reference Source Repository

src/models/EndpointConfig.js

/**
 * Object representing the configuration of a single endpoint.
 */
export class EndpointConfig {
	/**
	 * Creates a new instance of EndpointConfig.
	 * @param {string} path Path for the endpoint root.
	 * @param {Array<string|Function>} [methods=[]] Sets the methods available for use at the endpoint.
	 * @param {Array|Object} [state=[]] Sets the initial state of the endpoint data.
	 * @param {Array<EndpointConfig>} [nested] Contains the nested endpoints of the endpoint.
	 */
	constructor(path, methods = ["GET_ALL", "GET_BY_KEY", "POST", "PUT", "DELETE"], state = [], nested) {
		/**
		 * The root path for the endpoint.
		 * Note that all nested endpoint will be prefixed by their parents paths.
		 * @type {string}
		 */
		this.path = path;
		/**
		 * Available methods at the endpoint.
		 *
		 * For automatic generation the following keywords are available:
		 * - GET_ALL - Returns a list of all the resources in the endpoint state.
		 * - GET_BY_KEY - Returns a specified resource identified by the given {@link ServerConfig.identifierKey}.
		 * - GET_ONE - Currently only usable with `Object`-type state. See {@link EndpointConfig.state}.
		 * - POST - Creates a new resource at the given endpoint.
		 * - PUT - Updates a specified resource identified by the given {@link ServerConfig.identifierKey}.
		 * - DELETE - Deletes a specified resource identified by the given {@link ServerConfig.identifierKey}.
		 *
		 * You can also create a custom handler by passing a function that takes in the path and state as arguments.
		 *
		 * @example
		 * (path, state) => {
		 *   return {
		 *     path: `${path}/meta`,
		 *     method: 'GET',
		 *     handler: (request, reply) => reply({type: 'metaResponse', state})
		 *   }
		 * }
		 * @type {Array<string|Function>}
		 */
		this.methods = methods;
		/**
		 * The state object represents the current state of the endpoint data storage.
		 * If the type of the state is `Object` then the only available method to use is **GET_ONE**.
		 *
		 * @experimental The state management is still under consideration and might change greatly.
		 * @type {Array|Object}
		 */
		this.state = state;
		/** @type {Array<EndpointConfig>} */
		this.nested = nested;
	}
}

export default EndpointConfig;