url.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 Url {
  8. /**
  9. * Concatenate a base url and a tail
  10. * @param base
  11. * @param tails
  12. * @private
  13. */
  14. public static join (base: string, ...tails: string[]): string {
  15. let url = base
  16. tails.forEach((tail: string) => {
  17. url = url.replace(/^|\/$/g, '') + '/' + tail.replace(/^\/?|$/g, '')
  18. })
  19. return url
  20. }
  21. /**
  22. * Prepend the 'https://' part if neither 'http://' of 'https://' is present, else: does nothing
  23. *
  24. * @param url
  25. */
  26. public static prependHttps (url: string): string {
  27. if (!url.match(/^https?:\/\/.*/)) {
  28. url = 'https://' + url;
  29. }
  30. return url;
  31. }
  32. /**
  33. * Parse an URI to retrieve the page number
  34. *
  35. * @param uri
  36. * @param parameter
  37. * @param default_
  38. * @private
  39. */
  40. public static getParameter(
  41. uri: string,
  42. parameter: string,
  43. default_: string | null = null
  44. ): string | null {
  45. const urlParams = new URL(uri).searchParams;
  46. const res = urlParams.get('page');
  47. return res ?? default_
  48. }
  49. }
  50. export default Url