task.mjs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import fs from "node:fs";
  2. import FormData from "form-data";
  3. import Client from "./client.mjs";
  4. import logger from "./logger.mjs";
  5. export default (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(
  32. "post",
  33. options.path,
  34. options.returnOnError || false,
  35. options.data,
  36. );
  37. },
  38. /**
  39. * @param {object} options
  40. * @param {string} options.token JWT
  41. * @param {string} options.path API path
  42. * @param {object} options.files
  43. * @param {bool} [options.returnOnError] If true, will return instead of throwing errors
  44. * @returns {string}
  45. */
  46. backendApiPostFiles: (options) => {
  47. const api = new Client(config);
  48. api.setToken(options.token);
  49. const form = new FormData();
  50. for (const [key, value] of Object.entries(options.files)) {
  51. form.append(
  52. key,
  53. fs.createReadStream(`${config.fixturesFolder}/${value}`),
  54. );
  55. }
  56. return api.postForm(options.path, form, options.returnOnError || false);
  57. },
  58. /**
  59. * @param {object} options
  60. * @param {string} options.token JWT
  61. * @param {string} options.path API path
  62. * @param {object} options.data
  63. * @param {bool} [options.returnOnError] If true, will return instead of throwing errors
  64. * @returns {string}
  65. */
  66. backendApiPut: (options) => {
  67. const api = new Client(config);
  68. api.setToken(options.token);
  69. return api.request(
  70. "put",
  71. options.path,
  72. options.returnOnError || false,
  73. options.data,
  74. );
  75. },
  76. /**
  77. * @param {object} options
  78. * @param {string} options.token JWT
  79. * @param {string} options.path API path
  80. * @param {bool} [options.returnOnError] If true, will return instead of throwing errors
  81. * @returns {string}
  82. */
  83. backendApiDelete: (options) => {
  84. const api = new Client(config);
  85. api.setToken(options.token);
  86. return api.request(
  87. "delete",
  88. options.path,
  89. options.returnOnError || false,
  90. );
  91. },
  92. };
  93. };