{% extends "includes/base-layout.twig" %}

{% block title %}Configuration | Merest{% endblock %}
{% block content %}
<h1 class="page-title">API Configuration</h1>
<section>
    <article>
<a name="methods"></a>
<h3>Model methods</h3>
<p>As pointed <a href="conf-router.html#expose">above</a> the router level has two parameters that allow to call model methods through the api.
These parameters are:</p>
<ul>
<li>expose: <code>Object</code> - configuration for end-points that expose the instance methods defined on its schema</li>
<li>exposeStatic: <code>Object</code> - configuration for end-points that expose static model methods defined on its schema.</li>
</ul>
<p>The keys of these paramters are the names of the Model methods (instance or static).
The each value of the key could be on of types: <code>Boolean</code>|<code>String</code>|<code>Object</code></p>
<pre class="prettyprint source lang-javascript"><code>{
  expose: {
    methodName: `Boolean` | `String` | `Object`,
    methodName: `Boolean` | `String` | `Object`
    ...
  },
  exposeStatic: {
    methodName: `String` | `Object`,
    ...
  }
}</code></pre>

<h4>In case of <code>Boolean</code></h4>
<p>the value means if method-end-point is allowed (<code>true</code>) or is disabled (<code>false</code>).
<p>There is a special key <code>"*"</code> that allows to enable or to disable all methods together.</p>
<pre class="prettyprint source lang-javascript"><code>{
  expose: {'*': true} // all instance method will be exposed
}</code></pre>

<h4>In case of <code>String</code></h4>
<p><strong>merest</strong> mounts controller that calls correspondent method
to the HTTP-method specified by value (if value is on of Express-supported HTTP-methods).</p>
<p>Otherwise (the value is not a HTTP-method) <strong>merest</strong> mounts appropriate controller
to POST (or other method specified directly) and adds created method-end-point
to list with value as description.</p>

<h4>In case of <code>Object</code></h4>
<p><strong>merest</strong> uses the value to configure method-end-point with the following parameters:</p>
<ul>
<li><strong>method</strong>:<code>String</code> - the HTTP method to mount method-end-point (if omitted the method-controller will be mount on the POST)</li>
<li><strong>path</strong>: <code>String</code> - the <code>additional-path</code> to mount method-controller</li>
<li><strong>exec</strong>:<code>Function</code> - the Function that will be called instead of Model instance/static
method with context of Document or the Model. You could to call any Model method from this
function using <code>this</code>.</li>
<li><strong>middlewares</strong>: <code>Function|Array</code> - middleware function or array of such functions. The middleware(s) will be mounted to the method-end-point route as usual express middleware</li>
<li><strong>title</strong>: <code>String</code> - the description of the end-point</li>
</ul>

<h4>Signature of Model method that can be exposed</h4>
<p>The controller that calls certain Model method passes to the one two parameters:</p>
<ul>
  <li><strong>params</strong>: <code>Object</code> - parameters sent from client (in the Query String or in Request
    Body dependent on the HTTP method used to mount method-end-point)
  </li>
  <li><strong>callback</strong>: <code>Function</code> - Function that should be called after method processed.</li>
</ul>
For example the instance method could be defined with following interface:
<pre class="prettyprint source lang-javascript"><code>/**
 * @param  {Object} params
 * @param  {Function} callback
 * @return {undefined}
 */
modelSchema.methods.methodName = function (params, callback) {
  // this -- referencies the document instance
}
</code></pre>
and the static method with the same:
<pre class="prettyprint source lang-javascript"><code>/**
 * @param  {Object} params
 * @param  {Function} callback
 * @return {undefined}
 */
modelSchema.statics.methodName = function (params, callback) {
  // this -- referencies the model
}
</code></pre>
<p><strong>Signature of callback-function</strong>:</p>
<pre class="prettyprint source lang-javascript"><code>/**
 * @param  {Error} err                        exception raised in the method call
 * @param  {Object|Array|String} dataToReturn The data to be return in Response Body
 */
function callback(err, dataToReturn) {
  ...
}</code></pre>
<p>For example:</p>
<pre class="prettyprint source lang-javascript"><code>VectorSchema.methods.reverse = function(params, callback) {
  this.x = -this.x;
  this.y = -this.y;
  return this.save(callback);
}</code></pre><p>or in case of <code>statics</code>:</p>
<pre class="prettyprint source lang-javascript"><code>VectorSchema.statics.reverse = function(params, callback) {
  var self = this;
  self.update(params, {$mul: {x:-1, y:-1} }, {multi: true}, function (err) {
    if (err) return done(err);
    return self.find(callback);
  });
}</code></pre><p>Exposing the methods</p>
<pre class="prettyprint source lang-javascript"><code>var api = new merest.ModelAPIExpress();
api.expose(models.Vector, { // Model-routes level
  fields: 'x y',
  expose: { reverse: 'get' },
  exposeStatic: { reverse: 'get' }
});</code></pre>

<p>Getting the vector details:</p>
<pre class="prettyprint source lang-shell"><code>curl -g http://localhost:1337/api/v1/vectors/573f19d35b54089f3993605f/</code></pre>
<p>Output:</p>
<pre class="prettyprint source lang-shell"><code>{"_id": "573f19d35b54089f3993605f", "x": 1, "y": 2}</code></pre>
<p>Calling method-end-point:</p>
<pre class="prettyprint source lang-shell"><code>curl -g http://localhost:1337/api/v1/vectors/573f19d35b54089f3993605f/reverse</code></pre>
<p>Output:</p>
<pre class="prettyprint source lang-shell"><code>{"__v": 1, "_id": "573f19d35b54089f3993605f", "x": -1, "y": -2}</code></pre>
<p>Getting the vector details again:</p>
<pre class="prettyprint source lang-shell"><code>curl -g http://localhost:1337/api/v1/vectors/573f19d35b54089f3993605f/</code></pre>
<p>Output:</p>
<pre class="prettyprint source lang-shell"><code>{"_id": "573f19d35b54089f3993605f", "x": -1, "y": -2}</code></pre>

<p>Calling the static method:</p>
<pre class="prettyprint source lang-shell"><code>curl -g http://localhost:1337/api/v1/vectors/reverse</code></pre>
<p>Output:</p>
<pre class="prettyprint source lang-shell"><code>[
  {"_id": "...", "x": -1, "y": -2},
  {"_id": "...", "x": 0, "y": -3}
]</code></pre>

<p>And after that calling againg with parameters</p>
<pre class="prettyprint source lang-shell"><code>curl -g http://localhost:1337/api/v1/vectors/reverse?x=0</code></pre>
<p>Output:</p>
<pre class="prettyprint source lang-shell"><code>[
  {"_id": "...", "x": 0, "y": 3}
]</code></pre>

<p>Lookout the <strong>merest</strong> doesn't clean the response from exposed methods.
So, you should do it by your own code.</p>

<br class="clear">
<hr>
<a href='installation.html'>Next (Installation) ></a>
</article>
</section>
{% endblock %}
