api/services/HateoasService.js
/**
       * Private method creates a HATEOAS Response
       * Once the promise has been resolved, the HATEOAS response is
       * constructed from the links object.
       */
Private method creates a HATEOAS Response
Once the promise has been resolved, the HATEOAS response is
constructed from the links object.
Private method creates a HATEOAS Response Once the promise has been resolved the HATEOAS response is constructed from the links object .
function makeResponse(state) {
        var modelName = req.options.model || req.options.controller;
        var response = {
          version: HATEOAS_VERSION,
          href: HateoasService.getSelfLink(modelName),
          referrer: sails.config.appUrl + address.pathname,
          items: dataToJson(data),
          template: {
            rel: modelName
          }
        };
        if (_.isFunction(sails.models[modelName].getQueryLinks)) {
          response.queries = sails.models[modelName].getQueryLinks(req.user);
        }
        if (options && _.has(options, 'links')) {
          response.links = options.links;
          _.each(response.links, function (link) {
            if (link.rel === modelName) {
              link.isActive = true;
            }
          });
        }
        if (state) {
          _.each(state.links, addBaseUrl);
          _.each(state.queries, addBaseUrl);
          response = _.merge(response, Utils.Model.removeSystemFields(state));
        }
        if (!_.has(response.template, 'data')) {
          var startingModel = !_.has(options, 'templateOverride') ? modelName: options.templateOverride;
          response.template.rel = startingModel;
          client.hget("templates", startingModel, function (err, cachedTemplate) {
            if (cachedTemplate) {
              response.template = _.merge(response.template, JSON.parse(cachedTemplate));
            } else {
              var computedTemplate = self.makeTemplate(startingModel, []);
              client.hset("templates", startingModel, JSON.stringify(computedTemplate));
              client.expire("templates", 28800);
              response.template = _.merge(response.template, computedTemplate);
            }
            return response;
          });
        }
        return response;
      }
function makeResponse ( state ) { var modelName = req . options . model || req . options . controller ; var response = { version : HATEOAS_VERSION , href : HateoasService . getSelfLink ( modelName ) , referrer : sails . config . appUrl + address . pathname , items : dataToJson ( data ) , template : { rel : modelName } } ; if ( _ . isFunction ( sails . models [ modelName ] . getQueryLinks ) ) { response . queries = sails . models [ modelName ] . getQueryLinks ( req . user ) ; } if ( options && _ . has ( options , 'links' ) ) { response . links = options . links ; _ . each ( response . links , function ( link ) { if ( link . rel === modelName ) { link . isActive = true ; } } ) ; } if ( state ) { _ . each ( state . links , addBaseUrl ) ; _ . each ( state . queries , addBaseUrl ) ; response = _ . merge ( response , Utils . Model . removeSystemFields ( state ) ) ; } if ( ! _ . has ( response . template , 'data' ) ) { var startingModel = ! _ . has ( options , 'templateOverride' ) ? modelName : options . templateOverride ; response . template . rel = startingModel ; client . hget ( "templates" , startingModel , function ( err , cachedTemplate ) { if ( cachedTemplate ) { response . template = _ . merge ( response . template , JSON . parse ( cachedTemplate ) ) ; } else { var computedTemplate = self . makeTemplate ( startingModel , [ ] ) ; client . hset ( "templates" , startingModel , JSON . stringify ( computedTemplate ) ) ; client . expire ( "templates" , 28800 ) ; response . template = _ . merge ( response . template , computedTemplate ) ; } return response ; } ) ; } return response ; }
Private method creates a HATEOAS Response
 Once the promise has been resolved, the HATEOAS response is
 constructed from the links object.
Private method creates a HATEOAS Response Once the promise has been resolved the HATEOAS response is constructed from the links object .
// if getQueryLinks is defined as a Model function, set in response // when options.links is passed to res.ok, pass onward to HateoasService.create under req.links // if templateOverride was passed along with res.ok, use given template instead of req.options.model