task.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. const fs = require('fs');
  2. const FormData = require('form-data');
  3. const logger = require('./logger');
  4. const Client = require('./client');
  5. module.exports = function (config) {
  6. logger('Client Ready using', config.baseUrl);
  7. return {
  8. /**
  9. * @param {object} options
  10. * @param {string} options.path API path
  11. * @param {string} [options.token] JWT
  12. * @param {bool} [options.returnOnError] If true, will return instead of throwing errors
  13. * @returns {string}
  14. */
  15. backendApiGet: (options) => {
  16. const api = new Client(config);
  17. api.setToken(options.token);
  18. return api.request('get', options.path, options.returnOnError || false);
  19. },
  20. /**
  21. * @param {object} options
  22. * @param {string} options.token JWT
  23. * @param {string} options.path API path
  24. * @param {object} options.data
  25. * @param {bool} [options.returnOnError] If true, will return instead of throwing errors
  26. * @returns {string}
  27. */
  28. backendApiPost: (options) => {
  29. const api = new Client(config);
  30. api.setToken(options.token);
  31. return api.request('post', options.path, options.returnOnError || false, options.data);
  32. },
  33. /**
  34. * @param {object} options
  35. * @param {string} options.token JWT
  36. * @param {string} options.path API path
  37. * @param {object} options.files
  38. * @param {bool} [options.returnOnError] If true, will return instead of throwing errors
  39. * @returns {string}
  40. */
  41. backendApiPostFiles: (options) => {
  42. const api = new Client(config);
  43. api.setToken(options.token);
  44. const form = new FormData();
  45. for (let [key, value] of Object.entries(options.files)) {
  46. form.append(key, fs.createReadStream(config.fixturesFolder + '/' + value));
  47. }
  48. return api.postForm(options.path, form, options.returnOnError || false);
  49. },
  50. /**
  51. * @param {object} options
  52. * @param {string} options.token JWT
  53. * @param {string} options.path API path
  54. * @param {object} options.data
  55. * @param {bool} [options.returnOnError] If true, will return instead of throwing errors
  56. * @returns {string}
  57. */
  58. backendApiPut: (options) => {
  59. const api = new Client(config);
  60. api.setToken(options.token);
  61. return api.request('put', options.path, options.returnOnError || false, options.data);
  62. },
  63. /**
  64. * @param {object} options
  65. * @param {string} options.token JWT
  66. * @param {string} options.path API path
  67. * @param {bool} [options.returnOnError] If true, will return instead of throwing errors
  68. * @returns {string}
  69. */
  70. backendApiDelete: (options) => {
  71. const api = new Client(config);
  72. api.setToken(options.token);
  73. return api.request('delete', options.path, options.returnOnError || false);
  74. }
  75. };
  76. };