urlOptionsBuilder.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * Classe permettant de construire les options d'une URL
  3. */
  4. import TypesTesting from "~/services/utils/typesTesting";
  5. import {ListArgs, UrlArgs} from "~/types/interfaces";
  6. import {QUERY_TYPE} from "~/types/enums";
  7. class UrlOptionsBuilder {
  8. /**
  9. * Main méthode qui appellera les méthode privées correspondantes (getUrlOptionsImage, getUrlOptionsLists)
  10. * @param {UrlArgs} args
  11. * @return {string}
  12. */
  13. public static build(args: UrlArgs): Array<string> {
  14. let options: Array<string> = []
  15. if (args.type === QUERY_TYPE.IMAGE){
  16. options = [...options, this.getUrlOptionsImage()]
  17. }
  18. if (TypesTesting.isDataProviderArgs(args) && args.listArgs !== undefined) {
  19. options = [...options, ...this.getUrlOptionsLists(args.listArgs)]
  20. }
  21. return options
  22. }
  23. /**
  24. * Une image doit toujours avoir le time en options pour éviter les problème de cache
  25. * @private
  26. */
  27. private static getUrlOptionsImage(): string {
  28. return new Date().getTime().toString()
  29. }
  30. /**
  31. * Fonction renvoyant le tableau d'options d'une list
  32. * @param listArgs
  33. * @private
  34. */
  35. private static getUrlOptionsLists(listArgs: ListArgs): Array<string> {
  36. const options: Array<string> = []
  37. if (listArgs.itemsPerPage) {
  38. options.push(`itemsPerPage=${listArgs.itemsPerPage}`)
  39. }
  40. if (listArgs.page) {
  41. options.push(`page=${listArgs.page}`)
  42. }
  43. if (listArgs.filters) {
  44. for(const filter of listArgs.filters){
  45. options.push(`${filter.key}=${filter.value}`)
  46. }
  47. }
  48. return options
  49. }
  50. }
  51. export default UrlOptionsBuilder