urlUtils.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import _ from "lodash";
  2. /**
  3. * Classe permettant de construire une URL pour l'interrogation d'une API externe
  4. */
  5. class UrlUtils {
  6. /**
  7. * Concatenate a base url and a tail
  8. * @param base
  9. * @param tails
  10. * @private
  11. */
  12. public static join (base: string|number, ...tails: Array<string|number>): string {
  13. let url = String(base)
  14. tails.forEach((tail: string|number) => {
  15. url = url.replace(/^|\/$/g, '') + '/' + String(tail).replace(/^\/?|$/g, '')
  16. })
  17. return url
  18. }
  19. /**
  20. * Prepend the 'https://' part if neither 'http://' nor 'https://' prefixes are present, else does nothing
  21. *
  22. * @param url
  23. */
  24. public static prependHttps (url: string): string {
  25. if (!url.match(/^https?:\/\/.*/)) {
  26. url = 'https://' + url;
  27. }
  28. return url;
  29. }
  30. /**
  31. * Parse an URI to retrieve a parameter
  32. *
  33. * @param uri
  34. * @param parameter
  35. * @param default_
  36. * @private
  37. */
  38. public static getParameter (
  39. uri: string,
  40. parameter: string,
  41. default_: string | number | null = null
  42. ): string | number | null {
  43. uri = UrlUtils.prependHttps(uri)
  44. const urlParams = new URL(uri).searchParams;
  45. let value: string | number | null = urlParams.get(parameter);
  46. if (value && (default_ === null || Number.isInteger(default_)) && /^\d+$/.test(value)) {
  47. // On convertit automatiquement si et seulement la valeur par défaut est elle-même un entier ou n'est pas définie
  48. value = parseInt(value)
  49. }
  50. return value ?? default_
  51. }
  52. /**
  53. * Extrait l'ID de l'URI passée en paramètre
  54. * L'URI est supposée être de la forme `.../foo/bar/{id}`,
  55. * où l'id est un identifiant numérique, à moins que `isLiteral` soit défini comme vrai.
  56. * Dans ce cas, si `isLiteral` est vrai, l'id sera retourné sous forme de texte.
  57. *
  58. * @param uri
  59. * @param isLiteral
  60. */
  61. public static extractIdFromUri (uri: string, isLiteral: boolean = false): number|string|null {
  62. const partUri: Array<string> = uri.split('/')
  63. const id: string = partUri.pop() ?? ''
  64. if (!id || (!isLiteral && isNaN(id as any))) {
  65. throw new Error('no id found')
  66. }
  67. return isLiteral ? id : parseInt(id)
  68. }
  69. /**
  70. * Découpe une URI au niveau des '/'
  71. * Utilisé entre autres pour le breadcrumb
  72. *
  73. * Ex:
  74. *
  75. * foo/bar/1 => ['foo', 'bar', '1']
  76. * /foo/bar/1 => ['foo', 'bar', '1']
  77. * https://domain.com/foo/bar/1 => ['https:', 'domain.com', 'foo', 'bar', '1']
  78. *
  79. *
  80. * @param uri
  81. */
  82. public static split(uri: string) {
  83. return uri.split('/').filter((s) => s.length > 0)
  84. }
  85. /**
  86. * Format and add a query to an url
  87. * If the url already has a query, append the new parameters to it.
  88. * If the query is an empty object, does nothing.
  89. *
  90. * Ex:
  91. * addQuery('foo/bar', {}) => 'foo/bar'
  92. * addQuery('foo/bar', {'a': 1}) => 'foo/bar?a=1'
  93. * addQuery('foo/bar?a=1', {'b': 2}) => 'foo/bar?a=1&b=2'
  94. *
  95. * @param url
  96. * @param query
  97. */
  98. public static addQuery(url: string | URL, query: object): string {
  99. let urlObj = new URL(url, 'https://temporary-dommain.inexistent') as URL
  100. if (!_.isEmpty(query)) {
  101. urlObj = new URL(
  102. `${urlObj.origin}${urlObj.pathname}${urlObj.hash}?${new URLSearchParams([
  103. ...Array.from(urlObj.searchParams.entries()),
  104. ...Object.entries(query),
  105. ])}`
  106. )
  107. }
  108. let result = urlObj.toString()
  109. if (urlObj.host === 'temporary-dommain.inexistent') {
  110. result = result.replace('https://temporary-dommain.inexistent', '')
  111. }
  112. return result
  113. }
  114. /**
  115. * Make an ApiPlatform IRI for the given entity and id
  116. *
  117. * @see https://api-platform.com/docs/admin/handling-relations/
  118. */
  119. public static makeIRI(entity: string, id: number) {
  120. if (!_.isNumber(id)) {
  121. throw Error('Invalid id : ' + id)
  122. }
  123. return `/api/${entity}/${id}`
  124. }
  125. }
  126. export default UrlUtils