// RPC reply serializer, that should heppen in the end,
// because we pass errors in serialized form too
//


'use strict';


////////////////////////////////////////////////////////////////////////////////


module.exports = function (N) {

  N.wire.after('responder:rpc', { priority: 90 }, function rpc_serialize(env) {

    //
    // Set Content-Type and charset (override existing)
    //

    env.headers['Content-Type'] = 'application/json; charset=UTF-8';

    //
    // Status is always ok - errors are embedded into payload
    //

    env.status = N.io.OK;

    //
    // HEAD requested - no need for real rendering
    //

    if ('HEAD' === env.origin.req.method) {
      env.body = null;
      return;
    }

    //
    // Replace body with serialized payload
    //

    env.body = JSON.stringify({
      version:  N.runtime.version,
      error:    env.err,
      response: env.err ? null : env.response
    });
  });
};

// RPC reply serializer, that should heppen in the end,
// because we pass errors in serialized form too
//


'use strict';


////////////////////////////////////////////////////////////////////////////////


module.exports = function (N) {

  N.wire.after('responder:rpc', { priority: 90 }, function rpc_serialize(env) {

    // Set Content-Type and charset (override existing)
    env.headers['Content-Type'] = 'application/json; charset=UTF-8';

    // Status is always ok - errors are embedded into payload
    env.status = N.io.OK;

    // Expose API path of the executed server method, e.g. 'forum.index'
    env.response.data.head.apiPath = env.method;

    // HEAD requested - no need for real rendering
    if ('HEAD' === env.origin.req.method) {
      env.body = null;
      return;
    }

    // Replace body with serialized payload
    env.body = JSON.stringify({
      version:  N.runtime.version,
      error:    env.err,
      response: env.err ? null : env.response
    });
  });
};

Fix pagination links on the RPC page navigation.