urlBuilder.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import {Model} from '@vuex-orm/core'
  2. import {FileArgs, ImageArgs, ListArgs, UrlArgs} from '~/types/interfaces'
  3. import {QUERY_TYPE} from '~/types/enums'
  4. import {repositoryHelper} from '~/services/store/repository'
  5. import TypesTesting from "~/services/utils/typesTesting";
  6. import UrlOptionsBuilder from "~/services/connection/urlOptionsBuilder";
  7. /**
  8. * Classe permettant de construire une URL pour l'interrogation d'une API externe
  9. */
  10. class UrlBuilder {
  11. static ROOT = '/api/'
  12. /**
  13. * Main méthode qui appellera les méthode privées correspondantes (getEnumUrl, getModelUrl, getImageUrl)
  14. * @param {UrlArgs} args
  15. * @return {string}
  16. */
  17. public static build (args: UrlArgs): string {
  18. let url: string = ''
  19. switch (args.type) {
  20. case QUERY_TYPE.DEFAULT:
  21. url = UrlBuilder.getDefaultUrl( args.url, args.baseUrl)
  22. break;
  23. case QUERY_TYPE.ENUM:
  24. url = UrlBuilder.getEnumUrl(args.enumType)
  25. break;
  26. case QUERY_TYPE.MODEL:
  27. url = UrlBuilder.getModelUrl(args.model, args.rootModel, args.rootId)
  28. break;
  29. case QUERY_TYPE.IMAGE:
  30. if (!TypesTesting.isDataProviderArgs(args)) {
  31. throw new Error('*args* is not a dataProviderArgs')
  32. }
  33. if (!args.imgArgs) {
  34. throw new Error('*args* has no imgArgs')
  35. }
  36. url = UrlBuilder.getImageUrl(args.imgArgs, args.baseUrl)
  37. break;
  38. case QUERY_TYPE.FILE:
  39. if (!TypesTesting.isDataProviderArgs(args)) {
  40. throw new Error('*args* is not a dataProviderArgs')
  41. }
  42. if (!args.fileArgs) {
  43. throw new Error('*args* has no fileArgs')
  44. }
  45. url = UrlBuilder.getFileUrl(args.fileArgs, args.baseUrl)
  46. break;
  47. default:
  48. throw new Error('url, model, image or enum must be defined')
  49. break;
  50. }
  51. const options = UrlBuilder.buildOptions(args)
  52. return options.length > 0 ? `${url}?${UrlBuilder.buildOptions(args).join('&')}` : url
  53. }
  54. /**
  55. * Construction d'une URL qui ira concaténer la base URL avec l'url
  56. * @param url
  57. * @param baseUrl
  58. * @private
  59. */
  60. private static getDefaultUrl (url?: string, baseUrl: string|null = null): string {
  61. if (!url) {
  62. throw new Error('no url')
  63. }
  64. return baseUrl ? UrlBuilder.concat(baseUrl, url) : url
  65. }
  66. /**
  67. * Construction d'une URL Type Enum qui ira concaténer le type enum passé en paramètre avec la ROOT Url définie
  68. * @param {string} enumType
  69. * @return {string}
  70. */
  71. private static getEnumUrl (enumType?: string): string {
  72. if (typeof enumType === 'undefined') {
  73. throw new Error('enumType must be defined')
  74. }
  75. return UrlBuilder.concat(UrlBuilder.ROOT, 'enum', enumType)
  76. }
  77. /**
  78. * Construction d'une URL Type Model qui ira concaténer le nom de l'entité du model passé en paramètre
  79. * avec la ROOT Url définie (possibilité de récursivité si le root model est défini)
  80. *
  81. * @param {Model} model roles à tester
  82. * @param {Model} rootModel roles à tester
  83. * @param {number} rootId roles à tester
  84. * @return {string}
  85. */
  86. private static getModelUrl (model?: typeof Model, rootModel?: typeof Model, rootId?: number): string {
  87. if (typeof model === 'undefined') {
  88. throw new Error('model must be defined')
  89. }
  90. const entity = repositoryHelper.getEntity(model)
  91. if (typeof rootModel !== 'undefined') {
  92. if (typeof rootId === 'undefined') {
  93. throw new Error('Root ID must be defined')
  94. }
  95. const rootUrl = UrlBuilder.getModelUrl(rootModel) as string
  96. return String(`${rootUrl}/${rootId}/${entity}`).toString()
  97. }
  98. return UrlBuilder.concat(UrlBuilder.ROOT, entity)
  99. }
  100. /**
  101. * 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
  102. * @param {ImageArgs} imgArgs
  103. * @param {string} baseUrl
  104. * @return {string}
  105. */
  106. private static getImageUrl (imgArgs: ImageArgs, baseUrl: string = ''): string {
  107. const downloadUrl = `files/${imgArgs.id}/download/${imgArgs.height}x${imgArgs.width}`
  108. return UrlBuilder.concat(baseUrl, UrlBuilder.ROOT, downloadUrl)
  109. }
  110. /**
  111. * Construction d'une URL qui ira concaténer la base URL avec le Root et l'uri files
  112. * @param args
  113. * @param baseUrl
  114. * @private
  115. */
  116. private static getFileUrl (args: FileArgs, baseUrl: string = ''): string {
  117. return UrlBuilder.concat(baseUrl, UrlBuilder.ROOT, `download/${args.fileId}`)
  118. }
  119. /**
  120. * Concatenate a base url and a tail
  121. * @param base
  122. * @param tails
  123. * @private
  124. */
  125. public static concat (base: string, ...tails: string[]): string {
  126. let url = base
  127. tails.forEach((tail: string) => {
  128. url = url.replace(/^|\/$/g, '') + '/' + tail.replace(/^\/?|$/g, '')
  129. })
  130. return url
  131. }
  132. /**
  133. * Prepend the 'https://' part if neither 'http://' of 'https://' is present, else: does nothing
  134. *
  135. * @param url
  136. */
  137. public static prependHttps (url: string): string {
  138. if (!url.match(/^https?:\/\/.*/)) {
  139. url = 'https://' + url;
  140. }
  141. return url;
  142. }
  143. /**
  144. * Main méthode qui appellera les méthode privées correspondantes (getUrlOptionsImage, getUrlOptionsLists)
  145. * @param {UrlArgs} args
  146. * @return {string}
  147. */
  148. public static buildOptions(args: UrlArgs): Array<string> {
  149. let options: Array<string> = []
  150. if (args.type === QUERY_TYPE.IMAGE){
  151. options = [...options, this.getUrlOptionsImage()]
  152. }
  153. if (TypesTesting.isDataProviderArgs(args) && args.listArgs !== undefined) {
  154. options = [...options, ...this.getUrlOptionsLists(args.listArgs)]
  155. }
  156. return options
  157. }
  158. /**
  159. * Une image doit toujours avoir le time en options pour éviter les problème de cache
  160. * @private
  161. */
  162. private static getUrlOptionsImage(): string {
  163. return new Date().getTime().toString()
  164. }
  165. /**
  166. * Fonction renvoyant le tableau d'options d'une list
  167. * @param listArgs
  168. * @private
  169. */
  170. private static getUrlOptionsLists(listArgs: ListArgs): Array<string> {
  171. const options: Array<string> = []
  172. if (listArgs.itemsPerPage) {
  173. options.push(`itemsPerPage=${listArgs.itemsPerPage}`)
  174. }
  175. if (listArgs.page) {
  176. options.push(`page=${listArgs.page}`)
  177. }
  178. if (listArgs.filters) {
  179. for(const filter of listArgs.filters){
  180. options.push(`${filter.key}=${filter.value}`)
  181. }
  182. }
  183. return options
  184. }
  185. }
  186. export default UrlBuilder