repo_description: Parse Server for Node.js / Express
repo_topics: baas backend file-storage graphql graphql-api graphql-relay graphql-server hacktoberfest mbaas mongodb nodejs notifications object-storage parse-platform parse-server postgres relay rest-api
code: /**
   * @module SecurityCheck
   */
  
  import Utils from '../Utils';
  import { isFunction, isString } from 'lodash';
  
  /**
   * A security check.
   * @class
   */
  class Check {
    /**
     * Constructs a new security check.
     * @param {Object} params The parameters.
     * @param {String} params.title The title.
     * @param {String} params.warning The warning message if the check fails.
     * @param {String} params.solution The solution to fix the check.
     * @param {Promise} params.check The check as synchronous or asynchronous function.
     */
    constructor(params) {
      this._validateParams(params);
      const { title, warning, solution, check } = params;
  
      this.title = title;
      this.warning = warning;
      this.solution = solution;
      this.check = check;
  
      // Set default properties
      this._checkState = CheckState.none;
      this.error;
    }
  
    /**
     * Returns the current check state.
     * @return {CheckState} The check state.
     */
    checkState() {
      return this._checkState;
    }
  
    async run() {
      // Get check as synchronous or asynchronous function
      const check = Utils.isPromise(this.check) ? await this.check : this.check;
  
      // Run check
      try {
        check();
        this._checkState = CheckState.success;
      } catch (e) {
        this.stateFailError = e;
        this._checkState = CheckState.fail;
      }
    }
  
    /**
     * Validates the constructor parameters.
     * @param {Object} params The parameters to validate.
     */
    _validateParams(params) {
      Utils.validateParams(params, {
        group: { t: 'string', v: isString },
        title: { t: 'string', v: isString },
        warning: { t: 'string', v: isString },
        solution: { t: 'string', v: isString },
        check: { t: 'function', v: isFunction },
      });
    }
  }
  
  /**
   * The check state.
   */
  const CheckState = Object.freeze({
    none: 'none',
    fail: 'fail',
    success: 'success',
  });
  
  export default Check;
  module.exports = {
    Check,
    CheckState,
  };
  