1

I'm working on a sails hook (project hook). I want to make it configurable and need a way to provide default values.

I couldn't find information on the sails hooks documentation.

Here is an example hook:

// api/hooks/customHook.js
module.exports = function customHook(sails) {
  return {
    configure: function () {
      console.log("Configure: ");
      console.log(sails.config[this.configKey]);
    },

    initialize: function (cb) {
      console.log("Initialize: ");
      console.log(sails.config[this.configKey]);
    },
  };
};

When I lift my app, without having created the config/customhook.js file, I get the following output:

Configure:
undefined
Initialize:
undefined


I need a way to define a default value for a configurable value (let's name it configurableValue). Is there anything in sails to help us out or do we have to do it "manually", which can be a pain if we have nested configuration keys:

// api/hooks/customHook.js
module.exports = function customHook(sails) {
  return {
    configure: function () {
      if (typeof sails.config[this.configKey] == "undefined") {
        sails.config[this.configKey] = {};
      }

      if (typeof sails.config[this.configKey].configurableValue == "undefined") {
        sails.config[this.configKey].configurableValue = "defaultValue";
      }
    },

    initialize: function (cb) {
      console.log("Initialize: ");
      console.log(sails.config[this.configKey]);
    },
  };
};

P.S.: I know there are many NPM modules out there that could help me, including lodash, but I'm looking a the sails-recommended way to do it.

nstCactus
  • 5,141
  • 2
  • 30
  • 41

0 Answers0