urlBuilder.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import {FileArgs, ImageArgs, ListArgs, UrlArgs} from '~/types/interfaces'
  2. import {QUERY_TYPE} from '~/types/enums'
  3. import TypesTesting from "~/services/utils/typesTesting";
  4. /**
  5. * Classe permettant de construire une URL pour l'interrogation d'une API externe
  6. */
  7. class UrlBuilder {
  8. static ROOT = '/api/'
  9. /**
  10. * Concatenate a base url and a tail
  11. * @param base
  12. * @param tails
  13. * @private
  14. */
  15. public static join (base: string, ...tails: string[]): string {
  16. let url = base
  17. tails.forEach((tail: string) => {
  18. url = url.replace(/^|\/$/g, '') + '/' + tail.replace(/^\/?|$/g, '')
  19. })
  20. return url
  21. }
  22. /**
  23. * Prepend the 'https://' part if neither 'http://' of 'https://' is present, else: does nothing
  24. *
  25. * @param url
  26. */
  27. public static prependHttps (url: string): string {
  28. if (!url.match(/^https?:\/\/.*/)) {
  29. url = 'https://' + url;
  30. }
  31. return url;
  32. }
  33. /**
  34. * Construction d'une URL "image" qui ira concaténer l'id de l'image à downloeader passé en paramètre avec la ROOT Url définie
  35. * @param {ImageArgs} imgArgs
  36. * @param {string} baseUrl
  37. * @return {string}
  38. */
  39. private static getImageUrl (imgArgs: ImageArgs, baseUrl: string = ''): string {
  40. const downloadUrl = `files/${imgArgs.id}/download/${imgArgs.height}x${imgArgs.width}`
  41. return UrlBuilder.join(baseUrl, UrlBuilder.ROOT, downloadUrl)
  42. }
  43. /**
  44. * Construction d'une URL qui ira concaténer la base URL avec le Root et l'uri files
  45. * @param args
  46. * @param baseUrl
  47. * @private
  48. */
  49. private static getFileUrl (args: FileArgs, baseUrl: string = ''): string {
  50. return UrlBuilder.join(baseUrl, UrlBuilder.ROOT, `download/${args.fileId}`)
  51. }
  52. /**
  53. * Main méthode qui appellera les méthode privées correspondantes (getUrlOptionsImage, getUrlOptionsLists)
  54. * @param {UrlArgs} args
  55. * @return {string}
  56. */
  57. public static buildOptions(args: UrlArgs): Array<string> {
  58. let options: Array<string> = []
  59. if (args.type === QUERY_TYPE.IMAGE){
  60. options = [...options, this.getUrlOptionsImage()]
  61. }
  62. if (TypesTesting.isDataProviderArgs(args) && args.listArgs !== undefined) {
  63. options = [...options, ...this.getUrlOptionsLists(args.listArgs)]
  64. }
  65. return options
  66. }
  67. /**
  68. * Une image doit toujours avoir le time en options pour éviter les problème de cache
  69. * @private
  70. */
  71. private static getUrlOptionsImage(): string {
  72. return new Date().getTime().toString()
  73. }
  74. /**
  75. * Fonction renvoyant le tableau d'options d'une list
  76. * @param listArgs
  77. * @private
  78. */
  79. private static getUrlOptionsLists(listArgs: ListArgs): Array<string> {
  80. const options: Array<string> = []
  81. if (listArgs.itemsPerPage) {
  82. options.push(`itemsPerPage=${listArgs.itemsPerPage}`)
  83. }
  84. if (listArgs.page) {
  85. options.push(`page=${listArgs.page}`)
  86. }
  87. if (listArgs.filters) {
  88. for(const filter of listArgs.filters){
  89. options.push(`${filter.key}=${filter.value}`)
  90. }
  91. }
  92. return options
  93. }
  94. }
  95. export default UrlBuilder