Example #1
0
ExpressRequest.prototype._formatArgs = function (args) {
  var url  = args[0];
  var opts = args[1];
  var cb   = args[2];

  if (isFunction(url)) {
    cb = url;
    opts = null;
    url = null;
  }
  else if (isObject(url)) {
    cb = opts;
    opts = url;
    url = null;
  }
  else if (isFunction(opts)) {
    cb = opts;
    opts = null;
  }
  else {}

  opts = extend(opts || {}, this.defaultOpts);
  opts.url = url || opts.url || opts.uri;

  return {
    opts: opts,
    cb: cb
  };
};
primus.graphql = function (query, vars, files, operationName, cb) {
  debug('graphql', query, vars)
  if (isFunction(vars)) {
    cb = vars
    vars = undefined
    files = undefined
  }
  if (isFunction(files)) {
    cb = files
    files = undefined
  }
  if (isFunction(operationName)) {
    cb = operationName
    operationName = undefined
  }
  // parse query operation
  var end = Math.min.apply(Math, [query.indexOf(' '), query.indexOf('{')].filter(notEquals(-1)))
  var queryOperation = query.slice(0, end)
  // create payload
  var payload = {}
  payload.id = uuid.v4()
  payload.query = query
  payload.variables = vars
  payload.operationName = operationName
  if (files) {
    payload.files = files
  }
  // Setup data handler
  if (!~primus.listeners('data').indexOf(handleData)) {
    primus.on('data', handleData)
  }
  // Make request
  var data = {}
  data[key] = payload
  if (queryOperation === 'subscription') {
    // subscription returns an observable
    assert(!cb, 'cannot use callbacks for subscriptions (returns an observable)')
    return this._observeGraphQL(data)
  } else {
    // query, mutation, or malformatted
    var promise
    var err
    debug('write', data)
    var writeSuccess = primus.write(data)
    if (!writeSuccess) {
      // Request failed
      err = new Error('primus-graphql: write failed')
      return maybe(cb, Promise.reject(err))
    }
    // Response promise
    promise = new Promise(function (resolve, reject) {
      resEE.once(payload.id, resolve)
    })
    return maybe(cb, promise)
  }
}
Example #3
0
 async.eachSeries(cancelTags, _this._channel.cancel.bind(_this._channel), function () {
   cancelTags.forEach(function (cancelTag) {
     delete _this._consumerTags[cancelTag];
   });
   if (isFunction(cb)) {
     cb.apply(_this, arguments);
   }
 });
Example #4
0
Group.prototype.createProject = function (opts, cb) {
  if (isFunction (opts)) {
    cb = opts;
    opts = null;
  }
  opts = opts || {};
  opts.json = opts.json || {};
  if (!this.id()) {
    throw new Error('Owner id is required');
  }
  opts.json.owner = this.id();
  return User.prototype.createProject.call(this, opts, cb);
};
Example #5
0
      .then(() => {
        if (!Util.canUseIntercom()) { return }

        if (!this.client) {
          throw new Error(`Base._wrap: invalid ${this.name} client`)
        }

        const clientMethod = this.client[method]
        if (!isFunction(clientMethod)) {
          throw new Error(
            `Base._wrap: ${this.name} client has no method '${method}'`
          )
        }

        return clientMethod.apply(
          this.client,
          // NOTE: The first argument is the method name, so we skip it here
          Array.prototype.slice.call(arguments, 1)
        )
      })
Example #6
0
function createTest (fn) {
  assert(isFunction(fn), '"fn" should be a function')
  return new TestTerm(fn)
}
Example #7
0
function deepMatch (actual, validator, _refs, _varIdMap) {
  debug('deepMatch\n %o', arguments)
  // assert(actual, '"actual" is required')
  // assert(validator, '"validator" is required')
  actual = isFunction(actual && actual.build) ? actual.build() : actual
  validator = isFunction(validator && validator.build) ? validator.build() : validator
  if (validator && validator.fn && validator.__is_rv_test_term) {
    debug('validator opts\n %o\n %o', validator, actual)
    validator = TestTerm.prototype.build.call(validator)
    debug('validator opts build\n %o\n %o', validator, actual)
  }
  _refs = _refs || {}
  _varIdMap = _varIdMap || {}

  if (isFunction(validator)) {
    debug('validator is function\n %o\n %o', actual, _refs)
    return new Promise(function (resolve, reject) {
      if (!validator(actual, _refs)) {
        reject(new ValidationError('value did not pass test'))
        return
      }
      resolve(true)
    })
  } else if (Array.isArray(validator)) {
    var termId = actual[0]
    var termArgs = actual[1]
    var termOpts = actual[2]
    var validatorTermId = validator[0]
    var validatorTermArgs = validator[1]
    var validatorTermOpts = validator[2]
    debug('validator is array\n %o\n %o\n %o\n %o\n %o\n %o',
      termId,
      termArgs,
      termOpts,
      validatorTermId,
      validatorTermArgs,
      validatorTermOpts)

    if (termId !== validatorTermId) {
      debug('term ids mismatch %o %o', termId, validatorTermId)
      return Promise.reject(new ValidationError('term ids mismatch'))
    }

    return deepMatch(termOpts || {}, validatorTermOpts || {}, _refs, _varIdMap)
      .catch(function (err) {
        debug('term opts mismatch\n %o\n %o', termOpts, validatorTermOpts)
        err instanceof ValidationError
          ? err.message = 'term opts mismatch: ' + err.message
          : err
        throw err
      })
      .then(function () {
        if (termTypeIs(validatorTermId, 'VAR')) {
          validatorTermArgs = validatorTermArgs.map(function (id) {
            debug('_varIdMap cache\n %o', _varIdMap)
            return _varIdMap[id]
          })
          debug('term is VAR\n %o\n %o', termArgs, validatorTermArgs)
          var argsMatch = deepEqual(termArgs, validatorTermArgs, { strict: true })
          return boolToPromise(argsMatch, 'term args mismatch')
        } else if (termTypeIs(validatorTermId, 'FUNC')) {
          var termVarIds = termArgs[0][1]
          var validatorTermVarIds = validatorTermArgs[0][1]
          debug('term is FUNC\n %o\n %o', termVarIds, validatorTermVarIds)
          // Commented out for now: arg length may not indicate a diff query
          // if (termVarIds.length !== validatorTermVarIds.length) {
          //   return false
          // }
          validatorTermVarIds.forEach(function (varId, i) {
            debug('_varIdMap val\n %o\n %o', varId, termVarIds[i])
            _varIdMap[varId] = termVarIds[i]
          })
          termArgs = termArgs.slice(1)
          validatorTermArgs = validatorTermArgs.slice(1)
          debug('FUNC args\n %o\n %o', termArgs, validatorTermArgs)
        }
        // make sure all args
        var funcArgsMatch = termArgs.map(function (arg, i) {
          debug('FUNC map arg\n %o\n %o', arg, validatorTermArgs[i])
          return deepMatch(arg, validatorTermArgs[i], _refs, _varIdMap)
        })
        return Promise.all(funcArgsMatch).catch(function (err) {
          err instanceof ValidationError
            ? err.message = 'func term args mismatch: ' + err.message
            : err
          throw err
        }).then(function () {
          return true
        })
      })
  } else if (isObject(validator)) {
    debug('validator is object\n %o\n %o', actual, validator)
    if (!isObject(actual) || Object.keys(validator).length !== Object.keys(actual).length) {
      return Promise.reject(new ValidationError('object mismatch'))
    }
    var objsMatch = Object.keys(validator).map(function (key) {
      var valItem = validator[key]
      var actualItem = actual[key]
      return deepMatch(actualItem, valItem, _refs, _varIdMap)
    })
    return Promise.all(objsMatch).catch(function (err) {
      err instanceof ValidationError
        ? err.message = 'object mismatch: ' + err.message
        : err
      throw err
    }).then(function () {
      return true
    })
  } else {
    return boolToPromise(actual === validator, 'values mismatch')
  }
}