You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.5 KiB

  1. 'use strict';
  2. var core = require('../'),
  3. isArray = require('lodash/isArray'),
  4. isFunction = require('lodash/isFunction'),
  5. isObjectLike = require('lodash/isObjectLike');
  6. module.exports = function (options) {
  7. var errorText = 'Please verify options'; // For better minification because this string is repeating
  8. if (!isObjectLike(options)) {
  9. throw new TypeError(errorText);
  10. }
  11. if (!isFunction(options.client)) {
  12. throw new TypeError(errorText + '.client');
  13. }
  14. if (!isArray(options.expose) || options.expose.length === 0) {
  15. throw new TypeError(errorText + '.expose');
  16. }
  17. var thenExposed = false;
  18. for ( var i = 0; i < options.expose.length; i+=1 ) {
  19. if (options.expose[i] === 'then') {
  20. thenExposed = true;
  21. break;
  22. }
  23. }
  24. if (!thenExposed) {
  25. throw new Error('Please expose "then"');
  26. }
  27. var plumbing = core({
  28. PromiseImpl: options.PromiseImpl,
  29. constructorMixin: options.constructorMixin
  30. });
  31. return function (requestOptions) {
  32. var self = {};
  33. plumbing.init.call(self, requestOptions);
  34. var request = options.client(requestOptions);
  35. for ( var k = 0; k < options.expose.length; k+=1 ) {
  36. var method = options.expose[k];
  37. plumbing[ method === 'promise' ? 'exposePromise' : 'exposePromiseMethod' ](
  38. request,
  39. self,
  40. '_rp_promise',
  41. method
  42. );
  43. }
  44. return request;
  45. };
  46. };