urlBuilder.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { Model } from '@vuex-orm/core'
  2. import { UrlArgs } from '~/types/interfaces'
  3. import { QUERY_TYPE } from '~/types/enums'
  4. import { repositoryHelper } from '~/services/store/repository'
  5. /**
  6. * Classe permettant de construire une URL pour l'interrogation d'une API externe
  7. */
  8. class UrlBuilder {
  9. static ROOT = '/api/'
  10. /**
  11. * Main méthode qui appellera les méthode privées correspondantes (getDefaultUrl, getEnumUrl, getModelUrl)
  12. * @param {UrlArgs} args
  13. * @return {string}
  14. */
  15. public static build (args: UrlArgs): string {
  16. switch (args.type) {
  17. case QUERY_TYPE.DEFAULT:
  18. return UrlBuilder.getDefaultUrl(args.url)
  19. case QUERY_TYPE.ENUM:
  20. return UrlBuilder.getEnumUrl(args.enumType)
  21. case QUERY_TYPE.MODEL:
  22. return UrlBuilder.getModelUrl(args.model, args.rootModel, args.rootId)
  23. default:
  24. throw new Error('url, model or enum must be defined')
  25. }
  26. }
  27. /**
  28. * Construction d'une URL "traditionnelle" qui ira concaténer l'url passée en paramètre avec la ROOT Url définie
  29. * @param {string} url
  30. * @return {string}
  31. */
  32. private static getDefaultUrl (url?: string): string {
  33. if (typeof url === 'undefined') {
  34. throw new TypeError('url must be defined')
  35. }
  36. return UrlBuilder.concat(UrlBuilder.ROOT, url)
  37. }
  38. /**
  39. * Construction d'une URL Type Enum qui ira concaténer le type enum passé en paramètre avec la ROOT Url définie
  40. * @param {string} enumType
  41. * @return {string}
  42. */
  43. private static getEnumUrl (enumType?: string): string {
  44. if (typeof enumType === 'undefined') {
  45. throw new TypeError('enumType must be defined')
  46. }
  47. return UrlBuilder.concat(UrlBuilder.ROOT, 'enum', enumType)
  48. }
  49. /**
  50. * Construction d'une URL Type Model qui ira concaténer le nom de l'entité du model passé en paramètre
  51. * avec la ROOT Url définie (possibilité de récursivité si le root model est défini)
  52. *
  53. * @param {Model} model roles à tester
  54. * @param {Model} rootModel roles à tester
  55. * @param {number} rootId roles à tester
  56. * @return {string}
  57. */
  58. private static getModelUrl (model?: typeof Model, rootModel?: typeof Model, rootId?: number): string {
  59. if (typeof model === 'undefined') {
  60. throw new TypeError('model must be defined')
  61. }
  62. const entity = repositoryHelper.getEntity(model)
  63. if (typeof rootModel !== 'undefined') {
  64. if (typeof rootId === 'undefined') {
  65. throw new TypeError('Root ID must be defined')
  66. }
  67. const rootUrl = UrlBuilder.getModelUrl(rootModel) as string
  68. return String(`${rootUrl}/${rootId}/${entity}`).toString()
  69. }
  70. return UrlBuilder.concat(UrlBuilder.ROOT, entity)
  71. }
  72. /**
  73. * Concatenate a base url and a tail
  74. * @param base
  75. * @param tails
  76. * @private
  77. */
  78. public static concat (base: string, ...tails: string[]): string {
  79. let url = base
  80. tails.forEach((tail: string) => {
  81. url = url.replace(/^|\/$/g, '') + '/' + tail.replace(/^\/?|$/g, '')
  82. })
  83. return url
  84. }
  85. }
  86. export default UrlBuilder