Exemplo n.º 1
0
 /**
  * Set a custom validate function. It will be called before
  * the move operation
  *
  * @method validate
  *
  * @param  {Function} callback
  *
  * @chainable
  */
 validate (callback) {
   if (typeof (callback) !== 'function') {
     throw GE.InvalidArgumentException.invalidParameter('file.validate expects a function', callback)
   }
   this._validateFn = callback.bind(this)
   return this
 }
Exemplo n.º 2
0
  /**
   * Registers an array of middleware for `server` or `global`
   * type.
   *
   * @method _registerMiddleware
   *
   * @param  {String}            type
   * @param  {Array}             middleware
   * @param  {String}            errorMessage
   *
   * @return {void}
   *
   * @private
   */
  _registerMiddleware (type, middleware, errorMessage) {
    if (!Array.isArray(middleware)) {
      throw GE.InvalidArgumentException.invalidParameter(errorMessage, middleware)
    }

    const store = this._middleware[type]

    middleware.forEach((item) => {
      /**
       * Throw exception if middleware is not a string or a function
       */
      this._ensureRightMiddlewareType(item)

      /**
       * Converting middleware identifier { function, string } to an object, which can be
       * used directly to execute middleware.
       */
      const packet = this._middlewareIdentifierToPacket(item)

      /**
       * Show warning for duplicate middleware
       */
      if (store.find((i) => i.namespace === packet.namespace)) {
        this._warnFn(duplicateMiddlewareWarning(type, item))
        return
      }

      store.push(packet)
    })
  }
Exemplo n.º 3
0
 /**
  * Register a new blueprint with model or table name
  * and callback to be called to return the fake data
  * for model instance of table insert query.
  *
  * @method blueprint
  *
  * @param  {String}   name
  * @param  {Function} callback
  *
  * @chainable
  *
  * @example
  * ```js
  * Factory.blueprint('App/Model/User', (fake) => {
  *   return {
  *     username: fake.username(),
  *     password: async () => {
  *       return await Hash.make('secret')
  *     }
  *   }
  * })
  * ```
  */
 blueprint (name, callback) {
   if (typeof (callback) !== 'function') {
     throw GE
       .InvalidArgumentException
       .invalidParameter('Factory.blueprint expects a callback as 2nd parameter', callback)
   }
   this._blueprints.push({ name, callback })
   return this
 }
Exemplo n.º 4
0
 /**
  * Setting providers that will later be registered
  * and booted.
  *
  * @method providers
  *
  * @param  {Array} arrayOfProviders
  *
  * @chainable
  */
 providers (arrayOfProviders) {
   if (arrayOfProviders instanceof Array === false) {
     throw GE
       .InvalidArgumentException
       .invalidParameter('register expects an array of providers to be registered', arrayOfProviders)
   }
   this._providers = this._getProvidersInstance(arrayOfProviders)
   return this
 }
Exemplo n.º 5
0
  /**
   * Creates an array of model instances in parallel
   *
   * @method createMany
   *
   * @param  {Array}   arrayOfRelatedInstances
   * @param  {Object}  [trx]
   *
   * @return {Array}
   */
  async saveMany (arrayOfRelatedInstances, trx) {
    if (!Array.isArray(arrayOfRelatedInstances)) {
      throw GE
        .InvalidArgumentException
        .invalidParameter('hasMany.saveMany expects an array of related model instances', arrayOfRelatedInstances)
    }

    await this._persistParentIfRequired(trx)
    return Promise.all(arrayOfRelatedInstances.map((relatedInstance) => this.save(relatedInstance, trx)))
  }
Exemplo n.º 6
0
  /**
   * Creates an array of model instances in parallel
   *
   * @method createMany
   *
   * @param  {Array}   arrayOfPayload
   * @param  {Object}  [trx]
   *
   * @return {Array}
   */
  async createMany (arrayOfPayload, trx) {
    if (!Array.isArray(arrayOfPayload)) {
      throw GE
        .InvalidArgumentException
        .invalidParameter('hasMany.createMany expects an array of values', arrayOfPayload)
    }

    await this._persistParentIfRequired(trx)
    return Promise.all(arrayOfPayload.map((payload) => this.create(payload, trx)))
  }
Exemplo n.º 7
0
  /**
   * Flash data to the session
   *
   * @method flash
   *
   * @param  {Object} data
   *
   * @chainable
   */
  flash (data) {
    if (!_.isPlainObject(data)) {
      throw GE.InvalidArgumentException.invalidParameter('Flash data should be an object', data)
    }
    const flashMessage = this.get('__flash__', null)

    if (!flashMessage) {
      this.put('__flash__', data)
    } else {
      _.merge(flashMessage, data)
    }
    return this
  }
Exemplo n.º 8
0
  /**
   * Add a listener to file. It is important to attach a callback and
   * handle the processing of the file. Also only one listener can
   * be added at a given point of time, since 2 parties processing
   * a single file doesn't make much sense.
   *
   * @method file
   *
   * @param  {String}   name
   * @param  {Object}   options
   * @param  {Function} callback
   *
   * @chainable
   */
  file (name, options = {}, callback) {
    debug('attached callback for %s file', name)
    if (typeof (callback) !== 'function') {
      throw GE.InvalidArgumentException.invalidParameter('multipart.file expects callback to be a function', callback)
    }

    if (name === '*') {
      this._wildcard = { options, callback }
    } else {
      this._filesToAccess[name] = { options, callback }
    }

    return this
  }
Exemplo n.º 9
0
  /**
   * Extend authenticator by adding a new scheme or serializer.
   * You make use of this method via `Ioc.extend` interface
   *
   * @method extend
   *
   * @param  {String} key
   * @param  {Object} implementation
   * @param  {String} type
   *
   * @return {void}
   */
  extend (key, implementation, type) {
    if (type === 'scheme') {
      this._schemes[key] = implementation
      return
    }

    if (type === 'serializer') {
      this._serializers[key] = implementation
      return
    }

    throw GE
      .InvalidArgumentException
      .invalidParameter(`Auth.extend type must be a serializer or scheme, instead received ${type}`)
  }
Exemplo n.º 10
0
  constructor (middlewareFn, warnFn) {
    if (typeof (middlewareFn) !== 'string' || !middlewareFn) {
      throw GE
        .InvalidArgumentException
        .invoke('make sure to define the middleware fn. Report issue to package author', 500)
    }

    this._middleware = {
      global: [],
      server: [],
      named: {},
      handle: middlewareFn
    }

    this._warnFn = typeof (warnFn) === 'function' ? warnFn : function () {}
  }
Exemplo n.º 11
0
  /**
   * Register an object of named middleware
   *
   * @method registerNamed
   *
   * @param  {Object}      middleware
   *
   * @return {void}
   *
   * @throws {InvalidArgumentException} If middleware is not an object with key/value pair.
   *
   * @example
   * ```js
   * middleware.registerNamed({
   *   auth: 'Adonis/Middleware/Auth'
   * })
   * ```
   */
  registerNamed (middleware) {
    if (!_.isPlainObject(middleware)) {
      throw GE
        .InvalidArgumentException
        .invalidParameter('server.registerNamed accepts a key/value pair of middleware', middleware)
    }

    _.each(middleware, (item, name) => {
      /**
       * Throw exception if middleware is not a string or a function
       */
      this._ensureRightMiddlewareType(item)

      /**
       * Converting middleware identifier { function, string } to an object, which can be
       * used directly to execute middleware.
       */
      this._middleware.named[name] = this._middlewareIdentifierToPacket(item)
    })
  }
Exemplo n.º 12
0
 /**
  * Validates the handler to make sure it is a function
  * or a string, which is considered to be a reference
  * to the IoC container.
  *
  * @method _validateHandler
  *
  * @param  {Function|String}         handler
  *
  * @return {void}
  *
  * @private
  */
 _validateHandler (handler) {
   if (['string', 'function'].indexOf(typeof (handler)) === -1) {
     throw GE.InvalidArgumentException.invalidParameter('Cannot instantiate route without route handler', handler)
   }
 }
Exemplo n.º 13
0
 /**
  * Validates the group closure to make sure
  * it is a function
  *
  * @method _validateGroupClosure
  *
  * @param  {Function}            callback
  *
  * @return {void}
  *
  * @private
  */
 _validateGroupClosure (callback) {
   if (typeof (callback) !== 'function') {
     throw GE.InvalidArgumentException.invalidParamter('Route.group expects a callback', callback)
   }
 }
Exemplo n.º 14
0
 /**
  * Validates the resource controller to a valid
  * string. Existence of controller is validated
  * when the controller is resolved.
  *
  * @method _validateController
  *
  * @param  {String}            controller
  *
  * @return {void}
  *
  * @private
  */
 _validateController (controller) {
   if (typeof (controller) !== 'string') {
     throw GE.InvalidArgumentException.invalidParameter('Route.resource expects reference to a controller', controller)
   }
 }
Exemplo n.º 15
0
 /**
  * Validates the resource name to make sure it
  * is a valid string.
  *
  * @method _validateResourceName
  *
  * @param  {String}              resource
  *
  * @return {void}
  *
  * @private
  */
 _validateResourceName (resource) {
   if (typeof (resource) !== 'string') {
     throw GE.InvalidArgumentException.invalidParameter('Route.resource expects name to be a string', resource)
   }
 }
Exemplo n.º 16
0
 /**
  * Validate HTTP verbs to make sure it is an
  * array
  *
  * @method _validateVerbs
  *
  * @param  {Array}       verbs
  *
  * @return {void}
  *
  * @private
  */
 _validateVerbs (verbs) {
   if (!Array.isArray(verbs)) {
     throw GE.InvalidArgumentException.invalidParameter('New route expects HTTP verbs to be an array', verbs)
   }
 }
Exemplo n.º 17
0
 /**
  * Validates the route to make sure it is a
  * valid string
  *
  * @method _validateRoute
  *
  * @param  {String}       route
  *
  * @return {void}
  *
  * @private
  */
 _validateRoute (route) {
   if (typeof (route) !== 'string') {
     throw GE.InvalidArgumentException.invalidParameter('Cannot instantiate route without a valid url string', route)
   }
 }