task.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. const logger = require('./logger');
  2. const Client = require('./client');
  3. module.exports = function (config) {
  4. logger('Client Ready using', config.baseUrl);
  5. return {
  6. /**
  7. * @param {object} options
  8. * @param {string} options.path API path
  9. * @param {string} [options.token] JWT
  10. * @param {bool} [options.returnOnError] If true, will return instead of throwing errors
  11. * @returns {string}
  12. */
  13. backendApiGet: (options) => {
  14. const api = new Client(config);
  15. api.setToken(options.token);
  16. return api.get(options.path, options.returnOnError || false);
  17. },
  18. /**
  19. * @param {object} options
  20. * @param {string} options.token JWT
  21. * @param {string} options.path API path
  22. * @param {object} options.data
  23. * @param {bool} [options.returnOnError] If true, will return instead of throwing errors
  24. * @returns {string}
  25. */
  26. backendApiPost: (options) => {
  27. const api = new Client(config);
  28. api.setToken(options.token);
  29. return api.postJson(options.path, options.data, options.returnOnError || false);
  30. },
  31. /**
  32. * @param {object} options
  33. * @param {string} options.token JWT
  34. * @param {string} options.path API path
  35. * @param {object} options.data
  36. * @param {bool} [options.returnOnError] If true, will return instead of throwing errors
  37. * @returns {string}
  38. */
  39. backendApiPut: (options) => {
  40. const api = new Client(config);
  41. api.setToken(options.token);
  42. return api.putJson(options.path, options.data, options.returnOnError || false);
  43. },
  44. /**
  45. * @param {object} options
  46. * @param {string} options.token JWT
  47. * @param {string} options.path API path
  48. * @param {bool} [options.returnOnError] If true, will return instead of throwing errors
  49. * @returns {string}
  50. */
  51. backendApiDelete: (options) => {
  52. const api = new Client(config);
  53. api.setToken(options.token);
  54. return api.delete(options.path, options.returnOnError || false);
  55. }
  56. };
  57. };